Skip to content

Commit

Permalink
Remove Term::ANSIColor mixin from String.
Browse files Browse the repository at this point in the history
This solves issue rubocop#97, the problem with String#uncolored not working
due to interference from the colorize gem used by Coveralls.
  • Loading branch information
jonas054 committed Apr 25, 2013
1 parent 033f235 commit a28e88e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Bugs fixed

* [#90](https://github.com/bbatsov/rubocop/issues/90) Two cops crash when scanning code using super
* [#97](https://github.com/bbatsov/rubocop/issues/97) Build fails

### Misc

Expand Down
4 changes: 0 additions & 4 deletions lib/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
require 'ripper'
require 'term/ansicolor'

class String
include Term::ANSIColor
end

require 'rubocop/cop/offence'
require 'rubocop/cop/cop'
require 'rubocop/cop/encoding'
Expand Down
10 changes: 6 additions & 4 deletions lib/rubocop/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def run(args = ARGV)
rescue => e
errors_count += 1
warn "An error occurred while #{cop} was inspecting #{file}."
warn "To see the complete backtrace run rubocop -d."
warn 'To see the complete backtrace run rubocop -d.'
puts e.backtrace if $options[:debug]
end
total_offences += cop.offences.count
Expand Down Expand Up @@ -129,8 +129,8 @@ def display_summary(num_files, total_offences, errors_count)
else
"#{total_offences} offences"
end
puts "#{offences_string} detected"
.send(total_offences.zero? ? :green : :red)
puts Term::ANSIColor.send(total_offences.zero? ? :green : :red,
"#{offences_string} detected")

if errors_count > 0
plural = errors_count > 1 ? 's' : ''
Expand Down Expand Up @@ -227,7 +227,9 @@ def cops_on_duty(config)

def show_cops_on_duty(cops)
puts '== Reporting for duty =='
cops.each { |c| puts ' * '.yellow + c.to_s.green }
cops.each do |c|
puts Term::ANSIColor.yellow(' * ') + Term::ANSIColor.green(c.to_s)
end
puts '========================'
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/report/plain_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Report
class PlainText < Report
# Generates a string representation of the report
def generate
report = "== #{filename} ==\n".yellow
report = Term::ANSIColor.yellow("== #{filename} ==\n")
report << entries.join("\n")
end

Expand Down
36 changes: 18 additions & 18 deletions spec/rubocop/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def wait_for_output(output)
execute_rubocop do |stdout, stderr, pid|
Process.kill('INT', pid)
wait_for_output(stdout)
output = stdout.read.uncolored
output = Term::ANSIColor.uncolored(stdout.read)
expect(output).to match(/files? inspected/)
end
end
Expand All @@ -90,7 +90,7 @@ def wait_for_output(output)
wait_for_output(stderr) # Wait for "Exiting...".
Process.kill('INT', pid)
wait_for_output(stdout)
output = stdout.read.uncolored
output = Term::ANSIColor.uncolored(stdout.read)
expect(output).not_to match(/files? inspected/)
end
end
Expand All @@ -105,7 +105,7 @@ def wait_for_output(output)
end
begin
expect(cli.run(['example.rb'])).to eq(0)
expect($stdout.string.uncolored)
expect(Term::ANSIColor.uncolored($stdout.string))
.to eq("\n1 file inspected, no offences detected\n")
ensure
File.delete 'example.rb'
Expand All @@ -120,7 +120,7 @@ def wait_for_output(output)
end
begin
expect(cli.run(['example.rb'])).to eq(1)
expect($stdout.string.uncolored)
expect(Term::ANSIColor.uncolored($stdout.string))
.to eq ['== example.rb ==',
'C: 2: Trailing whitespace detected.',
'',
Expand All @@ -136,7 +136,7 @@ def wait_for_output(output)
File.open('example2.rb', 'w') { |f| f.puts "\tx = 0", 'puts x' }
begin
expect(cli.run(['--emacs', 'example1.rb', 'example2.rb'])).to eq(1)
expect($stdout.string.uncolored)
expect(Term::ANSIColor.uncolored($stdout.string))
.to eq(
['example1.rb:1: C: Missing utf-8 encoding comment.',
'example1.rb:1: C: Trailing whitespace detected.',
Expand All @@ -158,7 +158,7 @@ def wait_for_output(output)
File.open('example2.rb', 'w') { |f| f.puts "\tx = 0", 'puts x' }
begin
expect(cli.run(['--emacs', 'example1.rb', 'example2.rb'])).to eq(1)
expect($stdout.string.uncolored)
expect(Term::ANSIColor.uncolored($stdout.string))
.to eq(
['example1.rb:1: C: Trailing whitespace detected.',
"example1.rb:1: C: Surrounding space missing for operator '='.",
Expand Down Expand Up @@ -222,7 +222,7 @@ def wait_for_output(output)
end
begin
expect(cli.run(['-c', 'rubocop.yml', 'example1.rb'])).to eq(1)
expect($stdout.string.uncolored).to eq(
expect(Term::ANSIColor.uncolored($stdout.string)).to eq(
['== example1.rb ==',
'C: 1: Trailing whitespace detected.',
'',
Expand All @@ -246,7 +246,7 @@ def wait_for_output(output)
end
begin
expect(cli.run(['example_src/example1.rb'])).to eq(1)
expect($stdout.string.uncolored).to eq(
expect(Term::ANSIColor.uncolored($stdout.string)).to eq(
['== example_src/example1.rb ==',
'C: 1: Trailing whitespace detected.',
'',
Expand All @@ -270,7 +270,7 @@ def wait_for_output(output)
end
begin
expect(cli.run(['example_src/example1.rb'])).to eq(0)
expect($stdout.string.uncolored).to eq(
expect(Term::ANSIColor.uncolored($stdout.string)).to eq(
['', '1 file inspected, no offences detected',
''].join("\n"))
ensure
Expand All @@ -293,7 +293,7 @@ def wait_for_output(output)
end
begin
expect(cli.run(['example'])).to eq(1)
expect($stdout.string.uncolored).to eq(
expect(Term::ANSIColor.uncolored($stdout.string)).to eq(
['== example/lib/example1.rb ==',
'C: 2: Line is too long. [90/79]',
'',
Expand Down Expand Up @@ -325,7 +325,7 @@ def wait_for_output(output)
end
begin
expect(cli.run(['example_src/example1.rb'])).to eq(0)
expect($stdout.string.uncolored).to eq(
expect(Term::ANSIColor.uncolored($stdout.string)).to eq(
['', '1 file inspected, no offences detected',
''].join("\n"))
ensure
Expand All @@ -339,7 +339,7 @@ def wait_for_output(output)
# Need to pass an empty array explicitly
# so that the CLI does not refer arguments of `rspec`
cli.run([])
expect($stdout.string.uncolored).to match(
expect(Term::ANSIColor.uncolored($stdout.string)).to match(
/files inspected, no offences detected\n/
)
end
Expand All @@ -353,7 +353,7 @@ def wait_for_output(output)
begin
expect(cli.run(['--emacs', 'example.rb'])).to eq(1)
unexpected_part = RUBY_VERSION >= '2.0' ? 'end-of-input' : '$end'
expect($stdout.string.uncolored).to eq(
expect(Term::ANSIColor.uncolored($stdout.string)).to eq(
["example.rb:3: E: Syntax error, unexpected #{unexpected_part}, " +
'expecting keyword_end',
'',
Expand Down Expand Up @@ -401,7 +401,7 @@ def wait_for_output(output)
expect(cli.run(['--emacs', 'example.rb'])).to eq(1)
# all cops were disabled, then 2 were enabled again, so we
# should get 2 offences reported.
expect($stdout.string.uncolored).to eq(
expect(Term::ANSIColor.uncolored($stdout.string)).to eq(
['example.rb:8: C: Line is too long. [95/79]',
"example.rb:10: C: Prefer single-quoted strings when you don't " +
'need string interpolation or special symbols.',
Expand Down Expand Up @@ -431,7 +431,7 @@ def wait_for_output(output)
expect(cli.run(['--emacs', 'example.rb'])).to eq(1)
# 3 cops were disabled, then 2 were enabled again, so we
# should get 2 offences reported.
expect($stdout.string.uncolored).to eq(
expect(Term::ANSIColor.uncolored($stdout.string)).to eq(
['example.rb:8: C: Line is too long. [95/79]',
"example.rb:10: C: Prefer single-quoted strings when you don't " +
'need string interpolation or special symbols.',
Expand All @@ -450,7 +450,7 @@ def wait_for_output(output)
end
begin
expect(cli.run(['--emacs', 'example.rb'])).to eq(0)
expect($stdout.string.uncolored).to eq(
expect(Term::ANSIColor.uncolored($stdout.string)).to eq(
['',
'1 file inspected, no offences detected',
''].join("\n"))
Expand All @@ -468,7 +468,7 @@ def wait_for_output(output)
end
begin
expect(cli.run(['--emacs', 'example.rb'])).to eq(1)
expect($stdout.string.uncolored).to eq(
expect(Term::ANSIColor.uncolored($stdout.string)).to eq(
['example.rb:3: C: Line is too long. [95/79]',
'',
'1 file inspected, 1 offence detected',
Expand All @@ -491,7 +491,7 @@ def wait_for_output(output)
# Need to pass an empty array explicitly
# so that the CLI does not refer arguments of `rspec`
expect(cli.run([])).to eq(0)
expect($stdout.string.uncolored).to eq(
expect(Term::ANSIColor.uncolored($stdout.string)).to eq(
['', '1 file inspected, no offences detected',
''].join("\n"))
end
Expand Down

0 comments on commit a28e88e

Please sign in to comment.