Skip to content

Commit

Permalink
tests for helps
Browse files Browse the repository at this point in the history
  • Loading branch information
bckohan committed Jun 4, 2024
1 parent 583c152 commit d16a4f8
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 7 deletions.
4 changes: 2 additions & 2 deletions django_routines/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ def __len__(self):
return len(self.commands)

@property
def switches(self) -> t.Set[str]:
def switches(self) -> t.List[str]:
switches: t.Set[str] = set()
for command in self.commands:
if command.switches:
switches.update(command.switches)
return switches
return sorted(switches)

def plan(self, switches: t.Set[str]) -> t.List[RoutineCommand]:
return [
Expand Down
7 changes: 3 additions & 4 deletions django_routines/management/commands/routine.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import importlib
import importlib.util
import typing as t
from importlib.util import find_spec

import click
import typer
Expand All @@ -14,7 +13,7 @@
from django_routines import ROUTINE_SETTING, Routine, RoutineCommand, get_routine

width = 80
use_rich = importlib.util.find_spec("rich") is not None
use_rich = find_spec("rich") is not None
if use_rich:
from rich.console import Console

Expand Down Expand Up @@ -181,7 +180,7 @@ def _list(self) -> None:
ruler = f"[underline]{' ' * width}[/underline]\n" if use_rich else "-" * width
cmd_strings = "\n".join(command_strings)
cmd_strings = f"{'[bright]' if use_rich else ''}{cmd_strings}{'[/bright]' if use_rich else ''}"
help_txt = f"\b\n{routine.help_text}\n{ruler}\b\n\n{cmd_strings}\n"
help_txt = f"\b\n{routine.help_text}\n{ruler}\b\n{'' if use_rich else '\b\n'}{cmd_strings}\n"
grp = Command.group(
help=help_txt, short_help=routine.help_text, invoke_without_command=True
)(locals()[name])
Expand Down
153 changes: 152 additions & 1 deletion tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from io import StringIO
from importlib.util import find_spec
import pytest

from django.core.management import call_command, CommandError
from django.core.management import BaseCommand, call_command, CommandError
from django_typer import get_command
from django.test import TestCase
import re

Expand Down Expand Up @@ -219,3 +222,151 @@ def test_list_color(self):
def test_routine_with_bad_command(self):
with self.assertRaises(CommandError):
call_command("routine", "bad")

routine_help_rich = """
Usage: ./manage.py routine [OPTIONS] COMMAND [ARGS]...
Run batches of commands configured in settings.
╭─ Options ────────────────────────────────────────────────────────────────────╮
│ --help Show this message and exit. │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Django ─────────────────────────────────────────────────────────────────────╮
│ --verbosity INTEGER RANGE [0<=x<=3] Verbosity level; 0=minimal │
│ output, 1=normal output, │
│ 2=verbose output, 3=very │
│ verbose output │
│ [default: 1] │
│ --version Show program's version number │
│ and exit. │
│ --settings TEXT The Python path to a settings │
│ module, e.g. │
│ "myproject.settings.main". If │
│ this isn't provided, the │
│ DJANGO_SETTINGS_MODULE │
│ environment variable will be │
│ used. │
│ --pythonpath PATH A directory to add to the │
│ Python path, e.g. │
│ "/home/djangoprojects/myproje… │
│ [default: None] │
│ --traceback Raise on CommandError │
│ exceptions │
│ --no-color Don't colorize the command │
│ output. │
│ --force-color Force colorization of the │
│ command output. │
│ --skip-checks Skip system checks. │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Commands ───────────────────────────────────────────────────────────────────╮
│ bad Bad command test routine │
│ deploy Deploy the site application into production. │
│ test Test Routine 1 │
╰──────────────────────────────────────────────────────────────────────────────╯
"""

routine_test_help_rich = """
Usage: ./manage.py routine test [OPTIONS] COMMAND [ARGS]...
Test Routine 1
[0] track 2 | initial, demo
[1] track 0 (verbosity=0) | initial
[3] track 3 (demo=2)
[3] track 4 (demo=6)
[4] track 1
[6] track 5 | demo
╭─ Options ────────────────────────────────────────────────────────────────────╮
│ --all Include all switched commands. │
│ --demo │
│ --initial │
│ --help Show this message and exit. │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Commands ───────────────────────────────────────────────────────────────────╮
│ list List the commands that will be run. │
╰──────────────────────────────────────────────────────────────────────────────╯
"""

@pytest.mark.skipif(find_spec("rich") is None, reason="Rich is not installed.")
def test_helps_rich(self):
stdout = StringIO()
routine = get_command("routine", BaseCommand, stdout=stdout, no_color=True)
routine.print_help("./manage.py", "routine")
self.assertEqual(stdout.getvalue().strip(), self.routine_help_rich.strip())
stdout.truncate(0)
stdout.seek(0)

routine.print_help("./manage.py", "routine", "test")
self.assertEqual(
stdout.getvalue().strip().replace("\x08", ""),
self.routine_test_help_rich.strip(),
)

routine_help_no_rich = """
Usage: ./manage.py routine [OPTIONS] COMMAND [ARGS]...
Run batches of commands configured in settings.
Options:
--verbosity INTEGER RANGE Verbosity level; 0=minimal output, 1=normal
output, 2=verbose output, 3=very verbose output
[default: 1; 0<=x<=3]
--version Show program's version number and exit.
--settings TEXT The Python path to a settings module, e.g.
"myproject.settings.main". If this isn't
provided, the DJANGO_SETTINGS_MODULE environment
variable will be used.
--pythonpath PATH A directory to add to the Python path, e.g.
"/home/djangoprojects/myproject".
--traceback Raise on CommandError exceptions
--no-color Don't colorize the command output.
--force-color Force colorization of the command output.
--skip-checks Skip system checks.
--help Show this message and exit.
Commands:
bad Bad command test routine
deploy Deploy the site application into production.
test Test Routine 1
"""

routine_test_help_no_rich = """
Usage: ./manage.py routine test [OPTIONS] COMMAND [ARGS]...
Test Routine 1
-----------------------------------
[0] track 2 | initial, demo
[1] track 0 (verbosity=0) | initial
[3] track 3 (demo=2)
[3] track 4 (demo=6)
[4] track 1
[6] track 5 | demo
Options:
--all Include all switched commands.
--demo
--initial
--help Show this message and exit.
Commands:
list List the commands that will be run.
"""

@pytest.mark.skipif(find_spec("rich") is not None, reason="Rich is installed.")
def test_helps_no_rich(self):
stdout = StringIO()
routine = get_command("routine", BaseCommand, stdout=stdout, no_color=True)
routine.print_help("./manage.py", "routine")
self.assertEqual(stdout.getvalue().strip(), self.routine_help_no_rich.strip())
stdout.truncate(0)
stdout.seek(0)

routine.print_help("./manage.py", "routine", "test")
self.assertEqual(
stdout.getvalue().strip().replace("\x08", ""),
self.routine_test_help_no_rich.strip(),
)

0 comments on commit d16a4f8

Please sign in to comment.