Skip to content

Commit

Permalink
[Fix rubocop#8362] Add correctable offenses summary
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenquangminh0711 committed Aug 10, 2020
1 parent e27942a commit fcfe856
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 51 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,9 @@

## master (unreleased)

### Changes
* [#8362](https://github.com/rubocop-hq/rubocop/issues/8362): Add numbers of correctable offenses to summary. ([@nguyenquangminh0711][])

## 0.89.1 (2020-08-10)

### Bug fixes
Expand Down Expand Up @@ -4773,3 +4776,4 @@
[@dsavochkin]: https://github.com/dmytro-savochkin
[@sonalinavlakhe]: https://github.com/sonalinavlakhe
[@wcmonty]: https://github.com/wcmonty
[@nguyenquangminh0711]: https://github.com/nguyenquangminh0711
3 changes: 2 additions & 1 deletion lib/rubocop/formatter/auto_gen_config_formatter.rb
Expand Up @@ -9,7 +9,8 @@ def finished(inspected_files)

report_summary(inspected_files.size,
@total_offense_count,
@total_correction_count)
@total_correction_count,
@total_correctable_count)
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/rubocop/formatter/progress_formatter.rb
Expand Up @@ -45,7 +45,8 @@ def finished(inspected_files)

report_summary(inspected_files.size,
@total_offense_count,
@total_correction_count)
@total_correction_count,
@total_correctable_count)
end

def report_file_as_mark(offenses)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/formatter/quiet_formatter.rb
Expand Up @@ -5,7 +5,7 @@ module Formatter
# If no offenses are found, no output is displayed.
# Otherwise, SimpleTextFormatter's output is displayed.
class QuietFormatter < SimpleTextFormatter
def report_summary(file_count, offense_count, correction_count)
def report_summary(file_count, offense_count, correction_count, correctable_count)
super unless offense_count.zero?
end
end
Expand Down
42 changes: 36 additions & 6 deletions lib/rubocop/formatter/simple_text_formatter.rb
Expand Up @@ -23,6 +23,7 @@ class SimpleTextFormatter < BaseFormatter
def started(_target_files)
@total_offense_count = 0
@total_correction_count = 0
@total_correctable_count = 0
end

def file_finished(file, offenses)
Expand All @@ -35,7 +36,8 @@ def file_finished(file, offenses)
def finished(inspected_files)
report_summary(inspected_files.count,
@total_offense_count,
@total_correction_count)
@total_correction_count,
@total_correctable_count)
end

def report_file(file, offenses)
Expand All @@ -52,11 +54,13 @@ def report_file(file, offenses)
end
end

def report_summary(file_count, offense_count, correction_count)
def report_summary(file_count, offense_count, correction_count, correctable_count)
report = Report.new(file_count,
offense_count,
correction_count,
rainbow)
correctable_count,
rainbow,
safe_auto_correct: @options[:safe_auto_correct])

output.puts
output.puts report.summary
Expand All @@ -66,7 +70,9 @@ def report_summary(file_count, offense_count, correction_count)

def count_stats(offenses)
@total_offense_count += offenses.count
@total_correction_count += offenses.count(&:corrected?)
corrected = offenses.count(&:corrected?)
@total_correction_count += corrected
@total_correctable_count += offenses.count(&:correctable?) - corrected
end

def colored_severity_code(offense)
Expand Down Expand Up @@ -96,16 +102,30 @@ class Report
include Colorizable
include TextUtil

def initialize(file_count, offense_count, correction_count, rainbow)
# rubocop:disable Metrics/ParameterLists
def initialize(
file_count, offense_count, correction_count, correctable_count, rainbow,
safe_auto_correct: false
)
@file_count = file_count
@offense_count = offense_count
@correction_count = correction_count
@correctable_count = correctable_count
@rainbow = rainbow
@safe_auto_correct = safe_auto_correct
end
# rubocop:enable Metrics/ParameterLists

def summary
if @correction_count.positive?
"#{files} inspected, #{offenses} detected, #{corrections} corrected"
if @correctable_count.positive?
"#{files} inspected, #{offenses} detected, #{corrections} corrected,"\
" #{correctable}"
else
"#{files} inspected, #{offenses} detected, #{corrections} corrected"
end
elsif @correctable_count.positive?
"#{files} inspected, #{offenses} detected, #{correctable}"
else
"#{files} inspected, #{offenses} detected"
end
Expand All @@ -132,6 +152,16 @@ def corrections

colorize(text, color)
end

def correctable
if @safe_auto_correct
text = pluralize(@correctable_count, 'more offense')
"#{colorize(text, :yellow)} can be corrected with `rubocop -A`"
else
text = pluralize(@correctable_count, 'offense')
"#{colorize(text, :yellow)} auto-correctable"
end
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/rubocop/cli/cli_auto_gen_config_spec.rb
Expand Up @@ -199,7 +199,7 @@ def f
== example.rb ==
C: 2: 91: Layout/LineLength: Line is too long. [99/90]
1 file inspected, 1 offense detected
1 file inspected, 1 offense detected, 1 offense auto-correctable
OUTPUT
end
end
Expand Down Expand Up @@ -1111,7 +1111,7 @@ def function(arg1, arg2, arg3, arg4, arg5, arg6, arg7)
Inspecting 1 file
C
1 file inspected, 1 offense detected
1 file inspected, 1 offense detected, 1 offense auto-correctable
Created .rubocop_todo.yml.
OUTPUT
end
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/cli/cli_autocorrect_spec.rb
Expand Up @@ -1410,7 +1410,7 @@ def self.some_method(foo, bar: 1)
C: 2: 34: Style/Semicolon: Do not use semicolons to terminate expressions.
W: 3: 27: Lint/UnusedMethodArgument: Unused method argument - bar.
1 file inspected, 3 offenses detected
1 file inspected, 3 offenses detected, 3 more offenses can be corrected with `rubocop -A`
RESULT
end

Expand Down
78 changes: 69 additions & 9 deletions spec/rubocop/cli/cli_options_spec.rb
Expand Up @@ -177,7 +177,7 @@
'usage when having a single-line body. Another good ' \
'alternative is the usage of control flow &&/||.',
'',
'1 file inspected, 1 offense detected',
'1 file inspected, 1 offense detected, 1 offense auto-correctable',
''].join("\n"))
end

Expand Down Expand Up @@ -457,7 +457,7 @@ class SomeCop < Cop
'usage when having a single-line body. Another good ' \
'alternative is the usage of control flow &&/||.',
'',
'1 file inspected, 1 offense detected',
'1 file inspected, 1 offense detected, 1 offense auto-correctable',
''].join("\n"))
end
end
Expand All @@ -483,7 +483,7 @@ class SomeCop < Cop
== example.rb ==
C: 1: 6: Layout/TrailingWhitespace: Trailing whitespace detected.
1 file inspected, 1 offense detected
1 file inspected, 1 offense detected, 1 offense auto-correctable
RESULT
end
end
Expand All @@ -505,7 +505,7 @@ class SomeCop < Cop
C: 1: 5: Layout/SpaceAroundOperators: Surrounding space missing for operator ==.
C: 2: 1: Layout/IndentationStyle: Tab detected in indentation.
1 file inspected, 3 offenses detected
1 file inspected, 3 offenses detected, 3 offenses auto-correctable
RESULT
end

Expand All @@ -528,7 +528,7 @@ class SomeCop < Cop
C: 2: 1: Layout/IndentationStyle: Tab detected in indentation.
W: 2: 2: Lint/UselessAssignment: Useless assignment to variable - y.
1 file inspected, 3 offenses detected
1 file inspected, 3 offenses detected, 2 offenses auto-correctable
RESULT
end
end
Expand Down Expand Up @@ -1060,7 +1060,7 @@ def full_description_of_cop(cop)
C: 1: 1: Style/FrozenStringLiteralComment: Missing frozen string literal comment.
C: 1:121: Layout/LineLength: Line is too long. [130/120]
1 file inspected, 2 offenses detected
1 file inspected, 2 offenses detected, 2 offenses auto-correctable
RESULT
end
end
Expand Down Expand Up @@ -1208,7 +1208,7 @@ def badName
' end',
' ^^^',
'',
'3 files inspected, 15 offenses detected',
'3 files inspected, 15 offenses detected, 13 offenses auto-correctable',
''
].join("\n"))
end
Expand Down Expand Up @@ -1328,7 +1328,7 @@ def finished(processed_files)
C: 1: 1: Style/FrozenStringLiteralComment: Missing frozen string literal comment.
C: 1:121: Layout/LineLength: Line is too long. [130/120]
1 file inspected, 2 offenses detected
1 file inspected, 2 offenses detected, 2 offenses auto-correctable
RESULT

expect(File.read('emacs_output.txt'))
Expand Down Expand Up @@ -1518,6 +1518,66 @@ def f
end
end

describe 'with --auto-correct' do
let(:target_file) { 'example.rb' }

context 'all offenses are corrected' do
before do
create_file('.rubocop.yml', <<~YAML)
Style/FrozenStringLiteralComment:
Enabled: false
YAML
end

it 'succeeds when there is only a disabled offense' do
create_file(target_file, <<~RUBY)
a = "Hello"
RUBY

expect(cli.run(['--auto-correct', '--format', 'simple',
target_file])).to eq(1)

expect($stdout.string.lines.to_a.last)
.to eq('1 file inspected, 2 offenses detected, 1 offense corrected' \
"\n")
end
end

context 'no offense corrected, 1 offense auto-correctable' do
it 'succeeds when there is only a disabled offense' do
create_file(target_file, <<~RUBY)
a = 'Hello'.freeze
puts a
RUBY

expect(cli.run(['--auto-correct', '--format', 'simple',
target_file])).to eq(1)

expect($stdout.string.lines.to_a.last).to eq(
'1 file inspected, 1 offense detected, 1 more offense '\
"can be corrected with `rubocop -A`\n"
)
end
end

context 'a offense corrected, a offense auto-correctable' do
it 'succeeds when there is only a disabled offense' do
create_file(target_file, <<~RUBY)
a = "Hello".freeze
puts a
RUBY

expect(cli.run(['--auto-correct', '--format', 'simple',
target_file])).to eq(1)

expect($stdout.string.lines.to_a.last).to eq(
'1 file inspected, 2 offenses detected, 1 offense corrected, 1 more offense '\
"can be corrected with `rubocop -A`\n"
)
end
end
end

describe '--force-exclusion' do
context 'when explicitly excluded' do
let(:target_file) { 'example.rb' }
Expand Down Expand Up @@ -1600,7 +1660,7 @@ def f
== fake.rb ==
C: 1: 3: Style/SpecialGlobalVars: Prefer $INPUT_RECORD_SEPARATOR or $RS from the stdlib 'English' module (don't forget to require it) over $/.
1 file inspected, 1 offense detected
1 file inspected, 1 offense detected, 1 offense auto-correctable
RESULT
ensure
$stdin = STDIN
Expand Down

0 comments on commit fcfe856

Please sign in to comment.