Skip to content

Commit

Permalink
Fix crash when expect_corrections runs into an infinite loop (#8779)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvandersluis authored Sep 25, 2020
1 parent 73d657a commit da5b737
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
10 changes: 5 additions & 5 deletions lib/rubocop/rspec/expect_offense.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,14 @@ def expect_offense(source, file = nil, severity: nil, **replacements)
"#{@processed_source.diagnostics.map(&:render).join("\n")}"
end

offenses = _investigate(cop, @processed_source)
@offenses = _investigate(cop, @processed_source)
actual_annotations =
expected_annotations.with_offense_annotations(offenses)
expected_annotations.with_offense_annotations(@offenses)

expect(actual_annotations).to eq(expected_annotations), ''
expect(offenses.map(&:severity).uniq).to eq([severity]) if severity
expect(@offenses.map(&:severity).uniq).to eq([severity]) if severity

offenses
@offenses
end

def expect_correction(correction, loop: true)
Expand All @@ -157,7 +157,7 @@ def expect_correction(correction, loop: true)
break corrected_source if corrected_source == @processed_source.buffer.source

if iteration > RuboCop::Runner::MAX_ITERATIONS
raise RuboCop::Runner::InfiniteCorrectionLoop.new(@processed_source.path, [])
raise RuboCop::Runner::InfiniteCorrectionLoop.new(@processed_source.path, [@offenses])
end

# Prepare for next loop
Expand Down
6 changes: 5 additions & 1 deletion lib/rubocop/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ def initialize(path, offenses_by_iteration, loop_start: -1)
root_cause = offenses_by_iteration[loop_start..-1]
.map { |x| x.map(&:cop_name).uniq.join(', ') }
.join(' -> ')
super "Infinite loop detected in #{path} and caused by #{root_cause}"

message = 'Infinite loop detected'
message += " in #{path}" if path
message += " and caused by #{root_cause}" if root_cause
super message
end
end

Expand Down

0 comments on commit da5b737

Please sign in to comment.