diff options
Diffstat (limited to 'test/rubygems/test_gem_installer.rb')
-rw-r--r-- | test/rubygems/test_gem_installer.rb | 405 |
1 files changed, 210 insertions, 195 deletions
diff --git a/test/rubygems/test_gem_installer.rb b/test/rubygems/test_gem_installer.rb index 33950ad46c..23ac5b1f35 100644 --- a/test/rubygems/test_gem_installer.rb +++ b/test/rubygems/test_gem_installer.rb @@ -2,24 +2,24 @@ require 'rubygems/installer_test_case' class TestGemInstaller < Gem::InstallerTestCase - def util_setup_install - @gemhome = @installer_tmp - Gem.use_paths @installer_tmp + def setup + super - @spec = Gem::Specification.find_by_name 'a' - @user_spec = Gem::Specification.find_by_name 'b' + if __name__ =~ /^test_install(_|$)/ then + FileUtils.rm_r @spec.gem_dir + FileUtils.rm_r @user_spec.gem_dir + end - @installer.spec = @spec - @installer.gem_home = @installer_tmp - @installer.gem_dir = @spec.gem_dir - @user_installer.spec = @user_spec - @user_installer.gem_home = @installer_tmp + @config = Gem.configuration end - def test_app_script_text - util_setup_install + def teardown + super - @spec.version = 2 + Gem.configuration = @config + end + + def test_app_script_text util_make_exec @spec, '' expected = <<-EOF @@ -53,8 +53,6 @@ load Gem.bin_path('a', 'executable', version) end def test_build_extensions_none - util_setup_install - use_ui @ui do @installer.build_extensions end @@ -66,8 +64,7 @@ load Gem.bin_path('a', 'executable', version) end def test_build_extensions_extconf_bad - util_setup_install - + @installer.spec = @spec @spec.extensions << 'extconf.rb' e = assert_raises Gem::Installer::ExtensionBuildError do @@ -91,9 +88,9 @@ load Gem.bin_path('a', 'executable', version) end def test_build_extensions_unsupported - util_setup_install - - gem_make_out = File.join @gemhome, 'gems', @spec.full_name, 'gem_make.out' + @installer.spec = @spec + FileUtils.mkdir_p @spec.gem_dir + gem_make_out = File.join @spec.gem_dir, 'gem_make.out' @spec.extensions << nil e = assert_raises Gem::Installer::ExtensionBuildError do @@ -113,95 +110,148 @@ load Gem.bin_path('a', 'executable', version) FileUtils.rm_f gem_make_out end - def test_ensure_dependency - util_setup_install + def test_build_extensions_with_build_args + args = ["--aa", "--bb"] + @installer.build_args = args + @installer.spec = @spec + @spec.extensions << 'extconf.rb' - dep = Gem::Dependency.new 'a', '>= 2' - assert @installer.ensure_dependency(@spec, dep) + File.open File.join(@spec.gem_dir, "extconf.rb"), "w" do |f| + f.write <<-'RUBY' + puts "IN EXTCONF" + File.open 'extconf_args', 'w' do |f| + f.puts ARGV.inspect + end + + File.open 'Makefile', 'w' do |f| + f.puts "default:\n\techo built" + f.puts "install:\n\techo installed" + end + RUBY + end - dep = Gem::Dependency.new 'b', '> 2' - e = assert_raises Gem::InstallError do - @installer.ensure_dependency @spec, dep + use_ui @ui do + @installer.build_extensions end - assert_equal 'a requires b (> 2)', e.message + path = File.join @spec.gem_dir, "extconf_args" + + assert_equal args.inspect, File.read(path).strip end - def test_extract_files - util_setup_install + def test_check_executable_overwrite + @installer.generate_bin - format = Object.new - def format.file_entries - [[{'size' => 7, 'mode' => 0400, 'path' => 'thefile'}, 'content']] + @spec = Gem::Specification.new do |s| + s.files = ['lib/code.rb'] + s.name = "a" + s.version = "3" + s.summary = "summary" + s.description = "desc" + s.require_path = 'lib' end - @installer.format = format + util_make_exec + @installer.gem_dir = util_gem_dir @spec + @installer.wrappers = true + @installer.generate_bin - @installer.extract_files + installed_exec = File.join util_inst_bindir, 'executable' + assert File.exist? installed_exec - thefile_path = File.join(util_gem_dir, 'thefile') - assert_equal 'content', File.read(thefile_path) + wrapper = File.read installed_exec + assert_match %r|generated by RubyGems|, wrapper + end - unless Gem.win_platform? then - assert_equal 0400, File.stat(thefile_path).mode & 0777 + def test_check_executable_overwrite_default_bin_dir + if defined?(RUBY_FRAMEWORK_VERSION) + orig_RUBY_FRAMEWORK_VERSION = RUBY_FRAMEWORK_VERSION + Object.send :remove_const, :RUBY_FRAMEWORK_VERSION end - end + orig_bindir = Gem::ConfigMap[:bindir] + Gem::ConfigMap[:bindir] = Gem.bindir - def test_extract_files_bad_dest - util_setup_install + util_conflict_executable false - @installer.gem_dir = 'somedir' - @installer.format = nil - e = assert_raises ArgumentError do - @installer.extract_files - end + ui = Gem::MockGemUi.new "n\n" + use_ui ui do + e = assert_raises Gem::InstallError do + @installer.generate_bin + end - assert_equal 'format required to extract from', e.message + conflicted = File.join @gemhome, 'bin', 'executable' + assert_equal "\"executable\" from a conflicts with #{conflicted}", + e.message + end + ensure + Object.const_set :RUBY_FRAMEWORK_VERSION, orig_RUBY_FRAMEWORK_VERSION if + orig_RUBY_FRAMEWORK_VERSION + Gem::ConfigMap[:bindir] = orig_bindir end - def test_extract_files_relative - util_setup_install + def test_check_executable_overwrite_other_gem + util_conflict_executable true - format = Object.new - def format.file_entries - [[{'size' => 10, 'mode' => 0644, 'path' => '../thefile'}, '../thefile']] - end + ui = Gem::MockGemUi.new "n\n" - @installer.format = format + use_ui ui do + e = assert_raises Gem::InstallError do + @installer.generate_bin + end - e = assert_raises Gem::InstallError do - @installer.extract_files + assert_equal '"executable" from a conflicts with installed executable from conflict', + e.message end + end + + def test_check_executable_overwrite_other_gem_force + util_conflict_executable true + @installer.wrappers = true + @installer.force = true - dir = util_gem_dir - expected = "attempt to install file into \"../thefile\" under #{dir}" - assert_equal expected, e.message - assert_equal false, File.file?(File.join(@tempdir, '../thefile')), - "You may need to remove this file if you broke the test once" + @installer.generate_bin + + installed_exec = File.join util_inst_bindir, 'executable' + assert File.exist? installed_exec + + wrapper = File.read installed_exec + assert_match %r|generated by RubyGems|, wrapper end - def test_extract_files_absolute - util_setup_install + def test_check_executable_overwrite_other_non_gem + util_conflict_executable false + @installer.wrappers = true - format = Object.new - def format.file_entries - [[{'size' => 8, 'mode' => 0644, 'path' => '/thefile'}, '/thefile']] - end + @installer.generate_bin + + installed_exec = File.join util_inst_bindir, 'executable' + assert File.exist? installed_exec - @installer.format = format + wrapper = File.read installed_exec + assert_match %r|generated by RubyGems|, wrapper + end + + def test_ensure_dependency + quick_spec 'a' + + dep = Gem::Dependency.new 'a', '>= 2' + assert @installer.ensure_dependency(@spec, dep) + dep = Gem::Dependency.new 'b', '> 2' e = assert_raises Gem::InstallError do - @installer.extract_files + @installer.ensure_dependency @spec, dep end - assert_equal 'attempt to install file into /thefile', e.message - assert_equal false, File.file?(File.join('/thefile')), - "You may need to remove this file if you broke the test once" + assert_equal 'a requires b (> 2)', e.message end - def test_generate_bin_bindir - util_setup_install + def test_extract_files + @installer.extract_files + assert_path_exists File.join util_gem_dir, 'bin/executable' + end + + def test_generate_bin_bindir @installer.wrappers = true @spec.executables = %w[executable] @@ -227,16 +277,15 @@ load Gem.bin_path('a', 'executable', version) end def test_generate_bin_bindir_with_user_install_warning - util_setup_install - - bin_dir = Gem.win_platform? ? File.expand_path(ENV["WINDIR"]) : "/usr/bin" + bin_dir = Gem.win_platform? ? File.expand_path(ENV["WINDIR"]).upcase : + "/usr/bin" options = { :bin_dir => bin_dir, :install_dir => "/non/existant" } - inst = Gem::Installer.new nil, options + inst = Gem::Installer.new '', options Gem::Installer.path_warning = false @@ -248,16 +297,14 @@ load Gem.bin_path('a', 'executable', version) end def test_generate_bin_script - util_setup_install - @installer.wrappers = true util_make_exec @installer.gem_dir = util_gem_dir @installer.generate_bin - assert_equal true, File.directory?(util_inst_bindir) - installed_exec = File.join(util_inst_bindir, 'executable') - assert_equal true, File.exist?(installed_exec) + assert File.directory? util_inst_bindir + installed_exec = File.join util_inst_bindir, 'executable' + assert File.exist? installed_exec assert_equal mask, File.stat(installed_exec).mode unless win_platform? wrapper = File.read installed_exec @@ -265,8 +312,6 @@ load Gem.bin_path('a', 'executable', version) end def test_generate_bin_script_format - util_setup_install - @installer.format_executable = true @installer.wrappers = true util_make_exec @@ -282,8 +327,6 @@ load Gem.bin_path('a', 'executable', version) end def test_generate_bin_script_format_disabled - util_setup_install - @installer.wrappers = true util_make_exec @installer.gem_dir = util_gem_dir @@ -298,10 +341,7 @@ load Gem.bin_path('a', 'executable', version) end def test_generate_bin_script_install_dir - util_setup_install - @installer.wrappers = true - @spec.executables = %w[executable] gem_dir = File.join("#{@gemhome}2", "gems", @spec.full_name) gem_bindir = File.join gem_dir, 'bin' @@ -312,11 +352,12 @@ load Gem.bin_path('a', 'executable', version) @installer.gem_home = "#{@gemhome}2" @installer.gem_dir = gem_dir + @installer.bin_dir = File.join "#{@gemhome}2", 'bin' @installer.generate_bin installed_exec = File.join("#{@gemhome}2", "bin", 'executable') - assert_equal true, File.exist?(installed_exec) + assert File.exist? installed_exec assert_equal mask, File.stat(installed_exec).mode unless win_platform? wrapper = File.read installed_exec @@ -324,8 +365,6 @@ load Gem.bin_path('a', 'executable', version) end def test_generate_bin_script_no_execs - util_setup_install - util_execless @installer.wrappers = true @@ -335,8 +374,6 @@ load Gem.bin_path('a', 'executable', version) end def test_generate_bin_script_no_perms - util_setup_install - @installer.wrappers = true util_make_exec @@ -356,8 +393,6 @@ load Gem.bin_path('a', 'executable', version) end def test_generate_bin_script_no_shebang - util_setup_install - @installer.wrappers = true @spec.executables = %w[executable] @@ -381,8 +416,6 @@ load Gem.bin_path('a', 'executable', version) end def test_generate_bin_script_wrappers - util_setup_install - @installer.wrappers = true util_make_exec @installer.gem_dir = util_gem_dir @@ -408,8 +441,6 @@ load Gem.bin_path('a', 'executable', version) end def test_generate_bin_symlink - util_setup_install - return if win_platform? #Windows FS do not support symlinks @installer.wrappers = false @@ -425,8 +456,6 @@ load Gem.bin_path('a', 'executable', version) end def test_generate_bin_symlink_no_execs - util_setup_install - util_execless @installer.wrappers = false @@ -436,8 +465,6 @@ load Gem.bin_path('a', 'executable', version) end def test_generate_bin_symlink_no_perms - util_setup_install - @installer.wrappers = false util_make_exec @installer.gem_dir = util_gem_dir @@ -458,8 +485,6 @@ load Gem.bin_path('a', 'executable', version) end def test_generate_bin_symlink_update_newer - util_setup_install - return if win_platform? #Windows FS do not support symlinks @installer.wrappers = false @@ -480,7 +505,6 @@ load Gem.bin_path('a', 'executable', version) s.require_path = 'lib' end - @spec.version = 3 util_make_exec @installer.gem_dir = util_gem_dir @spec @installer.generate_bin @@ -491,8 +515,6 @@ load Gem.bin_path('a', 'executable', version) end def test_generate_bin_symlink_update_older - util_setup_install - return if win_platform? #Windows FS do not support symlinks @installer.wrappers = false @@ -529,8 +551,6 @@ load Gem.bin_path('a', 'executable', version) end def test_generate_bin_symlink_update_remove_wrapper - util_setup_install - return if win_platform? #Windows FS do not support symlinks @installer.wrappers = true @@ -538,8 +558,9 @@ load Gem.bin_path('a', 'executable', version) @installer.gem_dir = util_gem_dir @installer.generate_bin + installed_exec = File.join util_inst_bindir, 'executable' - assert_equal true, File.exist?(installed_exec) + assert File.exist? installed_exec @spec = Gem::Specification.new do |s| s.files = ['lib/code.rb'] @@ -549,21 +570,21 @@ load Gem.bin_path('a', 'executable', version) s.description = "desc" s.require_path = 'lib' end + util_make_exec + util_installer @spec, @gemhome @installer.wrappers = false - @spec.version = 3 - util_make_exec @installer.gem_dir = util_gem_dir + @installer.generate_bin - installed_exec = File.join(util_inst_bindir, 'executable') - assert_equal(File.join(util_gem_dir, 'bin', 'executable'), + + installed_exec = File.join util_inst_bindir, 'executable' + assert_equal(@spec.bin_file('executable'), File.readlink(installed_exec), "Ensure symlink moved to latest version") end def test_generate_bin_symlink_win32 - util_setup_install - old_win_platform = Gem.win_platform? Gem.win_platform = true @installer.wrappers = false @@ -588,8 +609,6 @@ load Gem.bin_path('a', 'executable', version) end def test_generate_bin_uses_default_shebang - util_setup_install - return if win_platform? #Windows FS do not support symlinks @installer.wrappers = true @@ -784,7 +803,7 @@ load Gem.bin_path('a', 'executable', version) File.open File.join('lib', 'other.rb'), 'w' do |f| f.puts '1' end use_ui ui do FileUtils.rm @gem - Gem::Builder.new(@spec).build + Gem::Package.build @spec end end @installer = Gem::Installer.new @gem @@ -799,27 +818,6 @@ load Gem.bin_path('a', 'executable', version) "code.rb from prior install of same gem shouldn't remain here") end - def test_install_bad_gem - gem = nil - - use_ui @ui do - Dir.chdir @tempdir do Gem::Builder.new(@spec).build end - gem = File.join @tempdir, @spec.file_name - end - - gem_data = File.open gem, 'rb' do |fp| fp.read 1024 end - File.open gem, 'wb' do |fp| fp.write gem_data end - - e = assert_raises Gem::InstallError do - use_ui @ui do - @installer = Gem::Installer.new gem - @installer.install - end - end - - assert_equal "invalid gem format for #{gem}", e.message - end - def test_install_check_dependencies @spec.add_dependency 'b', '> 5' util_setup_gem @@ -833,13 +831,15 @@ load Gem.bin_path('a', 'executable', version) def test_install_check_dependencies_install_dir gemhome2 = "#{@gemhome}2" - @spec.add_dependency 'b' + @spec.add_dependency 'd' - quick_gem 'b', 2 + quick_gem 'd', 2 FileUtils.mv @gemhome, gemhome2 - Gem::Specification.dirs = [gemhome2] # TODO: switch all dirs= to use_paths + # Don't leak any already activated gems into the installer, require + # that it work everything out on it's own. + Gem::Specification.reset util_setup_gem @@ -897,8 +897,6 @@ load Gem.bin_path('a', 'executable', version) FileUtils.rm_f File.join(Gem.dir, 'specifications') use_ui @ui do - Dir.chdir @tempdir do Gem::Builder.new(@spec).build end - @installer.install end @@ -992,7 +990,7 @@ load Gem.bin_path('a', 'executable', version) @spec.post_install_message = 'I am a shiny gem!' use_ui @ui do - path = Gem::Builder.new(@spec).build + path = Gem::Package.build @spec @installer = Gem::Installer.new path @installer.install @@ -1032,7 +1030,7 @@ load Gem.bin_path('a', 'executable', version) end def test_installation_satisfies_dependency_eh - util_setup_install + quick_spec 'a' dep = Gem::Dependency.new 'a', '>= 2' assert @installer.installation_satisfies_dependency?(dep) @@ -1042,8 +1040,6 @@ load Gem.bin_path('a', 'executable', version) end def test_shebang - util_setup_install - util_make_exec @spec, "#!/usr/bin/ruby" shebang = @installer.shebang 'executable' @@ -1052,8 +1048,6 @@ load Gem.bin_path('a', 'executable', version) end def test_shebang_arguments - util_setup_install - util_make_exec @spec, "#!/usr/bin/ruby -ws" shebang = @installer.shebang 'executable' @@ -1062,8 +1056,6 @@ load Gem.bin_path('a', 'executable', version) end def test_shebang_empty - util_setup_install - util_make_exec @spec, '' shebang = @installer.shebang 'executable' @@ -1071,8 +1063,6 @@ load Gem.bin_path('a', 'executable', version) end def test_shebang_env - util_setup_install - util_make_exec @spec, "#!/usr/bin/env ruby" shebang = @installer.shebang 'executable' @@ -1081,8 +1071,6 @@ load Gem.bin_path('a', 'executable', version) end def test_shebang_env_arguments - util_setup_install - util_make_exec @spec, "#!/usr/bin/env ruby -ws" shebang = @installer.shebang 'executable' @@ -1091,8 +1079,6 @@ load Gem.bin_path('a', 'executable', version) end def test_shebang_env_shebang - util_setup_install - util_make_exec @spec, '' @installer.env_shebang = true @@ -1105,8 +1091,6 @@ load Gem.bin_path('a', 'executable', version) end def test_shebang_nested - util_setup_install - util_make_exec @spec, "#!/opt/local/ruby/bin/ruby" shebang = @installer.shebang 'executable' @@ -1115,8 +1099,6 @@ load Gem.bin_path('a', 'executable', version) end def test_shebang_nested_arguments - util_setup_install - util_make_exec @spec, "#!/opt/local/ruby/bin/ruby -ws" shebang = @installer.shebang 'executable' @@ -1125,8 +1107,6 @@ load Gem.bin_path('a', 'executable', version) end def test_shebang_version - util_setup_install - util_make_exec @spec, "#!/usr/bin/ruby18" shebang = @installer.shebang 'executable' @@ -1135,8 +1115,6 @@ load Gem.bin_path('a', 'executable', version) end def test_shebang_version_arguments - util_setup_install - util_make_exec @spec, "#!/usr/bin/ruby18 -ws" shebang = @installer.shebang 'executable' @@ -1145,8 +1123,6 @@ load Gem.bin_path('a', 'executable', version) end def test_shebang_version_env - util_setup_install - util_make_exec @spec, "#!/usr/bin/env ruby18" shebang = @installer.shebang 'executable' @@ -1155,8 +1131,6 @@ load Gem.bin_path('a', 'executable', version) end def test_shebang_version_env_arguments - util_setup_install - util_make_exec @spec, "#!/usr/bin/env ruby18 -ws" shebang = @installer.shebang 'executable' @@ -1164,9 +1138,48 @@ load Gem.bin_path('a', 'executable', version) assert_equal "#!#{Gem.ruby} -ws", shebang end - def test_unpack - util_setup_install + def test_shebang_custom + conf = Gem::ConfigFile.new [] + conf[:custom_shebang] = 'test' + + Gem.configuration = conf + + util_make_exec @spec, "#!/usr/bin/ruby" + + shebang = @installer.shebang 'executable' + + assert_equal "#!test", shebang + end + + def test_shebang_custom_with_expands + bin_env = win_platform? ? '' : '/usr/bin/env' + conf = Gem::ConfigFile.new [] + conf[:custom_shebang] = '1 $env 2 $ruby 3 $exec 4 $name' + + Gem.configuration = conf + + util_make_exec @spec, "#!/usr/bin/ruby" + shebang = @installer.shebang 'executable' + + assert_equal "#!1 #{bin_env} 2 #{Gem.ruby} 3 executable 4 a", shebang + end + + def test_shebang_custom_with_expands_and_arguments + bin_env = win_platform? ? '' : '/usr/bin/env' + conf = Gem::ConfigFile.new [] + conf[:custom_shebang] = '1 $env 2 $ruby 3 $exec' + + Gem.configuration = conf + + util_make_exec @spec, "#!/usr/bin/ruby -ws" + + shebang = @installer.shebang 'executable' + + assert_equal "#!1 #{bin_env} 2 #{Gem.ruby} -ws 3 executable", shebang + end + + def test_unpack util_setup_gem dest = File.join @gemhome, 'gems', @spec.full_name @@ -1178,29 +1191,21 @@ load Gem.bin_path('a', 'executable', version) end def test_write_spec - util_setup_install - - spec_dir = File.join @gemhome, 'specifications' - spec_file = File.join spec_dir, @spec.spec_name - FileUtils.rm spec_file - refute File.exist?(spec_file) + FileUtils.rm @spec.spec_file + refute File.exist?(@spec.spec_file) @installer.spec = @spec @installer.gem_home = @gemhome @installer.write_spec - assert File.exist?(spec_file) - assert_equal @spec, eval(File.read(spec_file)) + assert File.exist?(@spec.spec_file) + assert_equal @spec, eval(File.read(@spec.spec_file)) end def test_write_spec_writes_cached_spec - util_setup_install - - spec_dir = File.join @gemhome, 'specifications' - spec_file = File.join spec_dir, @spec.spec_name - FileUtils.rm spec_file - refute File.exist?(spec_file) + FileUtils.rm @spec.spec_file + refute File.exist?(@spec.spec_file) @spec.files = %w[a.rb b.rb c.rb] @@ -1212,13 +1217,11 @@ load Gem.bin_path('a', 'executable', version) # cached specs have no file manifest: @spec.files = [] - assert_equal @spec, eval(File.read(spec_file)) + assert_equal @spec, eval(File.read(@spec.spec_file)) end def test_dir - util_setup_install - - assert_match %r!/installer/gems/a-2\z!, @installer.dir + assert_match %r!/gemhome/gems/a-2$!, @installer.dir end def old_ruby_required @@ -1238,6 +1241,18 @@ load Gem.bin_path('a', 'executable', version) @installer = util_installer @spec, @gemhome end + def util_conflict_executable wrappers + conflict = quick_gem 'conflict' do |spec| + util_make_exec spec + end + + util_build_gem conflict + + installer = util_installer conflict, @gemhome + installer.wrappers = wrappers + installer.generate_bin + end + def mask 0100755 & (~File.umask) end |