2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2021-06-01 23:32:47 -04:00
|
|
|
require_relative 'helper'
|
2007-11-10 02:48:56 -05:00
|
|
|
require 'rubygems/command_manager'
|
|
|
|
|
2011-01-28 18:46:47 -05:00
|
|
|
class TestGemCommandManager < Gem::TestCase
|
2019-11-11 01:03:57 -05:00
|
|
|
PROJECT_DIR = File.expand_path('../../..', __FILE__).tap(&Gem::UNTAINT)
|
2019-07-24 00:24:18 -04:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def setup
|
|
|
|
super
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
@command_manager = Gem::CommandManager.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_command
|
|
|
|
command = @command_manager.find_command 'install'
|
|
|
|
|
|
|
|
assert_kind_of Gem::Commands::InstallCommand, command
|
|
|
|
|
|
|
|
command = @command_manager.find_command 'ins'
|
|
|
|
|
|
|
|
assert_kind_of Gem::Commands::InstallCommand, command
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_command_ambiguous
|
2021-05-10 23:25:46 -04:00
|
|
|
e = assert_raise Gem::CommandLineError do
|
2012-11-29 01:52:18 -05:00
|
|
|
@command_manager.find_command 'u'
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 'Ambiguous command u matches [uninstall, unpack, update]',
|
|
|
|
e.message
|
|
|
|
end
|
|
|
|
|
2018-08-27 06:05:04 -04:00
|
|
|
def test_find_alias_command
|
|
|
|
command = @command_manager.find_command 'i'
|
|
|
|
|
|
|
|
assert_kind_of Gem::Commands::InstallCommand, command
|
|
|
|
end
|
|
|
|
|
2021-12-04 02:25:43 -05:00
|
|
|
def test_find_login_alias_command
|
|
|
|
command = @command_manager.find_command 'login'
|
|
|
|
|
|
|
|
assert_kind_of Gem::Commands::SigninCommand, command
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_logout_alias_comamnd
|
|
|
|
command = @command_manager.find_command 'logout'
|
|
|
|
|
|
|
|
assert_kind_of Gem::Commands::SignoutCommand, command
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def test_find_command_ambiguous_exact
|
|
|
|
ins_command = Class.new
|
|
|
|
Gem::Commands.send :const_set, :InsCommand, ins_command
|
|
|
|
|
|
|
|
@command_manager.register_command :ins
|
|
|
|
|
|
|
|
command = @command_manager.find_command 'ins'
|
|
|
|
|
|
|
|
assert_kind_of ins_command, command
|
|
|
|
ensure
|
|
|
|
Gem::Commands.send :remove_const, :InsCommand
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_command_unknown
|
2021-05-10 23:25:46 -04:00
|
|
|
e = assert_raise Gem::UnknownCommandError do
|
2012-11-29 01:52:18 -05:00
|
|
|
@command_manager.find_command 'xyz'
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 'Unknown command xyz', e.message
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2021-01-03 20:09:05 -05:00
|
|
|
def test_find_command_unknown_suggestions
|
2021-05-10 23:25:46 -04:00
|
|
|
e = assert_raise Gem::UnknownCommandError do
|
2021-01-03 20:09:05 -05:00
|
|
|
@command_manager.find_command 'pish'
|
|
|
|
end
|
|
|
|
|
|
|
|
message = 'Unknown command pish'.dup
|
|
|
|
|
|
|
|
if RUBY_VERSION >= "2.4" && defined?(DidYouMean::SPELL_CHECKERS) && defined?(DidYouMean::Correctable)
|
|
|
|
message << "\nDid you mean? \"push\""
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal message, e.message
|
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def test_run_interrupt
|
2011-01-18 19:08:49 -05:00
|
|
|
old_load_path = $:.dup
|
2019-07-24 00:57:59 -04:00
|
|
|
$: << File.expand_path("test/rubygems", PROJECT_DIR)
|
2011-01-18 19:08:49 -05:00
|
|
|
Gem.load_env_plugins
|
2010-04-22 04:24:42 -04:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
@command_manager.register_command :interrupt
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
use_ui @ui do
|
2021-05-10 23:25:46 -04:00
|
|
|
assert_raise Gem::MockGemUi::TermError do
|
2013-09-14 04:59:02 -04:00
|
|
|
@command_manager.run %w[interrupt]
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
assert_equal '', ui.output
|
|
|
|
assert_equal "ERROR: Interrupted\n", ui.error
|
|
|
|
end
|
2011-01-18 19:08:49 -05:00
|
|
|
ensure
|
|
|
|
$:.replace old_load_path
|
2011-05-31 23:45:05 -04:00
|
|
|
Gem::CommandManager.reset
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2010-04-22 04:24:42 -04:00
|
|
|
def test_run_crash_command
|
2011-01-18 19:08:49 -05:00
|
|
|
old_load_path = $:.dup
|
2019-07-24 00:57:59 -04:00
|
|
|
$: << File.expand_path("test/rubygems", PROJECT_DIR)
|
2011-01-18 19:08:49 -05:00
|
|
|
|
2010-04-22 04:24:42 -04:00
|
|
|
@command_manager.register_command :crash
|
|
|
|
use_ui @ui do
|
2021-05-10 23:25:46 -04:00
|
|
|
assert_raise Gem::MockGemUi::TermError do
|
2013-09-14 04:59:02 -04:00
|
|
|
@command_manager.run %w[crash]
|
2010-04-22 04:24:42 -04:00
|
|
|
end
|
|
|
|
assert_equal '', ui.output
|
|
|
|
err = ui.error.split("\n").first
|
|
|
|
assert_equal "ERROR: Loading command: crash (RuntimeError)", err
|
|
|
|
end
|
2011-01-18 19:08:49 -05:00
|
|
|
ensure
|
|
|
|
$:.replace old_load_path
|
2011-05-31 23:45:05 -04:00
|
|
|
@command_manager.unregister_command :crash
|
2010-04-22 04:24:42 -04:00
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def test_process_args_bad_arg
|
|
|
|
use_ui @ui do
|
2021-05-10 23:25:46 -04:00
|
|
|
assert_raise Gem::MockGemUi::TermError do
|
2013-09-14 04:59:02 -04:00
|
|
|
@command_manager.process_args %w[--bad-arg]
|
2011-01-28 18:46:47 -05:00
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_match(/invalid option: --bad-arg/i, @ui.error)
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
# HACK move to install command test
|
2007-11-10 02:48:56 -05:00
|
|
|
def test_process_args_install
|
|
|
|
#capture all install options
|
|
|
|
use_ui @ui do
|
|
|
|
check_options = nil
|
|
|
|
@command_manager['install'].when_invoked do |options|
|
|
|
|
check_options = options
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
#check defaults
|
2013-09-14 04:59:02 -04:00
|
|
|
@command_manager.process_args %w[install]
|
2012-11-29 01:52:18 -05:00
|
|
|
assert_equal %w[ri], check_options[:document].sort
|
2007-11-10 02:48:56 -05:00
|
|
|
assert_equal false, check_options[:force]
|
|
|
|
assert_equal :both, check_options[:domain]
|
|
|
|
assert_equal true, check_options[:wrappers]
|
|
|
|
assert_equal Gem::Requirement.default, check_options[:version]
|
2018-05-30 09:01:35 -04:00
|
|
|
assert_nil check_options[:install_dir]
|
|
|
|
assert_nil check_options[:bin_dir]
|
2007-11-10 02:48:56 -05:00
|
|
|
|
|
|
|
#check settings
|
|
|
|
check_options = nil
|
2013-09-14 04:59:02 -04:00
|
|
|
@command_manager.process_args %w[
|
2018-10-21 20:27:02 -04:00
|
|
|
install --force --local --document=ri,rdoc --install-dir .
|
2019-06-04 04:40:30 -04:00
|
|
|
--version 3.0 --no-wrapper --bindir .
|
2013-09-14 04:59:02 -04:00
|
|
|
]
|
2012-11-29 01:52:18 -05:00
|
|
|
assert_equal %w[rdoc ri], check_options[:document].sort
|
2007-11-10 02:48:56 -05:00
|
|
|
assert_equal true, check_options[:force]
|
|
|
|
assert_equal :local, check_options[:domain]
|
|
|
|
assert_equal false, check_options[:wrappers]
|
|
|
|
assert_equal Gem::Requirement.new('3.0'), check_options[:version]
|
|
|
|
assert_equal Dir.pwd, check_options[:install_dir]
|
2008-03-31 18:40:06 -04:00
|
|
|
assert_equal Dir.pwd, check_options[:bin_dir]
|
2007-11-10 02:48:56 -05:00
|
|
|
|
|
|
|
#check remote domain
|
|
|
|
check_options = nil
|
2013-09-14 04:59:02 -04:00
|
|
|
@command_manager.process_args %w[install --remote]
|
2007-11-10 02:48:56 -05:00
|
|
|
assert_equal :remote, check_options[:domain]
|
|
|
|
|
|
|
|
#check both domain
|
|
|
|
check_options = nil
|
2013-09-14 04:59:02 -04:00
|
|
|
@command_manager.process_args %w[install --both]
|
2007-11-10 02:48:56 -05:00
|
|
|
assert_equal :both, check_options[:domain]
|
|
|
|
|
|
|
|
#check both domain
|
|
|
|
check_options = nil
|
2013-09-14 04:59:02 -04:00
|
|
|
@command_manager.process_args %w[install --both]
|
2007-11-10 02:48:56 -05:00
|
|
|
assert_equal :both, check_options[:domain]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
# HACK move to uninstall command test
|
2007-11-10 02:48:56 -05:00
|
|
|
def test_process_args_uninstall
|
|
|
|
#capture all uninstall options
|
|
|
|
check_options = nil
|
|
|
|
@command_manager['uninstall'].when_invoked do |options|
|
|
|
|
check_options = options
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
#check defaults
|
2013-09-14 04:59:02 -04:00
|
|
|
@command_manager.process_args %w[uninstall]
|
2007-11-10 02:48:56 -05:00
|
|
|
assert_equal Gem::Requirement.default, check_options[:version]
|
|
|
|
|
|
|
|
#check settings
|
|
|
|
check_options = nil
|
2013-09-14 04:59:02 -04:00
|
|
|
@command_manager.process_args %w[uninstall foobar --version 3.0]
|
2007-11-10 02:48:56 -05:00
|
|
|
assert_equal "foobar", check_options[:args].first
|
|
|
|
assert_equal Gem::Requirement.new('3.0'), check_options[:version]
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
# HACK move to check command test
|
2007-11-10 02:48:56 -05:00
|
|
|
def test_process_args_check
|
|
|
|
#capture all check options
|
|
|
|
check_options = nil
|
|
|
|
@command_manager['check'].when_invoked do |options|
|
|
|
|
check_options = options
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
#check defaults
|
2013-09-14 04:59:02 -04:00
|
|
|
@command_manager.process_args %w[check]
|
2012-11-29 01:52:18 -05:00
|
|
|
assert_equal true, check_options[:alien]
|
2007-11-10 02:48:56 -05:00
|
|
|
|
|
|
|
#check settings
|
|
|
|
check_options = nil
|
2013-09-14 04:59:02 -04:00
|
|
|
@command_manager.process_args %w[check foobar --alien]
|
2007-11-10 02:48:56 -05:00
|
|
|
assert_equal true, check_options[:alien]
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
# HACK move to build command test
|
2007-11-10 02:48:56 -05:00
|
|
|
def test_process_args_build
|
|
|
|
#capture all build options
|
|
|
|
check_options = nil
|
|
|
|
@command_manager['build'].when_invoked do |options|
|
|
|
|
check_options = options
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
#check defaults
|
2013-09-14 04:59:02 -04:00
|
|
|
@command_manager.process_args %w[build]
|
2007-11-10 02:48:56 -05:00
|
|
|
#NOTE: Currently no defaults
|
|
|
|
|
|
|
|
#check settings
|
|
|
|
check_options = nil
|
2013-09-14 04:59:02 -04:00
|
|
|
@command_manager.process_args %w[build foobar.rb]
|
2007-11-10 02:48:56 -05:00
|
|
|
assert_equal 'foobar.rb', check_options[:args].first
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
# HACK move to query command test
|
2007-11-10 02:48:56 -05:00
|
|
|
def test_process_args_query
|
|
|
|
#capture all query options
|
|
|
|
check_options = nil
|
|
|
|
@command_manager['query'].when_invoked do |options|
|
|
|
|
check_options = options
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
#check defaults
|
2020-01-31 21:14:04 -05:00
|
|
|
Gem::Deprecate.skip_during do
|
|
|
|
@command_manager.process_args %w[query]
|
|
|
|
end
|
2008-03-31 18:40:06 -04:00
|
|
|
assert_equal(//, check_options[:name])
|
2007-11-10 02:48:56 -05:00
|
|
|
assert_equal :local, check_options[:domain]
|
|
|
|
assert_equal false, check_options[:details]
|
|
|
|
|
|
|
|
#check settings
|
|
|
|
check_options = nil
|
2020-01-31 21:14:04 -05:00
|
|
|
Gem::Deprecate.skip_during do
|
|
|
|
@command_manager.process_args %w[query --name foobar --local --details]
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
assert_equal(/foobar/i, check_options[:name])
|
|
|
|
assert_equal :local, check_options[:domain]
|
|
|
|
assert_equal true, check_options[:details]
|
|
|
|
|
|
|
|
#remote domain
|
|
|
|
check_options = nil
|
2020-01-31 21:14:04 -05:00
|
|
|
Gem::Deprecate.skip_during do
|
|
|
|
@command_manager.process_args %w[query --remote]
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
assert_equal :remote, check_options[:domain]
|
|
|
|
|
|
|
|
#both (local/remote) domains
|
|
|
|
check_options = nil
|
2020-01-31 21:14:04 -05:00
|
|
|
Gem::Deprecate.skip_during do
|
|
|
|
@command_manager.process_args %w[query --both]
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
assert_equal :both, check_options[:domain]
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
# HACK move to update command test
|
2007-11-10 02:48:56 -05:00
|
|
|
def test_process_args_update
|
|
|
|
#capture all update options
|
|
|
|
check_options = nil
|
|
|
|
@command_manager['update'].when_invoked do |options|
|
|
|
|
check_options = options
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
#check defaults
|
2013-09-14 04:59:02 -04:00
|
|
|
@command_manager.process_args %w[update]
|
2021-01-03 20:09:05 -05:00
|
|
|
assert_includes check_options[:document], 'ri'
|
2007-11-10 02:48:56 -05:00
|
|
|
|
|
|
|
#check settings
|
|
|
|
check_options = nil
|
2018-10-21 20:27:02 -04:00
|
|
|
@command_manager.process_args %w[update --force --document=ri --install-dir .]
|
2012-11-29 01:52:18 -05:00
|
|
|
assert_includes check_options[:document], 'ri'
|
2007-11-10 02:48:56 -05:00
|
|
|
assert_equal true, check_options[:force]
|
|
|
|
assert_equal Dir.pwd, check_options[:install_dir]
|
|
|
|
end
|
|
|
|
|
2020-01-31 21:14:04 -05:00
|
|
|
def test_deprecated_command
|
|
|
|
require 'rubygems/command'
|
|
|
|
foo_command = Class.new(Gem::Command) do
|
|
|
|
extend Gem::Deprecate
|
|
|
|
|
2020-04-19 09:41:54 -04:00
|
|
|
rubygems_deprecate_command
|
2020-01-31 21:14:04 -05:00
|
|
|
|
|
|
|
def execute
|
|
|
|
say "pew pew!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Gem::Commands.send(:const_set, :FooCommand, foo_command)
|
|
|
|
@command_manager.register_command(:foo, foo_command.new("foo"))
|
|
|
|
|
|
|
|
use_ui @ui do
|
|
|
|
@command_manager.process_args(%w[foo])
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "pew pew!\n", @ui.output
|
2020-03-28 20:11:40 -04:00
|
|
|
assert_match(/WARNING: foo command is deprecated. It will be removed in Rubygems [0-9]+/, @ui.error)
|
2020-01-31 21:14:04 -05:00
|
|
|
ensure
|
|
|
|
Gem::Commands.send(:remove_const, :FooCommand)
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|