Skip to content

Commit

Permalink
Merge pull request #3 from jekyll/add-specs
Browse files Browse the repository at this point in the history
  • Loading branch information
parkr committed Jan 5, 2014
2 parents 86f293c + 122b3ab commit ae5360d
Show file tree
Hide file tree
Showing 19 changed files with 236 additions and 53 deletions.
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--format progress
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: ruby
rvm:
- 2.1.0
- 2.0.0
- 1.9.3
script: "script/cibuild"
5 changes: 5 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
require "bundler/gem_tasks"
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
5 changes: 3 additions & 2 deletions jekyll-redirect-from.gemspec
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'jekyll/redirect_from/version'
require 'jekyll_redirect_from/version'

Gem::Specification.new do |spec|
spec.name = "jekyll-redirect-from"
spec.version = Jekyll::RedirectFrom::VERSION
spec.version = JekyllRedirectFrom::VERSION
spec.authors = ["Parker Moore"]
spec.email = ["parkrmoore@gmail.com"]
spec.description = %q{Seamlessly specify multiple redirection URLs for your pages and posts}
Expand All @@ -22,4 +22,5 @@ Gem::Specification.new do |spec|

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
end
4 changes: 4 additions & 0 deletions lib/jekyll-redirect-from.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require "jekyll"
require "jekyll_redirect_from/version"
require "jekyll_redirect_from/redirect_page"
require "jekyll_redirect_from/redirector"
41 changes: 0 additions & 41 deletions lib/jekyll/redirect_from/redirect_page.rb

This file was deleted.

5 changes: 0 additions & 5 deletions lib/jekyll/redirect_from/version.rb

This file was deleted.

40 changes: 40 additions & 0 deletions lib/jekyll_redirect_from/redirect_page.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module JekyllRedirectFrom
class RedirectPage < Jekyll::Page
# Initialize a new RedirectPage.
#
# site - The Site object.
# base - The String path to the source.
# dir - The String path between the source and the file.
# name - The String filename of the file.
def initialize(site, base, dir, name)
@site = site
@base = base
@dir = dir
@name = name

self.process(name)
self.data = {}
end

def generate_redirect_content(item_url)
self.output = self.content = <<-EOF
<!DOCTYPE html>
<html>
<head>
<title>Redirecting...</title>
<link rel="canonical" href="#{item_url}"/>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="0; url=#{item_url}" />
</head>
<body>
<p><strong>Redirecting...</strong></p>
<p><a href='#{item_url}'>Click here if you are not redirected.</a></p>
<script>
document.location.href = "#{item_url}";
</script>
</body>
</html>
EOF
end
end
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
require "jekyll/redirect_from/version"
require "jekyll/redirect_from/redirect_page"

module Jekyll
class RedirectFrom < Generator
module JekyllRedirectFrom
class Redirector < Jekyll::Generator
def generate(site)
original_pages = site.pages.dup
generate_alt_urls(site, site.posts)
Expand Down
3 changes: 3 additions & 0 deletions lib/jekyll_redirect_from/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module JekyllRedirectFrom
VERSION = "0.1.0"
end
3 changes: 3 additions & 0 deletions script/bootstrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#! /bin/bash

bundle install
4 changes: 4 additions & 0 deletions script/cibuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#! /bin/bash

script/bootstrap > /dev/null 2>&1
bundle exec rake spec
3 changes: 3 additions & 0 deletions spec/fixtures/_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
url: http://jekyllrb.com
gems:
- jekyll-redirect-from
6 changes: 6 additions & 0 deletions spec/fixtures/_posts/2014-01-03-redirect-me-plz.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Please redirect me, sir.
redirect_from: /posts/23128432159832/mary-had-a-little-lamb
---

Yay.
10 changes: 10 additions & 0 deletions spec/fixtures/multiple_redirect_urls.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
title: I have lots of redirect urls
redirect_from:
- help
- contact
- let-there/be/light-he-said
- /geepers/mccreepin
---

Lots of redirect urls
6 changes: 6 additions & 0 deletions spec/fixtures/one_redirect_url.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: I only have one redirect url.
redirect_from: mencius/was/my/father
---

One redirect url
50 changes: 50 additions & 0 deletions spec/jekyll_redirect_from/redirect_page_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require "spec_helper"

describe JekyllRedirectFrom::RedirectPage do
let(:redirect_page) { described_class.new(@site, @site.source, "posts/12435151125", "larry-had-a-little-lamb") }
let(:item_url) { File.join(@site.config["url"], "2014", "01", "03", "moving-to-jekyll.md") }
let(:page_content) { redirect_page.generate_redirect_content(item_url) }

context "#generate_redirect_content" do
it "sets the #content to the generated refresh page" do
expect(page_content).to eq(" <!DOCTYPE html>\n <html>\n <head>\n <title>Redirecting...</title>\n <link rel=\"canonical\" href=\"http://jekyllrb.com/2014/01/03/moving-to-jekyll.md\"/>\n <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n <meta http-equiv=\"refresh\" content=\"0; url=http://jekyllrb.com/2014/01/03/moving-to-jekyll.md\" />\n </head>\n <body>\n <p><strong>Redirecting...</strong></p>\n <p><a href='http://jekyllrb.com/2014/01/03/moving-to-jekyll.md'>Click here if you are not redirected.</a></p>\n <script>\n document.location.href = \"http://jekyllrb.com/2014/01/03/moving-to-jekyll.md\";\n </script>\n </body>\n </html>\n")
end

it "contains the meta refresh tag" do
expect(page_content).to include("<meta http-equiv=\"refresh\" content=\"0; url=#{item_url}\" />")
end

it "contains JavaScript redirect" do
expect(page_content).to include("document.location.href = \"http://jekyllrb.com/2014/01/03/moving-to-jekyll.md\";")
end

it "contains canonical link in header" do
expect(page_content).to include("<link rel=\"canonical\" href=\"http://jekyllrb.com/2014/01/03/moving-to-jekyll.md\"/>")
end

it "contains a clickable link to redirect" do
expect(page_content).to include("<a href='http://jekyllrb.com/2014/01/03/moving-to-jekyll.md'>Click here if you are not redirected.</a>")
end
end

context "when writing to disk" do
let(:redirect_page_full_path) { redirect_page.destination(@site.dest) }

before(:each) do
redirect_page.generate_redirect_content(item_url)
redirect_page.write(@site.dest)
end

it "fetches the path properly" do
expect(redirect_page_full_path).to match /\/spec\/fixtures\/\_site\/posts\/12435151125\/larry-had-a-little-lamb/
end

it "is written to the proper location" do
expect(File.exist?(redirect_page_full_path)).to be_true
end

it "writes the context we expect" do
expect(File.read(redirect_page_full_path)).to eql(page_content)
end
end
end
41 changes: 41 additions & 0 deletions spec/jekyll_redirect_from/redirector_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require "spec_helper"

describe JekyllRedirectFrom::Redirector do
let(:redirector) { described_class.new }
let(:post_to_redirect) { setup_post("2014-01-03-redirect-me-plz.md") }
let(:page_with_one) { setup_page("one_redirect_url.md") }
let(:page_with_many) { setup_page("multiple_redirect_urls.md") }

it "knows if a page or post is requesting a redirect page" do
expect(redirector.has_alt_urls?(post_to_redirect)).to be_true
end

it "handles one redirect path" do
expect(redirector.alt_urls(page_with_one)).to eql(["mencius/was/my/father"])
end

it "handles many redirect paths" do
expect(redirector.alt_urls(page_with_many)).to eql(["help", "contact", "let-there/be/light-he-said", "/geepers/mccreepin"])
end

context "refresh page generation" do
before(:all) do
described_class.new.generate(@site)
end

it "generates the refresh page for the post properly" do
expect(destination_file_exists?("posts/23128432159832/mary-had-a-little-lamb")).to be_true
end

it "generates the refresh pages for the page with multiple redirect_from urls" do
expect(destination_file_exists?("help")).to be_true
expect(destination_file_exists?("contact")).to be_true
expect(destination_file_exists?("let-there/be/light-he-said")).to be_true
expect(destination_file_exists?("/geepers/mccreepin")).to be_true
end

it "generates the refresh page for the page with one redirect_from url" do
expect(destination_file_exists?("mencius/was/my/father")).to be_true
end
end
end
48 changes: 48 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require "jekyll"
require File.expand_path("lib/jekyll-redirect-from.rb")

RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.run_all_when_everything_filtered = true
config.filter_run :focus
config.order = 'random'

config.expect_with :rspec do |c|
c.syntax = :expect
end

config.before(:all) do
Jekyll.logger.log_level = Jekyll::Stevenson::ERROR

@fixtures_path = Pathname.new(__FILE__).parent.join("fixtures")
@dest = @fixtures_path.join("_site")
@posts_src = File.join(@fixtures_path, "_posts")
@layouts_src = File.join(@fixtures_path, "_layouts")
@plugins_src = File.join(@fixtures_path, "_plugins")

@site = Jekyll::Site.new(Jekyll.configuration({
"source" => @fixtures_path.to_s,
"destination" => @dest.to_s,
"plugins" => @plugins_src
}))

@dest.rmtree if @dest.exist?
@site.process
end

config.after(:all) do
@dest.rmtree if @dest.exist?
end

def setup_post(file)
Jekyll::Post.new(@site, @fixtures_path, '', file)
end

def setup_page(file)
Jekyll::Page.new(@site, @fixtures_path, File.dirname(file), File.basename(file))
end

def destination_file_exists?(file)
File.exists?(File.join(@dest.to_s, file))
end
end

0 comments on commit ae5360d

Please sign in to comment.