Skip to content

Commit

Permalink
Merge pull request #307 from koic/support_no_corrections
Browse files Browse the repository at this point in the history
Make `AssertOffense` support `assert_no_corrections`
  • Loading branch information
koic committed Mar 16, 2024
2 parents c0357e6 + 4494792 commit 50e8859
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/new_support_no_corrections.md
@@ -0,0 +1 @@
* [#307](https://github.com/rubocop/rubocop-minitest/pull/307): Make `AssertOffense` support `assert_no_corrections`. ([@koic][])
20 changes: 17 additions & 3 deletions lib/rubocop/minitest/assert_offense.rb
Expand Up @@ -116,9 +116,9 @@ def assert_offense(source, file = nil, **replacements)

@processed_source = parse_source!(expected_annotations.plain_source, file)

offenses = _investigate(@cop, @processed_source)
@offenses = _investigate(@cop, @processed_source)

actual_annotations = expected_annotations.with_offense_annotations(offenses)
actual_annotations = expected_annotations.with_offense_annotations(@offenses)

assert_equal(expected_annotations.to_s, actual_annotations.to_s)
end
Expand All @@ -143,7 +143,7 @@ def assert_correction(correction, loop: true)
break corrected_source if @last_corrector.empty? || 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 All @@ -155,6 +155,20 @@ def assert_correction(correction, loop: true)
assert_equal(correction, new_source)
end

def assert_no_corrections
raise '`assert_no_corrections` must follow `assert_offense`' unless @processed_source

return if @last_corrector.empty?

# This is just here for a pretty diff if the source actually got changed
new_source = @last_corrector.rewrite
assert_equal(@processed_source.buffer.source, new_source)

# There is an infinite loop if a corrector is present that did not make
# any changes. It will cause the same offense/correction on the next loop.
raise RuboCop::Runner::InfiniteCorrectionLoop.new(@processed_source.path, [@offenses])
end

def setup_assertion
RuboCop::Formatter::DisabledConfigFormatter.config_to_allow_offenses = {}
RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {}
Expand Down

0 comments on commit 50e8859

Please sign in to comment.