Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report the count of files in the Worst and Offense Count formatters #11113

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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][])
13 changes: 8 additions & 5 deletions lib/rubocop/formatter/offense_count_formatter.rb
Expand Up @@ -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?
Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions lib/rubocop/formatter/worst_offenders_formatter.rb
Expand Up @@ -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

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/rubocop/cli/autocorrect_spec.rb
Expand Up @@ -1383,7 +1383,7 @@ def func2
1 Style/DefWithParentheses
1 Style/TrailingBodyOnMethodDefinition
--
15 Total
15 Total in 1 files

RESULT
end
Expand Down Expand Up @@ -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'))
Expand Down
10 changes: 5 additions & 5 deletions spec/rubocop/cli/options_spec.rb
Expand Up @@ -774,7 +774,7 @@ class SomeCop < Base
1 Layout/SpaceAroundOperators
1 Layout/TrailingWhitespace
--
6 Total
6 Total in 1 files

RESULT
end
Expand All @@ -799,7 +799,7 @@ class SomeCop < Base
1 Style/FrozenStringLiteralComment
1 Style/NumericLiterals
--
7 Total
7 Total in 1 files

RESULT
end
Expand Down Expand Up @@ -936,7 +936,7 @@ def on_send(node)
1 Style/FrozenStringLiteralComment
1 Style/NumericPredicate
--
7 Total
7 Total in 1 files

RESULT
end
Expand All @@ -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

Expand All @@ -981,7 +981,7 @@ def on_send(node)
1 Style/FrozenStringLiteralComment
1 Style/NumericLiterals
--
4 Total
4 Total in 1 files

RESULT
end
Expand Down
12 changes: 6 additions & 6 deletions spec/rubocop/cli_spec.rb
Expand Up @@ -346,7 +346,7 @@ def and_with_args

1 Style/FrozenStringLiteralComment
--
1 Total
1 Total in 1 files

RESULT
expect(File.read('example.rb'))
Expand Down Expand Up @@ -640,7 +640,7 @@ def initialize; end # rubocop:disable Style/RedundantInitialize

1 Style/AndOr
--
1 Total
1 Total in 1 files

RESULT
end
Expand All @@ -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
Expand All @@ -677,7 +677,7 @@ def initialize; end # rubocop:disable Style/RedundantInitialize

3 Layout/LineLength
--
3 Total
3 Total in 1 files

RESULT
end
Expand All @@ -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
Expand Down Expand Up @@ -988,7 +988,7 @@ def meow_at_4am?

1 Layout/TrailingWhitespace
--
1 Total
1 Total in 1 files

RESULT
end
Expand Down
23 changes: 15 additions & 8 deletions spec/rubocop/formatter/offense_count_formatter_spec.rb
Expand Up @@ -5,14 +5,15 @@

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|
File.expand_path(path)
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) }
Expand All @@ -38,19 +39,25 @@

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

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)
Expand All @@ -76,7 +83,7 @@
1 CopA
1 CopB
--
4 Total
4 Total in 1 files

OUTPUT
end
Expand All @@ -93,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
Expand All @@ -114,9 +121,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
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/formatter/worst_offenders_formatter_spec.rb
Expand Up @@ -30,7 +30,7 @@
3 spec/spec_helper.rb
2 lib/rubocop.rb
--
9 Total
9 Total in 3 files

OUTPUT
end
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/rake_task_spec.rb
Expand Up @@ -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!'
Expand Down