forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix rubocop#2481] Add WorstOffendersFormatter
As requested by Ben Nelson, this formatter lists all the offensive files in the inspected project, with the most offensive ones first. Output looks like: 4 test1.rb 2 test2.rb 1 test3.rb -- 7 Total
- Loading branch information
Showing
8 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# encoding: utf-8 | ||
|
||
require 'pathname' | ||
|
||
module RuboCop | ||
module Formatter | ||
# This formatter displays the list of offensive files, sorted by number of | ||
# offenses with the worst offenders first. | ||
# | ||
# Here's the format: | ||
# | ||
# 26 this/file/is/really/bad.rb | ||
# 3 just/ok.rb | ||
# -- | ||
# 29 Total | ||
class WorstOffendersFormatter < BaseFormatter | ||
attr_reader :offense_counts | ||
|
||
def started(target_files) | ||
super | ||
@offense_counts = {} | ||
end | ||
|
||
def file_finished(file, offenses) | ||
unless offenses.empty? | ||
path = Pathname.new(file).relative_path_from(Pathname.new(Dir.pwd)) | ||
@offense_counts[path] = offenses.size | ||
end | ||
end | ||
|
||
def finished(_inspected_files) | ||
report_summary(@offense_counts) | ||
end | ||
|
||
def report_summary(offense_counts) | ||
per_file_counts = ordered_offense_counts(offense_counts) | ||
total_count = total_offense_count(offense_counts) | ||
|
||
output.puts | ||
|
||
per_file_counts.each do |file_name, count| | ||
output.puts "#{count.to_s.ljust(total_count.to_s.length + 2)}" \ | ||
"#{file_name}\n" | ||
end | ||
output.puts '--' | ||
output.puts "#{total_count} Total" | ||
|
||
output.puts | ||
end | ||
|
||
def ordered_offense_counts(offense_counts) | ||
Hash[offense_counts.sort_by { |k, v| [-v, k] }] | ||
end | ||
|
||
def total_offense_count(offense_counts) | ||
offense_counts.values.inject(0, :+) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# encoding: utf-8 | ||
|
||
require 'spec_helper' | ||
|
||
module RuboCop | ||
module Formatter | ||
describe WorstOffendersFormatter do | ||
subject(:formatter) { described_class.new(output) } | ||
let(:output) { StringIO.new } | ||
|
||
let(:files) do | ||
%w(lib/rubocop.rb spec/spec_helper.rb bin/rubocop).map do |path| | ||
File.expand_path(path) | ||
end | ||
end | ||
|
||
describe '#finished' do | ||
context 'when there are many offenses' do | ||
let(:offense) { double('offense', cop_name: 'SomeCop') } | ||
|
||
before do | ||
formatter.started(files) | ||
files.each_with_index do |file, index| | ||
formatter.file_finished(file, [offense] * (index + 2)) | ||
end | ||
end | ||
|
||
it 'sorts by offense count first and then by cop name' do | ||
formatter.finished(files) | ||
expect(output.string).to eq(['', | ||
'4 bin/rubocop', | ||
'3 spec/spec_helper.rb', | ||
'2 lib/rubocop.rb', | ||
'--', | ||
'9 Total', | ||
'', | ||
''].join("\n")) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters