Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Merged
merged 2 commits into from

2 participants

@tpope

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.

@trans
Owner

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

@tpope

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.

@tpope

I pushed up an example of how that might look.

@trans
Owner

That looks good. Did the tests run okay?

@tpope

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

@tpope

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

@trans
Owner

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

@trans trans merged commit 7a69822 into turn-project:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 26, 2011
  1. @tpope
Commits on Aug 27, 2011
  1. @tpope
This page is out of date. Refresh to see the latest.
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
Something went wrong with that request. Please try again.