2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2012-11-29 01:52:18 -05:00
|
|
|
require 'rubygems/test_case'
|
|
|
|
require 'rubygems'
|
|
|
|
|
|
|
|
class TestGemRequire < Gem::TestCase
|
2014-09-13 23:30:02 -04:00
|
|
|
class Latch
|
|
|
|
def initialize count = 1
|
|
|
|
@count = count
|
|
|
|
@lock = Monitor.new
|
|
|
|
@cv = @lock.new_cond
|
|
|
|
end
|
|
|
|
|
|
|
|
def release
|
|
|
|
@lock.synchronize do
|
|
|
|
@count -= 1 if @count > 0
|
|
|
|
@cv.broadcast if @count.zero?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def await
|
|
|
|
@lock.synchronize do
|
|
|
|
@cv.wait_while { @count > 0 }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-03-05 18:11:43 -05:00
|
|
|
|
|
|
|
def setup
|
|
|
|
super
|
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
@old_loaded_features = $LOADED_FEATURES.dup
|
2013-03-05 18:11:43 -05:00
|
|
|
assert_raises LoadError do
|
2015-07-01 17:50:14 -04:00
|
|
|
require 'test_gem_require_a'
|
2013-03-05 18:11:43 -05:00
|
|
|
end
|
2015-07-01 17:50:14 -04:00
|
|
|
$LOADED_FEATURES.replace @old_loaded_features
|
2013-03-05 18:11:43 -05:00
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def assert_require(path)
|
|
|
|
assert require(path), "'#{path}' was already required"
|
|
|
|
end
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
def append_latch spec
|
|
|
|
dir = spec.gem_dir
|
|
|
|
Dir.chdir dir do
|
|
|
|
spec.files.each do |file|
|
|
|
|
File.open file, 'a' do |fp|
|
|
|
|
fp.puts "FILE_ENTERED_LATCH.release"
|
|
|
|
fp.puts "FILE_EXIT_LATCH.await"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
# 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
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
def test_concurrent_require
|
2015-10-29 20:54:12 -04:00
|
|
|
skip 'deadlock' if /^1\.8\./ =~ RUBY_VERSION
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
Object.const_set :FILE_ENTERED_LATCH, Latch.new(2)
|
|
|
|
Object.const_set :FILE_EXIT_LATCH, Latch.new(1)
|
|
|
|
|
|
|
|
a1 = new_spec "a", "1", nil, "lib/a.rb"
|
|
|
|
b1 = new_spec "b", "1", nil, "lib/b.rb"
|
|
|
|
|
|
|
|
install_specs a1, b1
|
|
|
|
|
|
|
|
append_latch a1
|
|
|
|
append_latch b1
|
|
|
|
|
|
|
|
t1 = Thread.new { assert_require 'a' }
|
|
|
|
t2 = Thread.new { assert_require 'b' }
|
|
|
|
|
|
|
|
# wait until both files are waiting on the exit latch
|
|
|
|
FILE_ENTERED_LATCH.await
|
|
|
|
|
|
|
|
# now let them finish
|
|
|
|
FILE_EXIT_LATCH.release
|
|
|
|
|
|
|
|
assert t1.join, "thread 1 should exit"
|
|
|
|
assert t2.join, "thread 2 should exit"
|
|
|
|
ensure
|
2015-10-29 20:54:12 -04:00
|
|
|
return if $! # skipping
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
Object.send :remove_const, :FILE_ENTERED_LATCH
|
|
|
|
Object.send :remove_const, :FILE_EXIT_LATCH
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def test_require_is_not_lazy_with_exact_req
|
2013-03-05 18:11:43 -05:00
|
|
|
a1 = new_spec "a", "1", {"b" => "= 1"}, "lib/test_gem_require_a.rb"
|
2012-11-29 01:52:18 -05:00
|
|
|
b1 = new_spec "b", "1", nil, "lib/b/c.rb"
|
|
|
|
b2 = new_spec "b", "2", nil, "lib/b/c.rb"
|
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
install_specs b1, b2, a1
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
assert_require 'test_gem_require_a'
|
|
|
|
assert_equal %w(a-1 b-1), loaded_spec_names
|
|
|
|
assert_equal unresolved_names, []
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
assert_require "b/c"
|
|
|
|
assert_equal %w(a-1 b-1), loaded_spec_names
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_require_is_lazy_with_inexact_req
|
2013-03-05 18:11:43 -05:00
|
|
|
a1 = new_spec "a", "1", {"b" => ">= 1"}, "lib/test_gem_require_a.rb"
|
2012-11-29 01:52:18 -05:00
|
|
|
b1 = new_spec "b", "1", nil, "lib/b/c.rb"
|
|
|
|
b2 = new_spec "b", "2", nil, "lib/b/c.rb"
|
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
install_specs b1, b2, a1
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
assert_require 'test_gem_require_a'
|
|
|
|
assert_equal %w(a-1), loaded_spec_names
|
|
|
|
assert_equal unresolved_names, ["b (>= 1)"]
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
assert_require "b/c"
|
|
|
|
assert_equal %w(a-1 b-2), loaded_spec_names
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_require_is_not_lazy_with_one_possible
|
2013-03-05 18:11:43 -05:00
|
|
|
a1 = new_spec "a", "1", {"b" => ">= 1"}, "lib/test_gem_require_a.rb"
|
2012-11-29 01:52:18 -05:00
|
|
|
b1 = new_spec "b", "1", nil, "lib/b/c.rb"
|
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
install_specs b1, a1
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
assert_require 'test_gem_require_a'
|
|
|
|
assert_equal %w(a-1 b-1), loaded_spec_names
|
|
|
|
assert_equal unresolved_names, []
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
assert_require "b/c"
|
|
|
|
assert_equal %w(a-1 b-1), loaded_spec_names
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
2013-10-15 20:14:16 -04:00
|
|
|
def test_require_can_use_a_pathname_object
|
|
|
|
a1 = new_spec "a", "1", nil, "lib/test_gem_require_a.rb"
|
|
|
|
|
|
|
|
install_specs a1
|
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
assert_require Pathname.new 'test_gem_require_a'
|
|
|
|
assert_equal %w(a-1), loaded_spec_names
|
|
|
|
assert_equal unresolved_names, []
|
2013-10-15 20:14:16 -04:00
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def test_activate_via_require_respects_loaded_files
|
2015-07-01 17:50:14 -04:00
|
|
|
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"
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
install_specs b1, b2, a1
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
require 'test_gem_require_a'
|
|
|
|
assert_equal unresolved_names, ["b (>= 1)"]
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
refute require('benchmark'), "benchmark should have already been loaded"
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
# 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.
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
assert_equal %w(a-1 b-2), loaded_spec_names
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_already_activated_direct_conflict
|
2015-07-01 17:50:14 -04:00
|
|
|
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")
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
install_specs c1, c2, b1, b2, a1
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
a1.activate
|
|
|
|
c1.activate
|
|
|
|
assert_equal %w(a-1 c-1), loaded_spec_names
|
|
|
|
assert_equal ["b (> 0)"], unresolved_names
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
assert require("ib")
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
assert_equal %w(a-1 b-1 c-1), loaded_spec_names
|
|
|
|
assert_equal [], unresolved_names
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_multiple_gems_with_the_same_path
|
2015-07-01 17:50:14 -04:00
|
|
|
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")
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
2015-07-01 17:50:14 -04:00
|
|
|
|
|
|
|
assert_equal "ib found in multiple gems: b, x", e.message
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_unable_to_find_good_unresolved_version
|
2015-07-01 17:50:14 -04:00
|
|
|
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"
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
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"
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
install_specs c1, c2, c3, b1, b2, a1
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
a1.activate
|
|
|
|
c1.activate
|
|
|
|
assert_equal %w(a-1 c-1), loaded_spec_names
|
|
|
|
assert_equal ["b (> 0)"], unresolved_names
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
e = assert_raises(Gem::LoadError) do
|
|
|
|
require("ib")
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
2015-07-01 17:50:14 -04:00
|
|
|
|
|
|
|
assert_equal "unable to find a version of 'b' to activate", e.message
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
2015-10-29 20:54:12 -04:00
|
|
|
def test_require_works_after_cleanup
|
|
|
|
a1 = new_default_spec "a", "1.0", nil, "a/b.rb"
|
|
|
|
b1 = new_default_spec "b", "1.0", nil, "b/c.rb"
|
|
|
|
b2 = new_default_spec "b", "2.0", nil, "b/d.rb"
|
|
|
|
|
|
|
|
install_default_gems a1
|
|
|
|
install_default_gems b1
|
|
|
|
install_default_gems b2
|
|
|
|
|
|
|
|
# Load default ruby gems fresh as if we've just started a ruby script.
|
|
|
|
Gem::Specification.reset
|
|
|
|
require 'rubygems'
|
|
|
|
Gem::Specification.stubs
|
|
|
|
|
|
|
|
# Remove an old default gem version directly from disk as if someone ran
|
|
|
|
# gem cleanup.
|
|
|
|
FileUtils.rm_rf(File.join @default_dir, "#{b1.full_name}")
|
|
|
|
FileUtils.rm_rf(File.join @default_spec_dir, "#{b1.full_name}.gemspec")
|
|
|
|
|
|
|
|
# Require gems that have not been removed.
|
|
|
|
assert_require 'a/b'
|
|
|
|
assert_equal %w(a-1.0), loaded_spec_names
|
|
|
|
assert_require 'b/d'
|
|
|
|
assert_equal %w(a-1.0 b-2.0), loaded_spec_names
|
|
|
|
end
|
|
|
|
|
2015-11-19 01:16:19 -05:00
|
|
|
def test_require_doesnt_traverse_development_dependencies
|
|
|
|
a = new_spec("a", "1", nil, "lib/a.rb")
|
|
|
|
z = new_spec("z", "1", "w" => "> 0")
|
|
|
|
w1 = new_spec("w", "1") { |s| s.add_development_dependency "non-existent" }
|
|
|
|
w2 = new_spec("w", "2") { |s| s.add_development_dependency "non-existent" }
|
|
|
|
|
|
|
|
install_specs a, w1, w2, z
|
|
|
|
|
|
|
|
assert gem("z")
|
|
|
|
assert_equal %w(z-1), loaded_spec_names
|
|
|
|
assert_equal ["w (> 0)"], unresolved_names
|
|
|
|
|
|
|
|
assert require("a")
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def test_default_gem_only
|
2015-07-01 17:50:14 -04:00
|
|
|
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
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_default_gem_and_normal_gem
|
2015-07-01 17:50:14 -04:00
|
|
|
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
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def loaded_spec_names
|
|
|
|
Gem.loaded_specs.values.map(&:full_name).sort
|
|
|
|
end
|
|
|
|
|
|
|
|
def unresolved_names
|
|
|
|
Gem::Specification.unresolved_deps.values.map(&:to_s).sort
|
|
|
|
end
|
|
|
|
end
|