Skip to content

Commit

Permalink
Merge pull request #3906 from rubygems/fix_install_as_default_with_in…
Browse files Browse the repository at this point in the history
…stall_dir

Make `--default` and `--install-dir` options to `gem install` play nice together
  • Loading branch information
deivid-rodriguez committed Sep 15, 2020
2 parents 025667f + 40ce23e commit 40fc322
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 58 deletions.
2 changes: 1 addition & 1 deletion lib/rubygems/installer.rb
Expand Up @@ -433,7 +433,7 @@ def spec_file
#

def default_spec_file
File.join Gem.default_specifications_dir, "#{spec.full_name}.gemspec"
File.join gem_home, "specifications", "default", "#{spec.full_name}.gemspec"
end

##
Expand Down
13 changes: 7 additions & 6 deletions lib/rubygems/installer_test_case.rb
Expand Up @@ -108,9 +108,9 @@ def util_make_exec(spec = @spec, shebang = "#!/usr/bin/ruby", bindir = "bin")
#
# And returns a Gem::Installer for the @spec that installs into @gemhome

def setup_base_installer
def setup_base_installer(force = true)
@gem = setup_base_gem
util_installer @spec, @gemhome
util_installer @spec, @gemhome, false, force
end

##
Expand Down Expand Up @@ -182,7 +182,7 @@ def util_setup_installer(&block)
# lib/code.rb
# ext/a/mkrf_conf.rb

def util_setup_gem(ui = @ui)
def util_setup_gem(ui = @ui, force = true)
@spec.files << File.join('lib', 'code.rb')
@spec.extensions << File.join('ext', 'a', 'mkrf_conf.rb')

Expand Down Expand Up @@ -214,17 +214,18 @@ def util_setup_gem(ui = @ui)
end
end

Gem::Installer.at @gem
Gem::Installer.at @gem, :force => force
end

##
# Creates an installer for +spec+ that will install into +gem_home+. If
# +user+ is true a user-install will be performed.

def util_installer(spec, gem_home, user=false)
def util_installer(spec, gem_home, user=false, force=true)
Gem::Installer.at(spec.cache_file,
:install_dir => gem_home,
:user_install => user)
:user_install => user,
:force => force)
end

@@symlink_supported = nil
Expand Down
33 changes: 10 additions & 23 deletions lib/rubygems/test_case.rb
Expand Up @@ -337,6 +337,7 @@ def setup
@git = ENV['GIT'] || (win_platform? ? 'git.exe' : 'git')

Gem.ensure_gem_subdirectories @gemhome
Gem.ensure_default_gem_subdirectories @gemhome

@orig_LOAD_PATH = $LOAD_PATH.dup
$LOAD_PATH.map! do |s|
Expand All @@ -362,26 +363,23 @@ def setup
Gem.send :remove_instance_variable, :@ruby_version if
Gem.instance_variables.include? :@ruby_version

FileUtils.mkdir_p @gemhome
FileUtils.mkdir_p @userhome

ENV['GEM_PRIVATE_KEY_PASSPHRASE'] = PRIVATE_KEY_PASSPHRASE

@default_dir = File.join @tempdir, 'default'
@default_spec_dir = File.join @default_dir, "specifications", "default"
if Gem.java_platform?
@orig_default_gem_home = RbConfig::CONFIG['default_gem_home']
RbConfig::CONFIG['default_gem_home'] = @default_dir
RbConfig::CONFIG['default_gem_home'] = @gemhome
else
Gem.instance_variable_set(:@default_dir, @default_dir)
Gem.instance_variable_set(:@default_dir, @gemhome)
end
FileUtils.mkdir_p @default_spec_dir

@orig_bindir = RbConfig::CONFIG["bindir"]
RbConfig::CONFIG["bindir"] = File.join @gemhome, "bin"

Gem::Specification.unresolved_deps.clear
Gem.use_paths(@gemhome)

Gem::Security.reset

Gem.loaded_specs.clear
Gem.instance_variable_set(:@activated_gem_paths, 0)
Gem.clear_default_specs
Expand Down Expand Up @@ -450,6 +448,8 @@ def teardown

Gem.ruby = @orig_ruby if @orig_ruby

RbConfig::CONFIG['bindir'] = @orig_bindir

if Gem.java_platform?
RbConfig::CONFIG['default_gem_home'] = @orig_default_gem_home
else
Expand Down Expand Up @@ -743,7 +743,7 @@ def util_clear_gems

def install_specs(*specs)
specs.each do |spec|
Gem::Installer.for_spec(spec).install
Gem::Installer.for_spec(spec, :force => true).install
end

Gem.searcher = nil
Expand All @@ -753,19 +753,6 @@ def install_specs(*specs)
# Installs the provided default specs including writing the spec file

def install_default_gems(*specs)
install_default_specs(*specs)

specs.each do |spec|
File.open spec.loaded_from, 'w' do |io|
io.write spec.to_ruby_for_cache
end
end
end

##
# Install the provided default specs

def install_default_specs(*specs)
specs.each do |spec|
installer = Gem::Installer.for_spec(spec, :install_as_default => true)
installer.install
Expand Down Expand Up @@ -794,7 +781,7 @@ def save_loaded_features
def new_default_spec(name, version, deps = nil, *files)
spec = util_spec name, version, deps

spec.loaded_from = File.join(@default_spec_dir, spec.spec_name)
spec.loaded_from = File.join(@gemhome, "specifications", "default", spec.spec_name)
spec.files = files

lib_dir = File.join(@tempdir, "default_gems", "lib")
Expand Down
2 changes: 1 addition & 1 deletion test/rubygems/test_gem_commands_cleanup_command.rb
Expand Up @@ -221,7 +221,7 @@ def test_execute_ignore_default_gem_verbose
@b_2 = util_spec 'b', 3

install_gem @b_1
install_default_specs @b_default
install_default_gems @b_default
install_gem @b_2

@cmd.options[:args] = []
Expand Down
2 changes: 1 addition & 1 deletion test/rubygems/test_gem_commands_contents_command.rb
Expand Up @@ -227,7 +227,7 @@ def test_execute_default_gem
nil, "default/gem.rb")
default_gem_spec.executables = ["default_command"]
default_gem_spec.files += ["default_gem.so"]
install_default_specs(default_gem_spec)
install_default_gems(default_gem_spec)

@cmd.options[:args] = %w[default]

Expand Down
2 changes: 1 addition & 1 deletion test/rubygems/test_gem_commands_pristine_command.rb
Expand Up @@ -577,7 +577,7 @@ def test_execute_unknown_gem_at_remote_source
def test_execute_default_gem
default_gem_spec = new_default_spec("default", "2.0.0.0",
nil, "default/gem.rb")
install_default_specs(default_gem_spec)
install_default_gems(default_gem_spec)

@cmd.options[:args] = %w[default]

Expand Down
6 changes: 3 additions & 3 deletions test/rubygems/test_gem_commands_query_command.rb
Expand Up @@ -644,7 +644,7 @@ def test_execute_show_default_gems
spec_fetcher {|fetcher| fetcher.spec 'a', 2 }

a1 = new_default_spec 'a', 1
install_default_specs a1
install_default_gems a1

use_ui @stub_ui do
@cmd.execute
Expand All @@ -663,7 +663,7 @@ def test_execute_show_default_gems
def test_execute_show_default_gems_with_platform
a1 = new_default_spec 'a', 1
a1.platform = 'java'
install_default_specs a1
install_default_gems a1

use_ui @stub_ui do
@cmd.execute
Expand All @@ -685,7 +685,7 @@ def test_execute_default_details
end

a1 = new_default_spec 'a', 1
install_default_specs a1
install_default_gems a1

@cmd.handle_options %w[-l -d]

Expand Down
2 changes: 1 addition & 1 deletion test/rubygems/test_gem_dependency_installer.rb
Expand Up @@ -749,7 +749,7 @@ def test_install_reinstall
inst = nil

Dir.chdir @tempdir do
inst = Gem::DependencyInstaller.new
inst = Gem::DependencyInstaller.new :force => true
inst.install 'a'
end

Expand Down
26 changes: 22 additions & 4 deletions test/rubygems/test_gem_installer.rb
Expand Up @@ -83,7 +83,7 @@ def test_check_executable_overwrite
end

def test_check_executable_overwrite_default_bin_dir
installer = setup_base_installer
installer = setup_base_installer(false)

bindir(Gem.bindir) do
util_conflict_executable false
Expand Down Expand Up @@ -143,7 +143,7 @@ def test_check_executable_overwrite_format_executable
end

def test_check_executable_overwrite_other_gem
installer = setup_base_installer
installer = setup_base_installer(false)

util_conflict_executable true

Expand Down Expand Up @@ -1134,7 +1134,7 @@ def test_install_with_no_prior_files
Gem::Package.build @spec
end
end
installer = Gem::Installer.at @gem
installer = Gem::Installer.at @gem, :force => true
build_rake_in do
use_ui @ui do
assert_equal @spec, installer.install
Expand Down Expand Up @@ -1337,7 +1337,7 @@ def test_install_extension_dir_is_removed_on_reinstall

# reinstall the gem, this is also the same as pristine
use_ui @ui do
installer = Gem::Installer.at path
installer = Gem::Installer.at path, :force => true
installer.install
end

Expand Down Expand Up @@ -1537,6 +1537,7 @@ def test_pre_install_checks_dependencies
installer = setup_base_installer
@spec.add_dependency 'b', '> 5'
installer = util_setup_gem
installer.force = false

use_ui @ui do
assert_raises Gem::InstallError do
Expand Down Expand Up @@ -2169,6 +2170,23 @@ def test_default_gem_with_exe_as_bindir
assert_equal ['exe/executable'], default_spec.files
end

def test_default_gem_to_specific_install_dir
@gem = setup_base_gem
installer = util_installer @spec, "#{@gemhome}2"
installer.options[:install_as_default] = true

use_ui @ui do
installer.install
end

assert_directory_exists File.join("#{@gemhome}2", 'specifications')
assert_directory_exists File.join("#{@gemhome}2", 'specifications', 'default')

default_spec = eval File.read File.join("#{@gemhome}2", 'specifications', 'default', 'a-2.gemspec')
assert_equal Gem::Version.new("2"), default_spec.version
assert_equal ['bin/executable'], default_spec.files
end

def test_package_attribute
gem = quick_gem 'c' do |spec|
util_make_exec spec, '#!/usr/bin/ruby', 'exe'
Expand Down
2 changes: 1 addition & 1 deletion test/rubygems/test_gem_specification.rb
Expand Up @@ -3838,7 +3838,7 @@ def test_load_default_gem

default_gem_spec = new_default_spec("default", "2.0.0.0",
nil, "default/gem.rb")
spec_path = File.join(@default_spec_dir, default_gem_spec.spec_name)
spec_path = File.join(@gemhome, "specifications", "default", default_gem_spec.spec_name)
write_file(spec_path) do |file|
file.print(default_gem_spec.to_ruby)
end
Expand Down
14 changes: 7 additions & 7 deletions test/rubygems/test_gem_uninstaller.rb
Expand Up @@ -177,7 +177,7 @@ def test_remove_plugins

@spec.files += %w[lib/rubygems_plugin.rb]

Gem::Installer.at(Gem::Package.build(@spec)).install
Gem::Installer.at(Gem::Package.build(@spec), :force => true).install

plugin_path = File.join Gem.plugindir, 'a_plugin.rb'
assert File.exist?(plugin_path), 'plugin not written'
Expand All @@ -194,7 +194,7 @@ def test_remove_plugins_with_install_dir

@spec.files += %w[lib/rubygems_plugin.rb]

Gem::Installer.at(Gem::Package.build(@spec)).install
Gem::Installer.at(Gem::Package.build(@spec), :force => true).install

plugin_path = File.join Gem.plugindir, 'a_plugin.rb'
assert File.exist?(plugin_path), 'plugin not written'
Expand All @@ -212,7 +212,7 @@ def test_regenerate_plugins_for

@spec.files += %w[lib/rubygems_plugin.rb]

Gem::Installer.at(Gem::Package.build(@spec)).install
Gem::Installer.at(Gem::Package.build(@spec), :force => true).install

plugin_path = File.join Gem.plugindir, 'a_plugin.rb'
assert File.exist?(plugin_path), 'plugin not written'
Expand Down Expand Up @@ -314,7 +314,7 @@ def test_uninstall_extension
use_ui @ui do
path = Gem::Package.build @spec

installer = Gem::Installer.at path
installer = Gem::Installer.at path, :force => true
installer.install
end

Expand Down Expand Up @@ -633,19 +633,19 @@ def test_uninstall_keeps_plugins_up_to_date
plugin_path = File.join Gem.plugindir, 'a_plugin.rb'

@spec.version = '1'
Gem::Installer.at(Gem::Package.build(@spec)).install
Gem::Installer.at(Gem::Package.build(@spec), :force => true).install

refute File.exist?(plugin_path), 'version without plugin installed, but plugin written'

@spec.files += %w[lib/rubygems_plugin.rb]
@spec.version = '2'
Gem::Installer.at(Gem::Package.build(@spec)).install
Gem::Installer.at(Gem::Package.build(@spec), :force => true).install

assert File.exist?(plugin_path), 'version with plugin installed, but plugin not written'
assert_match %r{\Arequire.*a-2/lib/rubygems_plugin\.rb}, File.read(plugin_path), 'written plugin has incorrect content'

@spec.version = '3'
Gem::Installer.at(Gem::Package.build(@spec)).install
Gem::Installer.at(Gem::Package.build(@spec), :force => true).install

assert File.exist?(plugin_path), 'version with plugin installed, but plugin removed'
assert_match %r{\Arequire.*a-3/lib/rubygems_plugin\.rb}, File.read(plugin_path), 'old version installed, but plugin updated'
Expand Down

0 comments on commit 40fc322

Please sign in to comment.