Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit cff6940

Browse files
committed
Merge pull request #2244 from lordofthelake/patch-1
Fix NoMethodError for NilClass for Java backtraces
2 parents d693161 + ad87512 commit cff6940

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/rspec/core/formatters/exception_presenter.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,13 @@ def read_failed_lines
204204
return ["Unable to find matching line from backtrace"]
205205
end
206206

207-
file_path, line_number = matching_line.match(/(.+?):(\d+)(|:\d+)/)[1..2]
207+
file_and_line_number = matching_line.match(/(.+?):(\d+)(|:\d+)/)
208+
209+
unless file_and_line_number
210+
return ["Unable to infer file and line number from backtrace"]
211+
end
212+
213+
file_path, line_number = file_and_line_number[1..2]
208214
max_line_count = RSpec.configuration.max_displayed_failure_line_count
209215
lines = SnippetExtractor.extract_expression_lines_at(file_path, line_number.to_i, max_line_count)
210216
RSpec.world.source_cache.syntax_highlighter.highlight(lines)

spec/rspec/core/formatters/exception_presenter_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,14 @@ def read_failed_lines
478478
end
479479
end
480480

481+
context "when the stack trace is from a java exception" do
482+
let(:exception) { instance_double(Exception, :backtrace => [ "org.jruby.SomeJavaException(Unknown Source)"]) }
483+
484+
it "reports that it was unable to infer a code location from the backtrace" do
485+
expect(read_failed_lines.first).to include("Unable to infer file and line number from backtrace")
486+
end
487+
end
488+
481489
context "when ruby reports a file that does not exist" do
482490
let(:file) { "#{__FILE__}/blah.rb" }
483491
let(:exception) { instance_double(Exception, :backtrace => [ "#{file}:1"]) }

0 commit comments

Comments
 (0)