Testing your gem against multiple rubies and rails versions with RVM

I recently wanted to make it easier for contributors to ActiveHash to test their changes against multiple versions of Rails, with multiple versions of Ruby. My stories looked like this:

As a contributor
I want to be able to run `bundle install`, then quickly run the suite spec suite against the latest released version of rails
So that I can develop quickly using a familiar workflow
As a gem maintainer
I want to be able to run the spec suite against 3 different versions of ruby, each with 5 different versions of rails
So that I can release the gem with confidence that I'm not going to break people's apps

In this post I’ll explain how I did that with a (relatively) simple shell script.

The Gemfile checks the ACTIVE_HASH_ACTIVERECORD_VERSION environment variable, and sets the correct gem version/git repo accordingly. If it gets a blank ACTIVE_HASH_ACTIVERECORD_VERSION it does not set the version in the Gemfile, so when the bash script calls bundle update activerecord it conveniently fetches the latest released version.

If you execute bundle update, it won’t have a ACTIVE_HASH_ACTIVERECORD_VERSION so it will fetch the latest, so the normal workflow still works as expected.

Finally, it runs the function in each of 3 rubies:

rvm use ruby-1.8.7@active_hash --create
run
rvm use ree-1.8.7@active_hash --create
run
rvm use ruby-1.9.2@active_hash --create
run
echo 'Success!'

RVM allows you to run a command with all of your installed rubies, but in this case I wanted to target 3 particular versions, and ensure that they work. RVM will fail if you don’t have the latest version of each ruby installed, and prompt you to install it. And since set -e was called at the top of the file, there’s no need to check the return code of any of the functions because the script will fail if any individual command fails.

If you are a gem maintainer, this is an easy way to ensure that your gem is highly compatible with standard Ruby / Rails setups.