Permalink
Browse files

Nailed the last activation/require test

  • Loading branch information...
1 parent 7d35d79 commit 98594cbf71dbe5238567342efcddd8d48ce929e3 @zenspider zenspider committed Mar 1, 2011
Showing with 80 additions and 12 deletions.
  1. +9 −6 lib/rubygems/custom_require.rb
  2. +28 −1 lib/rubygems/gem_path_searcher.rb
  3. +12 −0 lib/rubygems/specification.rb
  4. +31 −5 test/rubygems/test_gem.rb
@@ -30,14 +30,17 @@ def require path
gem_original_require path
else
specs = Gem.searcher.find_in_unresolved path
-
- if specs.empty? then
- gem_original_require path
+ unless specs.empty? then
+ specs = [specs.last]
else
- spec = specs.first
- Gem.activate spec.name, spec.version # FIX: holy shit this is dumb
- return gem_original_require(path)
+ specs = Gem.searcher.find_in_unresolved_tree path
+ end
+
+ specs.each do |spec|
+ Gem.activate spec.name, spec.version # FIX: this is dumb
end
+
+ return gem_original_require path
end
rescue LoadError => load_error
if load_error.message.end_with?(path) and Gem.try_activate(path) then
@@ -73,12 +73,39 @@ def find_in_unresolved(glob)
end || []
end
+ def find_in_unresolved_tree glob
+ # HACK violation
+ # TODO: inverted responsibility
+ specs = Gem.unresolved_deps.values.map { |dep|
+ Gem.source_index.search dep, true
+ }.flatten
+
+ specs.reverse_each do |spec|
+ trails = matching_paths(spec, glob)
+ next if trails.empty?
+ return trails.map(&:reverse).sort.first.reverse
+ end
+
+ []
+ end
+
##
# Attempts to find a matching path using the require_paths of the given
# +spec+.
def matching_file?(spec, path)
- !matching_files(spec, path).empty?
+ not matching_files(spec, path).empty?
+ end
+
+ def matching_paths(spec, path)
+ trails = []
+
+ spec.traverse do |from_spec, dep, to_spec, trail|
+ next unless to_spec.conflicts.empty?
+ trails << trail unless matching_files(to_spec, path).empty?
+ end
+
+ trails
end
##
@@ -1547,6 +1547,18 @@ def conflicts
conflicts
end
+ def traverse trail = [], &b
+ trail = trail + [self]
+ runtime_dependencies.each do |dep|
+ dep_specs = Gem.source_index.search dep, true
+ dep_specs.each do |dep_spec|
+ b[self, dep, dep_spec, trail + [dep_spec]]
+ dep_spec.traverse(trail, &b) unless
+ trail.map(&:name).include? dep_spec.name
+ end
+ end
+ end
+
def dependent_specs
runtime_dependencies.map { |dep| Gem.source_index.search dep, true }.flatten
end
View
@@ -101,15 +101,13 @@ def test_self_activate_ambiguous_direct
assert_equal %w(a-1 b-2 c-2), loaded_spec_names
assert_equal [], unresolved_names
end
-
- flunk
end
def test_self_activate_ambiguous_indirect
save_loaded_features do
a1 = new_spec "a", "1", "b" => "> 0"
b1 = new_spec "b", "1", "c" => ">= 1"
- b2 = new_spec "b", "2", "c" => ">= 1"
+ b2 = new_spec "b", "2", "c" => ">= 2"
c1 = new_spec "c", "1", nil, "lib/d.rb"
c2 = new_spec "c", "2", nil, "lib/d.rb"
@@ -124,7 +122,28 @@ def test_self_activate_ambiguous_indirect
assert_equal %w(a-1 b-2 c-2), loaded_spec_names
assert_equal [], unresolved_names
end
- flunk
+ end
+
+ def test_self_activate_ambiguous_unrelated
+ save_loaded_features do
+ a1 = new_spec "a", "1", "b" => "> 0"
+ b1 = new_spec "b", "1", "c" => ">= 1"
+ b2 = new_spec "b", "2", "c" => ">= 2"
+ c1 = new_spec "c", "1"
+ c2 = new_spec "c", "2"
+ d1 = new_spec "d", "1", nil, "lib/d.rb"
+
+ install_specs a1, b1, b2, c1, c2
+
+ Gem.activate "a", "= 1"
+ assert_equal %w(a-1), loaded_spec_names
+ assert_equal ["b (> 0)"], unresolved_names
+
+ require "d"
+
+ assert_equal %w(a-1 d-1), loaded_spec_names
+ assert_equal ["b (> 0)"], unresolved_names
+ end
end
def test_self_activate_ambiguous_indirect_conflict
@@ -147,7 +166,14 @@ def test_self_activate_ambiguous_indirect_conflict
assert_equal %w(a-2 b-1 c-1), loaded_spec_names
assert_equal [], unresolved_names
end
- flunk
+ end
+
+ def test_require_missing
+ save_loaded_features do
+ assert_raises ::LoadError do
+ require "q"
+ end
+ end
end
def test_self_activate_loaded

0 comments on commit 98594cb

Please sign in to comment.