Skip to content

Commit 8d68486

Browse files
willnetmcmire
authored andcommitted
Respect extra_failure_lines in RSpec metadata
RSpec tests can have associated metadata, which is just a hash of content. One magic metadata property is `extra_failure_lines`. If this property is set, then RSpec will read its content and include it in the output it prints if the test fails. This property particularly comes into play in RSpec system specs, a wrapper around Rails system tests. If a system test fails, a screenshot is automatically taken of the page (provided the driver supports it) and a message is printed which details the path to the screenshot. RSpec's wrapper will capture output generated while the test is running and then store it in `extra_failure_lines`. By doing this, output (including the screenshot path, but potentially other information as well) is effectively moved to the spec failure message. This gem, as it patches key parts of `rspec-rails`, doesn't support `extra_failure_lines`, so anything in this metadata property is effectively discarded. This commit restores support. Co-authored-by: willnet <netwillnet@gmail.com>
1 parent 3c1e8fd commit 8d68486

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-3
lines changed

lib/super_diff/rspec/monkey_patches.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,10 @@ def failure_line_groups
218218
}
219219
end
220220
end
221-
221+
@failure_line_groups << {
222+
lines: extra_failure_lines,
223+
already_colorized: true
224+
}
222225
@failure_line_groups
223226
end
224227
end
@@ -241,6 +244,24 @@ def failure_slash_error_lines
241244
lines
242245
end
243246

247+
# Override to ensure that each item in the array returned is one and
248+
# only one line and doesn't contain any newline characters
249+
def extra_failure_lines
250+
@extra_failure_lines ||=
251+
begin
252+
original_lines = Array(example.metadata[:extra_failure_lines])
253+
normalized_lines =
254+
original_lines.flat_map { |line| line.split("\n") }
255+
unless normalized_lines.empty?
256+
unless normalized_lines.first == ""
257+
normalized_lines.unshift("")
258+
end
259+
normalized_lines.push("") unless normalized_lines.last == ""
260+
end
261+
normalized_lines
262+
end
263+
end
264+
244265
# Exclude this file from being included in backtraces, so that the
245266
# SnippetExtractor prints the right thing
246267
def find_failed_line
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
require "spec_helper"
2+
3+
RSpec.describe "Integration with RSpec's magic metadata", type: :integration do
4+
it "includes extra_failure_lines in failure messages" do
5+
as_both_colored_and_uncolored do |color_enabled|
6+
snippet = <<~TEST.strip
7+
RSpec.describe "test" do
8+
it { expect(true).to be(false) }
9+
10+
after do
11+
RSpec.current_example.metadata[:extra_failure_lines] = "foo\nbar"
12+
end
13+
end
14+
TEST
15+
program =
16+
make_plain_test_program(
17+
snippet,
18+
color_enabled: color_enabled,
19+
preserve_as_whole_file: true
20+
)
21+
22+
expected_output =
23+
build_expected_output(
24+
color_enabled: color_enabled,
25+
test_name: "test is expected to equal false",
26+
snippet: "it { expect(true).to be(false) }",
27+
expectation:
28+
proc do
29+
line do
30+
plain "Expected "
31+
actual "true"
32+
plain " to equal "
33+
expected "false"
34+
plain "."
35+
end
36+
end,
37+
extra_failure_lines:
38+
proc do
39+
indent by: 5 do
40+
line "foo"
41+
line "bar"
42+
end
43+
end
44+
)
45+
46+
expect(program).to produce_output_when_run(expected_output).in_color(
47+
color_enabled
48+
)
49+
end
50+
end
51+
end

spec/support/integration/helpers.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,17 @@ def build_expected_output(
4848
color_enabled:,
4949
snippet:,
5050
expectation:,
51+
test_name: "test passes",
5152
key_enabled: true,
5253
newline_before_expectation: false,
5354
indentation: 7,
54-
diff: nil
55+
diff: nil,
56+
extra_failure_lines: nil
5557
)
5658
colored(color_enabled: color_enabled) do
5759
line "Failures:\n"
5860

59-
line "1) test passes", indent_by: 2
61+
line "1) #{test_name}", indent_by: 2
6062

6163
line indent_by: 5 do
6264
bold "Failure/Error: "
@@ -106,6 +108,11 @@ def build_expected_output(
106108
newline
107109
end
108110
end
111+
112+
if extra_failure_lines
113+
newline
114+
evaluate_block(&extra_failure_lines)
115+
end
109116
end
110117
end
111118

0 commit comments

Comments
 (0)