-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #439 from petehamilton/master
Add a CopCount formatter
- Loading branch information
Showing
8 changed files
with
133 additions
and
2 deletions.
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,50 @@ | ||
# encoding: utf-8 | ||
|
||
module Rubocop | ||
module Formatter | ||
# This formatter displays the list of offended cops with a count of how | ||
# many offences of their kind were found. Ordered by desc offence count | ||
# | ||
# Here's the format: | ||
# | ||
# (26) LineLength | ||
# (3) OneLineConditional | ||
class CopCountFormatter < BaseFormatter | ||
|
||
attr_reader :cop_offences | ||
|
||
def started(target_files) | ||
super | ||
@cop_offences = Hash.new(0) | ||
end | ||
|
||
def file_finished(file, offences) | ||
offences.each { |o| @cop_offences[o.cop_name] += 1 } | ||
end | ||
|
||
def finished(inspected_files) | ||
report_summary(inspected_files.count, | ||
ordered_cop_offences(@cop_offences)) | ||
end | ||
|
||
def report_summary(file_count, cop_offences) | ||
output.puts | ||
|
||
offence_count = total_offence_count(cop_offences) | ||
cop_offences.each do |cop_name, count| | ||
count_string = "(#{count.to_s})" | ||
output.puts "#{count_string.ljust(offence_count + 4)}#{cop_name}\n" | ||
end | ||
output.puts | ||
end | ||
|
||
def ordered_cop_offences(cop_offences) | ||
Hash[cop_offences.sort_by { |k, v| v }.reverse] | ||
end | ||
|
||
def total_offence_count(cop_offences = {}) | ||
cop_offences.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
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,52 @@ | ||
# encoding: utf-8 | ||
|
||
require 'spec_helper' | ||
require 'stringio' | ||
require 'tempfile' | ||
|
||
module Rubocop | ||
module Formatter | ||
describe SimpleTextFormatter do | ||
subject(:formatter) { Formatter::CopCountFormatter.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 '#file_finished' do | ||
before { formatter.started(files) } | ||
|
||
let(:finish) { formatter.file_finished(files.first, offences) } | ||
|
||
context 'when no offences are detected' do | ||
let(:offences) { [] } | ||
it 'shouldn\'t add to cop_offences' do | ||
expect { finish }.to_not change { formatter.cop_offences } | ||
end | ||
end | ||
|
||
context 'when any offences are detected' do | ||
let(:offences) { [double('offence', cop_name: 'OffendedCop')] } | ||
it 'should increment the count for the cop in cop_offences' do | ||
expect { finish }.to change { formatter.cop_offences } | ||
end | ||
end | ||
end | ||
|
||
describe '#report_summary' do | ||
context 'when a offence detected' do | ||
let(:cop_counts) { { 'OffendedCop' => 1 } } | ||
it 'shows the cop and the offence count' do | ||
formatter.report_summary(1, cop_counts) | ||
expect(output.string).to include( | ||
"\n(1) OffendedCop") | ||
end | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |