In case you missed it, Pat Eyler released a nugget known as RedGreen back in May. It adds color to your bland testing lifestyle. Read that post if you haven’t. It’s good. I’ll wait.
Okay, so you know RedGreen is run like so: $ ruby test_good.rb | Redgreen. You know it colorizes the stats bar printed at the end of a test run. You know it’s a great idea. Let’s hack.
Grab my version of redgreen from the Err gem server:
sudo gem install redgreen
What this gem does is give you a script, rg, which can be used to run tests in place of your ruby binary. It’s a real big hack so who knows if it works for any Rubys before / after 1.8.4. When it does work, it looks something like this:
Loaded suite test/test_parser
Started
...........F.........
Finished in 0.058691 seconds.
1) Failure:
test_parse_options(TestParser) [./test/test_parser.rb:12]:
Flunked.
21 tests, 33 assertions, 1 failures, 0 errors
If you’re on Windows, redgreen won’t work out of the box unless you’ve installed the Win32 console stuff as per gordon’s recommendation. The line of code he suggests adding is included in the gem, just get that console package and you should be okay. (I haven’t actually tested this. Let me know if it passes / fails.)
That’s it, more or less. Run your tests with rg test_file.rb and thank Pat. Hrm, that gives me an idea…
autotest
Rob Sanheim recently posted a hack which lets you use Pat Eyler’s RedGreen with autotest. You know, the autotest in ZenTest (sudo gem install ZenTest).
To get this gem version of redgreen working with autotest (from ZenTest 3.3.0), open up (or create) ~/.autotest and add this code wherever:
Autotest.send(:alias_method, :real_make_test_cmd, :make_test_cmd) Autotest.send(:define_method, :make_test_cmd) do |*args| real_make_test_cmd(*args).sub('test/unit', %[rubygems -e "require 'redgreen'"]) end
Another hack won’t hurt, right? Autotest internally uses the make_test_cmd method to build commands needed to run your tests. We’re intercepting the string it returns and replacing instances of test/unit with rubygems -e “require ‘redgreen’”.
Yum. Enjoy.
Update: Issues. Thanks commentators and e-maulers. The status bar redness has been fixed and the .autotest code has been updated.
Again:
sudo gem install redgreen
Thanks for the kind words (about the blog and about redgreen). Thanks even more for taking it places I hadn’t thought of. I should probably email/chat with the folks writing RedGreen hacks to see if someone wants to take it over.
Thanks for the link. Ryan Davis (part of ZenSpider, creators of autotest) has said that he will integrate all this stuff into the next version, fyi. So its just a temporary hack.
Very nice. Thank you.
Thanks for the inspiration, guys. It’s good to know autotest is going to officially color itself up soon.
Nice one. You can also pipe the rake task “rake test | redgreen” to get the whole package or even alias it in a simple rake task.
Another approach is to use Nuby on Rails’ method to generate dynamic rake tasks to achieve this. http://nubyonrails.com/articles/2006/07/28/foscon-and-living-dangerously-with-rake
1 minute rake hack:
task :rg do system “rake test | redgreen” end
rule ”” do |t| system “rake test:#{t.name} | redgreen” end
oops… bad formatting. sorry bout that.
cheers! (and props to alisdair for his l33t bug-noticing skillz)
Am I the only one already having a rg binary as a shortcut for ./script/generator? Otherwise great gem.. :)
Hi, I tried it on windows, but not worked. I have redgreen installed with win32console. I issued rb test_file.rb and got the following on prompt not the colored stuff: Started â†[32m.â†[0mâ†[32m.â†[0mâ†[32m.â†[0mâ†[32m.â†[0mâ†[32m.â†[0mâ†[32
Finished in 0.344 seconds.
â†[32m8 tests, 22 assertions, 0 failures, 0 errorsâ†[0m
Any idea: please mail me at bansalakhil30.10 at gmail dot com
There’s a bit more code needed to get this working on windows. Here’s the diff:
1d0 < require ‘Win32/Console/ANSI’ if PLATFORM =~ /win32/ 16a16 > 17a18,22 > if PLATFORM =~ /win32/ > require ‘Win32Console’ > @io = Win32::Console::ANSI::IO.new() > end >
Chime in.