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

Function test_cmd2_argparse_exception

tests/test_plugin.py:970–994  ·  view source on GitHub ↗

Verify Cmd2ArgparseErrors raised after calling a command prevent postcmd events from running but do not affect cmdfinalization events

(capsys)

Source from the content-addressed store, hash-verified

968
969
970def test_cmd2_argparse_exception(capsys) -> None:
971 """Verify Cmd2ArgparseErrors raised after calling a command prevent postcmd events from
972 running but do not affect cmdfinalization events
973 """
974 app = PluggedApp()
975 app.register_postcmd_hook(app.postcmd_hook)
976 app.register_cmdfinalization_hook(app.cmdfinalization_hook)
977
978 # First generate no exception and make sure postcmd_hook, postcmd, and cmdfinalization_hook run
979 app.onecmd_plus_hooks("argparse_cmd arg_val")
980 out, err = capsys.readouterr()
981 assert out == "arg_val\n"
982 assert not err
983 assert app.called_postcmd == 2
984 assert app.called_cmdfinalization == 1
985
986 app.reset_counters()
987
988 # Next cause an argparse exception and verify no postcmd stuff runs but cmdfinalization_hook still does
989 app.onecmd_plus_hooks("argparse_cmd")
990 out, err = capsys.readouterr()
991 assert not out
992 assert "Error: the following arguments are required: my_arg" in err
993 assert app.called_postcmd == 0
994 assert app.called_cmdfinalization == 1

Callers

nothing calls this directly

Calls 5

PluggedAppClass · 0.85
register_postcmd_hookMethod · 0.80
onecmd_plus_hooksMethod · 0.80
reset_countersMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…