diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..42b69f5 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,60 @@ +name: Continuous Integration + +on: + push: + branches: + - master + pull_request: + branches: + - master + +jobs: + ci: + if: "!contains(github.event.commits[0].message, '[ci skip]')" + name: 'Ruby ${{ matrix.ruby_version }}' + runs-on: 'ubuntu-latest' + + strategy: + fail-fast: false + matrix: + ruby_version: + - "2.7" + - "3.0" + - "3.3" + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 5 + - name: "Set up Ruby ${{ matrix.ruby_version }}" + uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby_version }} + bundler-cache: true + - name: Execute tests + run: bash script/test + - name: Check Style Offenses + run: bash script/fmt + - name: Test gem build + run: bundle exec gem build jekyll-redirect-from.gemspec + - name: Test gem install + run: bundle exec gem install jekyll-redirect-from --local --verbose + + ghp: + if: "!contains(github.event.commits[0].message, '[ci skip]')" + name: Ruby 3.3 with GitHub Pages Gem + runs-on: 'ubuntu-latest' + env: + GH_PAGES: true + strategy: + fail-fast: false + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 5 + - name: "Set up Ruby 3.3" + uses: ruby/setup-ruby@v1 + with: + ruby-version: 3.3 + bundler-cache: true + - name: Execute tests + run: bash script/test diff --git a/.gitignore b/.gitignore index 98dcaca..e47808f 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,8 @@ pkg rdoc spec/reports spec/fixtures/.jekyll-metadata +spec/fixtures/.jekyll-cache test/tmp test/version_tmp tmp +vendor/bundle diff --git a/.rubocop.yml b/.rubocop.yml index 6d673b2..64db333 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,14 +1,41 @@ +inherit_from: .rubocop_todo.yml + +require: rubocop-jekyll + inherit_gem: - jekyll: .rubocop.yml + rubocop-jekyll: .rubocop.yml AllCops: + TargetRubyVersion: 2.5 + SuggestExtensions: false Exclude: - vendor/**/* -Metrics/BlockLength: +Layout/LineEndStringConcatenationIndentation: + Enabled: true + +Lint/EmptyInPattern: + Enabled: false + +Naming/InclusiveLanguage: + Enabled: false +Naming/MemoizedInstanceVariableName: Exclude: - - spec/**/* + - 'lib/jekyll-redirect-from/page_without_a_file.rb' + +Performance/MapCompact: + Enabled: true +Performance/RedundantEqualityComparisonBlock: + Enabled: true +Performance/RedundantSplitRegexpArgument: + Enabled: true -Metrics/LineLength: +Style/ConcatArrayLiterals: Exclude: - - spec/**/* + - 'jekyll-redirect-from.gemspec' +Style/InPatternThen: + Enabled: false +Style/MultilineInPatternThen: + Enabled: false +Style/QuotedSymbols: + Enabled: true diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..b870d5e --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,27 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2021-09-17 12:49:45 UTC using RuboCop version 1.18.4. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 3 +# Cop supports --auto-correct. +# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. +# URISchemes: http, https +Layout/LineLength: + Max: 105 + +# Offense count: 2 +# Configuration parameters: AllowedMethods. +# AllowedMethods: enums +Lint/ConstantDefinitionInBlock: + Exclude: + - 'spec/jekyll_redirect_from/generator_spec.rb' + +# Offense count: 2 +# Configuration parameters: AllowComments, AllowEmptyLambdas. +Lint/EmptyBlock: + Exclude: + - 'spec/jekyll_redirect_from/generator_spec.rb' diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index aba38c0..0000000 --- a/.travis.yml +++ /dev/null @@ -1,17 +0,0 @@ -language: ruby -before_script: bundle update -script: "script/cibuild" -sudo: false -cache: bundler - -matrix: - include: - - # GitHub Pages - rvm: 2.4.0 - env: GH_PAGES=true - -rvm: - - 2.4 - - 2.3 - - 2.2 - - 2.1 diff --git a/Gemfile b/Gemfile index 668785f..661fc4f 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,8 @@ +# frozen_string_literal: true + source "https://rubygems.org" gemspec -if ENV["GH_PAGES"] - gem "github-pages" -end +gem "github-pages" if ENV["GH_PAGES"] +gem "jekyll", ENV["JEKYLL_VERSION"] if ENV["JEKYLL_VERSION"] +gem "kramdown-parser-gfm" if ENV["JEKYLL_VERSION"] == "~> 3.9" diff --git a/History.markdown b/History.markdown index 5001743..5926bd5 100644 --- a/History.markdown +++ b/History.markdown @@ -1,11 +1,72 @@ ## HEAD +### styles + + * style: Style/SpecialGlobalVars (#216) + +### Development Fixes + + * fix Gemfile to correctly install jekyll 3.9 (#224) + * Refactor specs for RedirectPage (#222) + * Lock to RuboCop v1.18.x (#242) + * Clean up gemspec (#243) + * Bump versions in CI workflow (#273) + +### Bug Fixes + + * Refactor Redirectable mixin to reduce allocations (#241) + +## 0.16.0 / 2020-01-26 + +### Minor Enhancements + + * Allows generation of `redirects.json` to be disabled (#207) + * Allow redirects from and for subclasses of page and document (#204) + +### Bug Fixes + + * Use `Hash#key?` instead of `Hash#keys.any?` (#201) + +### Development Fixes + + * Target Ruby 2.4 + * Stop testing with backwards-compatible site config (#211) + +### Documentation + + * Simplifies YAML for `redirect_to` (#185) + +## 0.15.0 / 2019-03-23 + +### Development Fixes + + * chore(deps): rubocop-jekyll 0.3 (#187) + +### Bug Fixes + + * Allow testing and using with Jekyll 4.x (#196) + +## 0.14.0 / 2018-06-29 + +### Minor Enhancements + + * Run javascript at first to avoid splash (#158) + +### Development Fixes + + * Use Rubocop 0.57 + * Target Ruby 2.3 + * Test against Ruby 2.5 (#173) + +## 0.13.0 / 2017-12-03 + * Test against same version of Ruby that GitHub Pages uses (#132) ### Development Fixes * Rubocop (#141) * Fix tests for jekyll 3.5.x (#160) + * Rubocop: autocorrect (#165) ### Minor Enhancements @@ -17,6 +78,10 @@ * Create redirects.json file (#147) +### Documentation + + * Update README.md (#167) + ## 0.12.1 / 2017-01-12 ### Development Fixes diff --git a/LICENSE.txt b/LICENSE.txt index 9830ee4..0295617 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,4 +1,4 @@ -Copyright (c) 2013 Parker Moore +Copyright (c) 2013-present Parker Moore and jekyll-redirect-from contributors MIT License diff --git a/README.md b/README.md index 7e67460..ba340d5 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ impractical to create new pages in the proper subdirectories so they, e.g. Instead of dealing with maintaining those pages for redirection, let `jekyll-redirect-from` handle it for you. -[![Build Status](https://travis-ci.org/jekyll/jekyll-redirect-from.svg?branch=master)](https://travis-ci.org/jekyll/jekyll-redirect-from) +[![Build Status](https://github.com/jekyll/jekyll-redirect-from/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/jekyll/jekyll-redirect-from/actions/workflows/ci.yml) ## How it Works @@ -31,13 +31,15 @@ Or install it yourself as: $ gem install jekyll-redirect-from -Once it's installed into your evironment, add it to your `_config.yml`: +Once it's installed into your environment, add it to your `_config.yml`: ```yaml -gems: +plugins: - jekyll-redirect-from ``` +💡 If you are using a Jekyll version less than 3.5.0, use the `gems` key instead of `plugins`. + If you're using Jekyll in `safe` mode to mimic GitHub Pages, make sure to add jekyll-redirect-from to your whitelist: @@ -50,7 +52,7 @@ Then run `jekyll --safe` like normal. ## Usage -The object of this gem is to allow an author to specify multiple URLs for a +The objective of this gem is to allow an author to specify multiple URLs for a page, such that the alternative URLs redirect to the new Jekyll URL. To use it, simply add the array to the YAML front-matter of your page or post: @@ -66,7 +68,7 @@ Redirects including a trailing slash will generate a corresponding subdirectory For example... -```text +```yaml redirect_from: - /post/123456789/my-amazing-post ``` @@ -79,7 +81,7 @@ redirect_from: While... -```text +```yaml redirect_from: - /post/123456789/my-amazing-post/ ``` @@ -94,7 +96,7 @@ These pages will contain an HTTP-REFRESH meta tag which redirect to your URL. You can also specify just **one url** like this: -```text +```yaml title: My other awesome post redirect_from: /post/123456798/ ``` @@ -111,13 +113,9 @@ Sometimes, you may want to redirect a site page to a totally different website. ```yaml title: My amazing post -redirect_to: - - http://www.github.com +redirect_to: http://www.github.com ``` -If you have multiple `redirect_to`s set, only the first one will be respected. - - **Note**: Using `redirect_to` or `redirect_from` with collections will only work with files which are output to HTML, such as `.md`, `.textile`, `.html` etc. ## Customizing the redirect template @@ -129,6 +127,19 @@ Your layout will get the following variables: * `page.redirect.from` - the relative path to the redirect page * `page.redirect.to` - the absolute URL (where available) to the target page +## Configuration + +You can configure this plugin in `_config.yml` by adding to the `redirect_from` key. + +### Disabling `redirects.json` + +By default, a file called `redirects.json`, which can be used for automated testing or to implement server-side redirects, will be included in the output. To exclude it from the output, set the `json` key to `false`: + +```yml +redirect_from: + json: false +``` + ## Contributing 1. Fork it diff --git a/Rakefile b/Rakefile index b7e9ed5..ee617bb 100644 --- a/Rakefile +++ b/Rakefile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "bundler/gem_tasks" require "rspec/core/rake_task" diff --git a/jekyll-redirect-from.gemspec b/jekyll-redirect-from.gemspec index ed00a1b..713ef0b 100644 --- a/jekyll-redirect-from.gemspec +++ b/jekyll-redirect-from.gemspec @@ -1,31 +1,26 @@ -# coding: utf-8 +# frozen_string_literal: true -lib = File.expand_path("lib", __dir__) -$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require "jekyll-redirect-from/version" +require_relative "lib/jekyll-redirect-from/version" Gem::Specification.new do |spec| spec.name = "jekyll-redirect-from" spec.version = JekyllRedirectFrom::VERSION spec.authors = ["Parker Moore"] spec.email = ["parkrmoore@gmail.com"] - spec.summary = "Seamlessly specify multiple redirection URLs " \ - "for your pages and posts" + spec.summary = "Seamlessly specify multiple redirection URLs for your pages and posts" spec.homepage = "https://github.com/jekyll/jekyll-redirect-from" spec.license = "MIT" - # rubocop:disable Style/SpecialGlobalVars - spec.files = `git ls-files`.split($/) - # rubocop:enable Style/SpecialGlobalVars - - spec.executables = spec.files.grep(%r!^bin/!) { |f| File.basename(f) } - spec.test_files = spec.files.grep(%r!^(test|spec|features)/!) + spec.files = `git ls-files lib`.split("\n").concat(%w(LICENSE.txt README.md History.markdown)) spec.require_paths = ["lib"] - spec.add_runtime_dependency "jekyll", "~> 3.3" - spec.add_development_dependency "bundler", "~> 1.3" - spec.add_development_dependency "rspec", "~> 3.5" - spec.add_development_dependency "rake", "~> 12.0" + spec.required_ruby_version = ">= 2.5.0" + + spec.add_runtime_dependency "jekyll", ">= 3.3", "< 5.0" + + spec.add_development_dependency "bundler" spec.add_development_dependency "jekyll-sitemap", "~> 1.0" - spec.add_development_dependency "rubocop", "~> 0.43" + spec.add_development_dependency "rake", "~> 13.0" + spec.add_development_dependency "rspec", "~> 3.5" + spec.add_development_dependency "rubocop-jekyll", "~> 0.13.0" end diff --git a/lib/jekyll-redirect-from.rb b/lib/jekyll-redirect-from.rb index 8e2a0aa..1922bc1 100644 --- a/lib/jekyll-redirect-from.rb +++ b/lib/jekyll-redirect-from.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "jekyll" require "jekyll-redirect-from/version" require "jekyll-redirect-from/generator" diff --git a/lib/jekyll-redirect-from/context.rb b/lib/jekyll-redirect-from/context.rb index 4f7de76..25cfcf9 100644 --- a/lib/jekyll-redirect-from/context.rb +++ b/lib/jekyll-redirect-from/context.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module JekyllRedirectFrom # Stubbed LiquidContext to support relative_url and absolute_url helpers class Context diff --git a/lib/jekyll-redirect-from/generator.rb b/lib/jekyll-redirect-from/generator.rb index a93560d..52e68f5 100644 --- a/lib/jekyll-redirect-from/generator.rb +++ b/lib/jekyll-redirect-from/generator.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module JekyllRedirectFrom class Generator < Jekyll::Generator safe true @@ -8,18 +10,19 @@ def generate(site) @redirects = {} # Inject our layout, unless the user has already specified a redirect layout' - unless site.layouts.keys.any? { |name| name == "redirect" } + unless site.layouts.key?("redirect") site.layouts["redirect"] = JekyllRedirectFrom::Layout.new(site) end # Must duplicate pages to modify while in loop (site.docs_to_write + site.pages.dup).each do |doc| - next unless JekyllRedirectFrom::CLASSES.include?(doc.class) + next unless redirectable_document?(doc) + generate_redirect_from(doc) generate_redirect_to(doc) end - generate_redirects_json + generate_redirects_json if generate_redirects_json? end private @@ -35,6 +38,7 @@ def generate_redirect_from(doc) def generate_redirect_to(doc) return unless doc.redirect_to + page = RedirectPage.redirect_to(doc, doc.redirect_to) doc.data.merge!(page.data) doc.content = doc.output = page.output @@ -43,10 +47,19 @@ def generate_redirect_to(doc) def generate_redirects_json return if File.exist? site.in_source_dir("redirects.json") + page = PageWithoutAFile.new(site, "", "", "redirects.json") page.content = redirects.to_json page.data["layout"] = nil site.pages << page end + + def redirectable_document?(doc) + doc.is_a?(Jekyll::Document) || doc.is_a?(Jekyll::Page) + end + + def generate_redirects_json? + site.config.dig("redirect_from", "json") != false + end end end diff --git a/lib/jekyll-redirect-from/layout.rb b/lib/jekyll-redirect-from/layout.rb index 82ad1c0..b39ca9d 100644 --- a/lib/jekyll-redirect-from/layout.rb +++ b/lib/jekyll-redirect-from/layout.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module JekyllRedirectFrom # A stubbed layout for our default redirect template # We cannot use the standard Layout class because of site.in_source_dir diff --git a/lib/jekyll-redirect-from/page_without_a_file.rb b/lib/jekyll-redirect-from/page_without_a_file.rb index dddf90e..9604cb8 100644 --- a/lib/jekyll-redirect-from/page_without_a_file.rb +++ b/lib/jekyll-redirect-from/page_without_a_file.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module JekyllRedirectFrom class PageWithoutAFile < Jekyll::Page def read_yaml(*) diff --git a/lib/jekyll-redirect-from/redirect.html b/lib/jekyll-redirect-from/redirect.html index 54ed750..6640310 100644 --- a/lib/jekyll-redirect-from/redirect.html +++ b/lib/jekyll-redirect-from/redirect.html @@ -3,9 +3,9 @@ Redirecting… +

Redirecting…

Click here if you are not redirected. - diff --git a/lib/jekyll-redirect-from/redirect_page.rb b/lib/jekyll-redirect-from/redirect_page.rb index 0bf64a9..58d022c 100644 --- a/lib/jekyll-redirect-from/redirect_page.rb +++ b/lib/jekyll-redirect-from/redirect_page.rb @@ -1,4 +1,4 @@ -# Encoding: utf-8 +# frozen_string_literal: true module JekyllRedirectFrom # Specialty page which implements the redirect path logic @@ -45,13 +45,13 @@ def read_yaml(_base, _name, _opts = {}) def set_paths(from, to) @context ||= context from = ensure_leading_slash(from) - data.merge!({ + data.merge!( "permalink" => from, "redirect" => { "from" => from, - "to" => to =~ %r!^https?://! ? to : absolute_url(to), - }, - }) + "to" => %r!^https?://!.match?(to) ? to : absolute_url(to), + } + ) end def redirect_from diff --git a/lib/jekyll-redirect-from/redirectable.rb b/lib/jekyll-redirect-from/redirectable.rb index f9dffd8..4512a03 100644 --- a/lib/jekyll-redirect-from/redirectable.rb +++ b/lib/jekyll-redirect-from/redirectable.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module JekyllRedirectFrom # Module which can be mixed in to documents (and pages) to provide # redirect_to and redirect_from helpers @@ -5,21 +7,15 @@ module Redirectable # Returns a string representing the relative path or URL # to which the document should be redirected def redirect_to - if to_liquid["redirect_to"].is_a?(Array) - to_liquid["redirect_to"].compact.first - else - to_liquid["redirect_to"] - end + meta_data = to_liquid["redirect_to"] + meta_data.is_a?(Array) ? meta_data.compact.first : meta_data end # Returns an array representing the relative paths to other # documents which should be redirected to this document def redirect_from - if to_liquid["redirect_from"].is_a?(Array) - to_liquid["redirect_from"].compact - else - [to_liquid["redirect_from"]].compact - end + meta_data = to_liquid["redirect_from"] + meta_data.is_a?(Array) ? meta_data.compact : [meta_data].compact end end end diff --git a/lib/jekyll-redirect-from/version.rb b/lib/jekyll-redirect-from/version.rb index 964cca0..56f5d47 100644 --- a/lib/jekyll-redirect-from/version.rb +++ b/lib/jekyll-redirect-from/version.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module JekyllRedirectFrom - VERSION = "0.12.1".freeze + VERSION = "0.16.0" end diff --git a/script/bootstrap b/script/bootstrap index 02278d9..e3126bf 100755 --- a/script/bootstrap +++ b/script/bootstrap @@ -1,3 +1,4 @@ #! /bin/bash +set -e -bundle install -j8 +bundle install -j8 || bundle install diff --git a/script/cibuild b/script/cibuild index 10ff25d..40589af 100755 --- a/script/cibuild +++ b/script/cibuild @@ -1,6 +1,6 @@ #! /bin/bash set -e -bundle exec rake spec -bundle exec rubocop -S -D -gem build jekyll-redirect-from.gemspec +script/test +script/fmt +bundle exec rake build diff --git a/script/fmt b/script/fmt new file mode 100755 index 0000000..913591a --- /dev/null +++ b/script/fmt @@ -0,0 +1,10 @@ +#!/bin/bash +set -e + +echo "RuboCop $(bundle exec rubocop --version)" +bundle exec rubocop -D -E $@ +success=$? +if ((success != 0)); then + echo -e "\nTry running \`script/fmt -a\` to automatically fix errors" +fi +exit $success diff --git a/script/release b/script/release index fbddd87..6c9fcf2 100755 --- a/script/release +++ b/script/release @@ -1,3 +1,4 @@ #! /bin/bash +script/cibuild bundle exec rake release diff --git a/spec/integrations_spec.rb b/spec/integrations_spec.rb index 404eed5..cb6f53d 100644 --- a/spec/integrations_spec.rb +++ b/spec/integrations_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe "JekyllRedirectFrom integration tests" do before { site.process } let(:relative_path) { "" } @@ -15,7 +17,7 @@ end context "multiple redirect froms" do - %w(help contact let-there/be/light-he-said geepers/mccreepin).each do |redirect| + ["help", "contact", "let-there/be/light-he-said", "geepers/mccreepin"].each do |redirect| context "the #{redirect} redirect" do let(:relative_path) { "#{redirect}.html" } diff --git a/spec/jekyll_redirect_from/context_spec.rb b/spec/jekyll_redirect_from/context_spec.rb index bdf3e0d..b619d03 100644 --- a/spec/jekyll_redirect_from/context_spec.rb +++ b/spec/jekyll_redirect_from/context_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe JekyllRedirectFrom::Context do subject { described_class.new(site) } diff --git a/spec/jekyll_redirect_from/generator_spec.rb b/spec/jekyll_redirect_from/generator_spec.rb index 536efcc..5a7767b 100644 --- a/spec/jekyll_redirect_from/generator_spec.rb +++ b/spec/jekyll_redirect_from/generator_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe JekyllRedirectFrom::Generator do before(:each) do site.read @@ -92,7 +94,7 @@ let(:redirects) { JSON.parse(contents) } let(:domain) { "http://jekyllrb.com" } - it "creates the redirets file" do + it "creates the redirects file" do expect(path).to exist end @@ -138,10 +140,32 @@ FileUtils.rm_f source_path end - it "doesn't overwrite redirets.json" do + it "doesn't overwrite redirects.json" do expect(path).to exist - expect(redirects).to eql({ "foo" => "bar" }) + expect(redirects).to eql("foo" => "bar") + end + end + + context "when explicitly disabled" do + let(:site) { Jekyll::Site.new(config.merge("redirect_from" => { "json" => false })) } + + it "does not create the redirects file" do + expect(path).to_not exist end end end + + context "redirectable_document?" do + let(:generator) { JekyllRedirectFrom::Generator.new } + + it "accepts subclasses of Jekyll::Document" do + SubclassOfJekyllDocument = Class.new(Jekyll::Document) { define_method(:initialize) {} } + expect(generator.send(:redirectable_document?, SubclassOfJekyllDocument.new)).to be_truthy + end + + it "accepts subclasses of Jekyll::Page" do + SubclassOfJekyllPage = Class.new(Jekyll::Page) { define_method(:initialize) {} } + expect(generator.send(:redirectable_document?, SubclassOfJekyllPage.new)).to be_truthy + end + end end diff --git a/spec/jekyll_redirect_from/layout_spec.rb b/spec/jekyll_redirect_from/layout_spec.rb index 12a0748..60d6f42 100644 --- a/spec/jekyll_redirect_from/layout_spec.rb +++ b/spec/jekyll_redirect_from/layout_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe JekyllRedirectFrom::Layout do subject { described_class.new(@site) } diff --git a/spec/jekyll_redirect_from/redirect_page_spec.rb b/spec/jekyll_redirect_from/redirect_page_spec.rb index fefcc8d..b5af971 100644 --- a/spec/jekyll_redirect_from/redirect_page_spec.rb +++ b/spec/jekyll_redirect_from/redirect_page_spec.rb @@ -1,148 +1,142 @@ -# Encoding: utf-8 +# frozen_string_literal: true describe JekyllRedirectFrom::RedirectPage do - let(:from) { "/foo" } - let(:to) { "/bar" } let(:site_url) { site.config["url"] } - subject { described_class.from_paths(site, from, to) } before { site.read } - context "being a page" do - before { subject.read_yaml(nil, nil, nil) } + shared_examples "a redirect page" do + context "being a page" do + before { page.read_yaml(nil, nil, nil) } - it "returns no content" do - expect(subject.content).to eql("") - end + it "returns no content" do + expect(page.content).to eql("") + end - it "returns no output" do - expect(subject.output).to eql("") - end + it "returns no output" do + expect(page.output).to eql("") + end - it "sets default data" do - expect(subject.to_liquid["layout"]).to eql("redirect") - expect(subject.to_liquid["sitemap"]).to be_falsey - end - end + it "sets default data" do + expect(page.to_liquid["layout"]).to eql("redirect") + expect(page.to_liquid["sitemap"]).to be_falsey + end - context "creating a page from paths" do - it "sets the permalink" do - expect(subject.to_liquid["permalink"]).to eql(from) - end + it "sets the paths" do + expect(page.to_liquid["permalink"]).to eql(from) + expect(page.to_liquid).to have_key("redirect") + expect(page.to_liquid["redirect"]["from"]).to eql(from) + expect(page.to_liquid["redirect"]["to"]).to eql("#{site_url}#{to}") + end + + it "sets the permalink" do + expect(page.to_liquid["permalink"]).to eql(from) + end - it "sets redirect metadata" do - expect(subject.to_liquid).to have_key("redirect") - expect(subject.to_liquid["redirect"]["from"]).to eql(from) - expect(subject.to_liquid["redirect"]["to"]).to eql("#{site_url}#{to}") + it "sets redirect metadata" do + expect(page.to_liquid).to have_key("redirect") + expect(page.to_liquid["redirect"]["from"]).to eql(from) + expect(page.to_liquid["redirect"]["to"]).to eql("#{site_url}#{to}") + end end - context "with a document" do - let(:doc) { site.documents.first } + context "generating" do + before { site.generate } + let(:output) { Jekyll::Renderer.new(site, page, site.site_payload).run } - context "redirect from" do - let(:page) { described_class.redirect_from(doc, from) } + it "renders the template" do + expect(output).to_not be_nil + expect(output.to_s).to_not be_empty + end - it "creates with redirect_from" do - expect(page.to_liquid["permalink"]).to eql(from) - expect(page.to_liquid).to have_key("redirect") - expect(page.to_liquid["redirect"]["from"]).to eql(from) - expected = "http://jekyllrb.com/2014/01/03/redirect-me-plz.html" - expect(page.to_liquid["redirect"]["to"]).to eql(expected) - end + it "contains the meta refresh tag" do + expect(output).to match("") end - context "redirect to" do - let(:page) { described_class.redirect_to(doc, to) } + it "contains the javascript redirect" do + expect(output).to match("") + end - context "redirecting to a path" do - let(:to) { "/bar" } + it "contains canonical link in header" do + expect(output).to match("") + end - it "redirects" do - expect(page.to_liquid["permalink"]).to eql("/2014/01/03/redirect-me-plz.html") - expect(page.to_liquid).to have_key("redirect") - expect(page.to_liquid["redirect"]["to"]).to eql("#{site_url}#{to}") - expect(page.to_liquid["redirect"]["from"]).to eql("/2014/01/03/redirect-me-plz.html") - end + it "contains the clickable link" do + expect(output).to match("Click here if you are not redirected.") + end + end + end - context "with no leading slash" do - let(:to) { "bar" } + context "redirecting to" do + let(:to) { "/bar" } + let(:doc) { site.documents.first } + subject(:page) { described_class.redirect_to(doc, to) } + let(:from) { doc.url } - it "redirects" do - expect(page.to_liquid).to have_key("redirect") - expect(page.to_liquid["redirect"]["to"]).to eql("#{site_url}/#{to}") - end - end + it_behaves_like "a redirect page" - context "with a trailing slash" do - let(:to) { "/bar/" } + context "a relative path" do + let(:to) { "/bar" } - it "redirects" do - expect(page.to_liquid).to have_key("redirect") - expect(page.to_liquid["redirect"]["to"]).to eql("#{site_url}#{to}") - end - end - end + it "redirects" do + expect(page.to_liquid["permalink"]).to eql("/2014/01/03/redirect-me-plz.html") + expect(page.to_liquid).to have_key("redirect") + expect(page.to_liquid["redirect"]["to"]).to eql("#{site_url}#{to}") + expect(page.to_liquid["redirect"]["from"]).to eql("/2014/01/03/redirect-me-plz.html") + end - context "redirecting to a URL" do - let(:to) { "https://foo.invalid" } + context "with no leading slash" do + let(:to) { "bar" } - it "redirects" do - expect(page.to_liquid["permalink"]).to eql("/2014/01/03/redirect-me-plz.html") - expect(page.to_liquid).to have_key("redirect") - expect(page.to_liquid["redirect"]["to"]).to eql("https://foo.invalid") - expect(page.to_liquid["redirect"]["from"]).to eql("/2014/01/03/redirect-me-plz.html") - end + it "redirects" do + expect(page.to_liquid).to have_key("redirect") + expect(page.to_liquid["redirect"]["to"]).to eql("#{site_url}/#{to}") end end - end - end - - context "setting the paths" do - let(:from) { "/foo2" } - let(:to) { "/bar2" } - before { subject.set_paths(from, to) } + context "with a trailing slash" do + let(:to) { "/bar/" } - it "sets the paths" do - expect(subject.to_liquid["permalink"]).to eql(from) - expect(subject.to_liquid).to have_key("redirect") - expect(subject.to_liquid["redirect"]["from"]).to eql(from) - expect(subject.to_liquid["redirect"]["to"]).to eql("#{site_url}#{to}") + it "redirects" do + expect(page.to_liquid).to have_key("redirect") + expect(page.to_liquid["redirect"]["to"]).to eql("#{site_url}#{to}") + end + end end - end - - context "generating" do - before { site.generate } - let(:output) { Jekyll::Renderer.new(site, subject, site.site_payload).run } - it "renders the template" do - expect(output).to_not be_nil - expect(output.to_s).to_not be_empty - end + context "an absolute URL" do + let(:to) { "https://foo.invalid" } - it "contains the meta refresh tag" do - expect(output).to match("") + it "redirects" do + expect(page.to_liquid["permalink"]).to eql("/2014/01/03/redirect-me-plz.html") + expect(page.to_liquid).to have_key("redirect") + expect(page.to_liquid["redirect"]["to"]).to eql("https://foo.invalid") + expect(page.to_liquid["redirect"]["from"]).to eql("/2014/01/03/redirect-me-plz.html") + end end + end - it "contains the javascript redirect" do - expect(output).to match("") - end + context "redirecting from" do + let(:from) { "/foo" } + let(:doc) { site.documents.first } + subject(:page) { described_class.redirect_from(doc, from) } + let(:to) { doc.url } - it "contains canonical link in header" do - expect(output).to match("") - end + it_behaves_like "a redirect page" - it "contains the clickable link" do - expect(output).to match("Click here if you are not redirected.") + it "sets liquid data for the page" do + expect(page.to_liquid["permalink"]).to eql(from) + expect(page.to_liquid).to have_key("redirect") + expect(page.to_liquid["redirect"]["from"]).to eql(from) + expected = "http://jekyllrb.com/2014/01/03/redirect-me-plz.html" + expect(page.to_liquid["redirect"]["to"]).to eql(expected) end - end - context "redirect from destination" do context "when redirect from has no extension" do let(:from) { "/foo" } it "adds .html" do expected = File.expand_path "foo.html", site.dest - expect(subject.destination("/")).to eql(expected) + expect(page.destination("/")).to eql(expected) end end @@ -151,7 +145,11 @@ it "knows to add the index.html" do expected = File.expand_path "foo/index.html", site.dest - expect(subject.destination("/")).to eql(expected) + expect(page.destination("/")).to eql(expected) + end + + it "uses HTML" do + expect(page.output_ext).to eql(".html") end end @@ -160,7 +158,7 @@ it "adds .html" do expected = File.expand_path "foo.html", site.dest - expect(subject.destination("/")).to eql(expected) + expect(page.destination("/")).to eql(expected) end end @@ -169,7 +167,11 @@ it "doesn't add .html" do expected = File.expand_path "foo.htm", site.dest - expect(subject.destination("/")).to eql(expected) + expect(page.destination("/")).to eql(expected) + end + + it "honors the extension" do + expect(page.output_ext).to eql(".htm") end end @@ -178,33 +180,11 @@ it "adds the slash" do expected = File.expand_path "foo.html", site.dest - expect(subject.destination("/")).to eql(expected) + expect(page.destination("/")).to eql(expected) end - end - end - - context "output extension" do - context "with an extension" do - let(:from) { "foo.htm" } - - it "honors the extension" do - expect(subject.output_ext).to eql(".htm") - end - end - - context "with a trailing slash" do - let(:from) { "foo/" } - - it "uses HTML" do - expect(subject.output_ext).to eql(".html") - end - end - - context "with no slash" do - let(:from) { "foo" } it "uses HTML" do - expect(subject.output_ext).to eql(".html") + expect(page.output_ext).to eql(".html") end end end diff --git a/spec/jekyll_redirect_from/redirectable_spec.rb b/spec/jekyll_redirect_from/redirectable_spec.rb index 09cfc92..c9a2108 100644 --- a/spec/jekyll_redirect_from/redirectable_spec.rb +++ b/spec/jekyll_redirect_from/redirectable_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class RedirectableTestHelper include JekyllRedirectFrom::Redirectable attr_reader :to_liquid diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ca35d6d..c089254 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "jekyll" require File.expand_path("lib/jekyll-redirect-from.rb") @@ -32,7 +34,7 @@ def dest_dir(*paths) end def config - Jekyll.configuration({ + Jekyll.configuration( "source" => fixtures_path.to_s, "destination" => fixtures_path.join("_site").to_s, "collections" => { @@ -40,15 +42,12 @@ def config "authors" => {}, }, "url" => "http://jekyllrb.com", - "gems" => [ - "jekyll-redirect-from", - "jekyll-sitemap", - ], + "plugins" => %w(jekyll-redirect-from jekyll-sitemap), "defaults" => [{ "scope" => { "path" => "" }, "values" => { "layout" => "layout" }, - },], - }).backwards_compatibilize + }] + ) end def site