Skip to content

Commit

Permalink
[Fix #14] Use term-ansicolor gem for text coloring
Browse files Browse the repository at this point in the history
Fix any monkey-patching issues from Colorize by using Term-ANSIColor
instead. Introduced the ColorFormat class that extends Term::ANSIColor.
To format output:

    puts ColorFormat.red("I am red.")
    puts ColorFormat.red(ColorFormat.underline("I am red and underlined"))
  • Loading branch information
donokuda committed May 7, 2013
1 parent 62cf552 commit e4c0733
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 34 deletions.
2 changes: 1 addition & 1 deletion coveralls-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Gem::Specification.new do |gem|
gem.version = Coveralls::VERSION

gem.add_dependency 'rest-client'
gem.add_dependency 'colorize'
gem.add_dependency 'term-ansicolor'
gem.add_dependency 'multi_json', '~> 1.3'
gem.add_dependency 'thor'

Expand Down
20 changes: 12 additions & 8 deletions lib/coveralls.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'colorize'
require 'term/ansicolor'

require "coveralls/version"
require "coveralls/configuration"
Expand All @@ -13,6 +13,10 @@ def format( result )
end
end

class ColorFormat
extend Term::ANSIColor
end

attr_accessor :testing, :noisy, :adapter, :run_locally

def wear!(simplecov_setting=nil, &block)
Expand Down Expand Up @@ -49,9 +53,9 @@ def setup!
# Load the appropriate adapter.
if @@adapter == :simplecov
::SimpleCov.formatter = Coveralls::SimpleCov::Formatter
puts "[Coveralls] Set up the SimpleCov formatter.".green
puts ColorFormat.green("[Coveralls] Set up the SimpleCov formatter.")
else
puts "[Coveralls] Couldn't find an appropriate adapter.".red
puts ColorFormat.red("[Coveralls] Couldn't find an appropriate adapter.")
end

end
Expand All @@ -61,17 +65,17 @@ def start!(simplecov_setting = 'test_frameworks', &block)
::SimpleCov.add_filter 'vendor'

if simplecov_setting
puts "[Coveralls] Using SimpleCov's '#{simplecov_setting}' settings.".green
puts ColorFormat.green("[Coveralls] Using SimpleCov's '#{simplecov_setting}' settings.")
if block_given?
::SimpleCov.start(simplecov_setting) { instance_eval &block }
else
::SimpleCov.start(simplecov_setting)
end
elsif block_given?
puts "[Coveralls] Using SimpleCov settings defined in block.".green
puts ColorFormat.green("[Coveralls] Using SimpleCov settings defined in block.")
::SimpleCov.start { instance_eval &block }
else
puts "[Coveralls] Using SimpleCov's default settings.".green
puts ColorFormat.green("[Coveralls] Using SimpleCov's default settings.")
::SimpleCov.start
end
end
Expand All @@ -81,12 +85,12 @@ def should_run?
# Fail early if we're not on a CI
unless ENV["CI"] || ENV["JENKINS_URL"] ||
ENV["COVERALLS_RUN_LOCALLY"] || @testing
puts "[Coveralls] Outside the Travis environment, not sending data.".yellow
puts ColorFormat.yellow("[Coveralls] Outside the Travis environment, not sending data.")
return false
end

if ENV["COVERALLS_RUN_LOCALLY"] || @run_locally
puts "[Coveralls] Creating a new job on Coveralls from local coverage results.".cyan
puts ColorFormat.cyan("[Coveralls] Creating a new job on Coveralls from local coverage results.")
end

true
Expand Down
20 changes: 10 additions & 10 deletions lib/coveralls/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ class API
def self.post_json(endpoint, hash)
disable_net_blockers!
url = endpoint_to_url(endpoint)
puts MultiJson.dump(hash, :pretty => true).green if ENV['COVERALLS_DEBUG']
puts ColorFormat.green("#{ MultiJson.dump(hash, :pretty => true) }") if ENV['COVERALLS_DEBUG']
hash = apified_hash hash
puts "[Coveralls] Submitting to #{API_BASE}".cyan
puts ColorFormat.cyan("[Coveralls] Submitting to #{API_BASE}")
response = RestClient.post(url, :json_file => hash_to_file(hash))
response_hash = MultiJson.load(response.to_str)
puts ("[Coveralls] " + response_hash['message']).cyan
puts ColorFormat.cyan("[Coveralls] " + response_hash['message'])
if response_hash['message']
puts ("[Coveralls] " + response_hash['url'].underline).cyan
puts ColorFormat.cyan("[Coveralls] " + ColorFormat.underline(response_hash['url']))
end
rescue RestClient::ServiceUnavailable
puts ("[Coveralls] API timeout occured, but data should still be processed").red
puts ColorFormat.red("[Coveralls] API timeout occured, but data should still be processed")
rescue RestClient::InternalServerError
puts ("[Coveralls] API internal error occured, we're on it!").red
puts ColorFormat.red("[Coveralls] API internal error occured, we're on it!")
end

private
Expand Down Expand Up @@ -58,11 +58,11 @@ def self.hash_to_file(hash)
def self.apified_hash hash
config = Coveralls::Configuration.configuration
if ENV['CI'] || ENV['COVERALLS_DEBUG'] || Coveralls.testing
puts "[Coveralls] Submiting with config:".yellow
puts MultiJson.dump(config, :pretty => true).
gsub(/"repo_token": "(.*?)"/,'"repo_token": "[secure]"').yellow
puts ColorFormat.yellow("[Coveralls] Submiting with config:")
puts ColorFormat.yellow(MultiJson.dump(config, :pretty => true).
gsub(/"repo_token": "(.*?)"/,'"repo_token": "[secure]"'))
end
hash.merge(config)
end
end
end
end
6 changes: 3 additions & 3 deletions lib/coveralls/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ def self.git
hash

rescue Exception => e
puts "Coveralls git error:".red
puts e.to_s.red
puts "#{ ColorFormat.red }Coveralls git error: #{ ColorFormat.reset }"
puts "#{ ColorFormat.red }#{ e.to_s.red }#{ ColorFormat.reset }"
nil
end

Expand Down Expand Up @@ -150,4 +150,4 @@ def self.relevant_env
end

end
end
end
24 changes: 12 additions & 12 deletions lib/coveralls/simplecov.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ def display_result(result)
if result.files.length > 0
puts "[Coveralls] Some handy coverage stats:"
else
puts "[Coveralls] There are no covered files.".yellow
puts ColorFormat.yellow("[Coveralls] There are no covered files.")
end
result.files.each do |f|
print " * "
print "#{short_filename(f.filename)}".cyan
print " => ".white
print ColorFormat.cyan(short_filename(f.filename))
print ColorFormat.white(" => ")
cov = "#{f.covered_percent.round}%"
if f.covered_percent > 90
print cov.green
print ColorFormat.green(cov)
elsif f.covered_percent > 80
print cov.yellow
print ColorFormat.yellow(cov)
else
print cov.red
print ColorFormat.red(cov)
end
puts ""
end
Expand Down Expand Up @@ -79,14 +79,14 @@ def format(result)
end

def display_error(e)
puts "Coveralls encountered an exception:".red
puts e.class.to_s.red
puts e.message.red
puts ColorFormat.red("Coveralls encountered an exception:")
puts ColorFormat.red(e.class.to_s)
puts ColorFormat.red(e.message)
e.backtrace.each do |line|
puts line.red
puts ColorFormat.red(line)
end if e.backtrace
if e.respond_to?(:response) && e.response
puts e.response.to_s.red
puts ColorFormat.red(e.response.to_s)
end
false
end
Expand All @@ -102,4 +102,4 @@ def short_filename(filename)

end
end
end
end

0 comments on commit e4c0733

Please sign in to comment.