Cucumber and IronRuby: It Runs!

Ever since I read about Cucumber, a user acceptance testing tool in Ruby, I’ve wanted to be able to use it, along with IronRuby for my .Net projects. I got it working once, briefly, using the directions on the Cucumber wiki at GitHub. With an uncertain combination of IronRuby updates and Cucumber updates, it stopped working for me. Well, this evening, I decided to delve into it once again, and I am now happy to report that it works! Well, works, as in runs the C# sample provided with the Cucumber gem. I know present to you the steps I took to get it working, from end to end (I’ll assume you have some version of Visual Studio 2008 with C#):

1 – Install MSysGit

Head on over the the MSysGit page, grab the latest Git-1.6.x.y install, and run it. At the time of this writing, I’m using 1.6.2.2-preview20090408

2 – Grab the IronRuby sources

I prefer to do all my development work in C:\Development. Pop open a Git Bash console (Start > Programs > Git > Git Bash) and issue the following commands:

cd /C/Development
git clone git://github.com/ironruby/ironruby.git
git pull

OR, if you’re behind a corporate firewall…

cd /C/Development
git clone http://github.com/ironruby/ironruby.git
git pull

3 – Set Up IronRuby Dev Environment

This comes straight from the dev.bat entry in the IronRuby wiki on GitHub:

We recommend you start your developing by running

1
C:\path\to\Merlin\Main\Languages\Ruby\Scripts\Dev.bat

. This batch file sets up the path, various environment variables, and aliases, which makes it easy to do builds and run tests.

It is recommended to setup a shortcut to Dev.bat on the desktop that you can just click to quickly get the pre-configured environment. To do this, create a shortcut on the Desktop to cmd.exe (where C:\Development\ironruby is the root of your GIT repo) looking like CmdShortcut.png at http://www.ironruby.net/Support/Images. The values of the text fields should be like this:

Target:

1
C:\Windows\System32\cmd.exe /k "c:\Development\IronRuby\Merlin\Main\Languages\Ruby\Scripts\Dev.bat"

Start in:

1
c:\Development\IronRuby\Merlin\Main\Languages\Ruby

I highly recommend reading the rest of that wiki entry. It explains how the IronRuby sources are laid out.

I’m also made a change to Dev.bat. On line 43, after :EnvDone, the script changes the path. I’m going to prepend the path to the IronRuby interpreter, which we’ll compile in the next step. Why prepend? Well, I also have MRI in my path, and I found that the commands in C:\Ruby\bin were getting called instead of the IronRuby versions. This is what I believe was leading to all the difficulties I had in the past. So, the call to SET PATH should look something like this:

1
set PATH=%MERLIN_ROOT%\Bin\debug;%PATH%;%MERLIN_ROOT%\Languages\Ruby\Scripts;%MERLIN_ROOT%\Languages\Ruby\Scripts\bin;%RUBY18_BIN%;%MERLIN_ROOT%\..\External.LCA_RESTRICTED\Languages\IronRuby\mspec\mspec\bin

Addendum – June 14th, 2009

After some feedback on this post from Jimmy Schementi and Jim Deville of the IronRuby team, you’re going to want to set another environment variable, called GEM_BIN. We will reference this variable later, when we go to install gems for IronRuby. After the line that sets GEM_PATH, about line 16, you want to include this additional line:

1
set GEM_BIN=%MERLIN_ROOT%\Languages\Ruby\Scripts\Bin

Adding this will allow us to tell Ruby Gems where to put the scripts needed to run cucumber, or any other gem. I’ve chosen %MERLIN_ROOT%\Languages\Ruby\Scripts\Bin, on Jimmy’s suggestion, because it is already in the path set by Dev.bat, and IronRuby distributes a number of other wrapper scripts, such as igem, irake, and irails in this directory.

4 – Compile IronRuby

Pop open the IronRuby Development console (the shortcut you created in the previous step), and run

1
brbd

(Build RuBy Debug).

5 – Install the Cucumber Gem

IronRuby comes with a command called igem, which runs the gem command using IronRuby, as opposed to the standard MRI. In that same development console, run:

1
igem install –-no-rdoc -–no-ri –-bindir %GEM_BIN% cucumber

That’s a lot to type! We could stick all these options into a .gemrc file in our home directory, but this is problematic if you have and use multiple versions of Ruby on your machine (and you most likely will). Any suggestions for accommodating the scenario of multiple Ruby versions with seperate Gem locations would be very much appreciated! Now, back to the show!

I’m choosing to include the

1
–-no-rdoc

and

1
-–no-ri

because RDoc and RI generation is slooooow, and I can’t recall the last time I looked at either.

I’m also passing the

1
–-bindir

argument so that the wrapper scripts for the gems I install will be placed into a directory that is in my path, but will not get overwritten every time I recompile IronRuby.

This will install the gem files into

1
C:\Development\ironruby\Merlin\External.LCA_RESTRICTED\Languages\Ruby\redist-libs\ironruby\gems\1.8\gems\cucumber-xxxx

, where xxxx is the latest version of Cucumber. I’m going to use cucumber-xxxx in the next step to signify the folder that cucumber gets installed into.

This will also install the

1
cucumber & cucumber.bat

wrapper scripts into

1
<strike>C:\Development\ironruby\Merlin\Main\Bin\debug</strike>C:\Development\ironruby\Merlin\Main\Languages\Ruby\Scripts\Bin

6 – Verify that it Works

Here comes the cool part. In that same IronRuby dev console window, do the following:

cd C:\Development\ironruby\Merlin\External.LCA_RESTRICTED\Languages\Ruby\redist-libs\ironruby\gems\1.8\gems\cucumber-xxxx\examples\cs
compile.bat
cucumber features

This is the output that I got:

*** WARNING: You must "gem install win32console" (1.2.0 or higher) to get colour
ed output on MRI/Windows
Feature: Addition
  In order to avoid silly mistakes
  As a math idiot
  I want to be told the sum of two numbers

unknown:0: warning: multiple values for a block parameter (2 for 1)
unknown:0: warning: multiple values for a block parameter (2 for 1)
  Scenario Outline: Add two numbers                    # features/addition.feature:6
unknown:0: warning: multiple values for a block parameter (2 for 1)
    Given I have entered
 into the calculator # features/step_definitons/calculator_steps.rb:9
unknown:0: warning: multiple values for a block parameter (2 for 1)
    And I have entered
 into the calculator   # features/step_definitons/calculator_steps.rb:9
unknown:0: warning: multiple values for a block parameter (2 for 1)
    When I press add                                   # features/step_definitons/calculator_steps.rb:13
unknown:0: warning: multiple values for a block parameter (2 for 1)
    Then the result should be  on the screen   # features/step_definitons/calculator_steps.rb:17

    Examples:
      | input_1 | input_2 | output |
      | 20      | 30      | 50     |
      | 2       | 5       | 7      |
      | 0       | 40      | 40     |

3 scenarios (3 passed)
12 steps (12 passed)
0m3.734s

Notice that there are still some warnings. I know John Lam and the rest of the IronRuby team are working hard for to get this working for the 1.0 release, due on or about July 23rd, 2009. Most importantly, though, it functions now. Cool beans!

SUPER KIND-OF IMPORTANT NOTE!

Any time you rebuild IronRuby, the C# compiler will erase the contents of the directory

1
C:\Development\IronRuby\Merlin\Main\Bin\Debug

. This includes the cucumber and cucumber.bat files. The only way I know to rectify this is to

1
<strike>igem install -–no-rdoc -–no-ri cucumber</strike>

once again. Thanks to feedback from the IronRuby team, I’ve detailed a fix above. You will only have to install gems once, regardless of how often you recompile IronRuby.

  1. ByteMaster says:

    Nice Post – I’m going to have to give this a try next week!

  2. Thanks for the great writeup Will!

    Feel free to update the Cucumber wiki page on the subject – with your new findings.

    Now – who wants to do something similar to Cuke4Duke for .NET?
    http://wiki.github.com/aslakhellesoy/cuke4duke

    Wouldn’t it be awesome with StepDefinitions in C# or even F#?

  3. rifraf says:

    This looks really good, but I’m afraid I get the following crash when running igem. I’ll try to debug it later, but any thoughts?

    V:\Play\IronRubyGit\ironruby\Merlin\Main\Languages\Ruby>igem install –no-rdoc -
    -no-ri –bindir %GEM_BIN% cucumber
    mscorlib:0:in `GetBytes’: Value does not fall within the expected range. (System
    ::Text::EncoderFallbackException)
    from v:\Play\IronRubyGit\ironruby\Merlin\Main\Languages\Ruby\Libraries.L
    CA_RESTRICTED\Builtins\IoOps.cs:692:in `write’
    from v:\Play\IronRubyGit\ironruby\Merlin\Main\Languages\Ruby\Libraries.L
    CA_RESTRICTED\Builtins\IoOps.cs:652:in `puts’
    from V:/Play/IronRubyGit/ironruby/Merlin/External.LCA_RESTRICTED/Languag
    es/Ruby/redist-libs/ruby/site_ruby/1.8/rubygems/user_interaction.rb:227:in `aler
    t_error’
    from V:/Play/IronRubyGit/ironruby/Merlin/External.LCA_RESTRICTED/Languag
    es/Ruby/redist-libs/ruby/site_ruby/1.8/rubygems/user_interaction.rb:103:in `aler
    t_error’
    from V:/Play/IronRubyGit/ironruby/Merlin/External.LCA_RESTRICTED/Languag
    es/Ruby/redist-libs/ruby/site_ruby/1.8/rubygems/command_manager.rb:77:in `run’
    from V:/Play/IronRubyGit/ironruby/Merlin/External.LCA_RESTRICTED/Languag
    es/Ruby/redist-libs/ruby/site_ruby/1.8/rubygems/gem_runner.rb:39:in `run’
    from V:/Play/IronRubyGit/ironruby/Merlin/Main/Languages/Ruby/Scripts/bin
    /igem:24

  4. Will says:

    Yeah, I started seeing that myself after a recent git pull of IronRuby. Looks like command line option parsing is busted right now. It will work if you omit the

    1
    --no-rdoc --no-ri --bindir %GEM_BIN%

    arguments. The drawback to this is that the wrapper scripts get installed in the

    1
    %MERLIN_ROOT%\Bin\Debug

    folder. The contents of this folder get wiped out by the compiler when you recompile IronRuby. So, you need to reinstall the gems again to get the wrapper scripts back.

    I’ve filed a bug for this. That’s the fun of living on the edge of pre-release software.

  5. rifraf says:

    Woo-hoo! Thanks.
    Though it’s still looking a bit scary for a viable IronRuby v1.0 release in a month’s time ;-(

  6. System::Text::EncoderFallbackException is caused by a presence of a Unicode character \u2013 (‘–’) in “-–no-ri” argument. If you just copy & paste the command line:

    igem install –-no-rdoc -–no-ri –-bindir %GEM_BIN% cucumber

    from above you get the error. If you typed it it should work.
    IronRuby should indeed report a better error and we’ll fix that.

  7. Hi,
    Nice post. I have a post on the similar lines at http://blog.webintellix.com/2009/10/how-to-use-cucumber-with-net-and-c.html. Need to get more and more people to use TDD in .NET/C#.

    Thanks,
    Rupak Ganguly
    http://blog.webintellix.com
    http://rails.webintellix.com

  1. [...] after I published my previous post on getting IronRuby working with Cucumber, I did a git pull and recompile from the IronRuby repo. Well, something changed which broke command [...]

  2. [...] hotgazpacho » Blog Archive » Cucumber and IronRuby: It Runs! – yes, it is an oxymo… – June 26th ( tags: cucumber ruby ironruby ) [...]