MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / test_subcommands

Function test_subcommands

tests/test_commandset.py:522–636  ·  view source on GitHub ↗
(manual_command_sets_app)

Source from the content-addressed store, hash-verified

520
521
522def test_subcommands(manual_command_sets_app) -> None:
523 base_cmds = LoadableBase(1)
524 badbase_cmds = LoadableBadBase(1)
525 fruit_cmds = LoadableFruits(1)
526 veg_cmds = LoadableVegetables(1)
527
528 # installing subcommands without base command present raises exception
529 with pytest.raises(CommandSetRegistrationError):
530 manual_command_sets_app.register_command_set(fruit_cmds)
531
532 # if the base command is present but isn't an argparse command, expect exception
533 manual_command_sets_app.register_command_set(badbase_cmds)
534 with pytest.raises(CommandSetRegistrationError):
535 manual_command_sets_app.register_command_set(fruit_cmds)
536
537 # verify that the Fruit commands weren't installed
538 cmds_cats, _help_topics = manual_command_sets_app._build_command_info()
539 assert "Fruits" not in cmds_cats
540 assert "cut" in manual_command_sets_app.get_all_commands()
541
542 # Now install the good base commands
543 manual_command_sets_app.unregister_command_set(badbase_cmds)
544 manual_command_sets_app.register_command_set(base_cmds)
545
546 # verify that we catch an attempt to register subcommands when the commandset isn't installed
547 with pytest.raises(CommandSetRegistrationError):
548 manual_command_sets_app._register_subcommands(fruit_cmds)
549
550 cmd_result = manual_command_sets_app.app_cmd("cut")
551 assert "This command does nothing without sub-parsers registered" in cmd_result.stderr
552
553 # verify that command set install without problems
554 manual_command_sets_app.register_command_set(fruit_cmds)
555 manual_command_sets_app.register_command_set(veg_cmds)
556 cmds_cats, _help_topics = manual_command_sets_app._build_command_info()
557 assert "Fruits" in cmds_cats
558
559 text = ""
560 line = f"cut {text}"
561 endidx = len(line)
562 begidx = endidx
563 completions = manual_command_sets_app.complete(text, line, begidx, endidx)
564
565 # check that the alias shows up correctly
566 assert completions.to_strings() == Completions.from_values(["banana", "bananer", "bokchoy"]).to_strings()
567
568 cmd_result = manual_command_sets_app.app_cmd("cut banana discs")
569 assert "cutting banana: discs" in cmd_result.stdout
570
571 text = ""
572 line = f"cut bokchoy {text}"
573 endidx = len(line)
574 begidx = endidx
575 completions = manual_command_sets_app.complete(text, line, begidx, endidx)
576
577 # verify that argparse completer in commandset functions correctly
578 assert completions.to_strings() == Completions.from_values(["diced", "quartered"]).to_strings()
579

Callers

nothing calls this directly

Calls 15

LoadableBaseClass · 0.85
LoadableBadBaseClass · 0.85
register_command_setMethod · 0.80
_build_command_infoMethod · 0.80
_register_subcommandsMethod · 0.80
app_cmdMethod · 0.80
to_stringsMethod · 0.80
from_valuesMethod · 0.80
disable_commandMethod · 0.80
enable_commandMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…