Say in my Gemfile I have
在我的Gemfile中说
group :test do
gem 'rspec-core', require: false
end
Is there an easy way to see if the test
group has been bundled? (like, in this case bundle
may have been called with or without --without test
).
有没有简单的方法来查看测试组是否已捆绑? (例如,在这种情况下,捆绑可能已经被调用,有或没有 - 没有测试)。
I couldn't find one, therefore I started looking for whether or not rspec-core
is requirable, and found a few apparent solutions:
我找不到一个,因此我开始寻找是否可以获得rspec-core,并找到了一些明显的解决方案:
Bundler.definition.index.search("rspec-core")
# or
Gem.loaded_specs["rspec-core"]
What's the most stable API to determine if the gem is requirable?
什么是最稳定的API来确定宝石是否可以获得?
(without trying to require it and rescuing LoadError
)
(不试图要求它并抢救LoadError)
2 个解决方案
#1
3
When a Rails app is generated it typically includes a line that uses Rails.env to determine which group to require. It should look something like this Bundler.require(:default, Rails.env)
. This typically happens in the initialization of a Rails App. Here is a snippet of some code that does that:
生成Rails应用程序时,它通常包含一行使用Rails.env来确定需要哪个组。它应该看起来像这个Bundler.require(:default,Rails.env)。这通常发生在Rails应用程序的初始化中。以下是一些代码的片段:
class Rails::Boot
def run
load_initializer
Rails::Initializer.class_eval do
def load_gems
@bundler_loaded ||= Bundler.require :default, Rails.env
end
end
Rails::Initializer.run(:set_load_path)
end
end
So if you Rails.env is test it will require all the gems in the test group.
因此,如果您对Rails.env进行测试,则需要测试组中的所有gem。
#2
0
I'm going to go ahead and say Gem.loaded_specs
is more reliable than going through bundler; I discovered a circumstance with bundler 1.12.5 where I have a gem from GitHub in one of the groups that I pass to without
, this causes Bundler.definition.index.search
to raise a Bundler::PathError
citing the gem from GitHub that I intentionally am not bundling.
我要继续说Gem.loaded_specs比通过bundler更可靠;我发现了一个捆绑1.12.5的情况,我在GitHub中有一个来自我传递给的组中的一个gem,这导致Bundler.definition.index.search引发一个Bundler :: PathError引用GitHub中的gem我故意不捆绑。
#1
3
When a Rails app is generated it typically includes a line that uses Rails.env to determine which group to require. It should look something like this Bundler.require(:default, Rails.env)
. This typically happens in the initialization of a Rails App. Here is a snippet of some code that does that:
生成Rails应用程序时,它通常包含一行使用Rails.env来确定需要哪个组。它应该看起来像这个Bundler.require(:default,Rails.env)。这通常发生在Rails应用程序的初始化中。以下是一些代码的片段:
class Rails::Boot
def run
load_initializer
Rails::Initializer.class_eval do
def load_gems
@bundler_loaded ||= Bundler.require :default, Rails.env
end
end
Rails::Initializer.run(:set_load_path)
end
end
So if you Rails.env is test it will require all the gems in the test group.
因此,如果您对Rails.env进行测试,则需要测试组中的所有gem。
#2
0
I'm going to go ahead and say Gem.loaded_specs
is more reliable than going through bundler; I discovered a circumstance with bundler 1.12.5 where I have a gem from GitHub in one of the groups that I pass to without
, this causes Bundler.definition.index.search
to raise a Bundler::PathError
citing the gem from GitHub that I intentionally am not bundling.
我要继续说Gem.loaded_specs比通过bundler更可靠;我发现了一个捆绑1.12.5的情况,我在GitHub中有一个来自我传递给的组中的一个gem,这导致Bundler.definition.index.search引发一个Bundler :: PathError引用GitHub中的gem我故意不捆绑。