简体   繁体   English

rubygems /添加运行时依赖项仅用于测试(或任何给定的环境)

[英]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 ) ? 当前正在开发可扩展规范功能的gem(它需要factory_bot ),如何仅在add_runtime_dependency中为相关组(此处为: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 ? 或者,如果用户未在项目中添加相关的依赖项,让gem升高是更好的做法吗?

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. 如果您的gem具有运行时依赖项,则应在gemspec中列出它,这是使Bundler进行依赖项解析以查看您的gem是否与Gemfile中的其他gem兼容的原因。

It is up to the end user to place your gem in a group in the Gemfile. 最终用户可以将自己的宝石放入Gemfile中的组中。 If they place it in the :test group it will only be loaded in the test environment. 如果他们将其放在:test组中,它将仅在测试环境中加载。 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. 请注意,您还可以在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. 这些依赖关系将在开发/测试gem本身时使用,但在通过捆绑程序安装gem时不会“传递”。

Is it a better practice to let the gem raise if the concerned dependency is not added by the user in its project ? 如果用户未在项目中添加相关的依赖项,让gem升起是更好的做法吗?

No. Ruby already has a good dependency resolver (Bundler). 红宝石已经有一个很好的依赖解析器(捆扎机)。 Use it. 用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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