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

F1c4a3bb1606cc4a61711e61e2fe6146?s=128

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!
  2. ϖϨϜɹγνϟψΪτ Prem Sichanugrist Developer at thoughtbot @sikachu /sikachu

  3. None
  4. Published your gem?

  5. Fully tested?

  6. Works with older version of dependencies?

  7. Works with older version of dependencies?

  8. Why testing with multiple versions?

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

  10. People install your gem

  11. Your gem is only tested against newer version of its

    dependency
  12. “This gem doesn’t work”

  13. None
  14. Works with latest version != Works with old versions

  15. ~>

  16. SemVer Semantic Versioning

  17. ~> 3.2

  18. ~> 3.2

  19. ~> 3.2

  20. People don’t follow SemVer

  21. People don’t follow SemVer

  22. None
  23. Supported Rails Versions • 3.1.x • 3.2.x

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

  25. Supported Rails Versions • 3.0.x • 3.1.x • 3.2.x •

    4.0.0
  26. Supported Rails Versions • 2.3.x • 3.0.x • 3.1.x •

    3.2.x • 4.0.0
  27. How?

  28. Before Bundler

  29. Uninstall and reinstall

  30. $ gem uninstall activesupport $ gem install activesupport -v 3.1.12

    $ rake $ gem install activesupport $ rake
  31. ·͍ͣ (Bad)

  32. 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
  33. Test in isolation

  34. Gemset

  35. rbenv (with rbenv-gemset)

  36. $ 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 # ...
  37. $ rvm @rails-3-1,@rails-3-2 do rake

  38. None
  39. 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
  40. None
  41. Multiple Gemfiles

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

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

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

  45. $ 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
  46. None
  47. None
  48. Still hard to maintain

  49. Still have to edit Gemfiles by hand

  50. Have to specify dependencies over and over

  51. Adding new dependency

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

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

  54. None
  55. “There should be a better way to do this”

  56. Appraisal

  57. Set multiple versions of dependencies in one place

  58. Merge dependencies and generate Gemfiles

  59. Setup environment for Bundler

  60. None
  61. Demo

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

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

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

    "rails-3-2" do gem "rails", "~> 3.2.13" end
  65. Rakefile: require "bundler/setup" require "appraisal" task "test" do # ...

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

    end
  67. # Generate Gemfiles $ rake appraisal:gemfiles # Install dependencies $

    rake appraisal:install
  68. # Run "test" task against all versions $ rake appraisal

    test # Run "test" task against a single version $ rake appraisal:rails-3-1 test
  69. 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
  70. None
  71. Show that your gem is working with multiple versions of

    dependency
  72. .travis.yml: gemfile: - gemfiles/rails_3_1.gemfile - gemfiles/rails_3_2.gemfile

  73. None
  74. None
  75. None
  76. Remember

  77. None
  78. Test it against multiple versions of its dependencies

  79. Test it against multiple versions of its dependencies by using

    Appraisal x
  80. None
  81. None
  82. RUBYKAIGI Promo code: $20 off first month of Prime http://learn.thoughtbot.com

    ͋Γ͕ͱ͏͍͟͝·͢ʂ