Ylan Segal

Testing With Multiple Ruby and Gem Versions

I found myself writting a gem for internal use that needed to run in several different ruby versions and MRI. In addition, the projects that it would be used in had other dependencies which also meant that it would need to function with different versions of dependencies. Here is how I ensured I was testing all scenarios.

Testing Multiple Ruby Versions

I started by leveraging rvm, which I use to manage multiple ruby versions on my development machine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/env bash

set -e

rubies=("ruby-2.0.0" "ruby-1.9.3" "jruby-1.6.7.2" "jruby-1.7.3")
for i in "${rubies[@]}"
do
  echo "====================================================="
  echo "$i: Start Test"
  echo "====================================================="
  rvm $i exec bundle
  rvm $i exec bundle exec rspec spec
  echo "====================================================="
  echo "$i: End Test"
  echo "====================================================="
done

The script basically just runs rspec on several versions of ruby. I used set -e to stop execution of the script if any commands has an exit code different than 0.

Testing Multiple Gem Versions

The next piece of the puzzle is the Appraisal gem from the guys at thoughbot. This gem lets you test your gem against different versions of dependencies. It is really elegant, and the instructions on the project page are really easy to follow. My Appraisals files like:

1
2
3
4
5
6
7
appraise "oauth2-latest-release" do
  gem "oauth2"
end

appraise "oauth2-0.4" do
  gem "oauth2", '0.4.1'
end

All Together Now

As a last step, I just modified my first script to use appraisals, so I now run all gem dependency variations against all rubies.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/env bash

set -e

rubies=("ruby-2.0.0" "ruby-1.9.3" "jruby-1.6.7.2" "jruby-1.7.3")
for i in "${rubies[@]}"
do
  echo "====================================================="
  echo "$i: Start Test"
  echo "====================================================="
  rvm $i exec bundle install
  rvm $i exec rake appraisal:install
  rvm $i exec rake appraisal
  echo "====================================================="
  echo "$i: End Test"
  echo "====================================================="
done

Simple, yet effective.