Skip to content

Commit cf92404

Browse files
authored
Improved test failure message (#78)
* Improved failure message Includes the failed statement and the backtrace. * Match fail message with regex due to changed backtrace in ruby 3.4 In rube 3.4 the formatted backtrace does not use the backtick (`) but encloses the label with only single quotes.
1 parent 94831ee commit cf92404

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

lib/ruby_lsp/ruby_lsp_rspec/rspec_formatter.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ def example_failed(notification)
4545
example = notification.example
4646
uri = uri_for(example)
4747
id = generate_id(example)
48-
RubyLsp::LspReporter.instance.record_fail(id: id, message: notification.exception.message, uri: uri)
48+
message = notification.message_lines.join("\n")
49+
message << "\n\n"
50+
message << notification.formatted_backtrace.map(&method(:adjust_backtrace)).map { |s| "# " + s }.join("\n")
51+
RubyLsp::LspReporter.instance.record_fail(id: id, message: message, uri: uri)
4952
end
5053

5154
def example_pending(notification)
@@ -69,6 +72,12 @@ def uri_for(example)
6972
def generate_id(example)
7073
[example, *example.example_group.parent_groups].reverse.map(&:location).join("::")
7174
end
75+
76+
def adjust_backtrace(backtrace)
77+
# Correct the backtrace entry so that vscode recognized it as a link to open
78+
parts = backtrace.split(":", 3)
79+
parts[0].sub(/^\./, "file://" + File.expand_path(".")) + ":" + parts[1] + " : " + parts[2]
80+
end
7281
end
7382
end
7483
end

spec/rspec_formatter_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
"method" => "fail",
8383
"params" => {
8484
"id" => "./spec/fixtures/rspec_example_spec.rb:11::./spec/fixtures/rspec_example_spec.rb:12::./spec/fixtures/rspec_example_spec.rb:17",
85-
"message" => "\nexpected: 1\n got: 2\n\n(compared using ==)\n",
85+
"message" => %r{Failure/Error: expect\(2\).to eq\(1\)\n\n expected: 1\n got: 2\n\n \(compared using ==\)\n\n# file://#{fixture_path}:18 : in [`']block \(3 levels\) in <top \(required\)>'},
8686
"uri" => "file://#{fixture_path}",
8787
},
8888
},
@@ -128,14 +128,14 @@
128128
"method" => "fail",
129129
"params" => {
130130
"id" => "./spec/fixtures/rspec_example_spec.rb:11::./spec/fixtures/rspec_example_spec.rb:12::./spec/fixtures/rspec_example_spec.rb:30",
131-
"message" => "oops",
131+
"message" => %r{Failure/Error: raise "oops"\n\nRuntimeError:\n oops\n\n# file://#{fixture_path}:31 : in [`']block \(3 levels\) in <top \(required\)>'},
132132
"uri" => "file://#{fixture_path}",
133133
},
134134
},
135135
{ "method" => "finish", "params" => {} },
136136
]
137137

138-
expect(events).to eq(expected)
138+
expect(events).to match(expected)
139139
end
140140

141141
describe "RubyLsp::RSpec::RSpecFormatter notifications" do
@@ -175,8 +175,8 @@
175175
end
176176

177177
it "invokes ProgressFormatter's example_failed" do
178-
exception = double("Exception", message: "error message")
179-
allow(notification).to receive(:exception).and_return(exception)
178+
allow(notification).to receive(:message_lines).and_return(["message lines"])
179+
allow(notification).to receive(:formatted_backtrace).and_return(["spec/example_spec.rb:13:in `something'"])
180180

181181
expect_any_instance_of(RSpec::Core::Formatters::ProgressFormatter).to receive(:example_failed)
182182

0 commit comments

Comments
 (0)