Archive | June 2011

More intelligent .rvmrc files

One of the joys of using RVM is the use of .rvmrc files, which makes it much easier to ensure that you are always using the right ruby and gemset on a per project basis. It’s also best practice to check you .rvmrc file into version control, because it serves as an important piece of documentation when working in a team, namely “this is the ruby version this application uses”.

However, the problem with using this method – described in more detail on the RVM site – is that it assumes all the members on the team are using gemsets.

Some of the guys in my team are installing their gems in a .bundle folder in the application (not part of version control!) and therefore don’t need gemsets. Both approaches have their pros and cons, but we needed a more flexible way of dealing with this subtle difference which didn’t force the creation and use of gemsets.

Here’s our more flexible .rvmrc file…

ruby_version="1.9.2"
gemset_name="red-dragon"

ok="\033[10;32m"
warn="\033[1;31m"
reset="\033[0m"

if rvm list strings | grep -q "${ruby_version}" ; then
  rvm use ${ruby_version}

  if rvm gemset list | grep -q "${gemset_name}" ; then
    echo -e "${ok}Using the ${gemset_name} gemset${reset}"
    rvm gemset use ${gemset_name}
  else
    echo -e "${ok}The ${gemset_name} gemset does not exist, using global gemset${reset}"
  fi

else
  echo -e "${warn}You do not have the required Ruby installed: ${ruby_version}${reset}"

fi

All we do is check for the presence of the required ruby, then check for the expected gemset. If both are present then use them. Otherwise, just tell the user they don’t exist and leave it up to them to take the appropriate action.

When you go into the project directory in your console, you get this delightful message:

Step through your cucumber features interactively

NOTE: This is probably only useful for features that run in browser mode i.e. NOT headless. Using Rails 3 and Capybara, these would be features tagged as @javascript.

Do you find yourself using the save_and_open_page method to debug your cucumber features? Or how about raising exceptions to break the scenario execution? I guess it works, but not very elegant…

One easy way to pause the scenario execution after each step is to use the cucumber callbacks.

Start by creating a hooks.rb file in your features/support folder. You can name the file whatever you want, but it must have a .rb extension. Then add the following snippet to your new file:

AfterStep('@pause') do
  print "Press Return to continue..."
  STDIN.getc
end

What we’re doing here is using the AfterStep callback to execute our code after each cucumber steps has finished. The parameter is telling cucumber to only do this for features that have the @pause tag. We then wait for a carriage return from the console before moving to the next step.

Now all you have to do is tag a feature with @pause and you will have to press Return after each step to walk through the scenario. This makes debugging a snap when you’re using something like Firebug etc.

Try it out for yourself.

Follow

Get every new post delivered to your Inbox.