From 612ebd10c061fefd6e4f1f736a95188bd6ae5146 Mon Sep 17 00:00:00 2001 From: Samuel Giddins Date: Sun, 12 Feb 2023 19:19:17 -0800 Subject: [rubygems/rubygems] Ensure dependencies are updated by default when running gem exec https://github.com/rubygems/rubygems/commit/664f3e1e5f --- lib/rubygems/commands/exec_command.rb | 10 +- test/rubygems/test_gem_commands_exec_command.rb | 117 ++++++++++++++++++++++-- 2 files changed, 114 insertions(+), 13 deletions(-) diff --git a/lib/rubygems/commands/exec_command.rb b/lib/rubygems/commands/exec_command.rb index 53b32c6353..be76980410 100644 --- a/lib/rubygems/commands/exec_command.rb +++ b/lib/rubygems/commands/exec_command.rb @@ -118,7 +118,7 @@ to the same gem path as user-installed gems. max_length = opts.map {|k, _| k.size }.max opts.each do |k, v| next if v.nil? - verbose "\t#{k.to_s.rjust(max_length)}: #{v} " + verbose "\t#{k.to_s.rjust(max_length)}: #{v}" end verbose "" end @@ -127,6 +127,7 @@ to the same gem path as user-installed gems. activate! rescue Gem::MissingSpecError verbose "#{Gem::Dependency.new(options[:gem_name], options[:version])} not available locally, installing from remote" + verbose "\t#{Gem::Specification.all_names}" install activate! end @@ -135,12 +136,11 @@ to the same gem path as user-installed gems. gem_name = options[:gem_name] gem_version = options[:version] - home = Gem.paths.home - home = File.join(home, "gem_exec") - Gem.use_paths(home, Gem.path + [home]) + home = File.join(Gem.dir, "gem_exec") + Gem.use_paths(home, [home] + Gem.path) suppress_always_install do - Gem.install(gem_name, gem_version) + Gem.install(gem_name, gem_version, minimal_deps: false) end rescue Gem::InstallError => e alert_error "Error installing #{gem_name}:\n\t#{e.message}" diff --git a/test/rubygems/test_gem_commands_exec_command.rb b/test/rubygems/test_gem_commands_exec_command.rb index 85cc786a02..f9a37b40fb 100644 --- a/test/rubygems/test_gem_commands_exec_command.rb +++ b/test/rubygems/test_gem_commands_exec_command.rb @@ -12,6 +12,12 @@ class TestGemCommandsExecCommand < Gem::TestCase @orig_args = Gem::Command.build_args common_installer_setup + + @gem_home = Gem.dir + @gem_path = Gem.path + + @installed_specs = [] + Gem.post_install {|installer| @installed_specs << installer.spec } end def teardown @@ -22,6 +28,23 @@ class TestGemCommandsExecCommand < Gem::TestCase Gem::Command.build_args = @orig_args end + def invoke(*args) + @ui.outs.truncate(0) + @ui.outs.rewind + @ui.errs.truncate(0) + @ui.errs.rewind + @installed_specs.clear + + @cmd.invoke *args + ensure + Gem::Specification.unresolved_deps.clear + Gem.loaded_specs.clear + Gem.instance_variable_set(:@activated_gem_paths, 0) + Gem.clear_default_specs + Gem.use_paths(@gem_home, @gem_path) + Gem.refresh + end + def test_error_with_no_arguments e = assert_raise Gem::CommandLineError do @cmd.invoke @@ -414,7 +437,7 @@ class TestGemCommandsExecCommand < Gem::TestCase def test_conservative spec_fetcher do |fetcher| - fetcher.gem "a", 1 do |s| + fetcher.download "a", 1 do |s| s.executables = %w[foo] s.files = %w[bin/foo lib/a.rb] @@ -424,16 +447,13 @@ class TestGemCommandsExecCommand < Gem::TestCase end end - util_clear_gems - use_ui @ui do - @cmd.invoke "--verbose", "--conservative", "a" + invoke "--verbose", "--conservative", "a" assert_include @ui.output, "a (>= 0) not available locally" assert_include @ui.output, "a-1 foo" + assert_equal %w[a-1], @installed_specs.map(&:original_name) end - @ui.outs.truncate(0) - spec_fetcher do |fetcher| fetcher.gem "a", 1 do |s| s.executables = %w[foo] @@ -444,7 +464,7 @@ class TestGemCommandsExecCommand < Gem::TestCase end end - fetcher.gem "a", 2 do |s| + fetcher.download "a", 2 do |s| s.executables = %w[foo] s.files = %w[bin/foo lib/a.rb] @@ -455,9 +475,90 @@ class TestGemCommandsExecCommand < Gem::TestCase end use_ui @ui do - @cmd.invoke "--verbose", "--conservative", "a" + invoke "--verbose", "--conservative", "a" assert_not_include @ui.output, "a (>= 0) not available locally" assert_include @ui.output, "a-1 foo" + assert_empty @installed_specs.map(&:original_name) + end + end + + def test_uses_newest_version + spec_fetcher do |fetcher| + fetcher.download "a", 1 do |s| + s.executables = %w[foo] + s.files = %w[bin/foo lib/a.rb] + + write_file File.join(*%W[gems #{s.original_name} bin foo]) do |f| + f << "Gem.ui.say #{s.original_name.dump} + ' ' + File.basename(__FILE__)" + end + end + end + + use_ui @ui do + invoke "a" + assert_include @ui.output, "a-1 foo" + end + + spec_fetcher do |fetcher| + fetcher.download "a", 1 do |s| + s.executables = %w[foo] + s.files = %w[bin/foo lib/a.rb] + + write_file File.join(*%W[gems #{s.original_name} bin foo]) do |f| + f << "Gem.ui.say #{s.original_name.dump} + ' ' + File.basename(__FILE__)" + end + end + + fetcher.download "a", 2 do |s| + s.executables = %w[foo] + s.files = %w[bin/foo lib/a.rb] + + write_file File.join(*%W[gems #{s.original_name} bin foo]) do |f| + f << "Gem.ui.say #{s.original_name.dump} + ' ' + File.basename(__FILE__)" + end + end + end + + use_ui @ui do + invoke "--verbose", "a:2" + refute_predicate @ui, :terminated? + assert_empty @ui.error + assert_include @ui.output, "a-2 foo" + assert_equal %w[a-2], @installed_specs.map(&:original_name) + end + end + + def test_uses_newest_version_of_dependency + spec_fetcher do |fetcher| + fetcher.gem "a", 1 do |s| + s.executables = %w[] + s.files = %w[lib/a.rb] + s.add_runtime_dependency "b" + end + + fetcher.gem "b", 1 do |s| + s.executables = %w[a] + s.files = %w[bin/a lib/a.rb] + + write_file File.join(*%W[gems #{s.original_name} bin a]) do |f| + f << "Gem.ui.say #{s.original_name.dump} + ' ' + File.basename(__FILE__)" + end + end + + fetcher.download "b", 2 do |s| + s.executables = %w[a] + s.files = %w[bin/a lib/a.rb] + + write_file File.join(*%W[gems #{s.original_name} bin a]) do |f| + f << "Gem.ui.say #{s.original_name.dump} + ' ' + File.basename(__FILE__)" + end + end + end + + use_ui @ui do + invoke "a" + assert_include @ui.output, "b-2 a" + assert_equal %w[b-2], @installed_specs.map(&:original_name) end end end -- cgit v1.2.3