diff --git a/src/manage/commands.py b/src/manage/commands.py index b9f389a..ce55ef3 100644 --- a/src/manage/commands.py +++ b/src/manage/commands.py @@ -841,13 +841,19 @@ def execute(self): class HelpWithErrorCommand(HelpCommand): CMD = "__help_with_error" + def __init__(self, args, root=None): + # Essentially disable argument processing for this command + super().__init__(args[:1], root) + self.args = args[1:] + def execute(self): - LOGGER.print(f"!R!Unknown command: {EXE_NAME} {' '.join(self.args)}!W!") + args = [EXE_NAME, *self.args] + LOGGER.print(f"!R!Unknown command: {' '.join(args)}!W!") LOGGER.print(COPYRIGHT) self.show_welcome(copyright=False) LOGGER.print(BaseCommand.usage_text()) LOGGER.print(BaseCommand.subcommands_list()) - LOGGER.print(f"The command !R!{EXE_NAME} {' '.join(self.args)}!W! was not recognized.") + LOGGER.print(f"The command !R!{' '.join(args)}!W! was not recognized.") class DefaultConfig(BaseCommand): diff --git a/tests/conftest.py b/tests/conftest.py index 241903f..41d993d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -21,11 +21,31 @@ LOGGER.level = DEBUG class LogCaptureHandler(list): + def skip_until(self, pattern, args=()): + return ('until', pattern, args) + def __call__(self, *cmp): - for x, y in zip(self, cmp): - assert re.match(y[0], x[0]) - assert x[1] == y[1] - assert len(self) == len(cmp) + it1 = iter(self) + for y in cmp: + if not isinstance(y, tuple): + op, pat, args = None, y, [] + elif len(y) == 3: + op, pat, args = y + elif len(y) == 2: + op = None + pat, args = y + + while True: + try: + x = next(it1) + except StopIteration: + pytest.fail(f"Not enough elements were logged looking for {pat}") + if op == 'until' and not re.match(pat, x[0]): + continue + assert re.match(pat, x[0]) + assert tuple(x[1]) == tuple(args) + break + @pytest.fixture def assert_log(): diff --git a/tests/test_commands.py b/tests/test_commands.py new file mode 100644 index 0000000..83ed443 --- /dev/null +++ b/tests/test_commands.py @@ -0,0 +1,19 @@ +import pytest +import secrets +from manage import commands + + +def test_help_with_error_command(assert_log, monkeypatch): + expect = secrets.token_hex(16) + cmd = commands.HelpWithErrorCommand( + [commands.HelpWithErrorCommand.CMD, expect, "-v", "-q"], + None + ) + monkeypatch.setattr(commands, "EXE_NAME", "pymanager-test") + monkeypatch.setattr(commands, "WELCOME", "") + cmd.execute() + assert_log( + assert_log.skip_until(rf".*Unknown command: pymanager-test {expect} -v -q.*"), + r"Python installation manager \d+\.\d+.*", + assert_log.skip_until(rf"The command .*?pymanager-test {expect} -v -q.*"), + )