fix warning in stub specifications
authorAaron Patterson <aaron.patterson@gmail.com>
Sat, 22 Nov 2014 01:35:58 +0000 (17:35 -0800)
committerAaron Patterson <aaron.patterson@gmail.com>
Sat, 22 Nov 2014 01:35:58 +0000 (17:35 -0800)
if the name and version methods haven't been called, the instance
variables will not be initialized, and calling to_spec will warn. This
ensures that the data has actually been loaded before looping through
the loaded specs.

lib/rubygems/stub_specification.rb
test/rubygems/test_gem_stub_specification.rb

index 49a6df43a3079bfc53b3ace9d8131cef1b1df548..e13fea24c9f6b6160f724c63a6a35cd1e18e9e6c 100644 (file)
@@ -154,9 +154,11 @@ class Gem::StubSpecification < Gem::BasicSpecification
   # The full Gem::Specification for this gem, loaded from evalling its gemspec
 
   def to_spec
-    @spec ||= Gem.loaded_specs.values.find { |spec|
-      spec.name == @name and spec.version == @version
-    }
+    @spec ||= if @data then
+                Gem.loaded_specs.values.find { |spec|
+                  spec.name == name and spec.version == version
+                }
+              end
 
     @spec ||= Gem::Specification.load(loaded_from)
     @spec.ignored = @ignored if instance_variable_defined? :@ignored
index d992567a5c8e7aa8ba36849104e112c2d3582f36..56b2c289c7a9266020ce5beb89ea553425c7e8fb 100644 (file)
@@ -122,6 +122,14 @@ class TestStubSpecification < Gem::TestCase
     assert_same real_foo, @foo.to_spec
   end
 
+  def test_to_spec_with_other_specs_loaded_does_not_warn
+    real_foo = util_spec @foo.name, @foo.version
+    real_foo.activate
+    bar = Gem::StubSpecification.new BAR
+    refute_predicate Gem.loaded_specs, :empty?
+    assert bar.to_spec
+  end
+
   def test_to_spec_activated
     assert @foo.to_spec.is_a?(Gem::Specification)
     assert_equal "foo", @foo.to_spec.name