@@ -149,6 +149,50 @@ def test_autoload_commands(command_sets_app):
149149 assert 'Command Set B' not in cmds_cats
150150
151151
152+ def test_command_synonyms ():
153+ """Test the use of command synonyms in CommandSets"""
154+
155+ class SynonymCommandSet (cmd2 .CommandSet ):
156+ def __init__ (self , arg1 ):
157+ super ().__init__ ()
158+ self ._arg1 = arg1
159+
160+ @cmd2 .with_argparser (cmd2 .Cmd2ArgumentParser (description = "Native Command" ))
161+ def do_builtin (self , _ ):
162+ pass
163+
164+ # Create a synonym to a command inside of this CommandSet
165+ do_builtin_synonym = do_builtin
166+
167+ # Create a synonym to a command outside of this CommandSet
168+ do_help_synonym = cmd2 .Cmd .do_help
169+
170+ cs = SynonymCommandSet ("foo" )
171+ app = WithCommandSets (command_sets = [cs ])
172+
173+ # Make sure the synonyms have the same parser as what they alias
174+ builtin_parser = app ._command_parsers .get (app .do_builtin )
175+ builtin_synonym_parser = app ._command_parsers .get (app .do_builtin_synonym )
176+ assert builtin_parser is not None
177+ assert builtin_parser is builtin_synonym_parser
178+
179+ help_parser = app ._command_parsers .get (cmd2 .Cmd .do_help )
180+ help_synonym_parser = app ._command_parsers .get (app .do_help_synonym )
181+ assert help_parser is not None
182+ assert help_parser is help_synonym_parser
183+
184+ # Unregister the CommandSet and make sure built-in command and synonyms are gone
185+ app .unregister_command_set (cs )
186+ assert not hasattr (app , "do_builtin" )
187+ assert not hasattr (app , "do_builtin_synonym" )
188+ assert not hasattr (app , "do_help_synonym" )
189+
190+ # Make sure the help command still exists, has the same parser, and works.
191+ assert help_parser is app ._command_parsers .get (cmd2 .Cmd .do_help )
192+ out , err = run_cmd (app , 'help' )
193+ assert app .doc_header in out
194+
195+
152196def test_custom_construct_commandsets ():
153197 command_set_b = CommandSetB ('foo' )
154198
@@ -291,7 +335,7 @@ def test_load_commandset_errors(command_sets_manual, capsys):
291335 cmd_set = CommandSetA ()
292336
293337 # create a conflicting command before installing CommandSet to verify rollback behavior
294- command_sets_manual ._install_command_function ('durian ' , cmd_set .do_durian )
338+ command_sets_manual ._install_command_function ('do_durian ' , cmd_set .do_durian )
295339 with pytest .raises (CommandSetRegistrationError ):
296340 command_sets_manual .register_command_set (cmd_set )
297341
@@ -316,13 +360,21 @@ def test_load_commandset_errors(command_sets_manual, capsys):
316360 assert "Deleting alias 'banana'" in err
317361 assert "Deleting macro 'apple'" in err
318362
363+ # verify command functions which don't start with "do_" raise an exception
364+ with pytest .raises (CommandSetRegistrationError ):
365+ command_sets_manual ._install_command_function ('new_cmd' , cmd_set .do_banana )
366+
367+ # verify methods which don't start with "do_" raise an exception
368+ with pytest .raises (CommandSetRegistrationError ):
369+ command_sets_manual ._install_command_function ('do_new_cmd' , cmd_set .on_register )
370+
319371 # verify duplicate commands are detected
320372 with pytest .raises (CommandSetRegistrationError ):
321- command_sets_manual ._install_command_function ('banana ' , cmd_set .do_banana )
373+ command_sets_manual ._install_command_function ('do_banana ' , cmd_set .do_banana )
322374
323375 # verify bad command names are detected
324376 with pytest .raises (CommandSetRegistrationError ):
325- command_sets_manual ._install_command_function ('bad command' , cmd_set .do_banana )
377+ command_sets_manual ._install_command_function ('do_bad command' , cmd_set .do_banana )
326378
327379 # verify error conflict with existing completer function
328380 with pytest .raises (CommandSetRegistrationError ):
0 commit comments