-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from jekyll/add-specs
- Loading branch information
Showing
19 changed files
with
236 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--color | ||
--format progress |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
7 changes: 2 additions & 5 deletions
7
lib/jekyll/redirect_from.rb → lib/jekyll_redirect_from/redirector.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module JekyllRedirectFrom | ||
VERSION = "0.1.0" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#! /bin/bash | ||
|
||
bundle install |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
url: http://jekyllrb.com | ||
gems: | ||
- jekyll-redirect-from |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |