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

Function test_subcommand_attachment_errors

tests/test_argparse_utils.py:448–480  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

446
447
448def test_subcommand_attachment_errors() -> None:
449 root_parser = Cmd2ArgumentParser(prog="root", description="root command")
450 child_parser = Cmd2ArgumentParser(prog="child", description="child command")
451
452 # Verify ValueError when subcommands are not supported
453 with pytest.raises(ValueError, match="Command 'root' does not support subcommands"):
454 root_parser.attach_subcommand([], "anything", child_parser)
455 with pytest.raises(ValueError, match="Command 'root' does not support subcommands"):
456 root_parser.detach_subcommand([], "anything")
457
458 # Allow subcommands for the next tests
459 root_parser.add_subparsers()
460
461 # Verify ValueError when path is invalid (find_parser() fails)
462 with pytest.raises(ValueError, match="Subcommand 'nonexistent' does not exist for 'root'"):
463 root_parser.attach_subcommand(["nonexistent"], "anything", child_parser)
464 with pytest.raises(ValueError, match="Subcommand 'nonexistent' does not exist for 'root'"):
465 root_parser.detach_subcommand(["nonexistent"], "anything")
466
467 # Verify ValueError when path is valid but subcommand name is wrong
468 with pytest.raises(ValueError, match="Subcommand 'fake' does not exist for 'root'"):
469 root_parser.detach_subcommand([], "fake")
470
471 # Verify TypeError when attaching a non-Cmd2ArgumentParser type
472 ap_parser = argparse.ArgumentParser(prog="non-cmd2-parser")
473 with pytest.raises(TypeError, match=r"must be an instance of 'Cmd2ArgumentParser' \(or a subclass\)"):
474 root_parser.attach_subcommand([], "sub", ap_parser) # type: ignore[arg-type]
475
476 # Verify ValueError when subcommand name already exists
477 sub_parser = Cmd2ArgumentParser(prog="sub")
478 root_parser.attach_subcommand([], "sub", sub_parser)
479 with pytest.raises(ValueError, match="Subcommand 'sub' already exists for 'root'"):
480 root_parser.attach_subcommand([], "sub", sub_parser)
481
482
483def test_subcommand_attachment_parser_class_override() -> None:

Callers

nothing calls this directly

Calls 3

attach_subcommandMethod · 0.95
detach_subcommandMethod · 0.95
Cmd2ArgumentParserClass · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…