Test the use of command synonyms in CommandSets
()
| 150 | |
| 151 | |
| 152 | def test_command_synonyms() -> None: |
| 153 | """Test the use of command synonyms in CommandSets""" |
| 154 | |
| 155 | class SynonymCommandSet(cmd2.CommandSet): |
| 156 | def __init__(self, arg1) -> None: |
| 157 | super().__init__() |
| 158 | self._arg1 = arg1 |
| 159 | |
| 160 | @cmd2.with_argparser(cmd2.Cmd2ArgumentParser(description="Native Command")) |
| 161 | def do_builtin(self, _) -> None: |
| 162 | """Builtin Command""" |
| 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 with subcommands. |
| 168 | # This will best test the synonym check in cmd2.Cmd._check_uninstallable() when |
| 169 | # we unregister this CommandSet. |
| 170 | do_alias_synonym = cmd2.Cmd.do_alias |
| 171 | |
| 172 | cs = SynonymCommandSet("foo") |
| 173 | app = WithCommandSets(command_sets=[cs]) |
| 174 | |
| 175 | # Make sure the synonyms have the same parser as what they alias |
| 176 | builtin_parser = app.command_parsers.get(app.do_builtin) |
| 177 | builtin_synonym_parser = app.command_parsers.get(app.do_builtin_synonym) |
| 178 | assert builtin_parser is not None |
| 179 | assert builtin_parser is builtin_synonym_parser |
| 180 | |
| 181 | alias_parser = app.command_parsers.get(cmd2.Cmd.do_alias) |
| 182 | alias_synonym_parser = app.command_parsers.get(app.do_alias_synonym) |
| 183 | assert alias_parser is not None |
| 184 | assert alias_parser is alias_synonym_parser |
| 185 | |
| 186 | # Unregister the CommandSet and make sure built-in command and synonyms are gone |
| 187 | app.unregister_command_set(cs) |
| 188 | assert not hasattr(app, "do_builtin") |
| 189 | assert not hasattr(app, "do_builtin_synonym") |
| 190 | assert not hasattr(app, "do_alias_synonym") |
| 191 | |
| 192 | # Make sure the alias command still exists, has the same parser, and works. |
| 193 | assert alias_parser is app.command_parsers.get(cmd2.Cmd.do_alias) |
| 194 | out, _err = run_cmd(app, "alias --help") |
| 195 | assert normalize(alias_parser.format_help())[0] in out |
| 196 | |
| 197 | |
| 198 | def test_custom_construct_commandsets() -> None: |
nothing calls this directly
no test coverage detected
searching dependent graphs…