You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sometimes you wrote a gem, you need to test it with multiple dependencies, and you use Travis CI. You don't know where to get started, this article is for you!
Gemfile for RubyGem
A RubyGem typically contains a .gemspec and a Gemfile. Personally I specified all runtime dependencies in .gemspec, and all non-runtime dependencies (gems for development, test environments) in Gemfile.
source"https://rubygems.org"# Specify your gem's dependencies in your-gem.gemspecgemspecgroup:developmentdo# development environment dependenciesendgroup:testdo# test environment dependenciesend
Suppose you wrote a gem foo that need to test from Ruby 2.0 to trunk Ruby, and from Rails 3 to Rails 5.
The build will run according to this order. So it is important you put the version you care the most on top (cannot fail), and the version you care the least at bottom (those can fail).
Some builds are impossible to run on Rails 5, like unsuppoted Ruby version (<= 2.0). Some builds are safe to fail, like build with trunk Ruby; You can specify which Ruby versions in .travis.yml that can exclude or can fail:
With fast_finish: true in place, as soon as these builds finish:
Ruby 2.3.1 (2.3.1)
Ruby 2.2.5 (2.2.5)
Ruby 2.1.x (2.1)
The whole build will be considered as PASSED / FAILED.
OK. So you now know how to run your build with different Ruby on Travis CI.
How about run the build with different dependencies (gems)?
Test with Multiple Dependencies
Thanks to Bundler, we can specify our dependencies in Gemfile. And Travis CI let you run your build with multiple Gemfile, if we want to test our build from Rails 3 to Rails 5:
Create a folder gemfiles on project root and put rails_3.gemfile, rails_4.gemfile, and rails_5.gemfile files. And you specify your dependencies in each file. Travis will now run your build with Ruby versions you specified combined with these gemfiles:
2.3.1 Rails 3
2.2.5 Rails 3
2.1.x Rails 3
2.0.x Rails 3
Trunk Ruby Rails 3
2.3.1 Rails 4
2.2.5 Rails 4
2.1.x Rails 4
2.0.x Rails 4
Trunk Ruby Rails 4
2.3.1 Rails 5
2.2.5 Rails 5
2.1.x Rails 5
2.0.x Rails 5
Trunk Ruby Rails 5
And you can also specify which Ruby with certain Rails version can fail, for example, Rails 5 required Ruby version 2.2.2, so Ruby version < 2.2.2 will fail:
Appraisal runs your tests across configurable, reproducible scenarios that describe variations in dependencies. For example, if you need to test with versions of Rails
First install appraisal gem to development environment:
Relaxes Ick's dependency on the `redis` gem.
Using the appraisal gem and a pattern from jollygoodcode/jollygoodcode.github.io#21 to test with a wider range of redis gem versions.
Intro
Sometimes you wrote a gem, you need to test it with multiple dependencies, and you use Travis CI. You don't know where to get started, this article is for you!
Gemfile for RubyGem
A RubyGem typically contains a
.gemspec
and aGemfile
. Personally I specified all runtime dependencies in.gemspec
, and all non-runtime dependencies (gems for development, test environments) inGemfile
.Suppose you wrote a gem
foo
that need to test from Ruby 2.0 to trunk Ruby, and from Rails 3 to Rails 5..travis.yml
Test with Multiple Ruby Versions
This will run your build with Ruby:
The build will run according to this order. So it is important you put the version you care the most on top (cannot fail), and the version you care the least at bottom (those can fail).
Some builds are impossible to run on Rails 5, like unsuppoted Ruby version (<= 2.0). Some builds are safe to fail, like build with trunk Ruby; You can specify which Ruby versions in
.travis.yml
that can exclude or can fail:With
fast_finish: true
in place, as soon as these builds finish:The whole build will be considered as PASSED / FAILED.
OK. So you now know how to run your build with different Ruby on Travis CI.
How about run the build with different dependencies (gems)?
Test with Multiple Dependencies
Thanks to Bundler, we can specify our dependencies in
Gemfile
. And Travis CI let you run your build with multipleGemfile
, if we want to test our build from Rails 3 to Rails 5:Create a folder
gemfiles
on project root and putrails_3.gemfile
,rails_4.gemfile
, andrails_5.gemfile
files. And you specify your dependencies in each file. Travis will now run your build with Ruby versions you specified combined with these gemfiles:And you can also specify which Ruby with certain Rails version can fail, for example, Rails 5 required Ruby version 2.2.2, so Ruby version < 2.2.2 will fail:
OK how do we create these gemfiles:
Gemfile
gemfiles/rails_3.gemfile
gemfiles/rails_4.gemfile
gemfiles/rails_5.gemfile
We will use thoughtbot's Appraisal tool to do that.
thoughtbot/appraisal
Appraisal runs your tests across configurable, reproducible scenarios that describe variations in dependencies. For example, if you need to test with versions of Rails
First install
appraisal
gem to development environment:Then creates a
Appraisals
file with following content:And put common dependencies in
Gemfile
:then
Appraisal
file can just be:Add:
to the top of your
Rakefile
.Run
$ appraisal install
command,appraisal
command-line program will read yourAppraisals
file and generate multipleGemfile
s togemfiles
folder.With everything in place, you can now run tests for Rails 3, 4, 5 with
appraisal
:Or run tests with all Rails:
To learn more about how appraisal works, please refer to the Appraisal README.md
Wrap Up
You learned...
Gemfile
and.gemspec
for RubyGemGemfile
s on Travis CIappraisal
to generateGemfile
sSee a real-life example here: gjtorikian/html-pipeline#257.
The text was updated successfully, but these errors were encountered: