/
post_spec.rb
186 lines (156 loc) · 5.75 KB
/
post_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# frozen_string_literal: true
RSpec.describe(Jekyll::Commands::Post) do
let(:name) { "A test post" }
let(:args) { [name] }
let(:posts_dir) { Pathname.new source_dir("_posts") }
let(:datestamp) { Time.now.strftime(Jekyll::Compose::DEFAULT_DATESTAMP_FORMAT) }
let(:timestamp) { Time.now.strftime(Jekyll::Compose::DEFAULT_TIMESTAMP_FORMAT) }
let(:filename) { "#{datestamp}-a-test-post.md" }
let(:path) { posts_dir.join(filename) }
before(:all) do
FileUtils.mkdir_p source_dir unless File.directory? source_dir
Dir.chdir source_dir
end
before(:each) do
FileUtils.mkdir_p posts_dir unless File.directory? posts_dir
allow(Jekyll::Compose::FileEditor).to receive(:system)
end
after(:each) do
FileUtils.rm_r posts_dir if File.directory? posts_dir
end
it "creates a new post" do
expect(path).not_to exist
capture_stdout { described_class.process(args) }
expect(path).to exist
end
it "creates a post with a specified date" do
path = posts_dir.join "2012-03-04-a-test-post.md"
expect(path).not_to exist
capture_stdout { described_class.process(args, { "date" => "2012-3-4" }) }
expect(path).to exist
expect(File.read(path)).to match(%r!date: 2012-03-04 00:00 \+0000!)
end
it "creates the post with a specified extension" do
html_path = posts_dir.join "#{datestamp}-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 post 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 post created at #{File.join("_posts", 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
it "creates the posts folder if necessary" do
FileUtils.rm_r posts_dir if File.directory? posts_dir
capture_stdout { described_class.process(args) }
expect(posts_dir).to exist
end
context "when the post already exists" do
let(:name) { "An existing post" }
let(:filename) { "#{Time.now.strftime(Jekyll::Compose::DEFAULT_DATESTAMP_FORMAT)}-an-existing-post.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 post already exists at _posts/#{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: post!)
end
end
context "when a configuration file exists" do
let(:config) { source_dir("_config.yml") }
let(:posts_dir) { Pathname.new source_dir("site", "_posts") }
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 "configuration is set" do
let(:posts_dir) { Pathname.new source_dir("_posts") }
let(:config_data) do
%(
jekyll_compose:
auto_open: true
post_default_front_matter:
description: my description
category:
)
end
it "creates post with the specified config" do
capture_stdout { described_class.process(args) }
post = File.read(path)
expect(post).to match(%r!description: my description!)
expect(post).to match(%r!category: !)
end
context "env variable EDITOR is set up" do
before { ENV["EDITOR"] = "nano" }
it "opens post in default editor" do
expect(Jekyll::Compose::FileEditor).to receive(:run_editor).with("nano", path.to_s)
capture_stdout { described_class.process(args) }
end
context "env variable JEKYLL_EDITOR is set up" do
before { ENV["JEKYLL_EDITOR"] = "nano" }
it "opens post in jekyll editor" do
expect(Jekyll::Compose::FileEditor).to receive(:run_editor).with("nano", path.to_s)
capture_stdout { described_class.process(args) }
end
end
end
end
context "and collections_dir is set" do
let(:collections_dir) { "my_collections" }
let(:posts_dir) { Pathname.new source_dir("site", collections_dir, "_posts") }
let(:config_data) do
%(
source: site
collections_dir: #{collections_dir}
)
end
it "should create posts 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, "_posts", filename).cyan
expect(output).to include("New post created at #{generated_path}")
end
end
end
context "when source option is set" do
let(:posts_dir) { Pathname.new source_dir("site", "_posts") }
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