summaryrefslogtreecommitdiff
path: root/test/rubygems/test_gem.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/rubygems/test_gem.rb')
-rw-r--r--test/rubygems/test_gem.rb212
1 files changed, 168 insertions, 44 deletions
diff --git a/test/rubygems/test_gem.rb b/test/rubygems/test_gem.rb
index e777527007..3c5eed3a04 100644
--- a/test/rubygems/test_gem.rb
+++ b/test/rubygems/test_gem.rb
@@ -24,6 +24,7 @@ class TestGem < RubyGemTestCase
expected = [
File.join(@gemhome, *%W[gems #{@a1.full_name} lib]),
File.join(@gemhome, *%W[gems #{@a2.full_name} lib]),
+ File.join(@gemhome, *%W[gems #{@a3a.full_name} lib]),
File.join(@gemhome, *%W[gems #{@a_evil9.full_name} lib]),
File.join(@gemhome, *%W[gems #{@b2.full_name} lib]),
File.join(@gemhome, *%W[gems #{@c1_2.full_name} lib]),
@@ -41,6 +42,56 @@ class TestGem < RubyGemTestCase
assert(!Gem.available?("monkeys"))
end
+ def test_self_bin_path_bin_name
+ util_exec_gem
+ assert_equal @abin_path, Gem.bin_path('a', 'abin')
+ end
+
+ def test_self_bin_path_bin_name_version
+ util_exec_gem
+ assert_equal @abin_path, Gem.bin_path('a', 'abin', '4')
+ end
+
+ def test_self_bin_path_name
+ util_exec_gem
+ assert_equal @exec_path, Gem.bin_path('a')
+ end
+
+ def test_self_bin_path_name_version
+ util_exec_gem
+ assert_equal @exec_path, Gem.bin_path('a', nil, '4')
+ end
+
+ def test_self_bin_path_no_default_bin
+ quick_gem 'a', '2' do |s|
+ s.executables = ['exec']
+ end
+ assert_raises(Gem::Exception) do
+ Gem.bin_path('a', '2')
+ end
+ end
+
+ def test_self_bin_path_no_bin_file
+ quick_gem 'a', '1'
+ assert_raises(Gem::Exception) do
+ Gem.bin_path('a', '1')
+ end
+ end
+
+ def test_self_bin_path_with_spaces
+ quick_gem 'sp ace', '3' do |s|
+ s.executables = ['exec']
+ end
+ path = Gem.bin_path('sp ace', 'exec')
+ assert_equal %w(" "), [path[0,1], path[-1,1]], "Path should be escaped"
+ end
+
+ def test_self_bin_path_not_found
+ assert_raises(Gem::GemNotFoundException) do
+ Gem.bin_path('non-existent')
+ end
+ end
+
def test_self_bindir
assert_equal File.join(@gemhome, 'bin'), Gem.bindir
assert_equal File.join(@gemhome, 'bin'), Gem.bindir(Gem.dir)
@@ -111,39 +162,30 @@ class TestGem < RubyGemTestCase
end
def test_self_default_exec_format
- orig_BASERUBY = Gem::ConfigMap[:BASERUBY]
- orig_RUBY_INSTALL_NAME = Gem::ConfigMap[:RUBY_INSTALL_NAME]
- Gem::ConfigMap[:BASERUBY] = 'ruby'
- Gem::ConfigMap[:RUBY_INSTALL_NAME] = 'ruby'
+ orig_RUBY_INSTALL_NAME = Gem::ConfigMap[:ruby_install_name]
+ Gem::ConfigMap[:ruby_install_name] = 'ruby'
assert_equal '%s', Gem.default_exec_format
ensure
- Gem::ConfigMap[:BASERUBY] = orig_BASERUBY
- Gem::ConfigMap[:RUBY_INSTALL_NAME] = orig_RUBY_INSTALL_NAME
+ Gem::ConfigMap[:ruby_install_name] = orig_RUBY_INSTALL_NAME
end
def test_self_default_exec_format_18
- orig_BASERUBY = Gem::ConfigMap[:BASERUBY]
- orig_RUBY_INSTALL_NAME = Gem::ConfigMap[:RUBY_INSTALL_NAME]
- Gem::ConfigMap[:BASERUBY] = 'ruby'
- Gem::ConfigMap[:RUBY_INSTALL_NAME] = 'ruby18'
+ orig_RUBY_INSTALL_NAME = Gem::ConfigMap[:ruby_install_name]
+ Gem::ConfigMap[:ruby_install_name] = 'ruby18'
assert_equal '%s18', Gem.default_exec_format
ensure
- Gem::ConfigMap[:BASERUBY] = orig_BASERUBY
- Gem::ConfigMap[:RUBY_INSTALL_NAME] = orig_RUBY_INSTALL_NAME
+ Gem::ConfigMap[:ruby_install_name] = orig_RUBY_INSTALL_NAME
end
def test_self_default_exec_format_jruby
- orig_BASERUBY = Gem::ConfigMap[:BASERUBY]
- orig_RUBY_INSTALL_NAME = Gem::ConfigMap[:RUBY_INSTALL_NAME]
- Gem::ConfigMap[:BASERUBY] = 'ruby'
- Gem::ConfigMap[:RUBY_INSTALL_NAME] = 'jruby'
+ orig_RUBY_INSTALL_NAME = Gem::ConfigMap[:ruby_install_name]
+ Gem::ConfigMap[:ruby_install_name] = 'jruby'
assert_equal 'j%s', Gem.default_exec_format
ensure
- Gem::ConfigMap[:BASERUBY] = orig_BASERUBY
- Gem::ConfigMap[:RUBY_INSTALL_NAME] = orig_RUBY_INSTALL_NAME
+ Gem::ConfigMap[:ruby_install_name] = orig_RUBY_INSTALL_NAME
end
def test_self_default_sources
@@ -166,7 +208,7 @@ class TestGem < RubyGemTestCase
def test_self_ensure_gem_directories_missing_parents
gemdir = File.join @tempdir, 'a/b/c/gemdir'
FileUtils.rm_rf File.join(@tempdir, 'a') rescue nil
- assert !File.exist?(File.join(@tempdir, 'a')),
+ refute File.exist?(File.join(@tempdir, 'a')),
"manually remove #{File.join @tempdir, 'a'}, tests are broken"
Gem.use_paths gemdir
@@ -179,14 +221,14 @@ class TestGem < RubyGemTestCase
def test_self_ensure_gem_directories_write_protected
gemdir = File.join @tempdir, "egd"
FileUtils.rm_r gemdir rescue nil
- assert !File.exist?(gemdir), "manually remove #{gemdir}, tests are broken"
+ refute File.exist?(gemdir), "manually remove #{gemdir}, tests are broken"
FileUtils.mkdir_p gemdir
FileUtils.chmod 0400, gemdir
Gem.use_paths gemdir
Gem.ensure_gem_subdirectories gemdir
- assert !File.exist?("#{gemdir}/cache")
+ refute File.exist?("#{gemdir}/cache")
ensure
FileUtils.chmod 0600, gemdir
end
@@ -196,14 +238,14 @@ class TestGem < RubyGemTestCase
gemdir = "#{parent}/a/b/c"
FileUtils.rm_r parent rescue nil
- assert !File.exist?(parent), "manually remove #{parent}, tests are broken"
+ refute File.exist?(parent), "manually remove #{parent}, tests are broken"
FileUtils.mkdir_p parent
FileUtils.chmod 0400, parent
Gem.use_paths(gemdir)
Gem.ensure_gem_subdirectories gemdir
- assert !File.exist?("#{gemdir}/cache")
+ refute File.exist?("#{gemdir}/cache")
ensure
FileUtils.chmod 0600, parent
end
@@ -223,18 +265,20 @@ class TestGem < RubyGemTestCase
end
def test_self_find_files
+ discover_path = File.join 'lib', 'foo', 'discover.rb'
+
foo1 = quick_gem 'foo', '1' do |s|
- s.files << 'lib/foo/discover.rb'
+ s.files << discover_path
end
foo2 = quick_gem 'foo', '2' do |s|
- s.files << 'lib/foo/discover.rb'
+ s.files << discover_path
end
- path = File.join 'gems', foo1.full_name, 'lib', 'foo', 'discover.rb'
+ path = File.join 'gems', foo1.full_name, discover_path
write_file(path) { |fp| fp.puts "# #{path}" }
- path = File.join 'gems', foo2.full_name, 'lib', 'foo', 'discover.rb'
+ path = File.join 'gems', foo2.full_name, discover_path
write_file(path) { |fp| fp.puts "# #{path}" }
@fetcher = Gem::FakeFetcher.new
@@ -245,18 +289,19 @@ class TestGem < RubyGemTestCase
Gem.searcher = nil
expected = [
- File.join(foo1.full_gem_path, 'lib', 'foo', 'discover.rb'),
- File.join(foo2.full_gem_path, 'lib', 'foo', 'discover.rb'),
+ File.expand_path('foo/discover.rb', File.dirname(__FILE__)),
+ File.join(foo2.full_gem_path, discover_path),
+ File.join(foo1.full_gem_path, discover_path),
]
- assert_equal expected, Gem.find_files('foo/discover').sort
+ assert_equal expected, Gem.find_files('foo/discover')
end
def test_self_latest_load_paths
util_make_gems
expected = [
- File.join(@gemhome, *%W[gems #{@a2.full_name} lib]),
+ File.join(@gemhome, *%W[gems #{@a3a.full_name} lib]),
File.join(@gemhome, *%W[gems #{@a_evil9.full_name} lib]),
File.join(@gemhome, *%W[gems #{@b2.full_name} lib]),
File.join(@gemhome, *%W[gems #{@c1_2.full_name} lib]),
@@ -298,7 +343,7 @@ class TestGem < RubyGemTestCase
apple_gem_home = File.join @tempdir, 'apple_gem_home'
Gem.const_set :APPLE_GEM_HOME, apple_gem_home
- assert Gem.path.include?(apple_gem_home)
+ assert_includes Gem.path, apple_gem_home
ensure
Gem.send :remove_const, :APPLE_GEM_HOME
end
@@ -309,7 +354,7 @@ class TestGem < RubyGemTestCase
apple_gem_home = File.join @tempdir, 'apple_gem_home'
Gem.const_set :APPLE_GEM_HOME, apple_gem_home
- assert !Gem.path.include?(apple_gem_home)
+ refute Gem.path.include?(apple_gem_home)
ensure
Gem.send :remove_const, :APPLE_GEM_HOME
end
@@ -400,17 +445,17 @@ class TestGem < RubyGemTestCase
def test_self_refresh
util_make_gems
- a1_spec = File.join @gemhome, "specifications", "#{@a1.full_name}.gemspec"
+ a1_spec = File.join @gemhome, "specifications", "#{@a1.full_name}.gemspec"
FileUtils.mv a1_spec, @tempdir
- assert !Gem.source_index.gems.include?(@a1.full_name)
+ refute Gem.source_index.gems.include?(@a1.full_name)
FileUtils.mv File.join(@tempdir, "#{@a1.full_name}.gemspec"), a1_spec
Gem.refresh
- assert Gem.source_index.gems.include?(@a1.full_name)
+ assert_includes Gem.source_index.gems, @a1.full_name
assert_equal nil, Gem.instance_variable_get(:@searcher)
end
@@ -463,11 +508,28 @@ class TestGem < RubyGemTestCase
Gem::ConfigMap[:EXEEXT] = orig_exe_ext
end
- def test_self_ruby_version
- version = RUBY_VERSION.dup
- version << ".#{RUBY_PATCHLEVEL}" if defined? RUBY_PATCHLEVEL
+ def test_self_ruby_version_1_8_5
+ util_set_RUBY_VERSION '1.8.5'
- assert_equal Gem::Version.new(version), Gem.ruby_version
+ assert_equal Gem::Version.new('1.8.5'), Gem.ruby_version
+ ensure
+ util_restore_RUBY_VERSION
+ end
+
+ def test_self_ruby_version_1_8_6p287
+ util_set_RUBY_VERSION '1.8.6', 287
+
+ assert_equal Gem::Version.new('1.8.6.287'), Gem.ruby_version
+ ensure
+ util_restore_RUBY_VERSION
+ end
+
+ def test_self_ruby_version_1_9_2dev_r23493
+ util_set_RUBY_VERSION '1.9.2', -1, 23493
+
+ assert_equal Gem::Version.new('1.9.2.dev.23493'), Gem.ruby_version
+ ensure
+ util_restore_RUBY_VERSION
end
def test_self_searcher
@@ -478,13 +540,12 @@ class TestGem < RubyGemTestCase
other = File.join @tempdir, 'other'
path = [@userhome, other].join File::PATH_SEPARATOR
Gem.send :set_paths, path
- path = Gem.path
- assert_equal path[0], @userhome
- assert_equal path[1], other
+ assert_equal [@userhome, other, @gemhome], Gem.path
end
def test_self_set_paths_nonexistent_home
+ ENV['GEM_HOME'] = @gemhome
Gem.clear_paths
other = File.join @tempdir, 'other'
@@ -493,7 +554,7 @@ class TestGem < RubyGemTestCase
Gem.send :set_paths, other
- refute File.exist?(File.join(other, 'gems'))
+ assert_equal [other, @gemhome], Gem.path
end
def test_self_source_index
@@ -538,6 +599,28 @@ class TestGem < RubyGemTestCase
end
end
+ def test_self_user_home_user_drive_and_path
+ Gem.clear_paths
+
+ # safe-keep env variables
+ orig_home, orig_user_profile = ENV['HOME'], ENV['USERPROFILE']
+ orig_user_drive, orig_user_path = ENV['HOMEDRIVE'], ENV['HOMEPATH']
+
+ # prepare the environment
+ ENV.delete('HOME')
+ ENV.delete('USERPROFILE')
+ ENV['HOMEDRIVE'] = 'Z:'
+ ENV['HOMEPATH'] = '\\Users\\RubyUser'
+
+ assert_equal "Z:\\Users\\RubyUser", Gem.user_home
+
+ ensure
+ ENV['HOME'] = orig_home
+ ENV['USERPROFILE'] = orig_user_profile
+ ENV['USERDRIVE'] = orig_user_drive
+ ENV['USERPATH'] = orig_user_path
+ end if '1.9' > RUBY_VERSION
+
def util_ensure_gem_dirs
Gem.ensure_gem_subdirectories @gemhome
@additional.each do |dir|
@@ -545,5 +628,46 @@ class TestGem < RubyGemTestCase
end
end
+ def util_exec_gem
+ spec, _ = quick_gem 'a', '4' do |s|
+ s.default_executable = 'exec'
+ s.executables = ['exec', 'abin']
+ end
+
+ @exec_path = File.join spec.full_gem_path, spec.bindir, 'exec'
+ @abin_path = File.join spec.full_gem_path, spec.bindir, 'abin'
+ end
+
+ def util_set_RUBY_VERSION(version, patchlevel = nil, revision = nil)
+ if Gem.instance_variables.include? :@ruby_version or
+ Gem.instance_variables.include? '@ruby_version' then
+ Gem.send :remove_instance_variable, :@ruby_version
+ end
+
+ @RUBY_VERSION = RUBY_VERSION
+ @RUBY_PATCHLEVEL = RUBY_PATCHLEVEL if defined?(RUBY_PATCHLEVEL)
+ @RUBY_REVISION = RUBY_REVISION if defined?(RUBY_REVISION)
+
+ Object.send :remove_const, :RUBY_VERSION
+ Object.send :remove_const, :RUBY_PATCHLEVEL if defined?(RUBY_PATCHLEVEL)
+ Object.send :remove_const, :RUBY_REVISION if defined?(RUBY_REVISION)
+
+ Object.const_set :RUBY_VERSION, version
+ Object.const_set :RUBY_PATCHLEVEL, patchlevel if patchlevel
+ Object.const_set :RUBY_REVISION, revision if revision
+ end
+
+ def util_restore_RUBY_VERSION
+ Object.send :remove_const, :RUBY_VERSION
+ Object.send :remove_const, :RUBY_PATCHLEVEL if defined?(RUBY_PATCHLEVEL)
+ Object.send :remove_const, :RUBY_REVISION if defined?(RUBY_REVISION)
+
+ Object.const_set :RUBY_VERSION, @RUBY_VERSION
+ Object.const_set :RUBY_PATCHLEVEL, @RUBY_PATCHLEVEL if
+ defined?(@RUBY_PATCHLEVEL)
+ Object.const_set :RUBY_REVISION, @RUBY_REVISION if
+ defined?(@RUBY_REVISION)
+ end
+
end