/
draft_spec.rb
137 lines (114 loc) · 3.98 KB
/
draft_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# frozen_string_literal: true
RSpec.describe(Jekyll::Commands::Draft) do
let(:name) { "A test post" }
let(:args) { [name] }
let(:drafts_dir) { Pathname.new source_dir("_drafts") }
let(:path) { drafts_dir.join("a-test-post.md") }
before(:all) do
FileUtils.mkdir_p source_dir unless File.directory? source_dir
Dir.chdir source_dir
end
before(:each) do
FileUtils.mkdir_p drafts_dir unless File.directory? drafts_dir
end
after(:each) do
FileUtils.rm_r drafts_dir if File.directory? drafts_dir
end
it "creates a new draft" do
expect(path).not_to exist
capture_stdout { described_class.process(args) }
expect(path).to exist
end
it "writes a helpful success message" do
output = capture_stdout { described_class.process(args) }
expect(output).to include("New draft created at #{"_drafts/a-test-post.md".cyan}")
end
it "errors with no arguments" do
expect(lambda {
capture_stdout { described_class.process }
}).to raise_error("You must specify a name.")
end
it "creates the drafts folder if necessary" do
FileUtils.rm_r drafts_dir if File.directory? drafts_dir
capture_stdout { described_class.process(args) }
expect(drafts_dir).to exist
end
it "creates the draft with a specified extension" do
html_path = drafts_dir.join "a-test-post.html"
expect(html_path).not_to exist
capture_stdout { described_class.process(args, "extension" => "html") }
expect(html_path).to exist
end
it "creates a new draft 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
context "when the draft already exists" do
let(:name) { "An existing draft" }
let(:path) { drafts_dir.join("an-existing-draft.md") }
before(:each) do
FileUtils.touch path
end
it "raises an error" do
expect(lambda {
capture_stdout { described_class.process(args) }
}).to raise_error("A draft already exists at _drafts/an-existing-draft.md")
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: post!)
end
end
context "when a configuration file exists" do
let(:config) { source_dir("_config.yml") }
let(:drafts_dir) { Pathname.new source_dir(File.join("site", "_drafts")) }
let(:config_data) do
%(
source: site
)
end
before(:each) do
File.open(config, "w") do |f|
f.write(config_data)
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
context "and collections_dir is set" do
let(:collections_dir) { "my_collections" }
let(:drafts_dir) { Pathname.new source_dir("site", collections_dir, "_drafts") }
let(:config_data) do
%(
source: site
collections_dir: #{collections_dir}
)
end
it "should create drafts at the correct location" do
expect(path).not_to exist
capture_stdout { described_class.process(args) }
expect(path).to exist
end
it "should write a helpful message when successful" do
output = capture_stdout { described_class.process(args) }
generated_path = File.join("site", collections_dir, "_drafts", "a-test-post.md").cyan
expect(output).to include("New draft created at #{generated_path}")
end
end
end
context "when source option is set" do
let(:drafts_dir) { Pathname.new source_dir(File.join("site", "_drafts")) }
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