This repository was archived by the owner on Nov 30, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 756
/
Copy pathadditional_cli_steps.rb
296 lines (243 loc) · 9.87 KB
/
additional_cli_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
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
require 'rspec/core' # to fix annoying "undefined method `configuration' for RSpec:Module (NoMethodError)"
require './spec/support/formatter_support'
# For Ruby 3.4.0 hash formatting
Then /^the output should print the (include|exclude)d tags {(\w+): (.*)}$/ do |word, key, value|
if RUBY_VERSION.to_f > 3.3
expect(all_output).to include "#{word} {#{key}: #{value}}"
else
expect(all_output).to include "#{word} {:#{key}=>#{value}}"
end
end
Then /^the output should contain in either hash syntax "(.*)"$/ do |string|
if RUBY_VERSION.to_f > 3.3
step "the output should contain \"#{string.gsub(/:(\w+)=>/, '\1: ')}\""
else
step "the output should contain \"#{string}\""
end
end
Then /^the output should contain all of these:$/ do |table|
table.raw.flatten.each do |string|
expect(all_output).to include(string)
end
end
Then /^the output should not contain any of these:$/ do |table|
table.raw.flatten.each do |string|
expect(all_output).not_to include(string)
end
end
Then /^the output should contain one of the following:$/ do |table|
matching_output = table.raw.flatten.select do |string|
all_output.include?(string)
end
expect(matching_output.count).to eq(1)
end
Then /^the example(?:s)? should(?: all)? pass$/ do
step %q{the output should contain "0 failures"}
step %q{the output should not contain "0 examples"}
step %q{the exit status should be 0}
end
Then /^the example(?:s)? should(?: all)? fail$/ do
step %q{the output should not contain "0 examples"}
step %q{the output should not contain "0 failures"}
step %q{the exit status should be 1}
example_summary = /(\d+) examples?, (\d+) failures?/.match(all_output)
example_count, failure_count = example_summary.captures
expect(failure_count).to eq(example_count)
end
Then /^the process should succeed even though no examples were run$/ do
step %q{the output should contain "0 examples, 0 failures"}
step %q{the exit status should be 0}
end
addition_example_formatter_output = <<-EOS
Addition
works
EOS
Then /^the output from `([^`]+)` (should(?: not)?) be in documentation format$/ do |cmd, should_or_not|
step %Q{I run `#{cmd}`}
step %q{the examples should all pass}
step %Q{the output from "#{cmd}" #{should_or_not} contain "#{addition_example_formatter_output}"}
end
Then(/^the output from `([^`]+)` should indicate it ran only the subtraction file$/) do |cmd|
step %Q{I run `#{cmd}`}
step %q{the examples should all pass}
step %Q{the output from "#{cmd}" should contain "1 example, 0 failures"}
step %Q{the output from "#{cmd}" should contain "Subtraction"}
step %Q{the output from "#{cmd}" should not contain "Addition"}
end
Then /^the backtrace\-normalized output should contain:$/ do |partial_output|
# ruby 1.9 includes additional stuff in the backtrace,
# so we need to normalize it to compare it with our expected output.
normalized_output = all_output.split("\n").map do |line|
line =~ /(^\s+# [^:]+:\d+)/ ? $1 : line # http://rubular.com/r/zDD7DdWyzF
end.join("\n")
expect(normalized_output).to include(partial_output)
end
Then /^the output should not contain any error backtraces$/ do
step %q{the output should not contain "lib/rspec/core"}
end
# This step can be generalized if it's ever used to test other colors
Then /^the failing example is printed (?:wrapped )?in (.*)$/ do |color|
code =
case color
when "magenta" then "\e[35m"
when /"(.*)"/ then "\e[#{$1}m"
end
# \e[35m = enable magenta
# \e[0m = reset colors
expect(all_output).to include(code + "F" + "\e[0m")
end
Then /^the output from `([^`]+)` should contain "(.*?)"$/ do |cmd, expected_output|
step %Q{I run `#{cmd}`}
step %Q{the output from "#{cmd}" should contain "#{expected_output}"}
end
Then /^the output from `([^`]+)` should not contain "(.*?)"$/ do |cmd, expected_output|
step %Q{I run `#{cmd}`}
step %Q{the output from "#{cmd}" should not contain "#{expected_output}"}
end
Then /^the output from `([^`]+)` should( not)? contain %R{(.*?)}$/ do |cmd, negated, expected_output|
step %Q{I run `#{cmd}`}
command = aruba.command_monitor.find(Aruba.platform.detect_ruby(cmd))
regexp = Regexp.new(expected_output)
if negated
expect(command.output).to_not match regexp
else
expect(command.output).to match regexp
end
end
Given /^I have a brand new project with no files$/ do
cd('.') do
expect(Dir["**/*"]).to eq([])
end
end
Given /^I have run `([^`]*)`$/ do |cmd|
run_command_and_stop(sanitize_text(cmd), :fail_on_error => true)
end
Given(/^a vendored gem named "(.*?)" containing a file named "(.*?)" with:$/) do |gem_name, file_name, file_contents|
gem_dir = "vendor/#{gem_name}-1.2.3"
step %Q{a file named "#{gem_dir}/#{file_name}" with:}, file_contents
set_environment_variable('RUBYOPT', ENV['RUBYOPT'] + " -I#{gem_dir}/lib")
end
When('I accept the recommended settings by removing `=begin` and `=end` from `spec_helper.rb`') do
cd('.') do
spec_helper = File.read("spec/spec_helper.rb")
expect(spec_helper).to include("=begin", "=end")
to_keep = spec_helper.lines.reject do |line|
line.start_with?("=begin") || line.start_with?("=end")
end
File.open("spec/spec_helper.rb", "w") { |f| f.write(to_keep.join) }
expect(File.read("spec/spec_helper.rb")).not_to include("=begin", "=end")
end
end
When /^I create "([^"]*)" with the following content:$/ do |file_name, content|
write_file(file_name, content)
end
Given(/^I have run `([^`]*)` once, resulting in "([^"]*)"$/) do |command, output_snippet|
step %Q{I run `#{command}`}
step %Q{the output from "#{command}" should contain "#{output_snippet}"}
end
Then /^the output should contain %R{(.*)}$/ do |output_regexp|
expect(all_output).to match(Regexp.new(output_regexp))
end
When(/^I fix "(.*?)" by replacing "(.*?)" with "(.*?)"$/) do |file_name, original, replacement|
cd('.') do
contents = File.read(file_name)
expect(contents).to include(original)
fixed = contents.sub(original, replacement)
File.open(file_name, "w") { |f| f.write(fixed) }
end
end
Given(/^I have not configured `example_status_persistence_file_path`$/) do
cd('.') do
return unless File.exist?("spec/spec_helper.rb")
return unless File.read("spec/spec_helper.rb").include?("example_status_persistence_file_path")
File.open("spec/spec_helper.rb", "w") { |f| f.write("") }
end
end
Given(/^files "(.*?)" through "(.*?)" with an unrelated passing spec in each file$/) do |file1, file2|
index_1 = Integer(file1[/\d+/])
index_2 = Integer(file2[/\d+/])
pattern = file1.sub(/\d+/, '%s')
index_1.upto(index_2) do |index|
write_file(pattern % index, <<-EOS)
RSpec.describe "Spec file #{index}" do
example { }
end
EOS
end
end
Then(/^bisect should (succeed|fail) with output like:$/) do |succeed, expected_output|
last_process = all_commands.last
expected_status = succeed == "succeed" ? 0 : 1
expect(last_process.exit_status).to eq(expected_status),
"Expected exit status of #{expected_status} but got #{last_process.exit_status} \n\n" \
"Output:\n\n#{last_process.stdout}"
expected = normalize_durations(expected_output)
actual = normalize_durations(last_process.stdout).sub(/\n+\Z/, '')
if !RSpec::Support::RubyFeatures.fork_supported?
expected.gsub!('runner: :fork', 'runner: :shell')
end
if expected.include?("# ...")
expected_start, expected_end = expected.split("# ...")
expect(actual).to start_with(expected_start).and end_with(expected_end)
else
expect(actual).to eq(expected)
end
end
When(/^I run `([^`]+)` and abort in the middle with ctrl\-c$/) do |cmd|
set_environment_variable('RUBYOPT', ENV['RUBYOPT'] + " -r#{File.expand_path("../../support/send_sigint_during_bisect.rb", __FILE__)}")
step "I run `#{cmd}`"
end
Then(/^it should fail and list all the failures:$/) do |string|
step %q{the exit status should not be 0}
expect(normalize_failure_output(all_output)).to include(normalize_failure_output(string))
end
Then(/^it should pass and list all the pending examples:$/) do |string|
step %q{the exit status should be 0}
expect(normalize_failure_output(all_output)).to include(normalize_failure_output(string))
end
Then(/^the output should report "slow before context hook" as the slowest example group$/) do
# These expectations are trying to guard against a regression that introduced
# this output:
# Top 1 slowest example groups:
# slow before context hook
# Inf seconds average (0.00221 seconds / 0 examples) RSpec::ExampleGroups::SlowBeforeContextHook::Nested
#
# Problems:
# - "Inf seconds"
# - 0 examples
# - "Nested" group listed (it should be the outer group)
# - The example group class name is listed (it should be the location)
output = all_output
expect(output).not_to match(/nested/i)
expect(output).not_to match(/inf/i)
expect(output).not_to match(/\b0 examples/i)
seconds = '\d+(?:\.\d+)? seconds'
expect(output).to match(
%r{Top 1 slowest example groups?:\n\s+slow before context hook\n\s+#{seconds} average \(#{seconds} / 1 example\) \./spec/example_spec\.rb:1}
)
end
Given(/^I have changed `([^`]+)` to `([^`]+)` in "(.*?)"$/) do |old_code, new_code, file_name|
cd('.') do
file_content = File.read(file_name)
expect(file_content).to include(old_code)
new_file_content = file_content.sub(old_code, new_code)
File.open(file_name, "w") { |f| f.write(new_file_content) }
end
end
Then /^the file `([^`]+)` should exist$/ do |file|
cd('.') do
File.exist?(file)
end
end
module Normalization
def normalize_failure_output(text)
whitespace_normalized = text.lines.map { |line| line.sub(/\s+$/, '').sub(/:in .*$/, '') }.join
# 1.8.7 and JRuby produce slightly different output for `Hash#fetch` errors, so we
# convert it to the same output here to match our expectation.
whitespace_normalized.
sub("IndexError", "KeyError").
sub(/key not found.*$/, "key not found")
end
end
World(Normalization)
World(FormatterSupport)