/
page_spec.rb
100 lines (82 loc) · 2.79 KB
/
page_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# frozen_string_literal: true
RSpec.describe(Jekyll::Commands::Page) do
let(:name) { "A test page" }
let(:args) { [name] }
let(:filename) { "a-test-page.md" }
let(:path) { Pathname.new(source_dir).join(filename) }
before(:all) do
FileUtils.mkdir_p source_dir unless File.directory? source_dir
Dir.chdir source_dir
end
after(:each) do
FileUtils.rm path if File.exist? path
end
it "creates a new page" do
expect(path).not_to exist
capture_stdout { described_class.process(args) }
expect(path).to exist
end
it "creates a new page with the specified extension" do
html_path = Pathname.new(source_dir).join "a-test-page.html"
FileUtils.rm html_path if File.exist? html_path
capture_stdout { described_class.process(args, "extension" => "html") }
expect(html_path).to exist
end
it "creates a new page with the specified layout" do
capture_stdout { described_class.process(args, "layout" => "other-layout") }
expect(File.read(path)).to match(%r!layout: other-layout!)
end
it "should write a helpful message when successful" do
output = capture_stdout { described_class.process(args) }
expect(output).to include("New page created at #{filename.cyan}")
end
it "errors with no arguments" do
expect(lambda {
capture_stdout { described_class.process }
}).to raise_error("You must specify a name.")
end
context "when the page already exists" do
let(:name) { "An existing page" }
let(:filename) { "an-existing-page.md" }
before(:each) do
FileUtils.touch path
end
it "displays a warning" do
output = capture_stdout { described_class.process(args) }
expect(output).to include("A page already exists at #{filename}".yellow)
end
it "overwrites if --force is given" do
expect(lambda {
capture_stdout { described_class.process(args, "force" => true) }
}).not_to raise_error
expect(File.read(path)).to match(%r!layout: page!)
end
end
context "when a configuration file exists" do
let(:config) { source_dir("_config.yml") }
let(:path) { Pathname.new(source_dir).join("site", filename) }
before(:each) do
File.open(config, "w") do |f|
f.write(%(
source: site
))
end
end
after(:each) do
FileUtils.rm(config)
end
it "should use source directory set by config" do
expect(path).not_to exist
capture_stdout { described_class.process(args) }
expect(path).to exist
end
end
context "when source option is set" do
let(:path) { Pathname.new(source_dir).join("site", filename) }
it "should use source directory set by command line option" do
expect(path).not_to exist
capture_stdout { described_class.process(args, "source" => "site") }
expect(path).to exist
end
end
end