Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

More edge cases per Evan's review

  • Loading branch information...
commit dcfba4e1de4c7388425eb5de5066cb5aa9bea6e6 1 parent fd50fb9
@zenspider zenspider authored
View
7 lib/rubygems.rb
@@ -1055,6 +1055,13 @@ def self.suffix_pattern
@suffix_pattern ||= "{#{suffixes.join(',')}}"
end
+ def self.loaded_path? path
+ # TODO: ruby needs a feature to let us query what's loaded in 1.8 and 1.9
+ $LOADED_FEATURES.find { |s|
+ s =~ /(^|\/)#{Regexp.escape path}#{Regexp.union(*Gem.suffixes)}$/
+ }
+ end
+
##
# Suffixes for require-able paths.
View
4 lib/rubygems/custom_require.rb
@@ -26,10 +26,10 @@ module Kernel
# that file has already been loaded is preserved.
def require path
- if Gem._unresolved.specs.empty? then
+ if Gem._unresolved.specs.empty? or Gem.loaded_path? path then
gem_original_require path
else
- specs = Gem.searcher.find_all path
+ specs = Gem.searcher.find_in_unresolved path
if specs.empty? then
gem_original_require path
View
8 lib/rubygems/gem_path_searcher.rb
@@ -63,6 +63,14 @@ def find_all(glob)
end || []
end
+ def find_in_unresolved(glob)
+ # HACK violation
+ (Gem._unresolved.specs || []).select do |spec|
+ # TODO: inverted responsibility
+ matching_file? spec, glob
+ end || []
+ end
+
##
# Attempts to find a matching path using the require_paths of the given
# +spec+.
View
30 test/rubygems/test_gem.rb
@@ -43,6 +43,10 @@ def test_self_activate
assert_activate %w[foo-1], foo
end
+ def loaded_spec_names
+ Gem.loaded_specs.values.map(&:full_name).sort
+ end
+
def test_self_activate_via_require
a1 = new_spec "a", "1", "b" => "= 1"
b1 = new_spec "b", "1", nil, "lib/b/c.rb"
@@ -50,7 +54,31 @@ def test_self_activate_via_require
Gem.activate "a", "= 1"
require "b/c"
- assert_equal %w(a-1 b-1), Gem.loaded_specs.values.map(&:full_name).sort
+
+ assert_equal %w(a-1 b-1), loaded_spec_names
+ end
+
+ def test_self_activate_via_require_not_too_eager
+ a1 = new_spec "a", "1", "b" => "= 1"
+ b1 = new_spec "b", "1", nil, "lib/b/c.rb"
+ b2 = new_spec "b", "2", nil, "lib/benchmark.rb"
+
+ Gem.activate "a", "= 1"
+ require 'benchmark'
+
+ assert_equal %w(a-1), loaded_spec_names
+ end
+
+ def test_self_activate_via_require_respects_loaded_files
+ require 'benchmark' # stdlib
+
+ a1 = new_spec "a", "1", "b" => "= 1"
+ b1 = new_spec "b", "1", nil, "lib/benchmark.rb"
+
+ Gem.activate "a", "= 1"
+
+ refute require('benchmark'), "benchmark should have already been loaded"
+ assert_equal %w(a-1), loaded_spec_names
end
def test_self_activate_loaded
Please sign in to comment.
Something went wrong with that request. Please try again.