Skip to content

Commit f452966

Browse files
committed
Make show_usage() function print directly and add tests
1 parent a769ff1 commit f452966

File tree

3 files changed

+45
-19
lines changed

3 files changed

+45
-19
lines changed

src/manage/commands.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ def execute(self):
513513
raise NotImplementedError(f"'{type(self).__name__}' does not implement 'execute()'")
514514

515515
@classmethod
516-
def usage_text_lines(cls):
516+
def show_usage(cls):
517517
if EXE_NAME.casefold() in ("py".casefold(), "pyw".casefold()):
518518
usage_docs = PY_USAGE_DOCS
519519
else:
@@ -538,38 +538,34 @@ def usage_text_lines(cls):
538538
usage_ljust = max(usage_ljust, 16) + 1
539539
sp = " " * usage_ljust
540540

541-
yield "!G!Usage:!W!"
541+
LOGGER.print("!G!Usage:!W!")
542542
for k, d in usage_docs:
543543
if k.endswith("\n") and len(logging.strip_colour(k)) >= usage_ljust:
544-
yield k.rstrip()
544+
LOGGER.print(k.rstrip())
545545
r = sp
546546
else:
547547
k = k.rstrip()
548548
r = k.ljust(usage_ljust + len(k) - len(logging.strip_colour(k)))
549549
for b in d.split(" "):
550550
if len(r) >= logging.CONSOLE_MAX_WIDTH:
551-
yield r.rstrip()
551+
LOGGER.print(r.rstrip())
552552
r = sp
553553
r += b + " "
554554
if r.rstrip():
555-
yield r
555+
LOGGER.print(r)
556556

557-
yield ""
557+
LOGGER.print()
558558
# TODO: Remove the 3.14 for stable release
559-
yield "Find additional information at !B!https://docs.python.org/3.14/using/windows!W!."
560-
yield ""
561-
562-
@classmethod
563-
def usage_text(cls):
564-
return "\n".join(cls.usage_text_lines())
559+
LOGGER.print("Find additional information at !B!https://docs.python.org/3.14/using/windows!W!.")
560+
LOGGER.print()
565561

566562
@classmethod
567563
def help_text(cls):
568564
return GLOBAL_OPTIONS_HELP_TEXT.replace("\r\n", "\n")
569565

570566
def help(self):
571567
if type(self) is BaseCommand:
572-
LOGGER.print(self.usage_text())
568+
self.show_usage()
573569
LOGGER.print(self.help_text())
574570
try:
575571
LOGGER.print(self.HELP_TEXT.lstrip())
@@ -857,7 +853,7 @@ def execute(self):
857853
LOGGER.print(COPYRIGHT)
858854
self.show_welcome(copyright=False)
859855
if not self.args:
860-
LOGGER.print(BaseCommand.usage_text())
856+
self.show_usage()
861857
LOGGER.print(BaseCommand.help_text())
862858
for a in self.args:
863859
try:
@@ -884,7 +880,7 @@ def execute(self):
884880
LOGGER.print(f"!R!Unknown command: {' '.join(args)}!W!")
885881
LOGGER.print(COPYRIGHT)
886882
self.show_welcome(copyright=False)
887-
LOGGER.print(BaseCommand.usage_text())
883+
self.show_usage()
888884
LOGGER.print(f"The command !R!{' '.join(args)}!W! was not recognized.")
889885

890886

tests/conftest.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
manage.EXE_NAME = "pymanager-pytest"
2222

2323

24+
import manage.commands
25+
manage.commands.WELCOME = ""
26+
27+
2428
from manage.logging import LOGGER, DEBUG
2529
LOGGER.level = DEBUG
2630

tests/test_commands.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,44 @@
33
from manage import commands
44

55

6-
def test_help_with_error_command(assert_log, monkeypatch):
6+
def test_pymanager_help_command(assert_log):
7+
cmd = commands.HelpCommand([commands.HelpCommand.CMD], None)
8+
cmd.execute()
9+
assert_log(
10+
assert_log.skip_until(r"Python installation manager \d+\.\d+.*"),
11+
assert_log.skip_until(".*pymanager-pytest exec -V.*"),
12+
assert_log.skip_until(".*pymanager-pytest exec -3.*"),
13+
assert_log.skip_until(".*pymanager-pytest install.*"),
14+
assert_log.skip_until(".*pymanager-pytest list.*"),
15+
assert_log.skip_until(".*pymanager-pytest uninstall.*"),
16+
)
17+
18+
19+
def test_py_help_command(assert_log, monkeypatch):
20+
monkeypatch.setattr(commands, "EXE_NAME", "py")
21+
cmd = commands.HelpCommand([commands.HelpCommand.CMD], None)
22+
cmd.execute()
23+
assert_log(
24+
assert_log.skip_until(r"Python installation manager \d+\.\d+.*"),
25+
assert_log.skip_until(".*pymanager-pytest -V.*"),
26+
assert_log.skip_until(".*pymanager-pytest -3.*"),
27+
assert_log.skip_until(".*py install.*"),
28+
assert_log.skip_until(".*py list.*"),
29+
assert_log.skip_until(".*py uninstall.*"),
30+
)
31+
32+
33+
def test_help_with_error_command(assert_log):
734
expect = secrets.token_hex(16)
835
cmd = commands.HelpWithErrorCommand(
936
[commands.HelpWithErrorCommand.CMD, expect, "-v", "-q"],
1037
None
1138
)
12-
monkeypatch.setattr(commands, "WELCOME", "")
1339
cmd.execute()
1440
assert_log(
15-
assert_log.skip_until(rf".*Unknown command: pymanager-pytest {expect} -v -q.*"),
41+
assert_log.skip_until(f".*Unknown command: pymanager-pytest {expect} -v -q.*"),
1642
r"Python installation manager \d+\.\d+.*",
17-
assert_log.skip_until(rf"The command .*?pymanager-pytest {expect} -v -q.*"),
43+
assert_log.skip_until(f"The command .*?pymanager-pytest {expect} -v -q.*"),
1844
)
1945

2046

0 commit comments

Comments
 (0)