Skip to content

Commit

Permalink
Color total warning count depending on number of warnings
Browse files Browse the repository at this point in the history
If there are no warnings, the output will be green, otherwise it will be
red.

Fixes troessner#287
  • Loading branch information
gilles-leblanc committed Aug 29, 2014
1 parent 90e2044 commit 70a5b02
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
5 changes: 4 additions & 1 deletion lib/reek/cli/report.rb
Expand Up @@ -40,6 +40,8 @@ def self.format(warning)
#
class Report
DefaultFormat = :text
NoWarningsColor = :green
WarningsColor = :red

def initialize(warning_formatter = SimpleWarningFormatter, report_formatter = ReportFormatter, sort_by_issue_count = false, format = DefaultFormat)
@warning_formatter = warning_formatter
Expand Down Expand Up @@ -103,7 +105,8 @@ def display_total_smell_count
end

def total_smell_count_message
Rainbow("#{@total_smell_count} total warning#{'s' unless @total_smell_count == 1 }\n").red
colour = has_smells? ? WarningsColor : NoWarningsColor
Rainbow("#{@total_smell_count} total warning#{'s' unless @total_smell_count == 1 }\n").color(colour)
end

def summarize_single_examiner(examiner)
Expand Down
46 changes: 35 additions & 11 deletions spec/reek/cli/report_spec.rb
Expand Up @@ -42,22 +42,46 @@
context 'with colors enabled' do
before :each do
Rainbow.enabled = true
@rpt.add_examiner(Examiner.new('def simple(a) a[3] end'))
@rpt.add_examiner(Examiner.new('def simple(a) a[3] end'))
@result = @rpt.gather_results
end

it 'has a header in color' do
expect(@result.first).to start_with "\e[36mstring -- \e[0m\e[33m2 warning\e[0m\e[33ms\e[0m"
context 'with non smelly files' do
before :each do
Rainbow.enabled = true
@rpt.add_examiner(Examiner.new('def simple() puts "a" end'))
@rpt.add_examiner(Examiner.new('def simple() puts "a" end'))
@result = @rpt.gather_results
end

it 'has a footer in color' do
stdout = StringIO.new
$stdout = stdout
@rpt.show
$stdout = STDOUT

expect(stdout.string).to end_with "\e[32m0 total warnings\n\e[0m"
end
end

it 'has a footer in color' do
stdout = StringIO.new
$stdout = stdout
@rpt.show
$stdout = STDOUT
context 'with smelly files' do
before :each do
Rainbow.enabled = true
@rpt.add_examiner(Examiner.new('def simple(a) a[3] end'))
@rpt.add_examiner(Examiner.new('def simple(a) a[3] end'))
@result = @rpt.gather_results
end

it 'has a header in color' do
expect(@result.first).to start_with "\e[36mstring -- \e[0m\e[33m2 warning\e[0m\e[33ms\e[0m"
end

it 'has a footer in color' do
stdout = StringIO.new
$stdout = stdout
@rpt.show
$stdout = STDOUT

expect(stdout.string).to end_with "\e[31m4 total warnings\n\e[0m"
expect(stdout.string).to end_with "\e[31m4 total warnings\n\e[0m"
end
end
end
end
Expand Down

0 comments on commit 70a5b02

Please sign in to comment.