diff --git a/lib/rubygems/specification.rb b/lib/rubygems/specification.rb index 9f6cdea8ad..4f2b64e652 100644 --- a/lib/rubygems/specification.rb +++ b/lib/rubygems/specification.rb @@ -828,8 +828,8 @@ class Gem::Specification < Gem::BasicSpecification # only returns stubs that match Gem.platforms def self.stubs_for(name) - if @@stubs - @@stubs_by_name[name] || [] + if @@stubs_by_name[name] + @@stubs_by_name[name] else pattern = "#{name}-*.gemspec" stubs = Gem.loaded_specs.values + diff --git a/test/rubygems/test_gem_specification.rb b/test/rubygems/test_gem_specification.rb index e0568506c5..f6caba896a 100644 --- a/test/rubygems/test_gem_specification.rb +++ b/test/rubygems/test_gem_specification.rb @@ -1179,6 +1179,25 @@ dependencies: [] Gem::Specification.class_variable_set(:@@stubs, nil) end + def test_self_stubs_for_lazy_loading + Gem.loaded_specs.clear + Gem::Specification.class_variable_set(:@@stubs, nil) + + dir_standard_specs = File.join Gem.dir, 'specifications' + + save_gemspec('a-1', '1', dir_standard_specs){|s| s.name = 'a' } + save_gemspec('b-1', '1', dir_standard_specs){|s| s.name = 'b' } + + assert_equal ['a-1'], Gem::Specification.stubs_for('a').map { |s| s.full_name } + assert_equal 1, Gem::Specification.class_variable_get(:@@stubs_by_name).length + assert_equal ['b-1'], Gem::Specification.stubs_for('b').map { |s| s.full_name } + assert_equal 2, Gem::Specification.class_variable_get(:@@stubs_by_name).length + + Gem.loaded_specs.delete 'a' + Gem.loaded_specs.delete 'b' + Gem::Specification.class_variable_set(:@@stubs, nil) + end + def test_self_stubs_for_mult_platforms # gems for two different platforms are installed with --user-install # the correct one should be returned in the array