IronRuby and RSpec with color

So I’m using Rspec to test some IronRuby code on my Vista box. I tried the —colour switch only to be told that I needed to install the win32console gem. No biggie right? I installed that gem and was told something about something about something, I don’t even remember it all and can’t find my notes now.

The thing that I do remember is that I still could not get color output from Rspec. So I did some googling (or is it googleing?) and found iron-term-ansicolor.rb in this github repo by hotgazpacho .

So I gave that a try. It worked well in ir, but still wouldn’t work with Rspec. hotgazpacho was opening Kernel#puts but it appeared that I needed to open IO#write to get console.exe to show the colored output. ( I use console.exe when I runs tests on my Vista box. It is great).

So, using the iron-term-ansicolor.rb code as a guide, I hacked together this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# somefile.rb
require 'mscorlib'


# add the trailing m  because some of the rspec output spans multiple lines
ANSI_REGEXP = /\e\[(.+?)m(.+?)(?=(\e\[0m|\z))/m
SsC=System::ConsoleColor

class IO
alias_method :original_write, :write

def write(*args)
   count = 0
   args.each do |arg|

   fg_color = System::Console.ForegroundColor
   bg_color = System::Console.BackgroundColor

  ##dkb
   if ANSI_REGEXP.match(arg)
     data = extract_ansi_data(arg)
   else
     data = {:text => arg}
   end 

   System::Console.ForegroundColor = data[:foreground] || fg_color
   System::Console.BackgroundColor = data[:background] || bg_color
   count = original_write(data[:text])
   System::Console.BackgroundColor = bg_color
   System::Console.ForegroundColor = fg_color
 end
 count
end

private
def extract_ansi_data(arg)
 fg_color_map =    Hash[30,SsC.Black,31,SsC.Red,32,SsC.DarkGreen ,
                             33,SsC.DarkYellow, 34, SsC.DarkBlue,35,SsC.DarkMagenta,
                              36,SsC.DarkCyan,37,SsC.Gray]
 bg_color_map = Hash[40,SsC.Black,41,SsC.DarkRed,42,SsC.DarkGreen,
                          43,SsC.DarkYellow, 44,SsC.DarkBlue, 45,SsC.DarkMagenta, 
                          46,SsC.DarkCyan,47,SsC.Gray]
 matches = ANSI_REGEXP.match(arg)
 fg_color = fg_color_map[matches[1].to_i] || System::Console.ForegroundColor
 bg_color = bg_color_map[matches[1].to_i] || System::Console.BackgroundColor
 { :foreground => fg_color, :background => bg_color,:text => matches[2]}
end

(Save this code somewhere in the IronRuby load path)

Then, I dove into the rspec code and in rspec-1.2.9\lib\spec\runner\options.rb I made this small change to the def colour= method (about line 191)

1
2
3
4
 ##comment out the line below
 #require 'Win32/Console/ANSI' ;\
 ## add this line
    require 'dkb-iron-term-ansicolor'  #or whatever you name the code above

With these 2 changes, I can use the -c option of Rspec to get the glorious green/red output when testing my IronRuby code. There may be a better, cleaner way, but I did a bit of searching and could not find it. This way seems to work, plus it gave me a good excuse to dig into the Rspec code.

Looking for more? Head on over to the archives.

Post a comment


(lesstile enabled - surround code blocks with ---)

Pages

Twitter_logo_s Github

Categories

There Must Be An Easier Way © David Blackmon. Valid XHTML and ATOM. Powered by Enki.