Skip to content

Commit

Permalink
[rubygems/rubygems] add global flag (-C) to change execution directory
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavothecoder authored and matzbot committed Dec 26, 2022
1 parent 28a1743 commit 08f6196
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
6 changes: 5 additions & 1 deletion lib/rubygems/command.rb
Expand Up @@ -630,7 +630,11 @@ def wrap(text, width) # :doc:
Usage:
gem -h/--help
gem -v/--version
gem command [arguments...] [options...]
gem [global options...] command [arguments...] [options...]
Global options:
-C PATH run as if gem was started in <PATH>
instead of the current working directory
Examples:
gem install rake
Expand Down
21 changes: 17 additions & 4 deletions lib/rubygems/command_manager.rb
Expand Up @@ -175,14 +175,20 @@ def process_args(args, build_args=nil)
when "-v", "--version" then
say Gem::VERSION
terminate_interaction 0
when "-C" then
args.shift
start_point = args.shift
if Dir.exist?(start_point)
Dir.chdir(start_point) { invoke_command(args, build_args) }
else
alert_error clean_text("#{start_point} isn't a directory.")
terminate_interaction 1
end
when /^-/ then
alert_error clean_text("Invalid option: #{args.first}. See 'gem --help'.")
terminate_interaction 1
else
cmd_name = args.shift.downcase
cmd = find_command cmd_name
cmd.deprecation_warning if cmd.deprecated?
cmd.invoke_with_build_args args, build_args
invoke_command(args, build_args)
end
end

Expand Down Expand Up @@ -237,4 +243,11 @@ def load_and_instantiate(command_name)
ui.backtrace e
end
end

def invoke_command(args, build_args)
cmd_name = args.shift.downcase
cmd = find_command cmd_name
cmd.deprecation_warning if cmd.deprecated?
cmd.invoke_with_build_args args, build_args
end
end
40 changes: 40 additions & 0 deletions test/rubygems/test_gem_command_manager.rb
Expand Up @@ -126,6 +126,46 @@ def test_run_crash_command
@command_manager.unregister_command :crash
end

def test_process_args_with_c_flag
custom_start_point = File.join @tempdir, "nice_folder"
FileUtils.mkdir_p custom_start_point

execution_path = nil
use_ui @ui do
@command_manager[:install].when_invoked do
execution_path = Dir.pwd
true
end
@command_manager.process_args %W[-C #{custom_start_point} install net-scp-4.0.0.gem --local]
end

assert_equal custom_start_point, execution_path
end

def test_process_args_with_c_flag_without_path
use_ui @ui do
assert_raise Gem::MockGemUi::TermError do
@command_manager.process_args %w[-C install net-scp-4.0.0.gem --local]
end
end

assert_match(/install isn't a directory./i, @ui.error)
end

def test_process_args_with_c_flag_path_not_found
custom_start_point = File.join @tempdir, "nice_folder"
FileUtils.mkdir_p custom_start_point
custom_start_point.tr!("_", "-")

use_ui @ui do
assert_raise Gem::MockGemUi::TermError do
@command_manager.process_args %W[-C #{custom_start_point} install net-scp-4.0.0.gem --local]
end
end

assert_match(/#{custom_start_point} isn't a directory./i, @ui.error)
end

def test_process_args_bad_arg
use_ui @ui do
assert_raise Gem::MockGemUi::TermError do
Expand Down

0 comments on commit 08f6196

Please sign in to comment.