Browse files

Merge pull request #50 from tpope/stdout_tty

Don't show colors if stdout is not tty and covert COLORIZE constant into colorize? method.
  • Loading branch information...
2 parents 6ec1e73 + 2ff0438 commit 7a698227ed21427b6bd2be961870303c9d675183 @trans trans committed Aug 26, 2011
View
6 lib/turn/autorun/testunit0.rb
@@ -34,7 +34,7 @@ def t_finished( elapsed_time )
pass = total - failure - error
bar = '=' * 78
- if COLORIZE
+ if colorize?
bar = if pass == total then ::ANSI::Code.green{bar}
else ::ANSI::Code.red{bar} end
end
@@ -49,7 +49,7 @@ def t_test_started( name )
method, file = name.scan(%r/^([^\(]+)\(([^\)]+)\)/o).flatten!
if @t_cur_file != file
@t_cur_file = file
- file = COLORIZE ? ::ANSI::Code.yellow{file} : file
+ file = colorize? ? ::ANSI::Code.yellow{file} : file
turn_out.puts file
end
turn_out.print " %-69s" % method
@@ -76,7 +76,7 @@ def t_fault( fault )
msg << fault.message.gsub("\n","\n\t")
end
- msg = ::ANSI::Code.magenta{msg} if COLORIZE
+ msg = ::ANSI::Code.magenta{msg} if colorize?
turn_out.puts msg
end
View
31 lib/turn/colorize.rb
@@ -12,47 +12,54 @@ module Turn
module Colorize
- COLORLESS_TERMINALS = ['dumb']
- COLORIZE = defined?(::ANSI::Code) && ENV.has_key?('TERM') && !COLORLESS_TERMINALS.include?(ENV['TERM'])
+ COLORLESS_TERMINALS = ['dumb']
+
+ def colorize?
+ defined?(::ANSI::Code) &&
+ ENV.has_key?('TERM') &&
+ !COLORLESS_TERMINALS.include?(ENV['TERM']) &&
+ $stdout.tty?
+ end
+ module_function :colorize?
def self.red(string)
- COLORIZE ? ::ANSI::Code.red{ string } : string
+ colorize? ? ::ANSI::Code.red{ string } : string
end
def self.green(string)
- COLORIZE ? ::ANSI::Code.green{ string } : string
+ colorize? ? ::ANSI::Code.green{ string } : string
end
def self.blue(string)
- COLORIZE ? ::ANSI::Code.blue{ string } : string
+ colorize? ? ::ANSI::Code.blue{ string } : string
end
def self.magenta(string)
- COLORIZE ? ::ANSI::Code.magenta{ string } : string
+ colorize? ? ::ANSI::Code.magenta{ string } : string
end
def self.bold(string)
- COLORIZE ? ::ANSI::Code.bold{ string } : string
+ colorize? ? ::ANSI::Code.bold{ string } : string
end
def self.pass(string)
- COLORIZE ? ::ANSI::Code.green{ string } : string
+ colorize? ? ::ANSI::Code.green{ string } : string
end
def self.fail(string)
- COLORIZE ? ::ANSI::Code.red{ string } : string
+ colorize? ? ::ANSI::Code.red{ string } : string
end
#def self.error(string)
- # COLORIZE ? ::ANSI::Code.white{ ::ANSI::Code.on_red{ string } } : string
+ # colorize? ? ::ANSI::Code.white{ ::ANSI::Code.on_red{ string } } : string
#end
def self.error(string)
- COLORIZE ? ::ANSI::Code.yellow{ string } : string
+ colorize? ? ::ANSI::Code.yellow{ string } : string
end
def self.skip(string)
- COLORIZE ? ::ANSI::Code.cyan{ string } : string
+ colorize? ? ::ANSI::Code.cyan{ string } : string
end
PASS = pass('PASS')
View
2 lib/turn/reporters/cue_reporter.rb
@@ -105,7 +105,7 @@ def finish_suite(suite)
pass = total - failure - error
bar = '=' * 78
- if COLORIZE
+ if colorize?
bar = if pass == total then Colorize.green(bar)
else Colorize.red(bar) end
end
View
2 lib/turn/reporters/outline_reporter.rb
@@ -137,7 +137,7 @@ def finish_suite(suite)
pass = total - failure - error
bar = '=' * 78
- if COLORIZE
+ if colorize?
bar = if pass == total then Colorize.green(bar)
else Colorize.red(bar) end
end
View
24 test/test_framework.rb
@@ -9,13 +9,27 @@ def test_ruby19_minitest
setup_test('MiniTest')
result = turn 'tmp/test.rb'
assert result.index('PASS')
- assert result.index('[0m') if $ansi
end
- def test_ruby19_minitest_without_color_on_dumb_terminal
- setup_test('MiniTest')
- result = turn_with_term 'dumb', 'tmp/test.rb'
- assert !result.index('[0m')
+ def test_ruby19_minitest_color
+ term, stdout = ENV['TERM'], $stdout
+ $stdout = $stdout.dup
+ def $stdout.tty?
+ true
+ end
+ ENV['TERM'] = 'xterm'
+ assert_equal true, Turn::Colorize.colorize?
+ ENV['TERM'] = 'dumb'
+ assert_equal false, Turn::Colorize.colorize?
+ ENV['TERM'] = nil
+ assert_equal false, Turn::Colorize.colorize?
+ ENV['TERM'] = 'xterm'
+ def $stdout.tty?
+ false
+ end
+ assert_equal false, Turn::Colorize.colorize?
+ ensure
+ ENV['TERM'], $stdout = term, stdout
end
def test_ruby19_minitest_force
View
4 work/turn.rb
@@ -67,14 +67,14 @@ def t_fault(fault)
when ::Test::Unit::Error
msg = "\t"
msg << fault.to_s.split("\n")[2..-1].join("\n\t")
- msg = ::ANSI::Code.magenta(msg) if COLORIZE
+ msg = ::ANSI::Code.magenta(msg) if colorize?
@t_test.error!(msg)
@t_reporter.error(msg)
when ::Test::Unit::Failure
msg = "\t"
msg << fault.location[0] << "\n\t"
msg << fault.message.gsub("\n","\n\t")
- msg = ::ANSI::Code.magenta(msg) if COLORIZE
+ msg = ::ANSI::Code.magenta(msg) if colorize?
@t_test.fail!(msg)
@t_reporter.fail(msg)
end

0 comments on commit 7a69822

Please sign in to comment.