Skip to content

Commit

Permalink
fix tests for no rich #166
Browse files Browse the repository at this point in the history
  • Loading branch information
bckohan committed Feb 5, 2025
1 parent 8b500af commit 00072fa
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django_typer.management import TyperCommand, COMMON_DEFAULTS
from typer.models import Context as TyperContext
from tests.utils import rich_installed


class Command(TyperCommand):
Expand All @@ -11,5 +12,10 @@ def handle(self, ctx: TyperContext):
assert self.__class__ is Command
assert isinstance(ctx, TyperContext)
assert not set(ctx.params.keys()).symmetric_difference(
[key for key in COMMON_DEFAULTS.keys() if key != "hide_locals"]
[
key
for key in COMMON_DEFAULTS.keys()
if key
not in ["hide_locals", *(["show_locals"] if not rich_installed else [])]
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
group,
)
from typer.models import Context as TyperContext
from tests.utils import rich_installed


class Command(TyperCommand):
Expand Down Expand Up @@ -42,7 +43,12 @@ def check_context(self, ctx, traceback, skip_checks=None):
else "skip_checks" in ctx.params
)
assert "traceback" in ctx.params
for param in [key for key in COMMON_DEFAULTS.keys() if key != "hide_locals"]:
for param in [
key
for key in COMMON_DEFAULTS.keys()
if key
not in ["hide_locals", *(["show_locals"] if not rich_installed else [])]
]:
if param not in self.suppressed_base_arguments:
assert param in ctx.params
return f"traceback={traceback}, skipchecks={skip_checks}"
38 changes: 28 additions & 10 deletions tests/test_finalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,32 @@
from django.test import TestCase

from django_typer.management import get_command
from tests.utils import run_command
from tests.utils import run_command, rich_installed


class TestFinalize(TestCase):
def test_finalize_multi_kwargs_run(self, command="finalize_multi_kwargs"):
def test_finalize_multi_kwargs_run(
self, command="finalize_multi_kwargs", show_locals=rich_installed
):
stdout, _, _ = run_command(command, "cmd1")
self.assertEqual(
stdout.strip(),
"finalized: ['cmd1 1'] | {'force_color': False, 'no_color': False, 'traceback': False, 'show_locals': None}",
"finalized: ['cmd1 1'] | {'force_color': False, 'no_color': False, 'traceback': False, 'show_locals': None}"
if show_locals
else "finalized: ['cmd1 1'] | {'force_color': False, 'no_color': False, 'traceback': False}",
)
stdout, _, _ = run_command(command, "cmd2", "3", "cmd1")
self.assertEqual(
stdout.strip(),
"finalized: ['cmd2 3', 'cmd1 1'] | {'force_color': False, 'no_color': False, 'traceback': False, 'show_locals': None}",
"finalized: ['cmd2 3', 'cmd1 1'] | {'force_color': False, 'no_color': False, 'traceback': False, 'show_locals': None}"
if show_locals
else "finalized: ['cmd2 3', 'cmd1 1'] | {'force_color': False, 'no_color': False, 'traceback': False}",
)

def test_finalize_multi_named_param_run(self):
self.test_finalize_multi_kwargs_run(command="finalize_multi_named_param")
self.test_finalize_multi_kwargs_run(
command="finalize_multi_named_param", show_locals=True
)

def test_finalize_no_params_run(self):
stdout, _, _ = run_command("finalize_multi_no_params", "cmd1")
Expand All @@ -37,14 +45,18 @@ def test_finalize_no_params_run(self):
"finalized: ['cmd2 3', 'cmd1 1']",
)

def test_finalize_multi_kwargs_call(self, command="finalize_multi_kwargs"):
def test_finalize_multi_kwargs_call(
self, command="finalize_multi_kwargs", show_locals=rich_installed
):
# todo - excluded common options should not appear?
call_command(command, "cmd1")
with contextlib.redirect_stdout(StringIO()) as out:
call_command(command, "cmd1")
self.assertEqual(
out.getvalue().strip(),
"finalized: ['cmd1 1'] | {'force_color': False, 'no_color': False, 'traceback': False, 'show_locals': None}",
"finalized: ['cmd1 1'] | {'force_color': False, 'no_color': False, 'traceback': False, 'show_locals': None}"
if show_locals
else "finalized: ['cmd1 1'] | {'force_color': False, 'no_color': False, 'traceback': False}",
)

out.truncate(0)
Expand All @@ -53,7 +65,9 @@ def test_finalize_multi_kwargs_call(self, command="finalize_multi_kwargs"):
call_command(command, "cmd2", "5", "cmd1")
self.assertEqual(
out.getvalue().strip(),
"finalized: ['cmd2 5', 'cmd1 1'] | {'force_color': False, 'no_color': False, 'traceback': False, 'show_locals': None}",
"finalized: ['cmd2 5', 'cmd1 1'] | {'force_color': False, 'no_color': False, 'traceback': False, 'show_locals': None}"
if show_locals
else "finalized: ['cmd2 5', 'cmd1 1'] | {'force_color': False, 'no_color': False, 'traceback': False}",
)

out.truncate(0)
Expand All @@ -70,11 +84,15 @@ def test_finalize_multi_kwargs_call(self, command="finalize_multi_kwargs"):
)
self.assertEqual(
out.getvalue().strip(),
"finalized: ['cmd2 3', 'cmd1 2'] | {'force_color': False, 'no_color': False, 'traceback': True, 'show_locals': None}",
"finalized: ['cmd2 3', 'cmd1 2'] | {'force_color': False, 'no_color': False, 'traceback': True, 'show_locals': None}"
if show_locals
else "finalized: ['cmd2 3', 'cmd1 2'] | {'force_color': False, 'no_color': False, 'traceback': True}",
)

def test_finalize_multi_named_param_call(self):
self.test_finalize_multi_kwargs_call(command="finalize_multi_named_param")
self.test_finalize_multi_kwargs_call(
command="finalize_multi_named_param", show_locals=True
)

def test_finalize_multi_no_params(self):
# todo - excluded common options should not appear?
Expand Down
14 changes: 10 additions & 4 deletions tests/test_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ def test_base_class_group_interface_matches(self):
def test_action_nargs(self):
# unclear if nargs is even necessary - no other test seems to exercise it, leaving in for
# base class compat reasons
from tests.utils import rich_installed

self.assertEqual(
get_command("basic")
.create_parser("./manage.py", "basic")
Expand All @@ -251,10 +253,14 @@ def test_action_nargs(self):
-1,
)
multi_parser = get_command("multi").create_parser("./manage.py", "multi")
self.assertEqual(multi_parser._actions[8].param.name, "files")
self.assertEqual(multi_parser._actions[8].nargs, -1)
self.assertEqual(multi_parser._actions[9].param.name, "flag1")
self.assertEqual(multi_parser._actions[9].nargs, 0)
self.assertEqual(
multi_parser._actions[8 if rich_installed else 7].param.name, "files"
)
self.assertEqual(multi_parser._actions[8 if rich_installed else 7].nargs, -1)
self.assertEqual(
multi_parser._actions[9 if rich_installed else 8].param.name, "flag1"
)
self.assertEqual(multi_parser._actions[9 if rich_installed else 8].nargs, 0)

def test_cmd_getattr(self):
from django_typer.management import TyperCommand
Expand Down

0 comments on commit 00072fa

Please sign in to comment.