@@ -149,6 +149,50 @@ 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
+
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
+
152
196
def test_custom_construct_commandsets ():
153
197
command_set_b = CommandSetB ('foo' )
154
198
@@ -291,7 +335,7 @@ def test_load_commandset_errors(command_sets_manual, capsys):
291
335
cmd_set = CommandSetA ()
292
336
293
337
# 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 )
295
339
with pytest .raises (CommandSetRegistrationError ):
296
340
command_sets_manual .register_command_set (cmd_set )
297
341
@@ -316,13 +360,21 @@ def test_load_commandset_errors(command_sets_manual, capsys):
316
360
assert "Deleting alias 'banana'" in err
317
361
assert "Deleting macro 'apple'" in err
318
362
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
+
319
371
# verify duplicate commands are detected
320
372
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 )
322
374
323
375
# verify bad command names are detected
324
376
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 )
326
378
327
379
# verify error conflict with existing completer function
328
380
with pytest .raises (CommandSetRegistrationError ):
0 commit comments