简体   繁体   中英

rubygems / add runtime dependency only for test (or any given env)

Currently working on a gem that extends spec functionalities (it requires factory_bot ), how to define it in the add_runtime_dependency only for the concerned group (here, :test ) ?

Or, is it a better practice to let the gem raise if the concerned dependency is not added by the user in its project ?

If your gem has a runtime dependency it should be listed in the gemspec as that is what allows Bundler to do dependency resolution to see if your gem is compatible with the other gems in the Gemfile.

It is up to the end user to place your gem in a group in the Gemfile. If they place it in the :test group it will only be loaded in the test environment. If they did not read the readme and placed it in the main group then its not your problem.

Gem::Specification.new do |s|
  # ...
  s.add_dependency 'factory_bot', version
  # ...
end 

Note that you can also list development dependencies in your gemspec.

Gem::Specification.new do |s|
  # ...
  s.add_development_dependency 'rubocop', '~> 0.44.1'
end

These dependencies will be used when developing/testing the gem itself but are not "passed on" when you install the gem via bundler.

Is it a better practice to let the gem raise if the concerned dependency is not added by the user in its project ?

No. Ruby already has a good dependency resolver (Bundler). Use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM