diff --git a/CHANGELOG.md b/CHANGELOG.md index 861cf55ff8f..a4644befb65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/lib/rubocop/formatter/auto_gen_config_formatter.rb b/lib/rubocop/formatter/auto_gen_config_formatter.rb index 8e098de0c6b..3f496cd86ef 100644 --- a/lib/rubocop/formatter/auto_gen_config_formatter.rb +++ b/lib/rubocop/formatter/auto_gen_config_formatter.rb @@ -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 diff --git a/lib/rubocop/formatter/progress_formatter.rb b/lib/rubocop/formatter/progress_formatter.rb index c60d7820cf4..3a3e9738c27 100644 --- a/lib/rubocop/formatter/progress_formatter.rb +++ b/lib/rubocop/formatter/progress_formatter.rb @@ -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) diff --git a/lib/rubocop/formatter/quiet_formatter.rb b/lib/rubocop/formatter/quiet_formatter.rb index 14b43d86e97..201a585f7d9 100644 --- a/lib/rubocop/formatter/quiet_formatter.rb +++ b/lib/rubocop/formatter/quiet_formatter.rb @@ -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 diff --git a/lib/rubocop/formatter/simple_text_formatter.rb b/lib/rubocop/formatter/simple_text_formatter.rb index 87bb43dc101..8528653f942 100644 --- a/lib/rubocop/formatter/simple_text_formatter.rb +++ b/lib/rubocop/formatter/simple_text_formatter.rb @@ -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) @@ -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) @@ -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 @@ -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) @@ -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 @@ -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 diff --git a/spec/rubocop/cli/cli_auto_gen_config_spec.rb b/spec/rubocop/cli/cli_auto_gen_config_spec.rb index 65becd178d4..c450fdda08c 100644 --- a/spec/rubocop/cli/cli_auto_gen_config_spec.rb +++ b/spec/rubocop/cli/cli_auto_gen_config_spec.rb @@ -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 @@ -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 diff --git a/spec/rubocop/cli/cli_autocorrect_spec.rb b/spec/rubocop/cli/cli_autocorrect_spec.rb index ac17ceaf361..88cca7b6572 100644 --- a/spec/rubocop/cli/cli_autocorrect_spec.rb +++ b/spec/rubocop/cli/cli_autocorrect_spec.rb @@ -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 diff --git a/spec/rubocop/cli/cli_options_spec.rb b/spec/rubocop/cli/cli_options_spec.rb index c3e03bfe63c..7c8dc969d5f 100644 --- a/spec/rubocop/cli/cli_options_spec.rb +++ b/spec/rubocop/cli/cli_options_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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')) @@ -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' } @@ -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 diff --git a/spec/rubocop/cli_spec.rb b/spec/rubocop/cli_spec.rb index cb2b0b564c6..16b9e17f83e 100644 --- a/spec/rubocop/cli_spec.rb +++ b/spec/rubocop/cli_spec.rb @@ -81,7 +81,7 @@ C: 1: 1: Layout/EndOfLine: Carriage return character detected. C: 1: 1: Style/FrozenStringLiteralComment: Missing frozen string literal comment. - 1 file inspected, 2 offenses detected + 1 file inspected, 2 offenses detected, 1 offense auto-correctable RESULT expect($stderr.string).to eq(<<~RESULT) #{abs('.rubocop.yml')}: Warning: no department given for EndOfLine. @@ -145,7 +145,7 @@ def and_with_args == example.rb == C: 3: 6: Layout/TrailingWhitespace: Trailing whitespace detected. - 1 file inspected, 1 offense detected + 1 file inspected, 1 offense detected, 1 offense auto-correctable RESULT end @@ -486,7 +486,7 @@ def and_with_args == example.rb == C: 1: 3: Layout/LineLength: Line is too long. [5/2] - 1 file inspected, 1 offense detected + 1 file inspected, 1 offense detected, 1 offense auto-correctable RESULT end @@ -729,7 +729,7 @@ def meow_at_4am? C: 9: 3: Layout/IndentationWidth: Use 2 (not 0) spaces for indented_internal_methods indentation. C: 15: 3: Layout/IndentationWidth: Use 2 (not 0) spaces for indented_internal_methods indentation. - 1 file inspected, 2 offenses detected + 1 file inspected, 2 offenses detected, 2 offenses auto-correctable RESULT end end @@ -818,7 +818,7 @@ def meow_at_4am? == example.rb == C: 3: 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 @@ -858,7 +858,7 @@ def meow_at_4am? == example1.rb == C: 3: 6: Layout/TrailingWhitespace: Trailing whitespace detected. - 2 files inspected, 2 offenses detected + 2 files inspected, 2 offenses detected, 2 offenses auto-correctable RESULT end @@ -884,7 +884,7 @@ def meow_at_4am? == example1.rb == C: 3: 6: Layout/TrailingWhitespace: Trailing whitespace detected. - 2 files inspected, 2 offenses detected + 2 files inspected, 2 offenses detected, 2 offenses auto-correctable RESULT end @@ -1032,7 +1032,7 @@ def meow_at_4am? == special.dsl == C: 3: 9: Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. - 1 file inspected, 1 offense detected + 1 file inspected, 1 offense detected, 1 offense auto-correctable RESULT end @@ -1072,7 +1072,7 @@ def meow_at_4am? == example1.rb == C: 3: 7: Layout/TrailingWhitespace: Trailing whitespace detected. - 1 file inspected, 1 offense detected + 1 file inspected, 1 offense detected, 1 offense auto-correctable RESULT end @@ -1095,7 +1095,7 @@ def meow_at_4am? == example1.rb == C: 3: 7: Layout/TrailingWhitespace: Trailing whitespace detected. - 1 file inspected, 1 offense detected + 1 file inspected, 1 offense detected, 1 offense auto-correctable RESULT end end @@ -1155,7 +1155,7 @@ def meow_at_4am? C: 4: 6: Style/PercentLiteralDelimiters: %q-literals should be delimited by ( and ). C: 4: 6: Style/RedundantPercentQ: Use %q only for strings that contain both single quotes and double quotes. - 1 file inspected, 3 offenses detected + 1 file inspected, 3 offenses detected, 3 offenses auto-correctable RESULT end @@ -1186,7 +1186,7 @@ def meow_at_4am? C: 1: 5: Style/CollectionMethods: Prefer find_all over select. C: 1: 26: Style/CollectionMethods: Prefer map over collect. - 1 file inspected, 2 offenses detected + 1 file inspected, 2 offenses detected, 2 offenses auto-correctable RESULT end @@ -1211,7 +1211,7 @@ def meow_at_4am? == example1.rb == C: 3: 1: Style/IfUnlessModifier: Favor modifier if 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 RESULT expect(result).to eq(1) end @@ -1234,7 +1234,7 @@ def meow_at_4am? == example_src/example1.rb == C: 3: 7: Layout/TrailingWhitespace: Trailing whitespace detected. - 1 file inspected, 1 offense detected + 1 file inspected, 1 offense detected, 1 offense auto-correctable RESULT end @@ -1273,7 +1273,7 @@ def meow_at_4am? == example/lib/example1.rb == C: 3:121: Layout/LineLength: Line is too long. [130/120] - 2 files inspected, 1 offense detected + 2 files inspected, 1 offense detected, 1 offense auto-correctable RESULT end @@ -1329,7 +1329,7 @@ def meow_at_4am? == example/tmp/test/example1.rb == C: 3:121: Layout/LineLength: Line is too long. [130/120] - 1 file inspected, 1 offense detected + 1 file inspected, 1 offense detected, 1 offense auto-correctable RESULT end @@ -1538,7 +1538,7 @@ def method(foo, bar, qux, fred, arg5, f) end #{'#' * 85} C: 3: 46: Style/CommentedKeyword: Do not place comments on the same line as the def keyword. E: 3:121: Layout/LineLength: Line is too long. [130/120] - 1 file inspected, 4 offenses detected + 1 file inspected, 4 offenses detected, 1 offense auto-correctable RESULT expect($stderr.string).to eq('') end diff --git a/spec/rubocop/formatter/auto_gen_config_formatter_spec.rb b/spec/rubocop/formatter/auto_gen_config_formatter_spec.rb index 24fb1c10b73..2ef800b7c64 100644 --- a/spec/rubocop/formatter/auto_gen_config_formatter_spec.rb +++ b/spec/rubocop/formatter/auto_gen_config_formatter_spec.rb @@ -101,7 +101,7 @@ def offense_with_severity(severity) it 'outputs report summary' do formatter.finished(files) expect(output.string).to include <<~OUTPUT - 3 files inspected, 1 offense detected + 3 files inspected, 1 offense detected, 1 offense auto-correctable OUTPUT end end diff --git a/spec/rubocop/formatter/quiet_formatter_spec.rb b/spec/rubocop/formatter/quiet_formatter_spec.rb index 843ab7f14a1..46aba5644cf 100644 --- a/spec/rubocop/formatter/quiet_formatter_spec.rb +++ b/spec/rubocop/formatter/quiet_formatter_spec.rb @@ -76,21 +76,21 @@ describe '#report_summary' do context 'when no files inspected' do it 'handles pluralization correctly' do - formatter.report_summary(0, 0, 0) + formatter.report_summary(0, 0, 0, 0) expect(output.string.empty?).to eq(true) end end context 'when a file inspected and no offenses detected' do it 'handles pluralization correctly' do - formatter.report_summary(1, 0, 0) + formatter.report_summary(1, 0, 0, 0) expect(output.string.empty?).to eq(true) end end context 'when a offense detected' do it 'handles pluralization correctly' do - formatter.report_summary(1, 1, 0) + formatter.report_summary(1, 1, 0, 0) expect(output.string).to eq(<<~OUTPUT) 1 file inspected, 1 offense detected @@ -98,9 +98,19 @@ end end + context 'when a offense detected and a offense correctable' do + it 'handles pluralization correctly' do + formatter.report_summary(1, 1, 0, 1) + expect(output.string).to eq(<<~OUTPUT) + + 1 file inspected, 1 offense detected, 1 offense auto-correctable + OUTPUT + end + end + context 'when 2 offenses detected' do it 'handles pluralization correctly' do - formatter.report_summary(2, 2, 0) + formatter.report_summary(2, 2, 0, 0) expect(output.string).to eq(<<~OUTPUT) 2 files inspected, 2 offenses detected @@ -108,9 +118,19 @@ end end + context 'when 2 offenses detected and 2 offenses correctable' do + it 'handles pluralization correctly' do + formatter.report_summary(2, 2, 0, 2) + expect(output.string).to eq(<<~OUTPUT) + + 2 files inspected, 2 offenses detected, 2 offenses auto-correctable + OUTPUT + end + end + context 'when an offense is corrected' do it 'prints about correction' do - formatter.report_summary(1, 1, 1) + formatter.report_summary(1, 1, 1, 0) expect(output.string).to eq(<<~OUTPUT) 1 file inspected, 1 offense detected, 1 offense corrected @@ -120,12 +140,22 @@ context 'when 2 offenses are corrected' do it 'handles pluralization correctly' do - formatter.report_summary(1, 1, 2) + formatter.report_summary(1, 1, 2, 0) expect(output.string).to eq(<<~OUTPUT) 1 file inspected, 1 offense detected, 2 offenses corrected OUTPUT end end + + context 'when 2 offenses are corrected and 2 offenses correctable' do + it 'handles pluralization correctly' do + formatter.report_summary(1, 1, 2, 2) + expect(output.string).to eq(<<~OUTPUT) + + 1 file inspected, 1 offense detected, 2 offenses corrected, 2 offenses auto-correctable + OUTPUT + end + end end end diff --git a/spec/rubocop/formatter/simple_text_formatter_spec.rb b/spec/rubocop/formatter/simple_text_formatter_spec.rb index c5e7b972f0f..f84a192477c 100644 --- a/spec/rubocop/formatter/simple_text_formatter_spec.rb +++ b/spec/rubocop/formatter/simple_text_formatter_spec.rb @@ -85,7 +85,7 @@ describe '#report_summary' do context 'when no files inspected' do it 'handles pluralization correctly' do - formatter.report_summary(0, 0, 0) + formatter.report_summary(0, 0, 0, 0) expect(output.string).to eq(<<~OUTPUT) 0 files inspected, no offenses detected @@ -95,7 +95,7 @@ context 'when a file inspected and no offenses detected' do it 'handles pluralization correctly' do - formatter.report_summary(1, 0, 0) + formatter.report_summary(1, 0, 0, 0) expect(output.string).to eq(<<~OUTPUT) 1 file inspected, no offenses detected @@ -105,7 +105,7 @@ context 'when a offense detected' do it 'handles pluralization correctly' do - formatter.report_summary(1, 1, 0) + formatter.report_summary(1, 1, 0, 0) expect(output.string).to eq(<<~OUTPUT) 1 file inspected, 1 offense detected @@ -113,9 +113,19 @@ end end + context 'when a offense detected and a offense auto-correctable' do + it 'handles pluralization correctly' do + formatter.report_summary(1, 1, 0, 1) + expect(output.string).to eq(<<~OUTPUT) + + 1 file inspected, 1 offense detected, 1 offense auto-correctable + OUTPUT + end + end + context 'when 2 offenses detected' do it 'handles pluralization correctly' do - formatter.report_summary(2, 2, 0) + formatter.report_summary(2, 2, 0, 0) expect(output.string).to eq(<<~OUTPUT) 2 files inspected, 2 offenses detected @@ -123,9 +133,19 @@ end end + context 'when 2 offenses detected and 2 offenses auto-correctable' do + it 'handles pluralization correctly' do + formatter.report_summary(2, 2, 0, 2) + expect(output.string).to eq(<<~OUTPUT) + + 2 files inspected, 2 offenses detected, 2 offenses auto-correctable + OUTPUT + end + end + context 'when an offense is corrected' do it 'prints about correction' do - formatter.report_summary(1, 1, 1) + formatter.report_summary(1, 1, 1, 0) expect(output.string).to eq(<<~OUTPUT) 1 file inspected, 1 offense detected, 1 offense corrected @@ -135,12 +155,22 @@ context 'when 2 offenses are corrected' do it 'handles pluralization correctly' do - formatter.report_summary(1, 1, 2) + formatter.report_summary(1, 1, 2, 0) expect(output.string).to eq(<<~OUTPUT) 1 file inspected, 1 offense detected, 2 offenses corrected OUTPUT end end + + context 'when 2 offenses are corrected and 2 offenses auto-correctable' do + it 'handles pluralization correctly' do + formatter.report_summary(1, 1, 2, 2) + expect(output.string).to eq(<<~OUTPUT) + + 1 file inspected, 1 offense detected, 2 offenses corrected, 2 offenses auto-correctable + OUTPUT + end + end end end