Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when expect_corrections runs into an infinite loop #8779

Merged
merged 1 commit into from Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/rubocop/rspec/expect_offense.rb
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
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