Skip to content

Commit

Permalink
Merge pull request rubocop#13375 from dvandersluis/issue/13374
Browse files Browse the repository at this point in the history
[Fix rubocop#13374] Return exit code 0 with `--display-only-correctable` and `--display-only-safe-correctable` when no offenses are displayed
  • Loading branch information
koic authored Oct 23, 2024
2 parents 7516094 + 9d4d874 commit f3aa4fb
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#13374](https://github.com/rubocop/rubocop/issues/13374): Return exit code 0 with `--display-only-correctable` and `--display-only-safe-correctable` when no offenses are displayed. ([@dvandersluis][])
16 changes: 10 additions & 6 deletions lib/rubocop/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def each_inspected_file(files)
offenses = process_file(file)
yield file

if offenses.any? { |o| considered_failure?(o) }
if offenses.any? { |o| considered_failure?(o) && offense_displayed?(o) }
break false if @options[:fail_fast]

next false
Expand Down Expand Up @@ -436,18 +436,22 @@ def considered_failure?(offense)
!offense.corrected? && offense.severity >= minimum_severity_to_fail
end

def offenses_to_report(offenses)
def offense_displayed?(offense)
if @options[:display_only_fail_level_offenses]
offenses.select { |o| considered_failure?(o) }
considered_failure?(offense)
elsif @options[:display_only_safe_correctable]
offenses.select { |o| supports_safe_autocorrect?(o) }
supports_safe_autocorrect?(offense)
elsif @options[:display_only_correctable]
offenses.select(&:correctable?)
offense.correctable?
else
offenses
true
end
end

def offenses_to_report(offenses)
offenses.select { |o| offense_displayed?(o) }
end

def supports_safe_autocorrect?(offense)
cop_class = Cop::Registry.global.find_by_cop_name(offense.cop_name)
default_cfg = default_config(offense.cop_name)
Expand Down
45 changes: 45 additions & 0 deletions spec/rubocop/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2166,6 +2166,51 @@ def method(foo, bar, qux, fred, arg5, f) end #{'#' * 85}
end
end

describe '--display-only-correctable' do
it 'returns 0 if there are no offenses shown' do
create_file('.rubocop.yml', <<~YAML)
AllCops:
SuggestExtensions: false
YAML
create_file('example.rb', <<~RUBY)
# frozen_string_literal: true
@@var = 0
RUBY
expect(cli.run(['--display-only-correctable', 'example.rb'])).to eq(0)
expect($stderr.string).to eq('')
expect($stdout.string).to eq(<<~RESULT)
Inspecting 1 file
.
1 file inspected, no offenses detected
RESULT
end
end

describe '--display-only-safe-correctable' do
it 'returns 0 if there are no offenses shown' do
create_file('.rubocop.yml', <<~YAML)
AllCops:
SuggestExtensions: false
YAML
create_file('example.rb', <<~RUBY)
# frozen_string_literal: true
if foo and bar
end
RUBY
expect(cli.run(['--display-only-safe-correctable', 'example.rb'])).to eq(0)
expect($stderr.string).to eq('')
expect($stdout.string).to eq(<<~RESULT)
Inspecting 1 file
.
1 file inspected, no offenses detected
RESULT
end
end

if RUBY_ENGINE == 'ruby' && !RuboCop::Platform.windows?
describe 'profiling' do
let(:cpu_profile) { File.join('tmp', 'rubocop-stackprof.dump') }
Expand Down

0 comments on commit f3aa4fb

Please sign in to comment.