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

Function test_load_commandset_errors

tests/test_commandset.py:336–387  ·  view source on GitHub ↗
(manual_command_sets_app, capsys)

Source from the content-addressed store, hash-verified

334
335
336def test_load_commandset_errors(manual_command_sets_app, capsys) -> None:
337 cmd_set = CommandSetA()
338
339 # create a conflicting command before installing CommandSet to verify rollback behavior
340 manual_command_sets_app._install_command_function("do_durian", cmd_set.do_durian)
341 with pytest.raises(CommandSetRegistrationError):
342 manual_command_sets_app.register_command_set(cmd_set)
343
344 # verify that the commands weren't installed
345 cmds_cats, _help_topics = manual_command_sets_app._build_command_info()
346
347 assert "Alone" not in cmds_cats
348 assert "Fruits" not in cmds_cats
349 assert not manual_command_sets_app._installed_command_sets
350
351 delattr(manual_command_sets_app, "do_durian")
352
353 # pre-create intentionally conflicting macro and alias names
354 manual_command_sets_app.app_cmd("macro create apple run_pyscript")
355 manual_command_sets_app.app_cmd("alias create banana run_pyscript")
356
357 # now install a command set and verify the commands are now present
358 manual_command_sets_app.register_command_set(cmd_set)
359 _out, err = capsys.readouterr()
360
361 # verify aliases and macros are deleted with warning if they conflict with a command
362 assert "Deleting alias 'banana'" in err
363 assert "Deleting macro 'apple'" in err
364
365 # verify command functions which don't start with "do_" raise an exception
366 with pytest.raises(CommandSetRegistrationError):
367 manual_command_sets_app._install_command_function("new_cmd", cmd_set.do_banana)
368
369 # verify methods which don't start with "do_" raise an exception
370 with pytest.raises(CommandSetRegistrationError):
371 manual_command_sets_app._install_command_function("do_new_cmd", cmd_set.on_register)
372
373 # verify duplicate commands are detected
374 with pytest.raises(CommandSetRegistrationError):
375 manual_command_sets_app._install_command_function("do_banana", cmd_set.do_banana)
376
377 # verify bad command names are detected
378 with pytest.raises(CommandSetRegistrationError):
379 manual_command_sets_app._install_command_function("do_bad command", cmd_set.do_banana)
380
381 # verify error conflict with existing completer function
382 with pytest.raises(CommandSetRegistrationError):
383 manual_command_sets_app._install_completer_function("durian", cmd_set.complete_durian)
384
385 # verify error conflict with existing help function
386 with pytest.raises(CommandSetRegistrationError):
387 manual_command_sets_app._install_help_function("cranberry", cmd_set.help_cranberry)
388
389
390class LoadableBase(cmd2.CommandSet):

Callers

nothing calls this directly

Calls 7

register_command_setMethod · 0.80
_build_command_infoMethod · 0.80
app_cmdMethod · 0.80
CommandSetAClass · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…