Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Don't show colors if stdout is not a tty #50

Merged
merged 2 commits into from

2 participants

Tim Pope 7rans
Tim Pope

This prevents raw color codes popping up if the output of turn is piped to a pager or redirected to a file, and is generally considered to be a best practice for command line tools. It also prevents them from showing up if turn is invoked inside ruby backticks, which has the unfortunate side effect of breaking the tests that looks for colors. I'm open to suggestions of how to fix this.

7rans
Owner

The tests in question could override this and temporarily force colorization to come out.

Tim Pope

Correct me if I'm missing something here, but since we are basically testing the color conditional itself, and since the test suite spawns turn in a subshell, there's not a good way to override code running there, correct? I mean I could pass in COLOR=force_if_stdout_tty or something, but then the actual logic gets convoluted.

It seems like it would be easier to just switch the COLORIZE constant to a colorize? method, and unit test that method, no?

To be clear, these are the tests in question.

Tim Pope

I pushed up an example of how that might look.

7rans
Owner

That looks good. Did the tests run okay?

Tim Pope

Everything's green. Or gray if you redirect to a file. :)

Tim Pope

I can squash the two to one commit if you like.

7rans
Owner

Na, it's okay. I just click the big green Merge button to my left :-)

7rans trans merged commit 7a69822 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 26, 2011
  1. Tim Pope
Commits on Aug 27, 2011
  1. Tim Pope
This page is out of date. Refresh to see the latest.
6 lib/turn/autorun/testunit0.rb
View
@@ -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
31 lib/turn/colorize.rb
View
@@ -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')
2  lib/turn/reporters/cue_reporter.rb
View
@@ -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
2  lib/turn/reporters/outline_reporter.rb
View
@@ -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
24 test/test_framework.rb
View
@@ -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
4 work/turn.rb
View
@@ -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
Something went wrong with that request. Please try again.