@@ -149,6 +149,49 @@ def test_autoload_commands(command_sets_app):
149
149
assert 'Command Set B' not in cmds_cats
150
150
151
151
152
+ def test_command_synonyms ():
153
+ """Test the use of command synonyms in CommandSets"""
154
+ class SynonymCommandSet (cmd2 .CommandSet ):
155
+ def __init__ (self , arg1 ):
156
+ super ().__init__ ()
157
+ self ._arg1 = arg1
158
+
159
+ @cmd2 .with_argparser (cmd2 .Cmd2ArgumentParser (description = "Native Command" ))
160
+ def do_builtin (self , _ ):
161
+ pass
162
+
163
+ # Create a synonym to a command inside of this CommandSet
164
+ do_builtin_synonym = do_builtin
165
+
166
+ # Create a synonym to a command outside of this CommandSet
167
+ do_help_synonym = cmd2 .Cmd .do_help
168
+
169
+ cs = SynonymCommandSet ("foo" )
170
+ app = WithCommandSets (command_sets = [cs ])
171
+
172
+ # Make sure the synonyms have the same parser as what they alias
173
+ builtin_parser = app ._command_parsers .get (app .do_builtin )
174
+ builtin_synonym_parser = app ._command_parsers .get (app .do_builtin_synonym )
175
+ assert builtin_parser is not None
176
+ assert builtin_parser is builtin_synonym_parser
177
+
178
+ help_parser = app ._command_parsers .get (cmd2 .Cmd .do_help )
179
+ help_synonym_parser = app ._command_parsers .get (app .do_help_synonym )
180
+ assert help_parser is not None
181
+ assert help_parser is help_synonym_parser
182
+
183
+ # Unregister the CommandSet and make sure built-in command and synonyms are gone
184
+ app .unregister_command_set (cs )
185
+ assert not hasattr (app , "do_builtin" )
186
+ assert not hasattr (app , "do_builtin_synonym" )
187
+ assert not hasattr (app , "do_help_synonym" )
188
+
189
+ # Make sure the help command still exists and works
190
+ assert hasattr (app , "do_help" )
191
+ out , err = run_cmd (app , 'help' )
192
+ assert app .doc_header in out
193
+
194
+
152
195
def test_custom_construct_commandsets ():
153
196
command_set_b = CommandSetB ('foo' )
154
197
@@ -291,7 +334,7 @@ def test_load_commandset_errors(command_sets_manual, capsys):
291
334
cmd_set = CommandSetA ()
292
335
293
336
# create a conflicting command before installing CommandSet to verify rollback behavior
294
- command_sets_manual ._install_command_function ('durian ' , cmd_set .do_durian )
337
+ command_sets_manual ._install_command_function ('do_durian ' , cmd_set .do_durian )
295
338
with pytest .raises (CommandSetRegistrationError ):
296
339
command_sets_manual .register_command_set (cmd_set )
297
340
@@ -316,13 +359,21 @@ def test_load_commandset_errors(command_sets_manual, capsys):
316
359
assert "Deleting alias 'banana'" in err
317
360
assert "Deleting macro 'apple'" in err
318
361
362
+ # verify command functions which don't start with "do_" raise an exception
363
+ with pytest .raises (CommandSetRegistrationError ):
364
+ command_sets_manual ._install_command_function ('new_cmd' , cmd_set .do_banana )
365
+
366
+ # verify methods which don't start with "do_" raise an exception
367
+ with pytest .raises (CommandSetRegistrationError ):
368
+ command_sets_manual ._install_command_function ('do_new_cmd' , cmd_set .on_register )
369
+
319
370
# verify duplicate commands are detected
320
371
with pytest .raises (CommandSetRegistrationError ):
321
- command_sets_manual ._install_command_function ('banana ' , cmd_set .do_banana )
372
+ command_sets_manual ._install_command_function ('do_banana ' , cmd_set .do_banana )
322
373
323
374
# verify bad command names are detected
324
375
with pytest .raises (CommandSetRegistrationError ):
325
- command_sets_manual ._install_command_function ('bad command' , cmd_set .do_banana )
376
+ command_sets_manual ._install_command_function ('do_bad command' , cmd_set .do_banana )
326
377
327
378
# verify error conflict with existing completer function
328
379
with pytest .raises (CommandSetRegistrationError ):
0 commit comments