From da5b737afd4baa6445c3dfb6e018006133d4898d Mon Sep 17 00:00:00 2001 From: Daniel Vandersluis Date: Fri, 25 Sep 2020 02:35:27 -0400 Subject: [PATCH] Fix crash when `expect_corrections` runs into an infinite loop (#8779) --- lib/rubocop/rspec/expect_offense.rb | 10 +++++----- lib/rubocop/runner.rb | 6 +++++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/rubocop/rspec/expect_offense.rb b/lib/rubocop/rspec/expect_offense.rb index c64c2359f01..828b60368e4 100644 --- a/lib/rubocop/rspec/expect_offense.rb +++ b/lib/rubocop/rspec/expect_offense.rb @@ -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) @@ -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 diff --git a/lib/rubocop/runner.rb b/lib/rubocop/runner.rb index f639f667e4f..cd46878d099 100644 --- a/lib/rubocop/runner.rb +++ b/lib/rubocop/runner.rb @@ -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