diff --git a/abacus/typer_cli/app.py b/abacus/typer_cli/app.py index 30b9fe9e..0cf941be 100644 --- a/abacus/typer_cli/app.py +++ b/abacus/typer_cli/app.py @@ -11,7 +11,8 @@ from abacus.typer_cli.show import show app = typer.Typer( - add_completion=False, help="A minimal yet valid double entry accounting system." + # add_completion=False, + help="A minimal yet valid double entry accounting system." ) app.add_typer(chart, name="chart") app.add_typer(ledger, name="ledger") diff --git a/abacus/typer_cli/sample.py b/abacus/typer_cli/sample.py new file mode 100644 index 00000000..77f2c015 --- /dev/null +++ b/abacus/typer_cli/sample.py @@ -0,0 +1,54 @@ +import click +import typer + +app = typer.Typer() + + +@app.command() +def top(): + """ + Top level command, form Typer + """ + print("The Typer app is at the top level") + + +@app.callback() +def callback(): + """ + Typer app, including Click subapp + """ + + +@click.command() +@click.option("--name", prompt="Your name", help="The person to greet.") +def hello(name): + """Simple program that greets NAME for a total of COUNT times.""" + click.echo("Hello %s!" % name) + + +typer_click_object = typer.main.get_command(app) + +typer_click_object.add_command(hello, "hello") + +from typer.testing import CliRunner + +typer_click_object._add_completion = False +typer_click_object.registered_callback = lambda x: ... +typer_click_object.pretty_exceptions_short = [] +# AttributeError: 'TyperGroup' object has no attribute 'registered_commands' + +runner = CliRunner() +runner.invoke(typer_click_object, ["hello", "--name", "Camila"]) +print(runner) + +""" +/home/codespace/.python/current/bin/python3 /workspaces/abacus/abacus/typer_cli/sample.py +Traceback (most recent call last): + File "/workspaces/abacus/abacus/typer_cli/sample.py", line 36, in + runner.invoke(typer_click_object, ["hello", "--name", "Camila"]) + File "/home/codespace/.python/current/lib/python3.10/site-packages/typer/testing.py", line 20, in invoke + use_cli = _get_command(app) + File "/home/codespace/.python/current/lib/python3.10/site-packages/typer/main.py", line 341, in get_command + if typer_instance._add_completion: +AttributeError: 'TyperGroup' object has no attribute '_add_completion' +""" diff --git a/tests/test_typer_cli.py b/tests/test_typer_cli.py index 1d28a245..4874d05b 100644 --- a/tests/test_typer_cli.py +++ b/tests/test_typer_cli.py @@ -5,7 +5,7 @@ import pytest from typer.testing import CliRunner -from abacus.typer_cli.app import combined_typer_click_app as app +from abacus.typer_cli.app import app from abacus.typer_cli.base import UserChartCLI from abacus.user_chart import AccountLabel, T @@ -94,6 +94,7 @@ class Setting: @pytest.mark.parametrize("setting", [chart_setting]) @pytest.mark.cli def test_script_as_setting(setting): + runner = CliRunner() with runner.isolated_filesystem() as f: for line in setting.script.split("\n"): if line: @@ -110,6 +111,14 @@ class Line: comment: str | None = None stdout_contains: list[str] = field(default_factory=list) + @property + def exit_code(self): + match self: + case HappyLine(_, _): + return 0 + case SadLine(_, _): + return 1 + @property def args(self): return split(self.script) @@ -127,6 +136,31 @@ class SadLine(Line): ... +import subprocess + + +def assert_subprocess(command: str, line: Line): + args = [command] + line.args + result = subprocess.run(args, text=True, capture_output=True) + print + print(result.stdout) + print(result.stderr) + assert result.returncode == line.exit_code + for s in line.stdout_contains: + assert s in result.stdout + + +def test_post_zzz(): + h = ( + HappyLine( + "post --entry asset:cash capital:eq 1000 --credit income:sales 50 --credit liability:vat 10 --debit asset:ar 60 --strict" + ) + .prints("True") + .prints("income:sales") + ) + assert_subprocess("bx", h) + + def all_happy(script: str): return [HappyLine(line) for line in script.split("\n") if line] @@ -141,10 +175,6 @@ def all_happy(script: str): [HappyLine("ledger init")], [SadLine("ledger unlink --yes")], [HappyLine("ledger init"), HappyLine("ledger unlink --yes")], - [HappyLine( - "post --entry asset:cash capital:eq 1000 --credit income:sales 50 --credit liability:vat 10 --debit asset:ar 60 --strict" - ).prints("True").prints("income:sales") - ], all_happy( """ chart init @@ -157,6 +187,7 @@ def all_happy(script: str): ) @pytest.mark.cli def test_by_line(lines): + runner = CliRunner() with runner.isolated_filesystem(): for line in lines: print(line) diff --git a/tests/x.py b/tests/x.py new file mode 100644 index 00000000..268008d2 --- /dev/null +++ b/tests/x.py @@ -0,0 +1,4 @@ +import subprocess + +r = subprocess.run(["echo", "100"], text=True, capture_output=True) +print(r)