1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
|
# frozen_string_literal: true
require 'spec_helper'
describe Commander do
include Commander::Methods
before :each do
$stderr = StringIO.new
mock_terminal
create_test_command
end
describe '#program' do
it 'should set / get program information' do
program :name, 'test'
expect(program(:name)).to eq('test')
end
it 'should allow arbitrary blocks of global help documentation' do
program :help, 'Copyright', 'TJ Holowaychuk'
expect(program(:help)['Copyright']).to eq('TJ Holowaychuk')
end
it 'should raise an error when required info has not been set' do
new_command_runner '--help'
program :version, ''
expect { run! }.to raise_error(Commander::Runner::CommandError)
end
it 'should allow aliases of help formatters' do
program :help_formatter, :compact
expect(program(:help_formatter)).to eq(Commander::HelpFormatter::TerminalCompact)
end
end
describe '#command' do
it 'should return a command instance when only the name is passed' do
expect(command(:test)).to be_instance_of(Commander::Command)
end
it 'should return nil when the command does not exist' do
expect(command(:im_not_real)).to be_nil
end
end
describe '#separate_switches_from_description' do
it 'should seperate switches and description returning both' do
switches, description = *Commander::Runner.separate_switches_from_description('-h', '--help', 'display help')
expect(switches).to eq(['-h', '--help'])
expect(description).to eq('display help')
end
end
describe '#switch_to_sym' do
it 'should return a symbol based on the switch name' do
expect(Commander::Runner.switch_to_sym('--trace')).to eq(:trace)
expect(Commander::Runner.switch_to_sym('--foo-bar')).to eq(:foo_bar)
expect(Commander::Runner.switch_to_sym('--[no-]feature"')).to eq(:feature)
expect(Commander::Runner.switch_to_sym('--[no-]feature ARG')).to eq(:feature)
expect(Commander::Runner.switch_to_sym('--file [ARG]')).to eq(:file)
expect(Commander::Runner.switch_to_sym('--colors colors')).to eq(:colors)
end
end
describe '#alias_command' do
it 'should alias a command' do
alias_command :foo, :test
expect(command(:foo)).to eq(command(:test))
end
it 'should pass arguments passed to the alias when called' do
gem_name = ''
new_command_runner 'install', 'gem', 'commander' do
command :install do |c|
c.option '--gem-name NAME', 'Install a gem'
c.when_called { |_, options| gem_name = options.gem_name }
end
alias_command :'install gem', :install, '--gem-name'
end.run!
expect(gem_name).to eq('commander')
end
end
describe '#global_option' do
it 'should be invoked when used in the args list' do
file = ''
new_command_runner 'test', '--config', 'foo' do
global_option('--config FILE') { |f| file = f }
end.run!
expect(file).to eq('foo')
end
it 'should be inherited by commands' do
quiet = nil
new_command_runner 'foo', '--quiet' do
global_option('--quiet', 'Suppress output')
command :foo do |c|
c.when_called { |_, options| quiet = options.quiet }
end
end.run!
expect(quiet).to be true
end
it 'should be inherited by commands when provided before the command name' do
option = nil
new_command_runner '--global-option', 'option-value', 'command_name' do
global_option('--global-option=GLOBAL', 'A global option')
command :command_name do |c|
c.when_called { |_, options| option = options.global_option }
end
end.run!
expect(option).to eq('option-value')
end
it 'should be inherited by commands even when a block is present' do
quiet = nil
new_command_runner 'foo', '--quiet' do
global_option('--quiet', 'Suppress output') {}
command :foo do |c|
c.when_called { |_, options| quiet = options.quiet }
end
end.run!
expect(quiet).to be true
end
it 'should be inherited by commands when the positive form of a [no-] option' do
quiet = nil
new_command_runner 'foo', '--quiet' do
global_option('--[no-]quiet', 'Suppress output') {}
command :foo do |c|
c.when_called { |_, options| quiet = options.quiet }
end
end.run!
expect(quiet).to be true
end
it 'should be inherited by commands when the negative form of a [no-] option' do
quiet = nil
new_command_runner 'foo', '--no-quiet' do
global_option('--[no-]quiet', 'Suppress output') {}
command :foo do |c|
c.when_called { |_, options| quiet = options.quiet }
end
end.run!
expect(quiet).to be false
end
it 'should allow command arguments before the global option' do
config = nil
args = nil
new_command_runner 'foo', '--config', 'config-value', 'arg1', 'arg2' do
global_option('-c', '--config CONFIG', String)
command :foo do |c|
c.when_called do |arguments, options|
options.default(config: 'default')
args = arguments
config = options.config
end
end
end.run!
expect(config).to eq('config-value')
expect(args).to eq(%w(arg1 arg2))
end
it 'should allow command arguments after the global option' do
config = nil
args = nil
new_command_runner 'foo', 'arg1', 'arg2', '--config', 'config-value' do
global_option('-c', '--config CONFIG', String)
command :foo do |c|
c.when_called do |arguments, options|
options.default(config: 'default')
args = arguments
config = options.config
end
end
end.run!
expect(config).to eq('config-value')
expect(args).to eq(%w(arg1 arg2))
end
it 'allows global options in the form option=value' do
config = nil
args = nil
new_command_runner 'test', 'arg1', '--config=config-value', 'arg2' do
global_option('-c', '--config CONFIG', String)
command :test do |c|
c.when_called do |arguments, options|
options.default(config: 'default')
args = arguments
config = options.config
end
end
end.run!
expect(config).to eq('config-value')
expect(args).to eq(%w[arg1 arg2])
end
end
describe '#parse_global_options' do
it 'should parse global options before command' do
global_option = nil
new_command_runner('--testing-global', 'foo') do
global_option('--testing-global') { global_option = 'MAGIC' }
command :foo do |c|
c.when_called {}
end
end.run!
expect(global_option).to eq('MAGIC')
end
it 'should parse global options after command' do
global_option = nil
new_command_runner('foo', '--testing-global') do
global_option('--testing-global') { global_option = 'MAGIC' }
command :foo do |c|
c.when_called {}
end
end.run!
expect(global_option).to eq('MAGIC')
end
it 'should parse global options placed before command options' do
global_option = nil
new_command_runner('foo', '--testing-global', '--testing-command') do
global_option('--testing-global') { global_option = 'MAGIC' }
command :foo do |c|
c.option('--testing-command') {}
c.when_called {}
end
end.run!
expect(global_option).to eq('MAGIC')
end
it 'should parse global options placed after command options' do
global_option = nil
new_command_runner('foo', '--testing-command', '--testing-global') do
global_option('--testing-global') { global_option = 'MAGIC' }
command :foo do |c|
c.option('--testing-command') {}
c.when_called {}
end
end.run!
expect(global_option).to eq('MAGIC')
end
it 'should parse global options surrounded by command options' do
global_option = nil
new_command_runner('foo', '--testing-command', '--testing-global', '--other-command') do
global_option('--testing-global') { global_option = 'MAGIC' }
command :foo do |c|
c.option('--testing-command') {}
c.option('--other-command') {}
c.when_called {}
end
end.run!
expect(global_option).to eq('MAGIC')
end
it 'should not parse command options' do
global_option = nil
command_option = nil
new_command_runner('foo', '--testing-command', '--testing-global') do
global_option('--testing-global') { global_option = 'MAGIC' }
command :foo do |c|
c.option('--testing-command') { command_option = 'NO!' }
c.when_called {}
end
end.parse_global_options
expect(command_option).to be_nil
expect(global_option).to eq('MAGIC')
end
it 'should not affect command arguments with values' do
global_option = nil
command_option = nil
new_command_runner('foo', '--testing-command', 'bar', '--testing-global') do
global_option('--testing-global') { global_option = 'MAGIC' }
command :foo do |c|
c.option('--testing-command VALUE') { |v| command_option = v }
c.when_called {}
end
end.run!
expect(command_option).to eq('bar')
expect(global_option).to eq('MAGIC')
end
it 'should not affect global arguments with values' do
global_option = nil
new_command_runner('foo', '--testing-command', '--testing-global', 'bar') do
global_option('--testing-global VALUE') { |v| global_option = v }
command :foo do |c|
c.option('--testing-command') {}
c.when_called {}
end
end.run!
expect(global_option).to eq('bar')
end
it 'should allow global arguments with values before command arguments (github issue #8)' do
global_option = nil
command_option = nil
new_command_runner('foo', '--config', 'path', 'bar') do
global_option('--config VALUE') { |v| global_option = v }
command :foo do |c|
c.option('bar') { command_option = 'bar' }
c.when_called {}
end
end.run!
expect(global_option).to eq('path')
expect(command_option).to eq('bar')
end
end
describe '#remove_global_options' do
it 'should remove only specified switches' do
options, args = [], []
options << { switches: ['-t', '--trace'] }
options << { switches: ['--help'] }
options << { switches: ['--paths PATHS'] }
args << '-t'
args << '--help'
args << '--command'
args << '--command-with-arg' << 'rawr'
args << '--paths' << '"lib/**/*.js","spec/**/*.js"'
command_runner.remove_global_options options, args
expect(args).to eq(['--command', '--command-with-arg', 'rawr'])
end
it 'should not swallow an argument unless it expects an argument' do
options, args = [], []
options << { switches: ['-n', '--no-arg'] }
options << { switches: ['-y', '--yes ARG'] }
options << { switches: ['-a', '--alternative=ARG'] }
args << '-n' << 'alpha'
args << '--yes' << 'deleted'
args << '-a' << 'deleted'
args << 'beta'
command_runner.remove_global_options options, args
expect(args).to eq(%w(alpha beta))
end
it 'should remove a switch that is the positive form of the [no-] option' do
options, args = [], []
options << { switches: ['-g', '--[no-]good'] }
options << { switches: ['-y', '--yes ARG'] }
options << { switches: ['-a', '--alternative=ARG'] }
args << '--good' << 'alpha'
args << '--yes' << 'deleted'
args << '-a' << 'deleted'
args << 'beta'
command_runner.remove_global_options options, args
expect(args).to eq(%w(alpha beta))
end
it 'should remove a switch that is the negative form of the [no-] option' do
options, args = [], []
options << { switches: ['-g', '--[no-]good'] }
options << { switches: ['-y', '--yes ARG'] }
options << { switches: ['-a', '--alternative=ARG'] }
args << '--no-good' << 'alpha'
args << '--yes' << 'deleted'
args << '-a' << 'deleted'
args << 'beta'
command_runner.remove_global_options options, args
expect(args).to eq(%w(alpha beta))
end
it 'should not remove options that start with a global option name' do
options, args = [], []
options << { switches: ['-v', '--version'] }
args << '--versionCode' << 'something'
command_runner.remove_global_options options, args
expect(args).to eq(%w(--versionCode something))
end
it 'should remove specified switches value provided via equals' do
options = [{ switches: ['--global GLOBAL'] }]
args = ['--command', '--global=option-value', 'arg']
command_runner.remove_global_options options, args
expect(args).to eq(['--command', 'arg'])
end
it 'should not remove extra values after switches' do
options = [{ switches: ['--global GLOBAL'] }]
args = ['--global', '--command', 'arg']
command_runner.remove_global_options options, args
expect(args).to eq(['--command', 'arg'])
end
end
describe '--trace' do
it 'should display pretty errors by default' do
expect do
new_command_runner 'foo' do
command(:foo) { |c| c.when_called { fail 'cookies!' } }
end.run!
end.to raise_error(TestSystemExit, /error: cookies!. Use --trace/)
end
it 'should display callstack when using this switch' do
expect do
new_command_runner 'foo', '--trace' do
command(:foo) { |c| c.when_called { fail 'cookies!' } }
end.run!
end.to raise_error(RuntimeError)
end
end
describe '#always_trace!' do
it 'should enable tracing globally, regardless of whether --trace was passed or not' do
expect do
new_command_runner 'foo' do
always_trace!
command(:foo) { |c| c.when_called { fail 'cookies!' } }
end.run!
end.to raise_error(RuntimeError)
end
end
describe '#never_trace!' do
it 'should disable tracing globally, regardless of whether --trace was passed or not' do
expect do
new_command_runner 'help', '--trace' do
never_trace!
end.run!
end.to raise_error(TestSystemExit, /invalid option: --trace/)
end
it 'should not prompt to use --trace switch on errors' do
msg = nil
begin
new_command_runner 'foo' do
never_trace!
command(:foo) { |c| c.when_called { fail 'cookies!' } }
end.run!
rescue TestSystemExit => e
msg = e.message
end
expect(msg).to match(/error: cookies!/)
expect(msg).not_to match(/--trace/)
end
end
context 'conflict between #always_trace! and #never_trace!' do
it 'respects the last used command' do
expect do
new_command_runner 'foo' do
never_trace!
always_trace!
command(:foo) { |c| c.when_called { fail 'cookies!' } }
end.run!
end.to raise_error(RuntimeError)
end
end
describe '--version' do
it 'should output program version' do
expect(run('--version')).to eq("test 1.2.3\n")
end
end
describe '--help' do
it 'should not output an invalid command message' do
expect(run('--help')).not_to eq("invalid command. Use --help for more information\n")
end
it 'can be used before or after the command and options' do
expect(run('test', '--help')).to eq("Implement help for test here\n")
end
it 'can be used after the command and command arguments' do
expect(run('test', 'command-arg', '--help')).to eq("Implement help for test here\n")
end
it 'can be used before a single-word command with command arguments' do
expect(run('help', 'test', 'command-arg')).to eq("Implement help for test here\n")
end
it 'can be used before a multi-word command with command arguments' do
expect(
run('help', 'module', 'install', 'command-arg') do
command('module install') { |c| c.when_called { say 'whee!' } }
end
).to eq("Implement help for module install here\n")
end
describe 'help_paging program information' do
it 'enables paging when enabled' do
run('--help') { program :help_paging, true }
expect(Commander::UI).to have_received(:enable_paging)
end
it 'is enabled by default' do
run('--help')
expect(Commander::UI).to have_received(:enable_paging)
end
it 'does not enable paging when disabled' do
run('--help') { program :help_paging, false }
expect(Commander::UI).not_to have_received(:enable_paging)
end
end
end
describe 'with invalid options' do
it 'should output an invalid option message' do
expect do
run('test', '--invalid-option')
end.to raise_error(TestSystemExit, /invalid option: --invalid-option/)
end
end
describe 'with invalid command passed' do
it 'should output an invalid command message' do
expect do
run('foo')
end.to raise_error(TestSystemExit, /invalid command. Use --help for more information/)
end
end
describe 'with invalid command passed to help' do
it 'should output an invalid command message' do
expect do
run('help', 'does_not_exist')
end.to raise_error(TestSystemExit, /invalid command. Use --help for more information/)
end
end
describe 'with invalid command passed to --help' do
it 'should output an invalid command message' do
expect do
run('--help', 'does_not_exist')
end.to raise_error(TestSystemExit, /invalid command. Use --help for more information/)
end
end
describe 'with invalid option passed to --help' do
it 'should output an invalid option message' do
expect do
run('--help', 'test', '--invalid-option')
end.to raise_error(TestSystemExit, /invalid option: --invalid-option/)
end
end
describe '#valid_command_names_from' do
it 'should return array of valid command names' do
new_command_runner do
command('foo bar') {}
command('foo bar foo') {}
expect(command_runner.valid_command_names_from('foo', 'bar', 'foo').sort).to eq(['foo bar', 'foo bar foo'])
end
end
it 'should return empty array when no possible command names exist' do
new_command_runner do
expect(command_runner.valid_command_names_from('fake', 'command', 'name')).to eq([])
end
end
it 'should match exact commands only' do
new_command_runner do
command('foo') {}
expect(command_runner.valid_command_names_from('foobar')).to eq([])
end
end
end
describe '#command_name_from_args' do
it 'should locate command within arbitrary arguments passed' do
new_command_runner '--help', '--arbitrary', 'test'
expect(command_runner.command_name_from_args).to eq('test')
end
it 'should locate command when provided after a global argument with value' do
new_command_runner '--global-option', 'option-value', 'test' do
global_option('--global-option=GLOBAL', 'A global option')
end
expect(command_runner.command_name_from_args).to eq('test')
end
it 'should support multi-word commands' do
new_command_runner '--help', '--arbitrary', 'some', 'long', 'command', 'foo'
command('some long command') {}
expect(command_runner.command_name_from_args).to eq('some long command')
end
it 'should match the longest possible command' do
new_command_runner '--help', '--arbitrary', 'foo', 'bar', 'foo'
command('foo bar') {}
command('foo bar foo') {}
expect(command_runner.command_name_from_args).to eq('foo bar foo')
end
it 'should use the left-most command name when multiple are present' do
new_command_runner 'help', 'test'
expect(command_runner.command_name_from_args).to eq('help')
end
end
describe '#active_command' do
it 'should resolve the active command' do
new_command_runner '--help', 'test'
expect(command_runner.active_command).to be_instance_of(Commander::Command)
end
it 'should resolve active command when invalid options are passed' do
new_command_runner '--help', 'test', '--arbitrary'
expect(command_runner.active_command).to be_instance_of(Commander::Command)
end
it 'should return nil when the command is not found' do
new_command_runner 'foo'
expect(command_runner.active_command).to be_nil
end
end
describe '#default_command' do
it 'should allow you to default any command when one is not explicitly passed' do
new_command_runner '--trace' do
default_command :test
expect(command(:test)).to receive(:run).once
expect(command_runner.active_command).to eq(command(:test))
end.run!
end
it 'should not prevent other commands from being called' do
new_command_runner 'foo', 'bar', '--trace' do
default_command :test
command(:'foo bar') {}
expect(command(:'foo bar')).to receive(:run).once
expect(command_runner.active_command).to eq(command(:'foo bar'))
end.run!
end
it 'should not prevent longer commands to use the same words as the default' do
new_command_runner 'foo', 'bar', 'something'
default_command :'foo bar'
command(:'foo bar') {}
command(:'foo bar something') {}
expect(command_runner.active_command).to eq(command(:'foo bar something'))
end
it 'should allow defaulting of command aliases' do
new_command_runner '--trace' do
default_command :foobar
alias_command :foobar, :test
expect(command(:test)).to receive(:run).once
end.run!
end
end
describe 'should function correctly' do
it 'when options are passed before the command name' do
new_command_runner '--verbose', 'test', 'foo', 'bar' do
@command.when_called do |args, options|
expect(args).to eq(%w(foo bar))
expect(options.verbose).to be true
end
end.run!
end
it 'when options are passed after the command name' do
new_command_runner 'test', '--verbose', 'foo', 'bar' do
@command.when_called do |args, options|
expect(args).to eq(%w(foo bar))
expect(options.verbose).to be true
end
end.run!
end
it 'when an argument passed is the same name as the command' do
new_command_runner 'test', '--verbose', 'foo', 'test', 'bar' do
@command.when_called do |args, options|
expect(args).to eq(%w(foo test bar))
expect(options.verbose).to be true
end
end.run!
end
it 'when using multi-word commands' do
new_command_runner '--verbose', 'my', 'command', 'something', 'foo', 'bar' do
command('my command') { |c| c.option('--verbose') }
expect(command_runner.command_name_from_args).to eq('my command')
expect(command_runner.args_without_command_name).to eq(['--verbose', 'something', 'foo', 'bar'])
end.run!
end
it 'when using multi-word commands with parts of the command name as arguments' do
new_command_runner '--verbose', 'my', 'command', 'something', 'my', 'command' do
command('my command') { |c| c.option('--verbose') }
expect(command_runner.command_name_from_args).to eq('my command')
expect(command_runner.args_without_command_name).to eq(['--verbose', 'something', 'my', 'command'])
end.run!
end
it 'when using multi-word commands with other commands using the same words' do
new_command_runner '--verbose', 'my', 'command', 'something', 'my', 'command' do
command('my command') {}
command('my command something') { |c| c.option('--verbose') }
expect(command_runner.command_name_from_args).to eq('my command something')
expect(command_runner.args_without_command_name).to eq(['--verbose', 'my', 'command'])
end.run!
end
end
describe 'options with optional arguments' do
it 'should return the argument when it is specified' do
new_command_runner 'foo', '--optional', 'arg1' do
command('foo') do |c|
c.option('--optional [argument]')
c.when_called do |_, options|
expect(options.optional).to eq('arg1')
end
end
end.run!
end
it 'should return true when no argument is specified for the option' do
new_command_runner 'foo', '--optional' do
command('foo') do |c|
c.option('--optional [argument]')
c.when_called do |_, options|
expect(options.optional).to be true
end
end
end.run!
end
end
describe 'with double dash' do
it 'should interpret the remainder as arguments' do
new_command_runner 'foo', '--', '-x' do
command('foo') do |c|
c.option '-x', 'Switch'
c.when_called do |args, options|
expect(args).to eq(%w(-x))
expect(options.x).to be_nil
end
end
end.run!
end
end
end
|