-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
284 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from django_typer.management import TyperCommand, command, initialize | ||
|
||
|
||
class Command(TyperCommand): | ||
def finalize(self, **kwargs): | ||
assert isinstance(self, Command) | ||
return "finalized" | ||
|
||
@initialize(result_callback=finalize) | ||
def init(self): | ||
return "init" | ||
|
||
@command() | ||
def cmd(self): | ||
return self |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from django_typer.management import TyperCommand, command | ||
from typing import List | ||
|
||
|
||
def finalize(self: List["Command"], **_): | ||
assert isinstance(self[0], Command) | ||
return "finalize".format(self) | ||
|
||
|
||
class Command(TyperCommand, result_callback=finalize, chain=True): | ||
@command() | ||
def cmd1(self): | ||
return self | ||
|
||
@command() | ||
def cmd2(self): | ||
return self |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from django_typer.management import Typer | ||
|
||
|
||
def finalize(self: "Command", **_): | ||
assert isinstance(self, Command) | ||
return "finalize" | ||
|
||
|
||
app = Typer(result_callback=finalize) | ||
|
||
|
||
@app.callback(result_callback=finalize) | ||
def callback(self): | ||
assert isinstance(self, Command) | ||
return self | ||
|
||
|
||
@app.command() | ||
def cmd(self): | ||
assert isinstance(self, Command) | ||
return self |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from django_typer.management import TyperCommand, command, finalize | ||
|
||
|
||
class Command(TyperCommand): | ||
@finalize() | ||
def final(self, result, **_): | ||
assert isinstance(self, Command) | ||
assert result == "handle" | ||
return "finalized: {}".format(result) | ||
|
||
def handle(self): | ||
return "handle" |
30 changes: 30 additions & 0 deletions
30
tests/apps/test_app/management/commands/finalize_multi_kwargs.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from django_typer.management import TyperCommand, command, finalize | ||
from click import get_current_context | ||
|
||
|
||
class Command(TyperCommand, chain=True): | ||
suppressed_base_arguments = { | ||
"verbosity", | ||
"skip_checks", | ||
"version", | ||
"settings", | ||
"pythonpath", | ||
} | ||
|
||
@finalize() | ||
def final(self, result, **kwargs): | ||
assert isinstance(self, Command) | ||
click_params = getattr(get_current_context(silent=True), "params", {}) | ||
assert len(kwargs) == len(click_params) | ||
for key, value in kwargs.items(): | ||
assert key in click_params | ||
assert click_params[key] == value | ||
return "finalized: {} | {}".format(result, kwargs) | ||
|
||
@command() | ||
def cmd1(self, arg1: int = 1): | ||
return "cmd1 {}".format(arg1) | ||
|
||
@command() | ||
def cmd2(self, arg2: int): | ||
return "cmd2 {}".format(arg2) |
22 changes: 22 additions & 0 deletions
22
tests/apps/test_app/management/commands/finalize_multi_named_param.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from django_typer.management import TyperCommand, command, finalize | ||
from click import get_current_context | ||
|
||
|
||
class Command(TyperCommand, chain=True): | ||
@finalize() | ||
def final(self, result, **kwargs): | ||
assert isinstance(self, Command) | ||
click_params = getattr(get_current_context(silent=True), "params", {}) | ||
assert len(kwargs) == len(click_params) | ||
for key, value in kwargs.items(): | ||
assert key in click_params | ||
assert click_params[key] == value | ||
return "finalized: {}".format(result) | ||
|
||
@command() | ||
def cmd1(self): | ||
return "cmd1" | ||
|
||
@command() | ||
def cmd2(self): | ||
return "cmd2" |
17 changes: 17 additions & 0 deletions
17
tests/apps/test_app/management/commands/finalize_multi_no_params.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from django_typer.management import TyperCommand, command, finalize | ||
from click import get_current_context | ||
|
||
|
||
class Command(TyperCommand, chain=True): | ||
@finalize() | ||
def final(self, result): | ||
assert isinstance(self, Command) | ||
return "finalized: {}".format(result) | ||
|
||
@command() | ||
def cmd1(self): | ||
return "cmd1" | ||
|
||
@command() | ||
def cmd2(self): | ||
return "cmd2" |
45 changes: 45 additions & 0 deletions
45
tests/apps/test_app/management/commands/finalize_subgroups.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from django_typer.management import TyperCommand, command, group, finalize | ||
|
||
|
||
class Command(TyperCommand): | ||
@finalize() | ||
def root_final(self, result, **_): | ||
assert isinstance(self, Command) | ||
assert result == "handle" | ||
return "root_final" | ||
|
||
@group() | ||
def grp1(self): | ||
return "grp1" | ||
|
||
@group() | ||
def grp2(self): | ||
return "grp2" | ||
|
||
@grp1.command() | ||
def cmd1(self): | ||
return "cmd1" | ||
|
||
@grp1.command() | ||
def cmd2(self): | ||
return "cmd2" | ||
|
||
@grp2.command() | ||
def cmd3(self): | ||
return "cmd3" | ||
|
||
@grp2.command() | ||
def cmd4(self): | ||
return "cmd4" | ||
|
||
@grp1.finalize() | ||
def grp1_final(self, result, **_): | ||
assert isinstance(self, Command) | ||
assert result in ["cmd1", "cmd2"] | ||
return "grp1_final" | ||
|
||
@grp2.finalize() | ||
def grp2_final(self, result, **_): | ||
assert isinstance(self, Command) | ||
assert result in ["cmd3", "cmd4"] | ||
return "grp2_final" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import contextlib | ||
from io import StringIO | ||
|
||
from django.core.management import call_command | ||
from django.test import TestCase | ||
|
||
from django_typer.management import get_command | ||
from tests.utils import run_command | ||
|
||
|
||
class TestFinalize(TestCase): | ||
def test_finalize1(self): | ||
out = StringIO() | ||
call_command("finalize1", "cmd", stdout=out) | ||
self.assertEqual(out.getvalue(), "finalized\n") | ||
|
||
def test_finalize2(self): | ||
out = StringIO() | ||
call_command("finalize2", "cmd1", "cmd2", stdout=out) | ||
self.assertEqual(out.getvalue(), "finalize\n") | ||
|
||
def test_finalize3(self): | ||
out = StringIO() | ||
call_command("finalize3", "cmd", stdout=out) | ||
self.assertEqual(out.getvalue(), "finalize\n") | ||
|
||
def test_finalize_multi_kwargs_run(self): | ||
stdout, _, _ = run_command("finalize_multi_kwargs", "cmd1") | ||
self.assertEqual( | ||
stdout.strip(), | ||
"finalized: ['cmd1 1'] | {'force_color': False, 'no_color': False, 'traceback': False}", | ||
) | ||
|
||
stdout, _, _ = run_command("finalize_multi_kwargs", "cmd2", "3", "cmd1") | ||
self.assertEqual( | ||
stdout.strip(), | ||
"finalized: ['cmd2 3', 'cmd1 1'] | {'force_color': False, 'no_color': False, 'traceback': False}", | ||
) | ||
|
||
def test_finalize_multi_kwargs_call(self): | ||
# todo - excluded common options should not appear? | ||
call_command("finalize_multi_kwargs", "cmd1") | ||
with contextlib.redirect_stdout(StringIO()) as out: | ||
call_command("finalize_multi_kwargs", "cmd1") | ||
self.assertEqual( | ||
out.getvalue().strip(), | ||
"finalized: ['cmd1 1'] | {'force_color': False, 'no_color': False, 'traceback': False}", | ||
) | ||
|
||
out.truncate(0) | ||
out.seek(0) | ||
|
||
call_command("finalize_multi_kwargs", "cmd2", "5", "cmd1") | ||
self.assertEqual( | ||
out.getvalue().strip(), | ||
"finalized: ['cmd2 5', 'cmd1 1'] | {'force_color': False, 'no_color': False, 'traceback': False}", | ||
) | ||
|
||
out.truncate(0) | ||
out.seek(0) | ||
|
||
call_command( | ||
"finalize_multi_kwargs", | ||
"--traceback", | ||
"cmd2", | ||
"3", | ||
"cmd1", | ||
"--arg1", | ||
"2", | ||
) | ||
self.assertEqual( | ||
out.getvalue().strip(), | ||
"finalized: ['cmd2 3', 'cmd1 2'] | {'force_color': False, 'no_color': False, 'traceback': True}", | ||
) | ||
|
||
def test_finalize_multi_kwargs_obj(self): | ||
from tests.apps.test_app.management.commands.finalize_multi_kwargs import ( | ||
Command, | ||
) | ||
|
||
out = StringIO() | ||
finalize4 = get_command("finalize_multi_kwargs", Command, stdout=out) | ||
self.assertEqual( | ||
finalize4.final([finalize4.cmd1(3), finalize4.cmd2(2)]).strip(), | ||
"finalized: ['cmd1 3', 'cmd2 2'] | {}", | ||
) | ||
|
||
def test_finalize5_run(self): | ||
stdout, _, _ = run_command("finalize5") | ||
self.assertEqual(stdout.strip(), "finalized: handle") | ||
|
||
def test_finalize5_call(self): | ||
out = StringIO() | ||
call_command("finalize5", stdout=out) | ||
self.assertEqual(out.getvalue().strip(), "finalized: handle") | ||
|
||
def test_finalize5_obj(self): | ||
from tests.apps.test_app.management.commands.finalize5 import Command | ||
|
||
out = StringIO() | ||
finalize5 = get_command("finalize5", Command, stdout=out) | ||
self.assertEqual( | ||
finalize5.final(finalize5()).strip(), | ||
"finalized: handle", | ||
) |