From 3d87dde1ac45f9ec5ba54d02feda26f1fcc6eb61 Mon Sep 17 00:00:00 2001 From: Hosam Aly Date: Sun, 23 Oct 2022 16:48:06 +0000 Subject: [PATCH 1/3] Report the count of files in the Worst formatter --- .../change_report_the_count_of_files_in_the_worst.md | 1 + lib/rubocop/formatter/worst_offenders_formatter.rb | 9 ++++++--- spec/rubocop/formatter/worst_offenders_formatter_spec.rb | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 changelog/change_report_the_count_of_files_in_the_worst.md diff --git a/changelog/change_report_the_count_of_files_in_the_worst.md b/changelog/change_report_the_count_of_files_in_the_worst.md new file mode 100644 index 00000000000..e5001a2c5ae --- /dev/null +++ b/changelog/change_report_the_count_of_files_in_the_worst.md @@ -0,0 +1 @@ +* [#11113](https://github.com/rubocop/rubocop/pull/11113): Report the count of files in the Worst formatter. ([@hosamaly][]) diff --git a/lib/rubocop/formatter/worst_offenders_formatter.rb b/lib/rubocop/formatter/worst_offenders_formatter.rb index a3b085391b7..21dd5bbc101 100644 --- a/lib/rubocop/formatter/worst_offenders_formatter.rb +++ b/lib/rubocop/formatter/worst_offenders_formatter.rb @@ -12,7 +12,7 @@ module Formatter # 26 this/file/is/really/bad.rb # 3 just/ok.rb # -- - # 29 Total + # 29 Total in 2 files class WorstOffendersFormatter < BaseFormatter attr_reader :offense_counts @@ -36,14 +36,17 @@ def finished(_inspected_files) def report_summary(offense_counts) per_file_counts = ordered_offense_counts(offense_counts) total_count = total_offense_count(offense_counts) + file_count = per_file_counts.size output.puts + column_width = total_count.to_s.length + 2 per_file_counts.each do |file_name, count| - output.puts "#{count.to_s.ljust(total_count.to_s.length + 2)}#{file_name}\n" + output.puts "#{count.to_s.ljust(column_width)}#{file_name}\n" end + output.puts '--' - output.puts "#{total_count} Total" + output.puts "#{total_count} Total in #{file_count} files" output.puts end diff --git a/spec/rubocop/formatter/worst_offenders_formatter_spec.rb b/spec/rubocop/formatter/worst_offenders_formatter_spec.rb index d0dd8cfe06a..f874d78e4c0 100644 --- a/spec/rubocop/formatter/worst_offenders_formatter_spec.rb +++ b/spec/rubocop/formatter/worst_offenders_formatter_spec.rb @@ -30,7 +30,7 @@ 3 spec/spec_helper.rb 2 lib/rubocop.rb -- - 9 Total + 9 Total in 3 files OUTPUT end From ea02910dfe474fa55f7aead5e36812dfac04669e Mon Sep 17 00:00:00 2001 From: Hosam Aly Date: Sun, 23 Oct 2022 17:37:28 +0000 Subject: [PATCH 2/3] [tests/refactor] Make OffenseCountFormatter specs representative of normal usage These specs called `file_finished` for one file only, even though the test sets up multiple files. The method is now called for each file. --- spec/rubocop/formatter/offense_count_formatter_spec.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/spec/rubocop/formatter/offense_count_formatter_spec.rb b/spec/rubocop/formatter/offense_count_formatter_spec.rb index 31dcd544f86..92399299e7a 100644 --- a/spec/rubocop/formatter/offense_count_formatter_spec.rb +++ b/spec/rubocop/formatter/offense_count_formatter_spec.rb @@ -12,7 +12,7 @@ end end - let(:finish) { formatter.file_finished(files.first, offenses) } + let(:finish) { files.each { |file| formatter.file_finished(file, offenses) } } describe '#file_finished' do before { formatter.started(files) } @@ -51,6 +51,8 @@ describe '#finished' do context 'when there are many offenses' do + let(:files) { super().take(1) } + let(:offenses) do %w[CopB CopA CopC CopC].map do |cop| instance_double(RuboCop::Cop::Offense, cop_name: cop) @@ -114,9 +116,9 @@ finish end - it 'has a progresbar' do + it 'has a progress bar' do formatter.finished(files) - expect(formatter.instance_variable_get(:@progressbar).progress).to eq 1 + expect(formatter.instance_variable_get(:@progressbar).progress).to eq 3 end end end From c0763f15aa71bd078bb183af5b01f5dba4767f1c Mon Sep 17 00:00:00 2001 From: Hosam Aly Date: Sun, 23 Oct 2022 17:39:46 +0000 Subject: [PATCH 3/3] Report the count of files in the Offense Count formatter --- ...ange_report_the_count_of_files_in_the_worst.md | 1 - ...rt_the_count_of_files_in_worst_and_offenses.md | 1 + lib/rubocop/formatter/offense_count_formatter.rb | 13 ++++++++----- spec/rubocop/cli/autocorrect_spec.rb | 4 ++-- spec/rubocop/cli/options_spec.rb | 10 +++++----- spec/rubocop/cli_spec.rb | 12 ++++++------ .../formatter/offense_count_formatter_spec.rb | 15 ++++++++++----- spec/rubocop/rake_task_spec.rb | 2 +- 8 files changed, 33 insertions(+), 25 deletions(-) delete mode 100644 changelog/change_report_the_count_of_files_in_the_worst.md create mode 100644 changelog/change_report_the_count_of_files_in_worst_and_offenses.md diff --git a/changelog/change_report_the_count_of_files_in_the_worst.md b/changelog/change_report_the_count_of_files_in_the_worst.md deleted file mode 100644 index e5001a2c5ae..00000000000 --- a/changelog/change_report_the_count_of_files_in_the_worst.md +++ /dev/null @@ -1 +0,0 @@ -* [#11113](https://github.com/rubocop/rubocop/pull/11113): Report the count of files in the Worst formatter. ([@hosamaly][]) diff --git a/changelog/change_report_the_count_of_files_in_worst_and_offenses.md b/changelog/change_report_the_count_of_files_in_worst_and_offenses.md new file mode 100644 index 00000000000..ea839907f30 --- /dev/null +++ b/changelog/change_report_the_count_of_files_in_worst_and_offenses.md @@ -0,0 +1 @@ +* [#11113](https://github.com/rubocop/rubocop/pull/11113): Report the count of files in the Worst and the Offense Count formatters. ([@hosamaly][]) diff --git a/lib/rubocop/formatter/offense_count_formatter.rb b/lib/rubocop/formatter/offense_count_formatter.rb index 8e444ff3dac..2830022cbca 100644 --- a/lib/rubocop/formatter/offense_count_formatter.rb +++ b/lib/rubocop/formatter/offense_count_formatter.rb @@ -12,13 +12,14 @@ module Formatter # 26 LineLength # 3 OneLineConditional # -- - # 29 Total + # 29 Total in 5 files class OffenseCountFormatter < BaseFormatter attr_reader :offense_counts def started(target_files) super @offense_counts = Hash.new(0) + @offending_files_count = 0 @style_guide_links = {} return unless output.tty? @@ -43,26 +44,28 @@ def file_finished(_file, offenses) if options[:display_style_guide] offenses.each { |o| @style_guide_links[o.cop_name] ||= o.message[/ \(http\S+\)\Z/] } end + @offending_files_count += 1 unless offenses.empty? @progressbar.increment if instance_variable_defined?(:@progressbar) end def finished(_inspected_files) - report_summary(@offense_counts) + report_summary(@offense_counts, @offending_files_count) end # rubocop:disable Metrics/AbcSize - def report_summary(offense_counts) + def report_summary(offense_counts, offending_files_count) per_cop_counts = ordered_offense_counts(offense_counts) total_count = total_offense_count(offense_counts) output.puts + column_width = total_count.to_s.length + 2 per_cop_counts.each do |cop_name, count| - output.puts "#{count.to_s.ljust(total_count.to_s.length + 2)}#{cop_name}" \ + output.puts "#{count.to_s.ljust(column_width)}#{cop_name}" \ "#{@style_guide_links[cop_name]}\n" end output.puts '--' - output.puts "#{total_count} Total" + output.puts "#{total_count} Total in #{offending_files_count} files" output.puts end diff --git a/spec/rubocop/cli/autocorrect_spec.rb b/spec/rubocop/cli/autocorrect_spec.rb index 004a5d2bcb3..9a620ba194e 100644 --- a/spec/rubocop/cli/autocorrect_spec.rb +++ b/spec/rubocop/cli/autocorrect_spec.rb @@ -1383,7 +1383,7 @@ def func2 1 Style/DefWithParentheses 1 Style/TrailingBodyOnMethodDefinition -- - 15 Total + 15 Total in 1 files RESULT end @@ -1491,7 +1491,7 @@ def primes limit 4 Layout/SpaceAfterComma 2 Style/WordArray -- - 6 Total + 6 Total in 1 files RESULT expect(File.read('example.rb')) diff --git a/spec/rubocop/cli/options_spec.rb b/spec/rubocop/cli/options_spec.rb index a002a81d3f0..aa9929fcd07 100644 --- a/spec/rubocop/cli/options_spec.rb +++ b/spec/rubocop/cli/options_spec.rb @@ -774,7 +774,7 @@ class SomeCop < Base 1 Layout/SpaceAroundOperators 1 Layout/TrailingWhitespace -- - 6 Total + 6 Total in 1 files RESULT end @@ -799,7 +799,7 @@ class SomeCop < Base 1 Style/FrozenStringLiteralComment 1 Style/NumericLiterals -- - 7 Total + 7 Total in 1 files RESULT end @@ -936,7 +936,7 @@ def on_send(node) 1 Style/FrozenStringLiteralComment 1 Style/NumericPredicate -- - 7 Total + 7 Total in 1 files RESULT end @@ -954,7 +954,7 @@ def on_send(node) expect($stderr.string).to eq('') expect(without_option.split($RS) - with_option.split($RS)) - .to eq(['1 Style/IfUnlessModifier', '7 Total']) + .to eq(['1 Style/IfUnlessModifier', '7 Total in 1 files']) end end @@ -981,7 +981,7 @@ def on_send(node) 1 Style/FrozenStringLiteralComment 1 Style/NumericLiterals -- - 4 Total + 4 Total in 1 files RESULT end diff --git a/spec/rubocop/cli_spec.rb b/spec/rubocop/cli_spec.rb index 4b5a4882314..997f31dd153 100644 --- a/spec/rubocop/cli_spec.rb +++ b/spec/rubocop/cli_spec.rb @@ -346,7 +346,7 @@ def and_with_args 1 Style/FrozenStringLiteralComment -- - 1 Total + 1 Total in 1 files RESULT expect(File.read('example.rb')) @@ -640,7 +640,7 @@ def initialize; end # rubocop:disable Style/RedundantInitialize 1 Style/AndOr -- - 1 Total + 1 Total in 1 files RESULT end @@ -657,7 +657,7 @@ def initialize; end # rubocop:disable Style/RedundantInitialize 3 Layout/LineLength 1 Style/AndOr -- - 4 Total + 4 Total in 1 files RESULT end @@ -677,7 +677,7 @@ def initialize; end # rubocop:disable Style/RedundantInitialize 3 Layout/LineLength -- - 3 Total + 3 Total in 1 files RESULT end @@ -694,7 +694,7 @@ def initialize; end # rubocop:disable Style/RedundantInitialize 3 Layout/LineLength 1 Style/AndOr -- - 4 Total + 4 Total in 1 files RESULT end @@ -988,7 +988,7 @@ def meow_at_4am? 1 Layout/TrailingWhitespace -- - 1 Total + 1 Total in 1 files RESULT end diff --git a/spec/rubocop/formatter/offense_count_formatter_spec.rb b/spec/rubocop/formatter/offense_count_formatter_spec.rb index 92399299e7a..dd1dc4f0ddb 100644 --- a/spec/rubocop/formatter/offense_count_formatter_spec.rb +++ b/spec/rubocop/formatter/offense_count_formatter_spec.rb @@ -5,6 +5,7 @@ let(:output) { StringIO.new } let(:options) { { display_style_guide: false } } + let(:file_count) { files.size } let(:files) do %w[lib/rubocop.rb spec/spec_helper.rb exe/rubocop].map do |path| @@ -38,13 +39,17 @@ describe '#report_summary' do context 'when an offense is detected' do - let(:cop_counts) { { 'OffendedCop' => 1 } } + let(:cop_counts) { { 'OffendedCop' => 3 } } before { formatter.started(files) } it 'shows the cop and the offense count' do - formatter.report_summary(cop_counts) - expect(output.string).to include("\n1 OffendedCop\n--\n1 Total") + formatter.report_summary(cop_counts, 2) + expect(output.string).to include(<<~OUTPUT) + 3 OffendedCop + -- + 3 Total in 2 files + OUTPUT end end end @@ -78,7 +83,7 @@ 1 CopA 1 CopB -- - 4 Total + 4 Total in 1 files OUTPUT end @@ -95,7 +100,7 @@ 1 CopA (https://rubystyle.guide#no-good-CopA) 1 CopB (https://rubystyle.guide#no-good-CopB) -- - 4 Total + 4 Total in 1 files OUTPUT end diff --git a/spec/rubocop/rake_task_spec.rb b/spec/rubocop/rake_task_spec.rb index 667eaca2bf2..23fac2d67d4 100644 --- a/spec/rubocop/rake_task_spec.rb +++ b/spec/rubocop/rake_task_spec.rb @@ -134,7 +134,7 @@ 1 Style/FrozenStringLiteralComment 1 Style/SpecialGlobalVars -- - 2 Total + 2 Total in 1 files RESULT expect($stderr.string.strip).to eq 'RuboCop failed!'