[rubygems/rubygems] Let @@stubs_by_name to be incrementally populated again.

Originally, the call to `.stubs_for` allowed to incrementally populate
the `@@stubs_by_name` (especially see the `"#{name}-*.gemspec"` pattern
in 4fa03bb7aac9f25f44394e818433fdda9962ae8d). Now it looks like it
expects that all stubs are loaded, but the `.stubs_for` still matches
the .gemspec files by the `name` pattern:

6d45e0f7ac/lib/rubygems/specification.rb (L845)

I think this was done by mistake incrementally by PR #1239 and
4cee8ca9199ac7b3ab8647e0b78615f55d3eb02b. I think the best option is to
get back to the original implementation, to let RubyGems incrementally
populate the array. Other option would be to replace the logic in
`.stub_for` by call to `.stubs`, but the means the performance
improvement from the original commit was lost.

https://github.com/rubygems/rubygems/commit/4d0e18185a
This commit is contained in:
Vít Ondruch 2019-04-02 10:52:44 +02:00 committed by Hiroshi SHIBATA
parent 15a4b7d769
commit acc86570dd
Notes: git 2020-05-08 14:14:08 +09:00
2 changed files with 21 additions and 2 deletions

View File

@ -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 +

View File

@ -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