Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Uniquify the spec list based on directory order priority

  • Loading branch information...
commit 7d43f1418a048bac277344df962aafd00797230b 1 parent 242bbda
Evan Phoenix evanphx authored evanphx committed
9 lib/rubygems/specification.rb
View
@@ -262,18 +262,19 @@ class Gem::Specification
def self._all # :nodoc:
unless defined?(@@all) && @@all then
- specs = []
+ specs = {}
- self.dirs.reverse_each { |dir|
+ self.dirs.each { |dir|
Dir[File.join(dir, "*.gemspec")].each { |path|
spec = Gem::Specification.load path.untaint
# #load returns nil if the spec is bad, so we just ignore
# it at this stage
- specs << spec if spec
+ specs[spec.full_name] ||= spec if spec
}
}
- @@all = specs
+ @@all = specs.values
+
_resort!
end
@@all
19 lib/rubygems/test_case.rb
View
@@ -243,7 +243,7 @@ def teardown
##
# Builds and installs the Gem::Specification +spec+
- def install_gem spec
+ def install_gem spec, options = {}
require 'rubygems/installer'
use_ui Gem::MockGemUi.new do
@@ -254,26 +254,14 @@ def install_gem spec
gem = File.join(@tempdir, File.basename(spec.cache_file)).untaint
- Gem::Installer.new(gem, :wrappers => true).install
+ Gem::Installer.new(gem, options.merge({:wrappers => true})).install
end
##
# Builds and installs the Gem::Specification +spec+ into the user dir
def install_gem_user spec
- require 'rubygems/installer'
-
- use_ui Gem::MockGemUi.new do
- Dir.chdir @tempdir do
- Gem::Builder.new(spec).build
- end
- end
-
- gem = File.join(@tempdir, File.basename(spec.cache_file)).untaint
-
- i = Gem::Installer.new(gem, :wrappers => true, :user_install => true)
- i.install
- i.spec
+ install_gem spec, :user_install => true
end
##
@@ -880,4 +868,3 @@ def v string
end
end
-
80 test/rubygems/test_gem.rb
View
@@ -1122,6 +1122,86 @@ def test_latest_load_paths
end
end
+ def test_gem_path_ordering
+ refute_equal Gem.dir, Gem.user_dir
+
+ write_file File.join(@tempdir, 'lib', "g.rb") { |fp| fp.puts "" }
+ write_file File.join(@tempdir, 'lib', 'm.rb') { |fp| fp.puts "" }
+
+ g = new_spec 'g', '1', nil, "lib/g.rb"
+ m = new_spec 'm', '1', nil, "lib/m.rb"
+
+ install_gem g, :install_dir => Gem.dir
+ m0 = install_gem m, :install_dir => Gem.dir
+ m1 = install_gem m, :install_dir => Gem.user_dir
+
+ assert_equal m0.gem_dir, File.join(Gem.dir, "gems", "m-1")
+ assert_equal m1.gem_dir, File.join(Gem.user_dir, "gems", "m-1")
+
+ tests = [
+ [:dir0, [ Gem.dir, Gem.user_dir], m0],
+ [:dir1, [ Gem.user_dir, Gem.dir], m1]
+ ]
+
+ tests.each do |_name, _paths, expected|
+ Gem.paths = { 'GEM_HOME' => _paths.first, 'GEM_PATH' => _paths }
+ Gem::Specification.reset
+ Gem.searcher = nil
+
+ assert_equal Gem::Dependency.new('m','1').to_specs,
+ Gem::Dependency.new('m','1').to_specs.sort
+
+ assert_equal \
+ [expected.gem_dir],
+ Gem::Dependency.new('m','1').to_specs.map(&:gem_dir).sort,
+ "Wrong specs for #{_name}"
+
+ spec = Gem::Dependency.new('m','1').to_spec
+
+ assert_equal \
+ File.join(_paths.first, "gems", "m-1"),
+ spec.gem_dir,
+ "Wrong spec before require for #{_name}"
+ refute spec.activated?, "dependency already activated for #{_name}"
+
+ gem "m"
+
+ spec = Gem::Dependency.new('m','1').to_spec
+ assert spec.activated?, "dependency not activated for #{_name}"
+
+ assert_equal \
+ File.join(_paths.first, "gems", "m-1"),
+ spec.gem_dir,
+ "Wrong spec after require for #{_name}"
+
+ spec.instance_variable_set :@activated, false
+ Gem.loaded_specs.delete(spec.name)
+ $:.delete(File.join(spec.gem_dir, "lib"))
+ end
+ end
+
+ def test_gem_path_ordering_short
+ write_file File.join(@tempdir, 'lib', "g.rb") { |fp| fp.puts "" }
+ write_file File.join(@tempdir, 'lib', 'm.rb') { |fp| fp.puts "" }
+
+ g = new_spec 'g', '1', nil, "lib/g.rb"
+ m = new_spec 'm', '1', nil, "lib/m.rb"
+
+ install_gem g, :install_dir => Gem.dir
+ install_gem m, :install_dir => Gem.dir
+ install_gem m, :install_dir => Gem.user_dir
+
+ Gem.paths = {
+ 'GEM_HOME' => Gem.dir,
+ 'GEM_PATH' => [ Gem.dir, Gem.user_dir]
+ }
+
+ assert_equal \
+ File.join(Gem.dir, "gems", "m-1"),
+ Gem::Dependency.new('m','1').to_spec.gem_dir,
+ "Wrong spec selected"
+ end
+
def with_plugin(path)
test_plugin_path = File.expand_path("test/rubygems/plugin/#{path}",
@@project_dir)
Please sign in to comment.
Something went wrong with that request. Please try again.