The best kittens, technology, and video games blog in the world.

Friday, June 01, 2007

Testing programs with multiple versions of Ruby

Jester And Minnie by Deathwaves from flickr (CC-NC-SA)
Long time ago when I was young and naive, I thought it's very unusual for a language to significantly change between minor versions. Later I learned that things break quite often, but I was still too lazy to routinely test them against all versions of Ruby. I'm still pretty lazy, so I automated it. Here are two snippets which might help you with multiversion testing. Instalation:

desc "Install many versions of Ruby"
task :install_rubies => [:install_ruby_1_8_4, :install_ruby_1_8_5, :install_ruby_1_8_6, :install_ruby_1_9]

desc "Install Ruby 1.8.4"
task :install_ruby_1_8_4 do
sh "wget -c ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.4.tar.gz"
sh "tar -xvzf ruby-1.8.4.tar.gz"
Dir.chdir("ruby-1.8.4") {
sh "./configure --prefix=/home/taw/local/ruby-1.8.4"
sh "make"
sh "make install"
}
end

desc "Install Ruby 1.8.5"
task :install_ruby_1_8_5 do
sh "wget -c ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.5.tar.gz"
sh "tar -xvzf ruby-1.8.5.tar.gz"
Dir.chdir("ruby-1.8.5") {
sh "./configure --prefix=/home/taw/local/ruby-1.8.5"
sh "make"
sh "make install"
}
end

desc "Install Ruby 1.8.6"
task :install_ruby_1_8_6 do
sh "wget -c ftp://ftp.ruby-lang.org/pub/ruby/ruby-1.8.6.tar.gz"
sh "tar -xvzf ruby-1.8.6.tar.gz"
Dir.chdir("ruby-1.8.6") {
sh "./configure --prefix=/home/taw/local/ruby-1.8.6"
sh "make"
sh "make install"
}
end

desc "Install Ruby 1.9"
task :install_ruby_1_9 do
sh "sudo apt-get install ruby1.9"
end
And testing:
desc "Run all tests with Ruby 1.8.6/1.8.5/1.8.4/1.9"
task :test_all_rubies => [:tests_1_8_6, :tests_1_8_5, :tests_1_8_4, :tests_1_9]

desc "Run tests with Ruby 1.8.4"
task :tests_1_8_4 do
ruby_bin = "/home/taw/local/ruby-1.8.4/bin/ruby"
sh ruby_bin, "./tests.rb"
end

desc "Run tests with Ruby 1.8.5"
task :tests_1_8_5 do
ruby_bin = "/home/taw/local/ruby-1.8.5/bin/ruby"
sh ruby_bin, "./tests.rb"
end

desc "Run tests with Ruby 1.8.6"
task :tests_1_8_6 do
ruby_bin = "/home/taw/local/ruby-1.8.6/bin/ruby"
sh ruby_bin, "./tests.rb"
end

desc "Run tests with Ruby 1.9"
task :tests_1_9 do
ruby_bin = "/usr/bin/ruby1.9"
sh ruby_bin, "./tests.rb"
end

5 comments:

Anonymous said...

What is the test framework you're using in the example code ?

taw said...

Anonymous: The snippets are Rakefile fragments. The tests are written in standard Ruby test/unit, and work under all versions of Ruby tests.rb simply requires all test_*.rb files.

Stephen Touset said...

Look how redundant 95% of that code is! Surely that can be written in a third of the lines of code.

taw said...

Stephen Touset: You're right, the code can easily be optimized, I just never bothered as I run it only once per computer.

Nathaniel said...

You should check out multiruby in the ZenTest gem... I think you'll like it :-)