Upgrade to Pro — share decks privately, control downloads, hide ads and more …

You have to test multiple versions of your gem's dependencies. You used Appraisal. It's super affective!

You have to test multiple versions of your gem's dependencies. You used Appraisal. It's super affective!

So, it seems like you've finished writing your awesome gem. However, are you sure that your gem is working perfectly against multiple versions of its dependency? Are you sure that you didn't break any backward compatibility? In this talk, I'm going to show you the approach we have been taken to test our gems against multiple versions of dependencies, such as testing against Rails 3.2 and Rails 3.1. I'm also going to introduce you to our gem called "Appraisal" which helps you generating Gemfiles to be used with Bundler, and also guide it to running your test suite against those multiple Gemfiles. Lastly, I'm going to show you how you can config Travis CI to test your gem against those multiple versions of dependencies.

Presented at RubyKaigi 2013 on May 30, 2013.

Video is available at http://vimeo.com/69748748

Prem Sichanugrist

May 30, 2013
Tweet

More Decks by Prem Sichanugrist

Other Decks in Programming

Transcript

  1. You have to test multiple versions
    of your gem’s dependencies.
    You used Appraisal. It’s super effective!

    View full-size slide

  2. ϖϨϜɹγνϟψΪτ
    Prem Sichanugrist
    Developer at thoughtbot
    @sikachu
    /sikachu

    View full-size slide

  3. Published your gem?

    View full-size slide

  4. Fully tested?

    View full-size slide

  5. Works with older
    version of dependencies?

    View full-size slide

  6. Works with older
    version of dependencies?

    View full-size slide

  7. Why testing with
    multiple versions?

    View full-size slide

  8. People doesn’t update their
    project’s dependencies

    View full-size slide

  9. People install your gem

    View full-size slide

  10. Your gem is only tested
    against newer version
    of its dependency

    View full-size slide

  11. “This gem doesn’t work”

    View full-size slide

  12. Works with latest version
    !=
    Works with old versions

    View full-size slide

  13. SemVer
    Semantic Versioning

    View full-size slide

  14. People don’t follow
    SemVer

    View full-size slide

  15. People don’t follow
    SemVer

    View full-size slide

  16. Supported Rails Versions
    • 3.1.x
    • 3.2.x

    View full-size slide

  17. Supported Rails Versions
    • 3.1.x
    • 3.2.x
    • 4.0.0

    View full-size slide

  18. Supported Rails Versions
    • 3.0.x
    • 3.1.x
    • 3.2.x
    • 4.0.0

    View full-size slide

  19. Supported Rails Versions
    • 2.3.x
    • 3.0.x
    • 3.1.x
    • 3.2.x
    • 4.0.0

    View full-size slide

  20. Before Bundler

    View full-size slide

  21. Uninstall and reinstall

    View full-size slide

  22. $ gem uninstall activesupport
    $ gem install activesupport -v 3.1.12
    $ rake
    $ gem install activesupport
    $ rake

    View full-size slide

  23. Why was this so bad?
    • Time consuming
    • Can’t test against multiple versions at once
    • Doesn’t handle dependency of dependency
    • Dependency hell
    • $ rm -Rf $GEM_HOME

    View full-size slide

  24. Test in isolation

    View full-size slide

  25. rbenv
    (with rbenv-gemset)

    View full-size slide

  26. $ rvm gemset create rails-3-2
    $ rvm gemset use rails-3-2
    $ gem install rails -v 3.2.13
    $ rake
    $ rvm gemset create rails-3-1
    $ rvm gemset use rails-3-1
    $ gem install rails -v 3.1.12
    $ rake
    # ...

    View full-size slide

  27. $ rvm @rails-3-1,@rails-3-2 do rake

    View full-size slide

  28. This is good, but ...
    • Waste of space; gems are not shared
    • Contributors need to use Ruby manager
    that supports Gemset
    • May need to have bootstrap script to setup
    gemsets

    View full-size slide

  29. Multiple Gemfiles

    View full-size slide

  30. !"" Gemfile
    !"" rails-3-1.gemfile
    !"" rails-3-2.gemfile
    ...

    View full-size slide

  31. rails-3-1.gemfile:
    source "http://rubygems.org"
    gem "rails", "~> 3.1.12"
    gem "rspec"

    View full-size slide

  32. rails-3-2.gemfile:
    source "http://rubygems.org"
    gem "rails", "~> 3.2.13"
    gem "rspec"

    View full-size slide

  33. $ bundle install --gemfile=rails-3-2.gemfile
    $ export BUNDLE_GEMFILE=rails-3-2.gemfile
    $ bundle exec rake
    $ bundle install --gemfile=rails-3-1.gemfile
    $ export BUNDLE_GEMFILE=rails-3-1.gemfile
    $ bundle exec rake

    View full-size slide

  34. Still hard to maintain

    View full-size slide

  35. Still have to edit
    Gemfiles by hand

    View full-size slide

  36. Have to specify
    dependencies over and over

    View full-size slide

  37. Adding new dependency

    View full-size slide

  38. Adding new dependency
    ... edit multiple files!

    View full-size slide

  39. No easy way to test all
    the versions at once

    View full-size slide

  40. “There should be a
    better way to do this”

    View full-size slide

  41. Set multiple versions of
    dependencies in one place

    View full-size slide

  42. Merge dependencies
    and generate Gemfiles

    View full-size slide

  43. Setup environment for
    Bundler

    View full-size slide

  44. Gemfile:
    source "http://rubygems.org"
    gem "rails"
    gem "rspec"
    gem "appraisal"

    View full-size slide

  45. Gemfile:
    source "http://rubygems.org"
    gemspec

    View full-size slide

  46. Appraisals:
    appraise "rails-3-1" do
    gem "rails", "~> 3.1.12"
    end
    appraise "rails-3-2" do
    gem "rails", "~> 3.2.13"
    end

    View full-size slide

  47. Rakefile:
    require "bundler/setup"
    require "appraisal"
    task "test" do
    # ...
    end

    View full-size slide

  48. Rakefile:
    require "bundler/setup"
    require "appraisal"
    task "test" do
    # ...
    end

    View full-size slide

  49. # Generate Gemfiles
    $ rake appraisal:gemfiles
    # Install dependencies
    $ rake appraisal:install

    View full-size slide

  50. # Run "test" task against all versions
    $ rake appraisal test
    # Run "test" task against a single version
    $ rake appraisal:rails-3-1 test

    View full-size slide

  51. Appraisal
    • Allow you to set dependencies in a single
    place
    • Generate sub-Gemfiles for you
    • Install multiple versions of dependencies
    • Setup Bundler to use the correct version
    of dependency

    View full-size slide

  52. Show that your gem is
    working with multiple
    versions of dependency

    View full-size slide

  53. .travis.yml:
    gemfile:
    - gemfiles/rails_3_1.gemfile
    - gemfiles/rails_3_2.gemfile

    View full-size slide

  54. Test it against multiple
    versions of its dependencies

    View full-size slide

  55. Test it against multiple
    versions of its dependencies
    by using Appraisal x

    View full-size slide

  56. RUBYKAIGI
    Promo code:
    $20 off first month of Prime
    http://learn.thoughtbot.com
    ͋Γ͕ͱ͏͍͟͝·͢ʂ

    View full-size slide