Skip to content

Commit 2591e3d

Browse files
committed
Add format test
1 parent 805f9c1 commit 2591e3d

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

tests/forms/test_validator.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import contextlib
2-
import os
31
import pathlib
42

53
import pytest
@@ -8,22 +6,13 @@
86
from click.testing import CliRunner
97
from pydantic import ValidationError
108

9+
from ..helper import switch_cwd
1110
from beanhub_cli.forms.validator import format_loc
1211
from beanhub_cli.forms.validator import merge_index_loc
1312
from beanhub_cli.forms.validator import validate_doc
1413
from beanhub_cli.main import cli
1514

1615

17-
@contextlib.contextmanager
18-
def switch_cwd(cwd: pathlib.Path):
19-
current_cwd = pathlib.Path.cwd()
20-
try:
21-
os.chdir(cwd)
22-
yield
23-
finally:
24-
os.chdir(current_cwd)
25-
26-
2716
@pytest.mark.parametrize(
2817
"loc, expected",
2918
[
@@ -118,9 +107,11 @@ def test_bad_schema(tmp_path: pathlib.Path, schema: dict, expected_errors: list)
118107
yaml.dump(schema, fo)
119108
with pytest.raises(ValidationError) as exc:
120109
validate_doc(doc_file)
110+
121111
def del_url(d):
122112
del d["url"]
123113
return d
114+
124115
assert list(map(del_url, exc.value.errors())) == expected_errors
125116

126117

tests/helper.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import contextlib
2+
import os
3+
import pathlib
4+
5+
6+
@contextlib.contextmanager
7+
def switch_cwd(cwd: pathlib.Path):
8+
current_cwd = pathlib.Path.cwd()
9+
try:
10+
os.chdir(cwd)
11+
yield
12+
finally:
13+
os.chdir(current_cwd)

tests/test_format.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pathlib
2+
import textwrap
3+
4+
from click.testing import CliRunner
5+
6+
from .helper import switch_cwd
7+
from beanhub_cli.main import cli
8+
9+
10+
def test_format_cmd(tmp_path: pathlib.Path, cli_runner: CliRunner):
11+
beanhub_dir = tmp_path / ".beanhub"
12+
beanhub_dir.mkdir()
13+
14+
bean_file = beanhub_dir / "sample.bean"
15+
bean_file.write_text(
16+
textwrap.dedent(
17+
"""\
18+
2024-06-27 open Assets:Cash
19+
"""
20+
)
21+
)
22+
23+
cli_runner.mix_stderr = False
24+
with switch_cwd(tmp_path):
25+
result = cli_runner.invoke(cli, ["format", str(bean_file)])
26+
assert result.exit_code == 0
27+
assert bean_file.read_text() == "2024-06-27 open Assets:Cash\n"

0 commit comments

Comments
 (0)