forked from jekyll/jekyll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep_definitions.rb
417 lines (326 loc) · 9.81 KB
/
step_definitions.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# frozen_string_literal: true
Before do
FileUtils.rm_rf(Paths.test_dir) if Paths.test_dir.exist?
FileUtils.mkdir_p(Paths.test_dir) unless Paths.test_dir.directory?
Dir.chdir(Paths.test_dir)
@timezone_before_scenario = ENV["TZ"]
end
#
After do
FileUtils.rm_rf(Paths.test_dir) if Paths.test_dir.exist?
Paths.output_file.delete if Paths.output_file.exist?
Paths.status_file.delete if Paths.status_file.exist?
Dir.chdir(Paths.test_dir.parent)
ENV["TZ"] = @timezone_before_scenario
end
#
Given(%r!^I have a blank site in "(.*)"$!) do |path|
unless File.exist?(path)
then FileUtils.mkdir_p(path)
end
end
#
Given(%r!^I do not have a "(.*)" directory$!) do |path|
Paths.test_dir.join(path).directory?
end
#
Given(%r!^I have an? "(.*)" page(?: with (.*) "(.*)")? that contains "(.*)"$!) do |file, key, value, text|
File.write(file, <<~DATA)
---
#{key || "layout"}: #{value || "none"}
---
#{text}
DATA
end
#
Given(%r!^I have an? "(.*)" file that contains "(.*)"$!) do |file, text|
File.write(file, text)
end
#
Given(%r!^I have an? (.*) (layout|theme) that contains "(.*)"$!) do |name, type, text|
folder = type == "layout" ? "_layouts" : "_theme"
destination_file = Pathname.new(File.join(folder, "#{name}.html"))
FileUtils.mkdir_p(destination_file.parent) unless destination_file.parent.directory?
File.write(destination_file, text)
end
#
Given(%r!^I have an? "(.*)" file with content:$!) do |file, text|
File.write(file, text)
end
#
Given(%r!^I have an? "(.*)" page with content:$!) do |file, text|
File.write(file, <<~DATA)
---
---
#{text}
DATA
end
#
Given(%r!^I have an? (.*) directory$!) do |dir|
unless File.directory?(dir)
then FileUtils.mkdir_p(dir)
end
end
#
Given(%r!^I have the following (draft|page|post)s?(?: (in|under) "([^"]+)")?:$!) do |status, direction, folder, table|
table.hashes.each do |input_hash|
title = slug(input_hash["title"])
ext = input_hash["type"] || "markdown"
filename = "#{title}.#{ext}" if %w(draft page).include?(status)
before, after = location(folder, direction)
dest_folder = "_drafts" if status == "draft"
dest_folder = "_posts" if status == "post"
dest_folder = "" if status == "page"
if status == "post"
parsed_date = Time.xmlschema(input_hash["date"]) rescue Time.parse(input_hash["date"])
input_hash["date"] = parsed_date
filename = "#{parsed_date.strftime("%Y-%m-%d")}-#{title}.#{ext}"
end
path = File.join(before, dest_folder, after, filename)
File.write(path, file_content_from_hash(input_hash))
end
end
#
Given(%r!^I have the following (draft|post)s? within the "(.*)" directory:$!) do |type, folder, table|
table.hashes.each do |input_hash|
title = slug(input_hash["title"])
parsed_date = Time.xmlschema(input_hash["date"]) rescue Time.parse(input_hash["date"])
filename = type == "draft" ? "#{title}.markdown" : "#{parsed_date.strftime("%Y-%m-%d")}-#{title}.markdown"
path = File.join(folder, "_#{type}s", filename)
File.write(path, file_content_from_hash(input_hash))
end
end
#
Given(%r!^I have the following documents? under the (.*) collection:$!) do |folder, table|
table.hashes.each do |input_hash|
title = slug(input_hash["title"])
filename = "#{title}.md"
dest_folder = "_#{folder}"
path = File.join(dest_folder, filename)
File.write(path, file_content_from_hash(input_hash))
end
end
#
Given(%r!^I have the following documents? under the "(.*)" collection within the "(.*)" directory:$!) do |label, dir, table|
table.hashes.each do |input_hash|
title = slug(input_hash["title"])
path = File.join(dir, "_#{label}", "#{title}.md")
File.write(path, file_content_from_hash(input_hash))
end
end
#
Given(%r!^I have the following documents? nested inside "(.*)" directory under the "(.*)" collection within the "(.*)" directory:$!) do |subdir, label, dir, table|
table.hashes.each do |input_hash|
title = slug(input_hash["title"])
path = File.join(dir, "_#{label}", subdir, "#{title}.md")
File.write(path, file_content_from_hash(input_hash))
end
end
#
Given(%r!^I have a configuration file with "(.*)" set to "(.*)"$!) do |key, value|
config = \
if source_dir.join("_config.yml").exist?
SafeYAML.load_file(source_dir.join("_config.yml"))
else
{}
end
config[key] = YAML.load(value)
Jekyll.set_timezone(value) if key == "timezone"
File.write("_config.yml", YAML.dump(config))
end
#
Given(%r!^I have a configuration file with:$!) do |table|
table.hashes.each do |row|
step %(I have a configuration file with "#{row["key"]}" set to "#{row["value"]}")
end
end
#
Given(%r!^I have a configuration file with "([^\"]*)" set to:$!) do |key, table|
File.open("_config.yml", "w") do |f|
f.write("#{key}:\n")
table.hashes.each do |row|
f.write("- #{row["value"]}\n")
end
end
end
#
Given(%r!^I have fixture collections(?: in "(.*)" directory)?$!) do |directory|
collections_dir = File.join(source_dir, directory.to_s)
FileUtils.cp_r Paths.source_dir.join("test", "source", "_methods"), collections_dir
FileUtils.cp_r Paths.source_dir.join("test", "source", "_thanksgiving"), collections_dir
FileUtils.cp_r Paths.source_dir.join("test", "source", "_tutorials"), collections_dir
end
#
Given(%r!^I wait (\d+) second(s?)$!) do |time, _|
sleep(time.to_f)
end
#
When(%r!^I run jekyll(.*)$!) do |args|
run_jekyll(args)
if args.include?("--verbose") || ENV["DEBUG"]
warn "\n#{jekyll_run_output}\n"
end
end
#
When(%r!^I run bundle(.*)$!) do |args|
run_bundle(args)
if args.include?("--verbose") || ENV["DEBUG"]
warn "\n#{jekyll_run_output}\n"
end
end
#
When(%r!^I run gem(.*)$!) do |args|
run_rubygem(args)
if args.include?("--verbose") || ENV["DEBUG"]
warn "\n#{jekyll_run_output}\n"
end
end
#
When(%r!^I run git add .$!) do
run_in_shell("git", "add", ".", "--verbose")
end
#
When(%r!^I decide to build the theme gem$!) do
Dir.chdir(Paths.theme_gem_dir)
[
"_includes/blank.html",
"_sass/blank.scss",
"assets/blank.scss",
"_config.yml"
].each do |filename|
File.new(filename, "w")
end
end
#
When(%r!^I change "(.*)" to contain "(.*)"$!) do |file, text|
File.open(file, "a") do |f|
f.write(text)
end
end
#
When(%r!^I delete the file "(.*)"$!) do |file|
File.delete(file)
end
#
Then(%r!^the (.*) directory should +(not )?exist$!) do |dir, negative|
if negative.nil?
expect(Pathname.new(dir)).to exist
else
expect(Pathname.new(dir)).to_not exist
end
end
#
Then(%r!^I should (not )?see "(.*)" in "(.*)"$!) do |negative, text, file|
step %(the "#{file}" file should exist)
regexp = Regexp.new(text, Regexp::MULTILINE)
if negative.nil? || negative.empty?
expect(file_contents(file)).to match regexp
else
expect(file_contents(file)).not_to match regexp
end
end
#
Then(%r!^I should (not )?see "(.*)" in "(.*)" if on Windows$!) do |negative, text, file|
step %(the "#{file}" file should exist)
regexp = Regexp.new(text, Regexp::MULTILINE)
if negative.nil? || negative.empty?
if Jekyll::Utils::Platforms.really_windows?
expect(file_contents(file)).to match regexp
else
expect(file_contents(file)).not_to match regexp
end
end
end
#
Then(%r!^I should (not )?see "(.*)" in "(.*)" unless Windows$!) do |negative, text, file|
step %(the "#{file}" file should exist)
regexp = Regexp.new(text, Regexp::MULTILINE)
if negative.nil? || negative.empty?
if Jekyll::Utils::Platforms.really_windows?
expect(file_contents(file)).not_to match regexp
else
expect(file_contents(file)).to match regexp
end
end
end
#
Then(%r!^I should see date "(.*)" in "(.*)" unless Windows$!) do |text, file|
step %(the "#{file}" file should exist)
regexp = Regexp.new(text)
if Jekyll::Utils::Platforms.really_windows? && !dst_active?
expect(file_contents(file)).not_to match regexp
else
expect(file_contents(file)).to match regexp
end
end
#
Then(%r!^I should see date "(.*)" in "(.*)" if on Windows$!) do |text, file|
step %(the "#{file}" file should exist)
regexp = Regexp.new(text)
if Jekyll::Utils::Platforms.really_windows? && !dst_active?
expect(file_contents(file)).to match regexp
else
expect(file_contents(file)).not_to match regexp
end
end
#
Then(%r!^I should see exactly "(.*)" in "(.*)"$!) do |text, file|
step %(the "#{file}" file should exist)
expect(file_contents(file).strip).to eq text
end
#
Then(%r!^I should see escaped "(.*)" in "(.*)"$!) do |text, file|
step %(I should see "#{Regexp.escape(text)}" in "#{file}")
end
#
Then(%r!^the "(.*)" file should +(not )?exist$!) do |file, negative|
if negative.nil?
expect(Pathname.new(file)).to exist
else
expect(Pathname.new(file)).to_not exist
end
end
#
Then(%r!^I should see today's time in "(.*)"$!) do |file|
step %(I should see "#{seconds_agnostic_time(Time.now)}" in "#{file}")
end
#
Then(%r!^I should see today's date in "(.*)"$!) do |file|
step %(I should see "#{Date.today}" in "#{file}")
end
#
Then(%r!^I should (not )?see "(.*)" in the build output$!) do |negative, text|
if negative.nil? || negative.empty?
expect(jekyll_run_output).to match Regexp.new(text)
else
expect(jekyll_run_output).not_to match Regexp.new(text)
end
end
#
Then(%r!^I should get an updated git index$!) do
index = %w(
.gitignore
Gemfile
LICENSE.txt
README.md
_config.yml
_includes/blank.html
_layouts/default.html
_layouts/page.html
_layouts/post.html
_sass/blank.scss
assets/blank.scss
my-cool-theme.gemspec
)
index.each do |file|
expect(jekyll_run_output).to match file
end
end
#
Then(%r!^I should get a zero exit(?:\-| )status$!) do
step %(I should see "EXIT STATUS: 0" in the build output)
end
#
Then(%r!^I should get a non-zero exit(?:\-| )status$!) do
step %(I should not see "EXIT STATUS: 0" in the build output)
end