Skip to content

Commit

Permalink
With --fail-level A ignore non-correctable offenses at :info severity
Browse files Browse the repository at this point in the history
The current implementation of `--fail-level autocorrect` fails for all
offenses, no matter what. Notably, this includes non-correctable
offenses with the :info severity.

Change the implementation to only fail:
* Any _correctable_ offense, regardless of severity
* Non-correctable offenses with severity :refactor (the default) or
  higher
  • Loading branch information
naveg authored and bbatsov committed Aug 9, 2023
1 parent 320ac56 commit c078235
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12091](https://github.com/rubocop/rubocop/pull/12091): With `--fail-level A` ignore non-correctable offenses at :info severity. ([@naveg][])
2 changes: 1 addition & 1 deletion docs/modules/ROOT/pages/usage/basic_usage.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ $ rubocop --only Rails/Blank,Layout/HeredocIndentation,Naming/FileName
| Inspect files in order of modification time and stops after first file with offenses.

| `--fail-level`
| Minimum xref:configuration.adoc#severity[severity] for exit with error code. Full severity name or upper case initial can be given. Normally, autocorrected offenses are ignored. Use `A` or `autocorrect` if you'd like them to trigger failure.
| Minimum xref:configuration.adoc#severity[severity] for exit with error code. Full severity name or upper case initial can be given. Normally, autocorrected offenses are ignored. Use `A` or `autocorrect` if you'd like any autocorrectable offense to trigger failure, regardless of severity.

| `--force-exclusion`
| Force excluding files specified in the configuration `Exclude` even if they are explicitly passed as arguments.
Expand Down
8 changes: 5 additions & 3 deletions lib/rubocop/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,10 @@ def formatter_set
end

def considered_failure?(offense)
# For :autocorrect level, any offense - corrected or not - is a failure.
return false if offense.disabled?

return true if @options[:fail_level] == :autocorrect
# For :autocorrect level, any correctable offense is a failure, regardless of severity
return true if @options[:fail_level] == :autocorrect && offense.correctable?

!offense.corrected? && offense.severity >= minimum_severity_to_fail
end
Expand Down Expand Up @@ -461,7 +461,9 @@ def minimum_severity_to_fail
@minimum_severity_to_fail ||= begin
# Unless given explicitly as `fail_level`, `:info` severity offenses do not fail
name = @options[:fail_level] || :refactor
RuboCop::Cop::Severity.new(name)

# autocorrect is a fake level - use the default
RuboCop::Cop::Severity.new(name == :autocorrect ? :refactor : name)
end
end

Expand Down
23 changes: 23 additions & 0 deletions spec/rubocop/cli/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1797,6 +1797,29 @@ def expect_offense_detected
expect_offense_detected
end

context 'when the cop has the "info" severity' do
before do
create_file(target_file, <<~RUBY)
Long::Line::Not::Autocorrectable
RUBY

create_file('.rubocop.yml', <<~YAML)
Layout/LineLength:
Max: 10
Severity: info
YAML
end

it 'succeeds when option is autocorrect and the offense is not autocorrectable' do
expect(cli.run(['--fail-level', 'autocorrect',
'--only', 'Layout/LineLength',
target_file])).to eq(0)
expect($stderr.string).to eq('')
expect($stdout.string.include?('1 file inspected, 1 offense detected')).to be(true)
expect($stdout.string.include?('Layout/LineLength')).to be(true)
end
end

context 'with --display-only-fail-level-offenses' do
it 'outputs offense message when fail-level is less than the severity' do
expect(cli.run(['--fail-level', 'refactor',
Expand Down

0 comments on commit c078235

Please sign in to comment.