Skip to content

Commit 44dae03

Browse files
authored
Fixes #30: Retain all options when executing __help_with_error (#35)
1 parent da32f40 commit 44dae03

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

src/manage/commands.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -841,13 +841,19 @@ def execute(self):
841841
class HelpWithErrorCommand(HelpCommand):
842842
CMD = "__help_with_error"
843843

844+
def __init__(self, args, root=None):
845+
# Essentially disable argument processing for this command
846+
super().__init__(args[:1], root)
847+
self.args = args[1:]
848+
844849
def execute(self):
845-
LOGGER.print(f"!R!Unknown command: {EXE_NAME} {' '.join(self.args)}!W!")
850+
args = [EXE_NAME, *self.args]
851+
LOGGER.print(f"!R!Unknown command: {' '.join(args)}!W!")
846852
LOGGER.print(COPYRIGHT)
847853
self.show_welcome(copyright=False)
848854
LOGGER.print(BaseCommand.usage_text())
849855
LOGGER.print(BaseCommand.subcommands_list())
850-
LOGGER.print(f"The command !R!{EXE_NAME} {' '.join(self.args)}!W! was not recognized.")
856+
LOGGER.print(f"The command !R!{' '.join(args)}!W! was not recognized.")
851857

852858

853859
class DefaultConfig(BaseCommand):

tests/conftest.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,31 @@
2121
LOGGER.level = DEBUG
2222

2323
class LogCaptureHandler(list):
24+
def skip_until(self, pattern, args=()):
25+
return ('until', pattern, args)
26+
2427
def __call__(self, *cmp):
25-
for x, y in zip(self, cmp):
26-
assert re.match(y[0], x[0])
27-
assert x[1] == y[1]
28-
assert len(self) == len(cmp)
28+
it1 = iter(self)
29+
for y in cmp:
30+
if not isinstance(y, tuple):
31+
op, pat, args = None, y, []
32+
elif len(y) == 3:
33+
op, pat, args = y
34+
elif len(y) == 2:
35+
op = None
36+
pat, args = y
37+
38+
while True:
39+
try:
40+
x = next(it1)
41+
except StopIteration:
42+
pytest.fail(f"Not enough elements were logged looking for {pat}")
43+
if op == 'until' and not re.match(pat, x[0]):
44+
continue
45+
assert re.match(pat, x[0])
46+
assert tuple(x[1]) == tuple(args)
47+
break
48+
2949

3050
@pytest.fixture
3151
def assert_log():

tests/test_commands.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pytest
2+
import secrets
3+
from manage import commands
4+
5+
6+
def test_help_with_error_command(assert_log, monkeypatch):
7+
expect = secrets.token_hex(16)
8+
cmd = commands.HelpWithErrorCommand(
9+
[commands.HelpWithErrorCommand.CMD, expect, "-v", "-q"],
10+
None
11+
)
12+
monkeypatch.setattr(commands, "EXE_NAME", "pymanager-test")
13+
monkeypatch.setattr(commands, "WELCOME", "")
14+
cmd.execute()
15+
assert_log(
16+
assert_log.skip_until(rf".*Unknown command: pymanager-test {expect} -v -q.*"),
17+
r"Python installation manager \d+\.\d+.*",
18+
assert_log.skip_until(rf"The command .*?pymanager-test {expect} -v -q.*"),
19+
)

0 commit comments

Comments
 (0)