Skip to content

Commit

Permalink
Merge pull request #10086 from dvandersluis/expect-no-corrections
Browse files Browse the repository at this point in the history
Guard against `expect_corrections` with no changes
  • Loading branch information
koic committed Sep 15, 2021
2 parents 90cace7 + 92fcdaf commit 80c9feb
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 40 deletions.
8 changes: 6 additions & 2 deletions lib/rubocop/rspec/expect_offense.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def expect_offense(source, file = nil, severity: nil, chomp: false, **replacemen
@offenses
end

# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity
def expect_correction(correction, loop: true, source: nil)
if source
expected_annotations = parse_annotations(source, raise_error: false)
Expand All @@ -136,6 +136,8 @@ def expect_correction(correction, loop: true, source: nil)

raise '`expect_correction` must follow `expect_offense`' unless @processed_source

source = @processed_source.raw_source

iteration = 0
new_source = loop do
iteration += 1
Expand All @@ -155,9 +157,11 @@ def expect_correction(correction, loop: true, source: nil)
_investigate(cop, @processed_source)
end

raise 'Use `expect_no_corrections` if the code will not change' if new_source == source

expect(new_source).to eq(correction)
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity

def expect_no_corrections
raise '`expect_no_corrections` must follow `expect_offense`' unless @processed_source
Expand Down
4 changes: 1 addition & 3 deletions spec/rubocop/cop/layout/line_length_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1094,9 +1094,7 @@ def baz(bar)
^^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [66/40]
RUBY

expect_correction(<<~RUBY)
"00000000000000000;0000000000000000000'000000;00000'0000;0000;000"
RUBY
expect_no_corrections
end
end
end
Expand Down
18 changes: 2 additions & 16 deletions spec/rubocop/cop/lint/heredoc_method_call_position_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,7 @@
3).foo
RUBY

# Should not autocorrect -- cannot always be done safely.
expect_correction(<<~RUBY)
<<-SQL
foo
SQL
.abc(1, 2,
3).foo
RUBY
expect_no_corrections
end
end

Expand All @@ -153,14 +146,7 @@
.foo
RUBY

# Should not autocorrect -- cannot always be done safely.
expect_correction(<<~RUBY)
<<-SQL
foo
SQL
.abc
.foo
RUBY
expect_no_corrections
end
end
end
Expand Down
4 changes: 1 addition & 3 deletions spec/rubocop/cop/lint/script_permission_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@
^^^^^^^^^^^^^^^ Script file #{filename} doesn't have execute permission.
RUBY

expect_correction(<<~RUBY)
#!/usr/bin/ruby
RUBY
expect_no_corrections
expect(file.stat.executable?).to be_truthy
end

Expand Down
27 changes: 11 additions & 16 deletions spec/rubocop/cop/style/empty_else_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,18 @@
end

context 'with an empty comment' do
let(:source) { <<~RUBY }
if cond
something
else
^^^^ Redundant `else`-clause.
# TODO
end
RUBY
let(:corrected_source) { <<~RUBY }
if cond
something
else
# TODO
end
RUBY
it 'does not auto-correct' do
expect_offense(<<~RUBY)
if cond
something
else
^^^^ Redundant `else`-clause.
# TODO
end
RUBY

it_behaves_like 'auto-correct', 'if'
expect_no_corrections
end
end
end

Expand Down

0 comments on commit 80c9feb

Please sign in to comment.