summaryrefslogtreecommitdiff
path: root/test/rubygems/test_require.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/rubygems/test_require.rb')
-rw-r--r--test/rubygems/test_require.rb251
1 files changed, 126 insertions, 125 deletions
diff --git a/test/rubygems/test_require.rb b/test/rubygems/test_require.rb
index dec5285..9690659 100644
--- a/test/rubygems/test_require.rb
+++ b/test/rubygems/test_require.rb
@@ -26,11 +26,11 @@ class TestGemRequire < Gem::TestCase
def setup
super
+ @old_loaded_features = $LOADED_FEATURES.dup
assert_raises LoadError do
- save_loaded_features do
- require 'test_gem_require_a'
- end
+ require 'test_gem_require_a'
end
+ $LOADED_FEATURES.replace @old_loaded_features
end
def assert_require(path)
@@ -49,6 +49,36 @@ class TestGemRequire < Gem::TestCase
end
end
+ # Providing -I on the commandline should always beat gems
+ def test_dash_i_beats_gems
+ a1 = new_spec "a", "1", {"b" => "= 1"}, "lib/test_gem_require_a.rb"
+ b1 = new_spec "b", "1", {"c" => "> 0"}, "lib/b/c.rb"
+ c1 = new_spec "c", "1", nil, "lib/c/c.rb"
+ c2 = new_spec "c", "2", nil, "lib/c/c.rb"
+
+ install_specs c1, c2, b1, a1
+
+ dir = Dir.mktmpdir
+ dash_i_arg = File.join dir, 'lib'
+
+ c_rb = File.join dash_i_arg, 'b', 'c.rb'
+
+ FileUtils.mkdir_p File.dirname c_rb
+ File.open(c_rb, 'w') { |f| f.write "class Object; HELLO = 'world' end" }
+
+ lp = $LOAD_PATH.dup
+
+ # Pretend to provide a commandline argument that overrides a file in gem b
+ $LOAD_PATH.unshift dash_i_arg
+
+ assert_require 'test_gem_require_a'
+ assert_require 'b/c' # this should be required from -I
+ assert_equal "world", ::Object::HELLO
+ ensure
+ $LOAD_PATH.replace lp
+ Object.send :remove_const, :HELLO if Object.const_defined? :HELLO
+ end
+
def test_concurrent_require
Object.const_set :FILE_ENTERED_LATCH, Latch.new(2)
Object.const_set :FILE_EXIT_LATCH, Latch.new(1)
@@ -82,16 +112,14 @@ class TestGemRequire < Gem::TestCase
b1 = new_spec "b", "1", nil, "lib/b/c.rb"
b2 = new_spec "b", "2", nil, "lib/b/c.rb"
- install_specs a1, b1, b2
+ install_specs b1, b2, a1
- save_loaded_features do
- assert_require 'test_gem_require_a'
- assert_equal %w(a-1 b-1), loaded_spec_names
- assert_equal unresolved_names, []
+ assert_require 'test_gem_require_a'
+ assert_equal %w(a-1 b-1), loaded_spec_names
+ assert_equal unresolved_names, []
- assert_require "b/c"
- assert_equal %w(a-1 b-1), loaded_spec_names
- end
+ assert_require "b/c"
+ assert_equal %w(a-1 b-1), loaded_spec_names
end
def test_require_is_lazy_with_inexact_req
@@ -99,32 +127,28 @@ class TestGemRequire < Gem::TestCase
b1 = new_spec "b", "1", nil, "lib/b/c.rb"
b2 = new_spec "b", "2", nil, "lib/b/c.rb"
- install_specs a1, b1, b2
+ install_specs b1, b2, a1
- save_loaded_features do
- assert_require 'test_gem_require_a'
- assert_equal %w(a-1), loaded_spec_names
- assert_equal unresolved_names, ["b (>= 1)"]
+ assert_require 'test_gem_require_a'
+ assert_equal %w(a-1), loaded_spec_names
+ assert_equal unresolved_names, ["b (>= 1)"]
- assert_require "b/c"
- assert_equal %w(a-1 b-2), loaded_spec_names
- end
+ assert_require "b/c"
+ assert_equal %w(a-1 b-2), loaded_spec_names
end
def test_require_is_not_lazy_with_one_possible
a1 = new_spec "a", "1", {"b" => ">= 1"}, "lib/test_gem_require_a.rb"
b1 = new_spec "b", "1", nil, "lib/b/c.rb"
- install_specs a1, b1
+ install_specs b1, a1
- save_loaded_features do
- assert_require 'test_gem_require_a'
- assert_equal %w(a-1 b-1), loaded_spec_names
- assert_equal unresolved_names, []
+ assert_require 'test_gem_require_a'
+ assert_equal %w(a-1 b-1), loaded_spec_names
+ assert_equal unresolved_names, []
- assert_require "b/c"
- assert_equal %w(a-1 b-1), loaded_spec_names
- end
+ assert_require "b/c"
+ assert_equal %w(a-1 b-1), loaded_spec_names
end
def test_require_can_use_a_pathname_object
@@ -132,129 +156,114 @@ class TestGemRequire < Gem::TestCase
install_specs a1
- save_loaded_features do
- assert_require Pathname.new 'test_gem_require_a'
- assert_equal %w(a-1), loaded_spec_names
- assert_equal unresolved_names, []
- end
+ assert_require Pathname.new 'test_gem_require_a'
+ assert_equal %w(a-1), loaded_spec_names
+ assert_equal unresolved_names, []
end
def test_activate_via_require_respects_loaded_files
- require 'benchmark' # stdlib
- save_loaded_features do
- a1 = new_spec "a", "1", {"b" => ">= 1"}, "lib/test_gem_require_a.rb"
- b1 = new_spec "b", "1", nil, "lib/benchmark.rb"
- b2 = new_spec "b", "2", nil, "lib/benchmark.rb"
+ a1 = new_spec "a", "1", {"b" => ">= 1"}, "lib/test_gem_require_a.rb"
+ b1 = new_spec "b", "1", nil, "lib/benchmark.rb"
+ b2 = new_spec "b", "2", nil, "lib/benchmark.rb"
- install_specs a1, b1, b2
+ install_specs b1, b2, a1
- require 'test_gem_require_a'
- assert_equal unresolved_names, ["b (>= 1)"]
+ require 'test_gem_require_a'
+ assert_equal unresolved_names, ["b (>= 1)"]
- refute require('benchmark'), "benchmark should have already been loaded"
+ refute require('benchmark'), "benchmark should have already been loaded"
- # We detected that we should activate b-2, so we did so, but
- # then original_require decided "I've already got benchmark.rb" loaded.
- # This case is fine because our lazy loading is provided exactly
- # the same behavior as eager loading would have.
+ # We detected that we should activate b-2, so we did so, but
+ # then original_require decided "I've already got benchmark.rb" loaded.
+ # This case is fine because our lazy loading is provided exactly
+ # the same behavior as eager loading would have.
- assert_equal %w(a-1 b-2), loaded_spec_names
- end
+ assert_equal %w(a-1 b-2), loaded_spec_names
end
def test_already_activated_direct_conflict
- save_loaded_features do
- a1 = new_spec "a", "1", { "b" => "> 0" }
- b1 = new_spec "b", "1", { "c" => ">= 1" }, "lib/ib.rb"
- b2 = new_spec "b", "2", { "c" => ">= 2" }, "lib/ib.rb"
- c1 = new_spec "c", "1", nil, "lib/d.rb"
- c2 = new_spec("c", "2", nil, "lib/d.rb")
+ a1 = new_spec "a", "1", { "b" => "> 0" }
+ b1 = new_spec "b", "1", { "c" => ">= 1" }, "lib/ib.rb"
+ b2 = new_spec "b", "2", { "c" => ">= 2" }, "lib/ib.rb"
+ c1 = new_spec "c", "1", nil, "lib/d.rb"
+ c2 = new_spec("c", "2", nil, "lib/d.rb")
- install_specs a1, b1, b2, c1, c2
+ install_specs c1, c2, b1, b2, a1
- a1.activate
- c1.activate
- assert_equal %w(a-1 c-1), loaded_spec_names
- assert_equal ["b (> 0)"], unresolved_names
+ a1.activate
+ c1.activate
+ assert_equal %w(a-1 c-1), loaded_spec_names
+ assert_equal ["b (> 0)"], unresolved_names
- assert require("ib")
+ assert require("ib")
- assert_equal %w(a-1 b-1 c-1), loaded_spec_names
- assert_equal [], unresolved_names
- end
+ assert_equal %w(a-1 b-1 c-1), loaded_spec_names
+ assert_equal [], unresolved_names
end
def test_multiple_gems_with_the_same_path
- save_loaded_features do
- a1 = new_spec "a", "1", { "b" => "> 0", "x" => "> 0" }
- b1 = new_spec "b", "1", { "c" => ">= 1" }, "lib/ib.rb"
- b2 = new_spec "b", "2", { "c" => ">= 2" }, "lib/ib.rb"
- x1 = new_spec "x", "1", nil, "lib/ib.rb"
- x2 = new_spec "x", "2", nil, "lib/ib.rb"
- c1 = new_spec "c", "1", nil, "lib/d.rb"
- c2 = new_spec("c", "2", nil, "lib/d.rb")
-
- install_specs a1, b1, b2, c1, c2, x1, x2
-
- a1.activate
- c1.activate
- assert_equal %w(a-1 c-1), loaded_spec_names
- assert_equal ["b (> 0)", "x (> 0)"], unresolved_names
-
- e = assert_raises(Gem::LoadError) do
- require("ib")
- end
-
- assert_equal "ib found in multiple gems: b, x", e.message
+ a1 = new_spec "a", "1", { "b" => "> 0", "x" => "> 0" }
+ b1 = new_spec "b", "1", { "c" => ">= 1" }, "lib/ib.rb"
+ b2 = new_spec "b", "2", { "c" => ">= 2" }, "lib/ib.rb"
+ x1 = new_spec "x", "1", nil, "lib/ib.rb"
+ x2 = new_spec "x", "2", nil, "lib/ib.rb"
+ c1 = new_spec "c", "1", nil, "lib/d.rb"
+ c2 = new_spec("c", "2", nil, "lib/d.rb")
+
+ install_specs c1, c2, x1, x2, b1, b2, a1
+
+ a1.activate
+ c1.activate
+ assert_equal %w(a-1 c-1), loaded_spec_names
+ assert_equal ["b (> 0)", "x (> 0)"], unresolved_names
+
+ e = assert_raises(Gem::LoadError) do
+ require("ib")
end
+
+ assert_equal "ib found in multiple gems: b, x", e.message
end
def test_unable_to_find_good_unresolved_version
- save_loaded_features do
- a1 = new_spec "a", "1", { "b" => "> 0" }
- b1 = new_spec "b", "1", { "c" => ">= 2" }, "lib/ib.rb"
- b2 = new_spec "b", "2", { "c" => ">= 3" }, "lib/ib.rb"
+ a1 = new_spec "a", "1", { "b" => "> 0" }
+ b1 = new_spec "b", "1", { "c" => ">= 2" }, "lib/ib.rb"
+ b2 = new_spec "b", "2", { "c" => ">= 3" }, "lib/ib.rb"
- c1 = new_spec "c", "1", nil, "lib/d.rb"
- c2 = new_spec "c", "2", nil, "lib/d.rb"
- c3 = new_spec "c", "3", nil, "lib/d.rb"
+ c1 = new_spec "c", "1", nil, "lib/d.rb"
+ c2 = new_spec "c", "2", nil, "lib/d.rb"
+ c3 = new_spec "c", "3", nil, "lib/d.rb"
- install_specs a1, b1, b2, c1, c2, c3
+ install_specs c1, c2, c3, b1, b2, a1
- a1.activate
- c1.activate
- assert_equal %w(a-1 c-1), loaded_spec_names
- assert_equal ["b (> 0)"], unresolved_names
+ a1.activate
+ c1.activate
+ assert_equal %w(a-1 c-1), loaded_spec_names
+ assert_equal ["b (> 0)"], unresolved_names
- e = assert_raises(Gem::LoadError) do
- require("ib")
- end
-
- assert_equal "unable to find a version of 'b' to activate", e.message
+ e = assert_raises(Gem::LoadError) do
+ require("ib")
end
+
+ assert_equal "unable to find a version of 'b' to activate", e.message
end
def test_default_gem_only
- save_loaded_features do
- default_gem_spec = new_default_spec("default", "2.0.0.0",
- nil, "default/gem.rb")
- install_default_specs(default_gem_spec)
- assert_require "default/gem"
- assert_equal %w(default-2.0.0.0), loaded_spec_names
- end
+ default_gem_spec = new_default_spec("default", "2.0.0.0",
+ nil, "default/gem.rb")
+ install_default_specs(default_gem_spec)
+ assert_require "default/gem"
+ assert_equal %w(default-2.0.0.0), loaded_spec_names
end
def test_default_gem_and_normal_gem
- save_loaded_features do
- default_gem_spec = new_default_spec("default", "2.0.0.0",
- nil, "default/gem.rb")
- install_default_specs(default_gem_spec)
- normal_gem_spec = new_spec("default", "3.0", nil,
- "lib/default/gem.rb")
- install_specs(normal_gem_spec)
- assert_require "default/gem"
- assert_equal %w(default-3.0), loaded_spec_names
- end
+ default_gem_spec = new_default_spec("default", "2.0.0.0",
+ nil, "default/gem.rb")
+ install_default_specs(default_gem_spec)
+ normal_gem_spec = new_spec("default", "3.0", nil,
+ "lib/default/gem.rb")
+ install_specs(normal_gem_spec)
+ assert_require "default/gem"
+ assert_equal %w(default-3.0), loaded_spec_names
end
def loaded_spec_names
@@ -264,12 +273,4 @@ class TestGemRequire < Gem::TestCase
def unresolved_names
Gem::Specification.unresolved_deps.values.map(&:to_s).sort
end
-
- def save_loaded_features
- old_loaded_features = $LOADED_FEATURES.dup
- yield
- ensure
- $LOADED_FEATURES.replace old_loaded_features
- end
-
end