colored output using watchr in rails app
I'm using watchr in my rails3 app since I can't get autotest to work right. Watchr has been great but the only problem is I can't get colored output. I'm using this as my watchr file
If I run my tests manually via "rspec spec/" then I get colored output. However, letting watchr run the tests (via modifying one of my files)开发者_StackOverflow the output is no longer in color.
One thing I notice is that this part seems to prevent colored output somehow
def run(cmd)
puts(cmd)
`#{cmd}`
end
Normally, watchr runs the tests using this (in my watchr file)
result = run "rake spec"
But if I instead do the following I get colored output.
result = system "rake spec"
Only problem is that the above line no longer captures the output to "result", which in turn mans growl notifications no longer work.
Any ideas on how I can have the best of both worlds? Growl notifications AND my test output in color?
I just ran into this issue as well. The solution is to provide the --tty option, in addition to --color, to the rspec command line invocation, e.g.:
result = run("bundle exec rspec --color --tty #{spec}")
According to this thread on the rspec google group:
The --tty option is there to tell RSpec that the output is being streamed to a terminal as opposed to a file, in which case we don't want to print the color codes even if --color is invoked.
If you then "puts" the result back to the terminal, you'll see the original coloring. If you want to parse that output for Growl, you'll want to strip out the ansi color codes using the strip_ansi method Bill Turner provided.
Hope that helps!
This is how I have something along those lines it in my .watchr file:
def run(cmd)
`#{cmd}`
end
def run_spec_file(spec)
system('clear')
puts "Running #{spec}"
result = run("bundle exec rspec #{spec}")
puts result
growl result.split("\n").last rescue nil
end
And the growl bit is this:
def growl(message)
growlnotify = `which growlnotify`.chomp
unless growlnotify.empty?
title = "Test Results"
image = message.match(/\s0\s(errors|failures)/) ? "~/.watchr_images/ruby_green.png" : "~/.watchr_images/ruby_red.png"
options = "-w -n Watchr --image '#{File.expand_path(image)}' -m '#{strip_ansi(message)}' '#{title}'"
run("#{growlnotify} #{options} &")
end
end
Which expects some images in a ~/.watchr_images/ directory on your dev box, and the growlnotify binary installed as well.
EDIT: Oh, and you may also want to check what's in your spec.opts file if there is one - it may not have --color
specified.
And the strip_ansi
in the growl method is used to strip the color codes so it will display in the growl notice.
def strip_ansi(m)
m.gsub(/\e\[[^m]*m/, '')
end
The output not showing up in color has to do with saving the test output to a variable. The way I have watchr setup it save "rake spec" output to a variable, then this variable is output to the terminal using "puts".
def run(cmd)
puts(cmd)
`#{cmd}`
end
# what's called to run my tests
result = run "rake spec"
# then output it
puts result
Now I just need to figure out how to output the info in color!
精彩评论