summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Giddins <segiddins@segiddins.me>2023-02-13 17:22:54 -0800
committergit <svn-admin@ruby-lang.org>2023-02-22 08:42:17 +0000
commit47d4f73ee755a0ae7b26eb2e35b820202e008763 (patch)
tree3b7b6f762ed7379cc84b2c3347dfb81a1ed07faa
parent612ebd10c061fefd6e4f1f736a95188bd6ae5146 (diff)
[rubygems/rubygems] Remove platform option for gem exec
Also fix native extensions that load dependencies https://github.com/rubygems/rubygems/commit/a06f9870c7
-rw-r--r--lib/rubygems/commands/exec_command.rb30
-rw-r--r--test/rubygems/test_gem_commands_exec_command.rb148
2 files changed, 170 insertions, 8 deletions
diff --git a/lib/rubygems/commands/exec_command.rb b/lib/rubygems/commands/exec_command.rb
index be76980410..b525d7fa2b 100644
--- a/lib/rubygems/commands/exec_command.rb
+++ b/lib/rubygems/commands/exec_command.rb
@@ -1,5 +1,6 @@
# frozen_string_literal: true
require_relative "../command"
+require_relative "../dependency_installer"
require_relative "../package"
require_relative "../version_option"
@@ -11,7 +12,6 @@ class Gem::Commands::ExecCommand < Gem::Command
version: Gem::Requirement.default,
}
- add_platform_option
add_version_option
add_prerelease_option "to be installed"
@@ -55,6 +55,8 @@ to the same gem path as user-installed gems.
end
def execute
+ gem_paths = { "GEM_HOME": Gem.paths.home, "GEM_PATH": Gem.paths.path, "GEM_SPEC_CACHE": Gem.paths.spec_cache_dir }
+
check_executable
print_command
@@ -66,6 +68,8 @@ to the same gem path as user-installed gems.
end
load!
+ ensure
+ Gem.paths = gem_paths
end
private
@@ -127,7 +131,6 @@ 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
@@ -137,11 +140,30 @@ to the same gem path as user-installed gems.
gem_version = options[:version]
home = File.join(Gem.dir, "gem_exec")
- Gem.use_paths(home, [home] + Gem.path)
+
+ ENV["GEM_PATH"] = ([home] + Gem.path).join(Gem.path_separator)
+ ENV["GEM_HOME"] = home
+ Gem.clear_paths
+
+ install_options = options.merge(
+ minimal_deps: false,
+ wrappers: true
+ )
suppress_always_install do
- Gem.install(gem_name, gem_version, minimal_deps: false)
+ dep_installer = Gem::DependencyInstaller.new install_options
+
+ request_set = dep_installer.resolve_dependencies gem_name, gem_version
+
+ verbose "Gems to install:"
+ request_set.sorted_requests.each do |activation_request|
+ verbose "\t#{activation_request.full_name}"
+ end
+
+ request_set.install install_options
end
+
+ Gem::Specification.reset
rescue Gem::InstallError => e
alert_error "Error installing #{gem_name}:\n\t#{e.message}"
terminate_interaction 1
diff --git a/test/rubygems/test_gem_commands_exec_command.rb b/test/rubygems/test_gem_commands_exec_command.rb
index f9a37b40fb..5f988d5b5c 100644
--- a/test/rubygems/test_gem_commands_exec_command.rb
+++ b/test/rubygems/test_gem_commands_exec_command.rb
@@ -15,6 +15,7 @@ class TestGemCommandsExecCommand < Gem::TestCase
@gem_home = Gem.dir
@gem_path = Gem.path
+ @test_arch = RbConfig::CONFIG["arch"]
@installed_specs = []
Gem.post_install {|installer| @installed_specs << installer.spec }
@@ -138,6 +139,147 @@ class TestGemCommandsExecCommand < Gem::TestCase
end
end
+ def test_gem_with_platforms
+ spec_fetcher do |fetcher|
+ fetcher.download "a", 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}"
+ end
+ end
+
+ fetcher.download "a", 2 do |s|
+ s.executables = %w[a]
+ s.files = %w[bin/a lib/a.rb]
+ s.platform = "x86_64-darwin"
+
+ write_file File.join(*%W[gems #{s.original_name} bin a]) do |f|
+ f << "Gem.ui.say #{s.original_name.dump}"
+ end
+ end
+ end
+
+ use_ui @ui do
+ invoke "a:2"
+ assert_equal "a-2\n", @ui.output
+ end
+
+ use_ui @ui do
+ util_set_arch "x86_64-darwin-18"
+ invoke "a:2"
+ assert_equal "a-2-x86_64-darwin\n", @ui.output
+ end
+ end
+
+ def test_gem_with_platform_dependencies
+ platforms = Gem.platforms.dup
+
+ spec_fetcher do |fetcher|
+ fetcher.download "a", 2 do |s|
+ s.executables = %w[a]
+ s.files = %w[bin/a lib/a.rb]
+ s.add_runtime_dependency "with_platform"
+
+ write_file File.join(*%W[gems #{s.original_name} bin a]) do |f|
+ f << 'require "with_platform"' << "\n"
+ f << 'Gem.ui.say Gem.loaded_specs.each_value.map(&:original_name).sort.join("\n")'
+ end
+ end
+
+ fetcher.download "with_platform", 2 do |s|
+ s.files = %w[lib/with_platform.rb]
+ s.platform = Gem::Platform.local
+ end
+
+ fetcher.download "with_platform", 2 do |s|
+ s.files = %w[lib/with_platform.rb]
+ end
+ end
+
+ use_ui @ui do
+ util_set_arch "unknown-unknown"
+ invoke "a"
+ assert_equal "a-2\nwith_platform-2\n", @ui.output
+ end
+
+ use_ui @ui do
+ util_set_arch @test_arch
+ invoke "a"
+ assert_empty @ui.error
+ assert_equal "a-2\nwith_platform-2-#{Gem::Platform.local}\n", @ui.output
+ end
+ end
+
+ def test_gem_with_platform_and_platform_dependencies
+ platforms = Gem.platforms.dup
+
+ spec_fetcher do |fetcher|
+ fetcher.download "a", 2 do |s|
+ s.executables = %w[a]
+ s.files = %w[bin/a lib/a.rb]
+ s.add_runtime_dependency "with_platform"
+ s.platform = Gem::Platform.local.to_s
+
+ write_file File.join(*%W[gems #{s.original_name} bin a]) do |f|
+ f << 'require "with_platform"' << "\n"
+ f << 'Gem.ui.say Gem.loaded_specs.each_value.map(&:original_name).sort.join("\n")'
+ end
+ end
+
+ fetcher.download "a", 2 do |s|
+ s.executables = %w[a]
+ s.files = %w[bin/a lib/a.rb extconf.rb]
+ s.add_runtime_dependency "with_platform"
+
+ write_file File.join(*%W[gems #{s.original_name} bin a]) do |f|
+ f << 'require "with_platform"' << "\n"
+ f << 'Gem.ui.say Gem.loaded_specs.each_value.map(&:original_name).sort.join("\n")'
+ end
+
+ s.extensions = %w[extconf.rb]
+ write_file File.join(*%W[gems #{s.original_name} extconf.rb]) do |f|
+ f.write <<-RUBY
+ gem('with_platform', '~> 2.0')
+ require 'with_platform'
+ gem 'sometimes_used'
+ require 'sometimes_used'
+ require "mkmf"
+ create_makefile("#{s.name}")
+ RUBY
+ end
+ end
+
+ fetcher.download "with_platform", 2 do |s|
+ s.files = %w[lib/with_platform.rb]
+ s.platform = Gem::Platform.local.to_s
+ end
+
+ fetcher.download "with_platform", 2 do |s|
+ s.files = %w[lib/with_platform.rb]
+ s.add_runtime_dependency "sometimes_used"
+ end
+
+ fetcher.download "sometimes_used", 2 do |s|
+ s.files = %w[lib/sometimes_used.rb]
+ end
+ end
+
+ use_ui @ui do
+ util_set_arch "unknown-unknown"
+ invoke "a"
+ assert_equal "Building native extensions. This could take a while...\na-2\nsometimes_used-2\nwith_platform-2\n", @ui.output
+ end
+
+ use_ui @ui do
+ util_set_arch @test_arch
+ invoke "a"
+ assert_empty @ui.error
+ assert_equal "a-2-#{Gem::Platform.local}\nwith_platform-2-#{Gem::Platform.local}\n", @ui.output
+ end
+ end
+
def test_gem_with_other_executable_name
spec_fetcher do |fetcher|
fetcher.gem "a", 2 do |s|
@@ -382,7 +524,7 @@ class TestGemCommandsExecCommand < Gem::TestCase
def test_pre_version_option
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]
@@ -390,7 +532,7 @@ class TestGemCommandsExecCommand < Gem::TestCase
f << "Gem.ui.say #{s.original_name.dump} + ' ' + File.basename(__FILE__)"
end
end
- fetcher.gem "a", "1.1.a" do |s|
+ fetcher.download "a", "1.1.a" do |s|
s.executables = %w[foo ]
s.files = %w[bin/foo lib/a.rb]
@@ -400,8 +542,6 @@ class TestGemCommandsExecCommand < Gem::TestCase
end
end
- util_clear_gems
-
use_ui @ui do
@cmd.invoke "-v", ">= 0.a", "a"
assert_equal "a-1.1.a foo\n", @ui.output