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 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
|
# frozen_string_literal: true
require 'helper'
module Cri
class CommandTestCase < Cri::TestCase
def simple_cmd
Cri::Command.define do
name 'moo'
usage 'moo [options] arg1 arg2 ...'
summary 'does stuff'
description 'This command does a lot of stuff.'
option :a, :aaa, 'opt a', argument: :optional do |value, cmd|
$stdout.puts "#{cmd.name}:#{value}"
end
required :b, :bbb, 'opt b'
optional :c, :ccc, 'opt c'
flag :d, :ddd, 'opt d'
forbidden :e, :eee, 'opt e'
required :t, :transform, 'opt t', transform: method(:Integer)
run do |opts, args, c|
$stdout.puts "Awesome #{c.name}!"
$stdout.puts args.join(',')
opts_strings = []
opts.each_pair { |k, v| opts_strings << "#{k}=#{v}" }
$stdout.puts opts_strings.sort.join(',')
end
end
end
def bare_cmd
Cri::Command.define do
name 'moo'
run do |_opts, _args|
end
end
end
def nested_cmd
super_cmd = Cri::Command.define do
name 'super'
usage 'super [command] [options] [arguments]'
summary 'does super stuff'
description 'This command does super stuff.'
option :a, :aaa, 'opt a', argument: :optional do |value, cmd|
$stdout.puts "#{cmd.name}:#{value}"
end
required :b, :bbb, 'opt b'
optional :c, :ccc, 'opt c'
flag :d, :ddd, 'opt d'
forbidden :e, :eee, 'opt e'
end
super_cmd.define_command do
name 'sub'
aliases 'sup'
usage 'sub [options]'
summary 'does subby stuff'
description 'This command does subby stuff.'
option :m, :mmm, 'opt m', argument: :optional
required :n, :nnn, 'opt n'
optional :o, :ooo, 'opt o'
flag :p, :ppp, 'opt p'
forbidden :q, :qqq, 'opt q'
run do |opts, args|
$stdout.puts 'Sub-awesome!'
$stdout.puts args.join(',')
opts_strings = []
opts.each_pair { |k, v| opts_strings << "#{k}=#{v}" }
$stdout.puts opts_strings.join(',')
end
end
super_cmd.define_command do
name 'sink'
usage 'sink thing_to_sink'
summary 'sinks stuff'
description 'Sinks stuff (like ships and the like).'
run do |_opts, _args|
end
end
super_cmd
end
def nested_cmd_with_run_block
super_cmd = Cri::Command.define do
name 'super'
usage 'super [command] [options] [arguments]'
summary 'does super stuff'
description 'This command does super stuff.'
run do |_opts, _args|
$stdout.puts 'super'
end
end
super_cmd.define_command do
name 'sub'
aliases 'sup'
usage 'sub [options]'
summary 'does subby stuff'
description 'This command does subby stuff.'
run do |_opts, _args|
$stdout.puts 'sub'
end
end
super_cmd
end
def test_invoke_simple_without_opts_or_args
out, err = capture_io_while do
simple_cmd.run(%w[])
end
assert_equal ['Awesome moo!', '', ''], lines(out)
assert_equal [], lines(err)
end
def test_invoke_simple_with_args
out, err = capture_io_while do
simple_cmd.run(%w[abc xyz])
end
assert_equal ['Awesome moo!', 'abc,xyz', ''], lines(out)
assert_equal [], lines(err)
end
def test_invoke_simple_with_opts
out, err = capture_io_while do
simple_cmd.run(%w[-c -b x])
end
assert_equal ['Awesome moo!', '', 'bbb=x,ccc=true'], lines(out)
assert_equal [], lines(err)
end
def test_invoke_simple_with_missing_opt_arg
out, err = capture_io_while do
err = assert_raises SystemExit do
simple_cmd.run(%w[-b])
end
assert_equal 1, err.status
end
assert_equal [], lines(out)
assert_equal ['moo: option requires an argument -- b'], lines(err)
end
def test_invoke_simple_with_missing_opt_arg_no_exit
out, err = capture_io_while do
simple_cmd.run(%w[-b], {}, hard_exit: false)
end
assert_equal [], lines(out)
assert_equal ['moo: option requires an argument -- b'], lines(err)
end
def test_invoke_simple_with_illegal_opt
out, err = capture_io_while do
err = assert_raises SystemExit do
simple_cmd.run(%w[-z])
end
assert_equal 1, err.status
end
assert_equal [], lines(out)
assert_equal ['moo: unrecognised option -- z'], lines(err)
end
def test_invoke_simple_with_illegal_opt_no_exit
out, err = capture_io_while do
simple_cmd.run(%w[-z], {}, hard_exit: false)
end
assert_equal [], lines(out)
assert_equal ['moo: unrecognised option -- z'], lines(err)
end
def test_invoke_simple_with_invalid_value_for_opt
out, err = capture_io_while do
err = assert_raises SystemExit do
simple_cmd.run(%w[-t nope])
end
assert_equal 1, err.status
end
assert_equal [], lines(out)
assert_equal ['moo: invalid value "nope" for --transform option'], lines(err)
end
def test_invoke_simple_with_invalid_value_for_opt_no_exit
out, err = capture_io_while do
simple_cmd.run(%w[-t nope], {}, hard_exit: false)
end
assert_equal [], lines(out)
assert_equal ['moo: invalid value "nope" for --transform option'], lines(err)
end
def test_invoke_simple_with_opt_with_block
out, err = capture_io_while do
simple_cmd.run(%w[-a 123])
end
assert_equal ['moo:123', 'Awesome moo!', '', 'aaa=123'], lines(out)
assert_equal [], lines(err)
end
def test_invoke_nested_without_opts_or_args
out, err = capture_io_while do
err = assert_raises SystemExit do
nested_cmd.run(%w[])
end
assert_equal 1, err.status
end
assert_equal [], lines(out)
assert_equal ['super: no command given'], lines(err)
end
def test_invoke_nested_without_opts_or_args_no_exit
out, err = capture_io_while do
nested_cmd.run(%w[], {}, hard_exit: false)
end
assert_equal [], lines(out)
assert_equal ['super: no command given'], lines(err)
end
def test_invoke_nested_with_correct_command_name
out, err = capture_io_while do
nested_cmd.run(%w[sub])
end
assert_equal ['Sub-awesome!', '', ''], lines(out)
assert_equal [], lines(err)
end
def test_invoke_nested_with_incorrect_command_name
out, err = capture_io_while do
err = assert_raises SystemExit do
nested_cmd.run(%w[oogabooga])
end
assert_equal 1, err.status
end
assert_equal [], lines(out)
assert_equal ["super: unknown command 'oogabooga'"], lines(err)
end
def test_invoke_nested_with_incorrect_command_name_no_exit
out, err = capture_io_while do
nested_cmd.run(%w[oogabooga], {}, hard_exit: false)
end
assert_equal [], lines(out)
assert_equal ["super: unknown command 'oogabooga'"], lines(err)
end
def test_invoke_nested_with_ambiguous_command_name
out, err = capture_io_while do
err = assert_raises SystemExit do
nested_cmd.run(%w[s])
end
assert_equal 1, err.status
end
assert_equal [], lines(out)
assert_equal ["super: 's' is ambiguous:", ' sink sub'], lines(err)
end
def test_invoke_nested_with_ambiguous_command_name_no_exit
out, err = capture_io_while do
nested_cmd.run(%w[s], {}, hard_exit: false)
end
assert_equal [], lines(out)
assert_equal ["super: 's' is ambiguous:", ' sink sub'], lines(err)
end
def test_invoke_nested_with_alias
out, err = capture_io_while do
nested_cmd.run(%w[sup])
end
assert_equal ['Sub-awesome!', '', ''], lines(out)
assert_equal [], lines(err)
end
def test_invoke_nested_with_options_before_command
out, err = capture_io_while do
nested_cmd.run(%w[-a 666 sub])
end
assert_equal ['super:666', 'Sub-awesome!', '', 'aaa=666'], lines(out)
assert_equal [], lines(err)
end
def test_invoke_nested_with_run_block
out, err = capture_io_while do
nested_cmd_with_run_block.run(%w[])
end
assert_equal ['super'], lines(out)
assert_equal [], lines(err)
out, err = capture_io_while do
nested_cmd_with_run_block.run(%w[sub])
end
assert_equal ['sub'], lines(out)
assert_equal [], lines(err)
end
def test_help_nested
def $stdout.tty?
true
end
help = nested_cmd.subcommands.find { |cmd| cmd.name == 'sub' }.help
assert help.include?("USAGE\e[0m\e[0m\n \e[32msuper\e[0m \e[32msub\e[0m [options]\n")
end
def test_help_with_and_without_colors
def $stdout.tty?
true
end
help_on_tty = simple_cmd.help
def $stdout.tty?
false
end
help_not_on_tty = simple_cmd.help
assert_includes help_on_tty, "\e[31mUSAGE\e[0m\e[0m\n \e[32mmoo"
assert_includes help_not_on_tty, "USAGE\n moo"
end
def test_help_for_bare_cmd
bare_cmd.help
end
def test_help_with_optional_options
def $stdout.tty?
true
end
cmd = Cri::Command.define do
name 'build'
flag :s, nil, 'short'
flag nil, :long, 'long'
end
help = cmd.help
assert_match(/--long.*-s/m, help)
assert_match(/^ \e\[33m--long\e\[0m long$/, help)
assert_match(/^ \e\[33m-s\e\[0m short$/, help)
end
def test_help_with_different_option_types_short_and_long
def $stdout.tty?
true
end
cmd = Cri::Command.define do
name 'build'
required :r, :required, 'required value'
flag :f, :flag, 'forbidden value'
optional :o, :optional, 'optional value'
end
help = cmd.help
assert_match(/^ \e\[33m-r\e\[0m \e\[33m--required\e\[0m=<value> required value$/, help)
assert_match(/^ \e\[33m-f\e\[0m \e\[33m--flag\e\[0m forbidden value$/, help)
assert_match(/^ \e\[33m-o\e\[0m \e\[33m--optional\e\[0m\[=<value>\] optional value$/, help)
end
def test_help_with_different_option_types_short
def $stdout.tty?
true
end
cmd = Cri::Command.define do
name 'build'
required :r, nil, 'required value'
flag :f, nil, 'forbidden value'
optional :o, nil, 'optional value'
end
help = cmd.help
assert_match(/^ \e\[33m-r\e\[0m <value> required value$/, help)
assert_match(/^ \e\[33m-f\e\[0m forbidden value$/, help)
assert_match(/^ \e\[33m-o\e\[0m \[<value>\] optional value$/, help)
end
def test_help_with_different_option_types_long
def $stdout.tty?
true
end
cmd = Cri::Command.define do
name 'build'
required nil, :required, 'required value'
flag nil, :flag, 'forbidden value'
optional nil, :optional, 'optional value'
end
help = cmd.help
assert_match(/^ \e\[33m--required\e\[0m=<value> required value$/, help)
assert_match(/^ \e\[33m--flag\e\[0m forbidden value$/, help)
assert_match(/^ \e\[33m--optional\e\[0m\[=<value>\] optional value$/, help)
end
def test_help_with_multiple_groups
help = nested_cmd.subcommands.find { |cmd| cmd.name == 'sub' }.help
assert_match(/OPTIONS.*OPTIONS FOR SUPER/m, help)
end
def test_modify_with_block_argument
cmd = Cri::Command.define do |c|
c.name 'build'
end
assert_equal 'build', cmd.name
cmd.modify do |c|
c.name 'compile'
end
assert_equal 'compile', cmd.name
end
def test_help_with_wrapped_options
def $stdout.tty?
true
end
cmd = Cri::Command.define do
name 'build'
flag nil, :longflag, 'This is an option with a very long description that should be wrapped'
end
help = cmd.help
assert_match(/^ \e\[33m--longflag\e\[0m This is an option with a very long description that$/, help)
assert_match(/^ should be wrapped$/, help)
end
def test_modify_without_block_argument
cmd = Cri::Command.define do
name 'build'
end
assert_equal 'build', cmd.name
cmd.modify do
name 'compile'
end
assert_equal 'compile', cmd.name
end
def test_new_basic_root
cmd = Cri::Command.new_basic_root.modify do
name 'mytool'
end
# Check option definitions
assert_equal 1, cmd.option_definitions.size
opt_defn = cmd.option_definitions.to_a[0]
assert_equal 'help', opt_defn.long
# Check subcommand
assert_equal 1, cmd.subcommands.size
assert_equal 'help', cmd.subcommands.to_a[0].name
end
def test_define_with_block_argument
cmd = Cri::Command.define do |c|
c.name 'moo'
end
assert_equal 'moo', cmd.name
end
def test_define_without_block_argument
cmd = Cri::Command.define do
name 'moo'
end
assert_equal 'moo', cmd.name
end
def test_define_subcommand_with_block_argument
cmd = bare_cmd
cmd.define_command do |c|
c.name 'baresub'
end
assert_equal 'baresub', cmd.subcommands.to_a[0].name
end
def test_define_subcommand_without_block_argument
cmd = bare_cmd
cmd.define_command do
name 'baresub'
end
assert_equal 'baresub', cmd.subcommands.to_a[0].name
end
def test_backtrace_includes_filename
error = assert_raises RuntimeError do
Cri::Command.define('raise "boom"', 'mycommand.rb')
end
assert_match(/mycommand.rb/, error.backtrace.join("\n"))
end
def test_hidden_commands_single
cmd = nested_cmd
subcmd = simple_cmd
cmd.add_command subcmd
subcmd.modify do |c|
c.name 'old-and-deprecated'
c.summary 'does stuff the ancient, totally deprecated way'
c.be_hidden
end
refute cmd.help.include?('hidden commands omitted')
assert cmd.help.include?('hidden command omitted')
refute cmd.help.include?('old-and-deprecated')
refute cmd.help(verbose: true).include?('hidden commands omitted')
refute cmd.help(verbose: true).include?('hidden command omitted')
assert cmd.help(verbose: true).include?('old-and-deprecated')
end
def test_hidden_commands_multiple
cmd = nested_cmd
subcmd = simple_cmd
cmd.add_command subcmd
subcmd.modify do |c|
c.name 'first'
c.summary 'does stuff first'
end
subcmd = simple_cmd
cmd.add_command subcmd
subcmd.modify do |c|
c.name 'old-and-deprecated'
c.summary 'does stuff the old, deprecated way'
c.be_hidden
end
subcmd = simple_cmd
cmd.add_command subcmd
subcmd.modify do |c|
c.name 'ancient-and-deprecated'
c.summary 'does stuff the ancient, reallydeprecated way'
c.be_hidden
end
assert cmd.help.include?('hidden commands omitted')
refute cmd.help.include?('hidden command omitted')
refute cmd.help.include?('old-and-deprecated')
refute cmd.help.include?('ancient-and-deprecated')
refute cmd.help(verbose: true).include?('hidden commands omitted')
refute cmd.help(verbose: true).include?('hidden command omitted')
assert cmd.help(verbose: true).include?('old-and-deprecated')
assert cmd.help(verbose: true).include?('ancient-and-deprecated')
pattern = /ancient-and-deprecated.*first.*old-and-deprecated/m
assert_match(pattern, cmd.help(verbose: true))
end
def test_run_with_raw_args
cmd = Cri::Command.define do
name 'moo'
run do |_opts, args|
puts "args=#{args.join(',')}"
end
end
out, _err = capture_io_while do
cmd.run(%w[foo -- bar])
end
assert_equal "args=foo,bar\n", out
end
def test_run_without_block
cmd = Cri::Command.define do
name 'moo'
end
assert_raises(Cri::NotImplementedError) do
cmd.run([])
end
end
def test_runner_with_raw_args
cmd = Cri::Command.define do
name 'moo'
runner(Class.new(Cri::CommandRunner) do
def run
puts "args=#{arguments.join(',')}"
end
end)
end
out, _err = capture_io_while do
cmd.run(%w[foo -- bar])
end
assert_equal "args=foo,bar\n", out
end
def test_compare
foo = Cri::Command.define { name 'foo' }
bar = Cri::Command.define { name 'bar' }
qux = Cri::Command.define { name 'qux' }
assert_equal [bar, foo, qux], [foo, bar, qux].sort
end
def test_default_subcommand
subcommand = Cri::Command.define do
name 'sub'
run do |_opts, _args, _c|
$stdout.puts 'I am the subcommand!'
end
end
cmd = Cri::Command.define do
name 'super'
default_subcommand 'sub'
subcommand subcommand
end
out, _err = capture_io_while do
cmd.run([])
end
assert_equal "I am the subcommand!\n", out
end
def test_skip_option_parsing
command = Cri::Command.define do
name 'super'
skip_option_parsing
run do |_opts, args, _c|
puts "args=#{args.join(',')}"
end
end
out, _err = capture_io_while do
command.run(['--test', '-a', 'arg'])
end
assert_equal "args=--test,-a,arg\n", out
end
def test_subcommand_skip_option_parsing
super_cmd = Cri::Command.define do
name 'super'
option :a, :aaa, 'opt a', argument: :optional
end
super_cmd.define_command do
name 'sub'
skip_option_parsing
run do |opts, args, _c|
puts "opts=#{opts.inspect} args=#{args.join(',')}"
end
end
out, _err = capture_io_while do
super_cmd.run(['--aaa', 'test', 'sub', '--test', 'value'])
end
expected_aaa_test = { aaa: 'test' }.inspect
assert_equal "opts=#{expected_aaa_test} args=--test,value\n", out
end
def test_wrong_number_of_args
cmd = Cri::Command.define do
name 'publish'
param :filename
end
out, err = capture_io_while do
err = assert_raises SystemExit do
cmd.run([])
end
assert_equal 1, err.status
end
assert_equal [], lines(out)
assert_equal ['publish: incorrect number of arguments given: expected 1, but got 0'], lines(err)
end
def test_no_params_zero_args
dsl = Cri::CommandDSL.new
dsl.instance_eval do
name 'moo'
usage 'dunno whatever'
summary 'does stuff'
description 'This command does a lot of stuff.'
no_params
run do |_opts, args|
end
end
command = dsl.command
command.run([])
end
def test_no_params_one_arg
dsl = Cri::CommandDSL.new
dsl.instance_eval do
name 'moo'
usage 'dunno whatever'
summary 'does stuff'
description 'This command does a lot of stuff.'
no_params
run do |_opts, args|
end
end
command = dsl.command
out, err = capture_io_while do
err = assert_raises SystemExit do
command.run(['a'])
end
assert_equal 1, err.status
end
assert_equal [], lines(out)
assert_equal ['moo: incorrect number of arguments given: expected 0, but got 1'], lines(err)
end
def test_load_file
Dir.mktmpdir('foo') do |dir|
filename = "#{dir}/moo.rb"
File.write(filename, <<~CMD)
name 'moo'
usage 'dunno whatever'
summary 'does stuff'
description 'This command does a lot of stuff.'
no_params
run do |_opts, args|
end
CMD
cmd = Cri::Command.load_file(filename)
assert_equal('moo', cmd.name)
end
end
def test_load_file_infer_name_false
Dir.mktmpdir('foo') do |dir|
filename = "#{dir}/moo.rb"
File.write(filename, <<~CMD)
usage 'dunno whatever'
summary 'does stuff'
description 'This command does a lot of stuff.'
no_params
run do |_opts, args|
end
CMD
cmd = Cri::Command.load_file(filename)
assert_equal(nil, cmd.name)
end
end
def test_load_file_infer_name
Dir.mktmpdir('foo') do |dir|
filename = "#{dir}/moo.rb"
File.write(filename, <<~CMD)
usage 'dunno whatever'
summary 'does stuff'
description 'This command does a lot of stuff.'
no_params
run do |_opts, args|
end
CMD
cmd = Cri::Command.load_file(filename, infer_name: true)
assert_equal('moo', cmd.name)
end
end
def test_load_file_infer_name_double
Dir.mktmpdir('foo') do |dir|
filename = "#{dir}/moo.rb"
File.write(filename, <<~CMD)
name 'oink'
usage 'dunno whatever'
summary 'does stuff'
description 'This command does a lot of stuff.'
no_params
run do |_opts, args|
end
CMD
cmd = Cri::Command.load_file(filename, infer_name: true)
assert_equal('moo', cmd.name)
end
end
def test_required_args_with_dash_h
dsl = Cri::CommandDSL.new
dsl.instance_eval do
name 'moo'
usage 'dunno whatever'
summary 'does stuff'
description 'This command does a lot of stuff.'
param :foo
option :h, :help, 'show help' do
$helped = true
exit 0
end
end
command = dsl.command
$helped = false
out, err = capture_io_while do
assert_raises SystemExit do
command.run(['-h'])
end
end
assert $helped
assert_equal [], lines(out)
assert_equal [], lines(err)
end
def test_propagate_options_two_levels_down
cmd_a = Cri::Command.define do
name 'a'
flag :t, :test, 'test'
end
cmd_b = cmd_a.define_command('b') do
end
cmd_b.define_command('c') do
run do |opts, _args|
puts "test? #{opts[:test].inspect}!"
end
end
# test -t last
out, err = capture_io_while do
cmd_a.run(%w[b c -t])
end
assert_equal ['test? true!'], lines(out)
assert_equal [], lines(err)
# test -t mid
out, err = capture_io_while do
cmd_a.run(%w[b -t c])
end
assert_equal ['test? true!'], lines(out)
assert_equal [], lines(err)
# test -t first
out, err = capture_io_while do
cmd_a.run(%w[-t b c])
end
assert_equal ['test? true!'], lines(out)
assert_equal [], lines(err)
end
def test_flag_defaults_to_false
cmd = Cri::Command.define do
name 'a'
option :f, :force2, 'push with force', argument: :forbidden
run do |opts, _args, _cmd|
puts "Force? #{opts[:force2].inspect}! Key present? #{opts.key?(:force2)}!"
end
end
out, err = capture_io_while do
cmd.run(%w[])
end
assert_equal ['Force? false! Key present? false!'], lines(out)
assert_equal [], lines(err)
end
def test_required_option_defaults_to_given_value
cmd = Cri::Command.define do
name 'a'
option :a, :animal, 'specify animal', argument: :required, default: 'cow'
run do |opts, _args, _cmd|
puts "Animal = #{opts[:animal]}! Key present? #{opts.key?(:animal)}!"
end
end
out, err = capture_io_while do
cmd.run(%w[])
end
assert_equal ['Animal = cow! Key present? false!'], lines(out)
assert_equal [], lines(err)
end
def test_optional_option_defaults_to_given_value
cmd = Cri::Command.define do
name 'a'
option :a, :animal, 'specify animal', argument: :optional, default: 'cow'
run do |opts, _args, _cmd|
puts "Animal = #{opts[:animal]}"
end
end
out, err = capture_io_while do
cmd.run(%w[])
end
assert_equal ['Animal = cow'], lines(out)
assert_equal [], lines(err)
end
def test_required_option_defaults_to_given_value_with_transform
cmd = Cri::Command.define do
name 'a'
option :a, :animal, 'specify animal', argument: :required, transform: lambda(&:upcase), default: 'cow'
run do |opts, _args, _cmd|
puts "Animal = #{opts[:animal]}"
end
end
out, err = capture_io_while do
cmd.run(%w[])
end
assert_equal ['Animal = cow'], lines(out)
assert_equal [], lines(err)
end
def test_option_definitions_are_not_shared_across_commands
root_cmd = Cri::Command.define do
name 'root'
option :r, :rrr, 'Rrr!', argument: :required
end
subcmd_a = root_cmd.define_command do
name 'a'
option :a, :aaa, 'Aaa!', argument: :required
run do |_opts, _args, cmd|
puts cmd.all_opt_defns.map(&:long).sort.join(',')
end
end
subcmd_b = root_cmd.define_command do
name 'b'
option :b, :bbb, 'Bbb!', argument: :required
run do |_opts, _args, cmd|
puts cmd.all_opt_defns.map(&:long).sort.join(',')
end
end
out, err = capture_io_while do
subcmd_a.run(%w[])
end
assert_equal ['aaa,rrr'], lines(out)
assert_equal [], lines(err)
out, err = capture_io_while do
subcmd_b.run(%w[])
end
assert_equal ['bbb,rrr'], lines(out)
assert_equal [], lines(err)
end
end
end
|