Skip to content

Commit

Permalink
#75: fixed tests, bx post now tested with subprocess
Browse files Browse the repository at this point in the history
epogrebnyak committed Jan 13, 2024
1 parent 4bd12e8 commit 4032651
Showing 4 changed files with 96 additions and 6 deletions.
3 changes: 2 additions & 1 deletion abacus/typer_cli/app.py
Original file line number Diff line number Diff line change
@@ -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")
54 changes: 54 additions & 0 deletions abacus/typer_cli/sample.py
Original file line number Diff line number Diff line change
@@ -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 <module>
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'
"""
41 changes: 36 additions & 5 deletions tests/test_typer_cli.py
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 4 additions & 0 deletions tests/x.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import subprocess

r = subprocess.run(["echo", "100"], text=True, capture_output=True)
print(r)

0 comments on commit 4032651

Please sign in to comment.