jekyll-redirect-from 0.11.0 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,12 @@
1
+ RSpec.describe JekyllRedirectFrom::Context do
2
+ subject { described_class.new(site) }
3
+
4
+ it "stores the site" do
5
+ expect(subject.site).to be_a(Jekyll::Site)
6
+ end
7
+
8
+ it "returns the register" do
9
+ expect(subject.registers).to have_key(:site)
10
+ expect(subject.registers[:site]).to be_a(Jekyll::Site)
11
+ end
12
+ end
@@ -0,0 +1,87 @@
1
+ RSpec.describe JekyllRedirectFrom::Generator do
2
+ before(:each) do
3
+ site.read
4
+ site.generate
5
+ site.render
6
+ end
7
+
8
+ context "layouts" do
9
+ context "a site with a redirect layout" do
10
+ before { site.layouts["redirect"] = "foo" }
11
+
12
+ it "doesn't inject the layout" do
13
+ expect(site.layouts["redirect"]).to eql("foo")
14
+ end
15
+ end
16
+
17
+ context "a site without a redirect layout" do
18
+ it "injects the layout" do
19
+ expect(site.layouts["redirect"]).to be_a(JekyllRedirectFrom::Layout)
20
+ end
21
+ end
22
+ end
23
+
24
+ context "redirect froms" do
25
+ context "pages" do
26
+ context "a page with a single redirect" do
27
+ let(:page) { site.pages.find { |p| p.url == "/some/other/path" } }
28
+
29
+ it "creates the redirect" do
30
+ expect(page).to_not be_nil
31
+ expect(page.output).to match("http://jekyllrb.com/one_redirect_from.html")
32
+ end
33
+ end
34
+
35
+ context "a page with multiple redirects" do
36
+ let(:redirects) do
37
+ ["/help", "/contact", "/let-there/be/light-he-said", "/geepers/mccreepin"]
38
+ end
39
+
40
+ it "creates all the redirects" do
41
+ redirects.each do |url|
42
+ page = site.pages.find { |p| p.url == url }
43
+ expect(page).to_not be_nil
44
+ expect(page.output).to match("http://jekyllrb.com/multiple_redirect_froms.html")
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ context "documents" do
51
+ let(:page) { site.pages.find { |p| p.url == "/articles/23128432159832/mary-had-a-little-lamb" } }
52
+
53
+ it "redirects" do
54
+ expect(page).to_not be_nil
55
+ expect(page.output).to match("http://jekyllrb.com/articles/redirect-me-plz.html")
56
+ end
57
+ end
58
+ end
59
+
60
+ context "redirect tos" do
61
+ context "pages" do
62
+ context "a single redirect to" do
63
+ let(:page) { site.pages.find { |p| p.url == "/one_redirect_to_url.html" } }
64
+
65
+ it "redirects" do
66
+ expect(page.output).to match("https://www.github.com")
67
+ end
68
+ end
69
+
70
+ context "multiple redirect tos" do
71
+ let(:page) { site.pages.find { |p| p.url == "/multiple_redirect_tos.html" } }
72
+
73
+ it "redirects to the first entry" do
74
+ expect(page.output).to match("https://www.jekyllrb.com")
75
+ end
76
+ end
77
+ end
78
+
79
+ context "documents" do
80
+ let(:doc) { site.documents.find { |p| p.url == "/articles/redirect-somewhere-else-plz.html" } }
81
+
82
+ it "redirects" do
83
+ expect(doc.output).to match("http://www.zombo.com")
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,32 @@
1
+ RSpec.describe JekyllRedirectFrom::Layout do
2
+ subject { described_class.new(@site) }
3
+
4
+ it "exposes the site" do
5
+ expect(subject.site).to eql(@site)
6
+ end
7
+
8
+ it "exposes the name" do
9
+ expect(subject.name).to eql("redirect.html")
10
+ end
11
+
12
+ it "exposes the path" do
13
+ expected = File.expand_path "../../lib/jekyll-redirect-from/redirect.html", File.dirname(__FILE__)
14
+ expect(subject.path).to eql(expected)
15
+ end
16
+
17
+ it "exposes the relative path" do
18
+ expect(subject.relative_path).to eql("_layouts/redirect.html")
19
+ end
20
+
21
+ it "exposes the ext" do
22
+ expect(subject.ext).to eql("html")
23
+ end
24
+
25
+ it "exposes data" do
26
+ expect(subject.data).to eql({})
27
+ end
28
+
29
+ it "exposes content" do
30
+ expect(subject.content).to match("Redirecting...")
31
+ end
32
+ end
@@ -1,76 +1,207 @@
1
1
  # Encoding: utf-8
2
2
 
3
- require "spec_helper"
4
-
5
3
  describe JekyllRedirectFrom::RedirectPage do
6
- let(:permalink) { "/posts/12435151125/larry-had-a-little-lamb" }
7
- let(:redirect_page) { new_redirect_page(permalink) }
8
- let(:item_url) { File.join(@site.config["url"], "2014", "01", "03", "moving-to-jekyll.md") }
9
- let(:page_content) { redirect_page.generate_redirect_content(item_url) }
4
+ let(:from) { "/foo" }
5
+ let(:to) { "/bar" }
6
+ let(:site_url) { site.config["url"] }
7
+ subject { described_class.from_paths(site, from, to) }
8
+ before { site.read }
9
+
10
+ context "being a page" do
11
+ before { subject.read_yaml(nil, nil, nil) }
12
+
13
+ it "returns no content" do
14
+ expect(subject.content).to eql("")
15
+ end
16
+
17
+ it "sets default data" do
18
+ expect(subject.to_liquid["layout"]).to eql("redirect")
19
+ expect(subject.to_liquid["sitemap"]).to be_falsey
20
+ end
21
+ end
22
+
23
+ context "creating a page from paths" do
24
+ it "sets the permalink" do
25
+ expect(subject.to_liquid["permalink"]).to eql(from)
26
+ end
27
+
28
+ it "sets redirect metadata" do
29
+ expect(subject.to_liquid).to have_key("redirect")
30
+ expect(subject.to_liquid["redirect"]["from"]).to eql(from)
31
+ expect(subject.to_liquid["redirect"]["to"]).to eql("#{site_url}#{to}")
32
+ end
33
+
34
+ context "with a document" do
35
+ let(:doc) { site.documents.first }
36
+
37
+ context "redirect from" do
38
+ let(:page) { described_class.redirect_from(doc, from) }
10
39
 
11
- context "#generate_redirect_content" do
12
- it "sets the #content to the generated refresh page" do
13
- expect(page_content).to eq("<!DOCTYPE html>\n<html lang=\"en-US\">\n<meta charset=\"utf-8\">\n<title>Redirecting…</title>\n<link rel=\"canonical\" href=\"#{item_url}\">\n<meta http-equiv=\"refresh\" content=\"0; url=#{item_url}\">\n<h1>Redirecting…</h1>\n<a href=\"#{item_url}\">Click here if you are not redirected.</a>\n<script>location=\"#{item_url}\"</script>\n</html>\n")
40
+ it "creates with redirect_from" do
41
+ expect(page.to_liquid["permalink"]).to eql(from)
42
+ expect(page.to_liquid).to have_key("redirect")
43
+ expect(page.to_liquid["redirect"]["from"]).to eql(from)
44
+ expected = "http://jekyllrb.com/2014/01/03/redirect-me-plz.html"
45
+ expect(page.to_liquid["redirect"]["to"]).to eql(expected)
46
+ end
47
+ end
48
+
49
+ context "redirect to" do
50
+ let(:page) { described_class.redirect_to(doc, to) }
51
+
52
+ context "redirecting to a path" do
53
+ let(:to) { "/bar" }
54
+
55
+ it "redirects" do
56
+ expect(page.to_liquid["permalink"]).to eql("/2014/01/03/redirect-me-plz.html")
57
+ expect(page.to_liquid).to have_key("redirect")
58
+ expect(page.to_liquid["redirect"]["to"]).to eql("#{site_url}#{to}")
59
+ expect(page.to_liquid["redirect"]["from"]).to eql("/2014/01/03/redirect-me-plz.html")
60
+ end
61
+
62
+ context "with no leading slash" do
63
+ let(:to) { "bar" }
64
+
65
+ it "redirects" do
66
+ expect(page.to_liquid).to have_key("redirect")
67
+ expect(page.to_liquid["redirect"]["to"]).to eql("#{site_url}/#{to}")
68
+ end
69
+ end
70
+
71
+ context "with a trailing slash" do
72
+ let(:to) { "/bar/" }
73
+
74
+ it "redirects" do
75
+ expect(page.to_liquid).to have_key("redirect")
76
+ expect(page.to_liquid["redirect"]["to"]).to eql("#{site_url}#{to}")
77
+ end
78
+ end
79
+ end
80
+
81
+ context "redirecting to a URL" do
82
+ let(:to) { "https://foo.invalid" }
83
+
84
+ it "redirects" do
85
+ expect(page.to_liquid["permalink"]).to eql("/2014/01/03/redirect-me-plz.html")
86
+ expect(page.to_liquid).to have_key("redirect")
87
+ expect(page.to_liquid["redirect"]["to"]).to eql("https://foo.invalid")
88
+ expect(page.to_liquid["redirect"]["from"]).to eql("/2014/01/03/redirect-me-plz.html")
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+
95
+ context "setting the paths" do
96
+ let(:from) { "/foo2" }
97
+ let(:to) { "/bar2" }
98
+
99
+ before { subject.set_paths(from, to) }
100
+
101
+ it "sets the paths" do
102
+ expect(subject.to_liquid["permalink"]).to eql(from)
103
+ expect(subject.to_liquid).to have_key("redirect")
104
+ expect(subject.to_liquid["redirect"]["from"]).to eql(from)
105
+ expect(subject.to_liquid["redirect"]["to"]).to eql("#{site_url}#{to}")
106
+ end
107
+ end
108
+
109
+ context "generating" do
110
+ before { site.generate }
111
+ let(:output) { Jekyll::Renderer.new(site, subject, site.site_payload).run }
112
+
113
+ it "renders the template" do
114
+ expect(output).to_not be_nil
115
+ expect(output.to_s).to_not be_empty
14
116
  end
15
117
 
16
118
  it "contains the meta refresh tag" do
17
- expect(page_content).to include("<meta http-equiv=\"refresh\" content=\"0; url=#{item_url}\">")
119
+ expect(output).to match("<meta http-equiv=\"refresh\" content=\"0; url=#{site_url}#{to}\">")
18
120
  end
19
121
 
20
- it "contains JavaScript redirect" do
21
- expect(page_content).to include("location=\"http://jekyllrb.com/2014/01/03/moving-to-jekyll.md\"")
122
+ it "contains the javascript redirect" do
123
+ expect(output).to match("<script>location=\"#{site_url}#{to}\"</script>")
22
124
  end
23
125
 
24
126
  it "contains canonical link in header" do
25
- expect(page_content).to include("<link rel=\"canonical\" href=\"http://jekyllrb.com/2014/01/03/moving-to-jekyll.md\">")
127
+ expect(output).to match("<link rel=\"canonical\" href=\"#{site_url}#{to}\">")
26
128
  end
27
129
 
28
- it "contains a clickable link to redirect" do
29
- 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>")
130
+ it "contains the clickable link" do
131
+ expect(output).to match("<a href=\"#{site_url}#{to}\">Click here if you are not redirected.</a>")
30
132
  end
31
133
  end
32
134
 
33
- context "when determining the write destination" do
34
- context "of a redirect page meant to be a dir" do
35
- let(:permalink_dir) { "/posts/1914798137981389/larry-had-a-little-lamb/" }
36
- let(:redirect_page) { new_redirect_page(permalink_dir) }
135
+ context "redirect from destination" do
136
+ context "when redirect from has no extension" do
137
+ let(:from) { "/foo" }
37
138
 
38
- it "knows to add the index.html if it's a folder" do
39
- expected = dest_dir("posts", "1914798137981389", "larry-had-a-little-lamb", "index.html").to_s
40
- dest = redirect_page.destination("/")
41
- dest = "#{@site.dest}#{dest}" unless dest.start_with? @site.dest
42
- expect(dest).to eql(expected)
139
+ it "adds .html" do
140
+ expected = File.expand_path "foo.html", site.dest
141
+ expect(subject.destination("/")).to eql(expected)
43
142
  end
44
143
  end
45
144
 
46
- context "of a redirect page meant to be a file" do
47
- it "knows not to add the index.html if it's not a folder" do
48
- expected = dest_dir("posts", "12435151125", "larry-had-a-little-lamb#{forced_output_ext}").to_s
49
- dest = redirect_page.destination("/")
50
- dest = "#{@site.dest}#{dest}" unless dest.start_with? @site.dest
51
- expect(dest).to eql(expected)
145
+ context "when redirect from is a directory" do
146
+ let(:from) { "/foo/" }
147
+
148
+ it "knows to add the index.html" do
149
+ expected = File.expand_path "foo/index.html", site.dest
150
+ expect(subject.destination("/")).to eql(expected)
52
151
  end
53
152
  end
54
- end
55
153
 
56
- context "when writing to disk" do
57
- let(:redirect_page_full_path) { redirect_page.destination(@site.dest) }
154
+ context "when redirect from is an HTML file" do
155
+ let(:from) { "/foo.html" }
58
156
 
59
- before(:each) do
60
- redirect_page.generate_redirect_content(item_url)
61
- redirect_page.write(@site.dest)
157
+ it "adds .html" do
158
+ expected = File.expand_path "foo.html", site.dest
159
+ expect(subject.destination("/")).to eql(expected)
160
+ end
62
161
  end
63
162
 
64
- it "fetches the path properly" do
65
- expect(redirect_page_full_path).to match /\/spec\/fixtures\/\_site\/posts\/12435151125\/larry-had-a-little-lamb#{forced_output_ext}$/
163
+ context "when redirect from is another extension" do
164
+ let(:from) { "/foo.htm" }
165
+
166
+ it "doesn't add .html" do
167
+ expected = File.expand_path "foo.htm", site.dest
168
+ expect(subject.destination("/")).to eql(expected)
169
+ end
66
170
  end
67
171
 
68
- it "is written to the proper location" do
69
- expect(File.exist?(redirect_page_full_path)).to be_truthy
172
+ context "when redirect from has no leading slash" do
173
+ let(:from) { "foo" }
174
+
175
+ it "adds the slash" do
176
+ expected = File.expand_path "foo.html", site.dest
177
+ expect(subject.destination("/")).to eql(expected)
178
+ end
179
+ end
180
+ end
181
+
182
+ context "output extension" do
183
+ context "with an extension" do
184
+ let(:from) { "foo.htm" }
185
+
186
+ it "honors the extension" do
187
+ expect(subject.output_ext).to eql(".htm")
188
+ end
70
189
  end
71
190
 
72
- it "writes the context we expect" do
73
- expect(File.read(redirect_page_full_path)).to eql(page_content)
191
+ context "with a trailing slash" do
192
+ let(:from) { "foo/" }
193
+
194
+ it "uses HTML" do
195
+ expect(subject.output_ext).to eql(".html")
196
+ end
197
+ end
198
+
199
+ context "with no slash" do
200
+ let(:from) { "foo" }
201
+
202
+ it "uses HTML" do
203
+ expect(subject.output_ext).to eql(".html")
204
+ end
74
205
  end
75
206
  end
76
207
  end
@@ -0,0 +1,61 @@
1
+ class RedirectableTestHelper
2
+ include JekyllRedirectFrom::Redirectable
3
+ attr_reader :to_liquid
4
+
5
+ def initialize(data)
6
+ @to_liquid = data
7
+ end
8
+ end
9
+
10
+ RSpec.describe JekyllRedirectFrom::Redirectable do
11
+ let(:data) { "" }
12
+ subject { RedirectableTestHelper.new(data) }
13
+
14
+ context "with strings" do
15
+ let(:data) { { "redirect_from" => "/foo", "redirect_to" => "/bar" } }
16
+
17
+ it "returns redirect_from" do
18
+ expect(subject.redirect_from).to eql(["/foo"])
19
+ end
20
+
21
+ it "returns redirect_to" do
22
+ expect(subject.redirect_to).to eql("/bar")
23
+ end
24
+ end
25
+
26
+ context "with arrays" do
27
+ let(:data) { { "redirect_from" => ["/foo"], "redirect_to" => ["/bar"] } }
28
+
29
+ it "returns redirect_from" do
30
+ expect(subject.redirect_from).to eql(["/foo"])
31
+ end
32
+
33
+ it "returns redirect_to" do
34
+ expect(subject.redirect_to).to eql("/bar")
35
+ end
36
+ end
37
+
38
+ context "with fields missing" do
39
+ let(:data) { {} }
40
+
41
+ it "returns an empty array for redirect_from" do
42
+ expect(subject.redirect_from).to eql([])
43
+ end
44
+
45
+ it "returns nil for redirect_to" do
46
+ expect(subject.redirect_to).to be_nil
47
+ end
48
+ end
49
+
50
+ context "with nils" do
51
+ let(:data) { { "redirect_from" => nil, "redirect_to" => nil } }
52
+
53
+ it "returns an empty array for redirect_from" do
54
+ expect(subject.redirect_from).to eql([])
55
+ end
56
+
57
+ it "returns nil for redirect_to" do
58
+ expect(subject.redirect_to).to be_nil
59
+ end
60
+ end
61
+ end
@@ -4,7 +4,6 @@ require File.expand_path("lib/jekyll-redirect-from.rb")
4
4
  RSpec.configure do |config|
5
5
  config.run_all_when_everything_filtered = true
6
6
  config.filter_run :focus
7
- config.order = 'random'
8
7
 
9
8
  config.expect_with :rspec do |c|
10
9
  c.syntax = :expect
@@ -12,77 +11,47 @@ RSpec.configure do |config|
12
11
 
13
12
  config.before(:each) do
14
13
  Jekyll.logger.log_level = :error
15
-
16
- @fixtures_path = Pathname.new(__FILE__).parent.join("fixtures")
17
- @dest = @fixtures_path.join("_site")
18
- @posts_src = @fixtures_path.join("_posts")
19
- @layouts_src = @fixtures_path.join("_layouts")
20
- @plugins_src = @fixtures_path.join("_plugins")
21
-
22
- @site = Jekyll::Site.new(Jekyll.configuration({
23
- "source" => @fixtures_path.to_s,
24
- "destination" => @dest.to_s,
25
- "plugins" => @plugins_src.to_s,
26
- "collections" => {
27
- "articles" => {"output" => true},
28
- "authors" => {}
29
- },
30
- "defaults" => [{
31
- "scope" => { "path" => "" },
32
- "values" => { "layout" => "layout" }
33
- }]
34
- }))
35
-
36
- @dest.rmtree if @dest.exist?
37
- @site.process
14
+ dest_path.rmtree if dest_path.exist?
15
+ site.reset
38
16
  end
39
17
 
40
18
  config.after(:each) do
41
- @dest.rmtree if @dest.exist?
42
- end
43
-
44
- def dest_dir(*paths)
45
- @dest.join(*paths)
46
- end
47
-
48
- def unpublished_doc
49
- @site.collections["authors"].docs.first
19
+ dest_path.rmtree if dest_path.exist?
50
20
  end
51
21
 
52
- def setup_doc(doc_filename)
53
- @site.collections["articles"].docs.find { |d| d.relative_path.match(doc_filename) }
22
+ def fixtures_path
23
+ Pathname.new(__FILE__).parent.join("fixtures")
54
24
  end
55
25
 
56
- def setup_post(file)
57
- Jekyll::Post.new(@site, @fixtures_path.to_s, '', file)
26
+ def dest_path
27
+ Pathname.new(site.dest)
58
28
  end
59
29
 
60
- def setup_page(file)
61
- Jekyll::Page.new(@site, @fixtures_path.to_s, File.dirname(file), File.basename(file))
62
- end
63
-
64
- def new_redirect_page(permalink)
65
- page = JekyllRedirectFrom::RedirectPage.new(@site, @site.source, "", "index.html")
66
- page.data['permalink'] = permalink
67
- page.data['sitemap'] = false
68
- page
69
- end
70
-
71
- def destination_sitemap
72
- @dest.join("sitemap.xml").read
73
- end
74
-
75
- def forced_output_ext
76
- JekyllRedirectFrom.jekyll_3? ? ".html" : ""
30
+ def dest_dir(*paths)
31
+ dest_path.join(*paths)
77
32
  end
78
- end
79
33
 
80
- class TestStringContainer
81
- def initialize(strValue)
82
- @val = strValue
34
+ def config
35
+ Jekyll.configuration({
36
+ "source" => fixtures_path.to_s,
37
+ "destination" => fixtures_path.join("_site").to_s,
38
+ "collections" => {
39
+ "articles" => { "output" => true },
40
+ "authors" => {}
41
+ },
42
+ "url" => "http://jekyllrb.com",
43
+ "gems" => [
44
+ "jekyll-redirect-from",
45
+ "jekyll-sitemap"
46
+ ],
47
+ "defaults" => [{
48
+ "scope" => { "path" => "" },
49
+ "values" => { "layout" => "layout" }
50
+ }]
51
+ })
83
52
  end
84
53
 
85
- def to_s
86
- @val
54
+ def site
55
+ @site ||= Jekyll::Site.new(config)
87
56
  end
88
57
  end