/
jekyll_steps.rb
152 lines (126 loc) · 3.67 KB
/
jekyll_steps.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
Before do
FileUtils.rm_rf(TEST_DIR)
FileUtils.mkdir(TEST_DIR)
Dir.chdir(TEST_DIR)
end
Given /^I have a blank site in "(.*)"$/ do |path|
FileUtils.mkdir(path)
end
# Like "I have a foo file" but gives a yaml front matter so jekyll actually processes it
Given /^I have an? "(.*)" page(?: with (.*) "(.*)")? that contains "(.*)"$/ do |file, key, value, text|
File.open(file, 'w') do |f|
f.write <<EOF
---
#{key || 'layout'}: #{value || 'nil'}
---
#{text}
EOF
end
end
Given /^I have an? "(.*)" file that contains "(.*)"$/ do |file, text|
File.open(file, 'w') do |f|
f.write(text)
end
end
Given /^I have an? (.*) (layout|theme) that contains "(.*)"$/ do |name, type, text|
folder = if type == 'layout'
'_layouts'
else
'_theme'
end
File.open(File.join(folder, name + '.html'), 'w') do |f|
f.write(text)
end
end
Given /^I have an? (.*) directory$/ do |dir|
FileUtils.mkdir_p(dir)
end
Given /^I have the following (draft|post)s?(?: (in|under) "([^"]+)")?:$/ do |status, direction, folder, table|
table.hashes.each do |post|
title = slug(post['title'])
ext = post['type'] || 'textile'
before, after = location(folder, direction)
if "draft" == status
folder_post = '_drafts'
filename = "#{title}.#{ext}"
elsif "post" == status
parsed_date = Time.xmlschema(post['date']) rescue Time.parse(post['date'])
folder_post = '_posts'
filename = "#{parsed_date.strftime('%Y-%m-%d')}-#{title}.#{ext}"
end
path = File.join(before, folder_post, after, filename)
matter_hash = {}
%w(title layout tag tags category categories published author path date).each do |key|
matter_hash[key] = post[key] if post[key]
end
matter = matter_hash.map { |k, v| "#{k}: #{v}\n" }.join.chomp
content = if post['input'] && post['filter']
"{{ #{post['input']} | #{post['filter']} }}"
else
post['content']
end
File.open(path, 'w') do |f|
f.write <<EOF
---
#{matter}
---
#{content}
EOF
end
end
end
Given /^I have a configuration file with "(.*)" set to "(.*)"$/ do |key, value|
File.open('_config.yml', 'w') do |f|
f.write("#{key}: #{value}\n")
end
end
Given /^I have a configuration file with:$/ do |table|
File.open('_config.yml', 'w') do |f|
table.hashes.each do |row|
f.write("#{row["key"]}: #{row["value"]}\n")
end
end
end
Given /^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
When /^I run jekyll$/ do
run_jekyll
end
When /^I run jekyll with drafts$/ do
run_jekyll(:drafts => true)
end
When /^I debug jekyll$/ do
run_jekyll(:debug => true)
end
When /^I change "(.*)" to contain "(.*)"$/ do |file, text|
File.open(file, 'a') do |f|
f.write(text)
end
end
Then /^the (.*) directory should exist$/ do |dir|
assert File.directory?(dir), "The directory \"#{dir}\" does not exist"
end
Then /^I should see "(.*)" in "(.*)"$/ do |text, file|
assert Regexp.new(text).match(File.open(file).readlines.join)
end
Then /^I should see escaped "(.*)" in "(.*)"$/ do |text, file|
assert Regexp.new(Regexp.escape(text)).match(File.open(file).readlines.join)
end
Then /^the "(.*)" file should exist$/ do |file|
assert File.file?(file)
end
Then /^the "(.*)" file should not exist$/ do |file|
assert !File.exists?(file)
end
Then /^I should see today's time in "(.*)"$/ do |file|
assert_match Regexp.new(Regexp.escape(Time.now.to_s)), File.open(file).readlines.join
end
Then /^I should see today's date in "(.*)"$/ do |file|
assert_match Regexp.new(Date.today.to_s), File.open(file).readlines.join
end