Properly colored test results in TextMate

Ruby on Rails, Software development Add comments

colored_textmate_tests1.pngI’ve been using this technique to color my Rails test results for quite some time and I absolutely love it. It really makes it easy to see if a test failed. However, when I ran my tests in TextMate (CMD-R) with colors enabled, I got garbage similar to this: ‘[1m [37m3 [0m [0m [37mtests [0m, [1m [37m3 [0m [0m [37massertions [0m. Not very pleasant!

I asked on IRC if there was an easy way to convert the color codes produced by the ‘color’ gem, but apparently, there wasn’t. I was adviced to disable coloring but that was not acceptable for me. I still want colors when I run my tests in Terminal. I’ve also been told to modify the Ruby bundle to disable colors only when TextMate ran the tests (since TextMate adds some basic coloring on its own). I wanted to keep the same color layout, regardless of whether I was in Terminal or in TextMate, so I decided to hack TextMate.

Since the test results are displayed as pure HTML, it was easy enough to write a parser that converts color codes to simple HTML. It involved editing a single file in TextMate.app so an upgrade is installed, you’ll have to do it again. Luckily, it’s a very simple hack…

  1. Open /Applications/TextMate.app/Contents/SharedSupport/Support/lib/escape.rb in your favorite text editor (TextMate!?)
  2. Change the last line of the htmlize method for colored_htmlize(str.gsub(”\n”, “<br />”))
  3. Add the content of this file at the end of escape.rb. Your escape.rb file should now look like this.
  4. That’s it! TextMate will now convert every “colored” string it it encounters into HTML.

If anyone knows how to make this work without editing files inside TextMate.app, I’d appreciate your comments, but from the debate we has on #textmate, the way I did it is probably the best, until the RubyMate team integrate my hack into the core code.

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in