Skip to content

Commit

Permalink
tests: Add basic test runner using pytest.
Browse files Browse the repository at this point in the history
  • Loading branch information
VorpalBlade committed Oct 10, 2022
1 parent e3b3f93 commit ab92a11
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
*~
.pytest_cache
__pycache__
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ Optional:

* python-keyring is required for the `keyring` transform to pull passwords from
kwallet/gnome-keyring/etc.
* pytest to run unit tests (only needed if you develop and submit patches).

## Limitations

Expand Down
28 changes: 28 additions & 0 deletions tests/data/basics.expected.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
line_both = True
line_sys_ignored=False

line_src=True
[section-both]
line_both = True
line_both_ignored = False
line_sys_ignored = False
line_formatting_mismatch_1=123
line_formatting_mismatch_2 = 123

line_formatting_nospaces_src=123
line_src = True
[section-both-ignored]
line_both = True


[section-sys]
line_sys_ignored = False

[section-sys-ignored]
line_sys = True
line_sys_ignored = False

[section][weird-kde-style]
something = Yes really
[section-src]
line_src = True
7 changes: 7 additions & 0 deletions tests/data/basics.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"args": [
"-is", "section-sys-ignored",
"-is", "section-src-ignored",
"-ikr", ".*", ".*_ignored$"
]
}
26 changes: 26 additions & 0 deletions tests/data/basics.src.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
line_both = True
line_src=True
line_src_ignored=False

[section-both]
line_both = True
line_both_ignored = True
line_src = True
line_src_ignored = False
line_formatting_nospaces_src=123
line_formatting_mismatch_1=123
line_formatting_mismatch_2 = 123

[section-both-ignored]
line_both = True

[section-src]
line_src = True
line_src_ignored = False

[section-src-ignored]
line_src = True
line_src_ignored = False

[section][weird-kde-style]
something = Yes really
29 changes: 29 additions & 0 deletions tests/data/basics.sys.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
line_both = True
line_sys=True
line_sys_ignored=False

[section-both]
line_both = False
line_both_ignored = False
line_sys = True
line_sys_ignored = False
line_formatting_nospaces_sys=123
line_formatting_mismatch_1 = 123
line_formatting_mismatch_2=123

[section-both-ignored]
line_both = False

[section-sys1]
line_sys = True

[section-sys]
line_sys = True
line_sys_ignored = False

[section-sys-ignored]
line_sys = True
line_sys_ignored = False

[section][weird-kde-style]
something = Yes really
34 changes: 34 additions & 0 deletions tests/test_runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import json
from pathlib import Path
import pytest
import subprocess

_TESTS_DIR = Path(__file__).parent
_DATA_DIR = _TESTS_DIR / "data"
_CHEZMOI_INI_MANAGER = _TESTS_DIR.parent / "bin" / "chezmoi_ini_manager.py"

# Discover tests
_TESTS = [e.stem for e in _DATA_DIR.glob("*.json")]


@pytest.mark.parametrize("name", _TESTS)
def test_example(name: str):
base_name = _DATA_DIR / name
with base_name.with_suffix(".json").open(mode="rt") as f:
options = json.load(f)
with base_name.with_suffix(".sys.ini").open(mode="rb") as sys_file:

result = subprocess.run(
[_CHEZMOI_INI_MANAGER, "-s", base_name.with_suffix(".src.ini")]
+ options["args"],
stdin=sys_file,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
assert result.returncode == options.get("expected_retcode", 0)
assert result.stderr.decode("utf-8", errors="strict") == options.get(
"expected_stderr", ""
)
with base_name.with_suffix(".expected.ini").open(mode="rb") as expected_file:
expected_data = expected_file.read()
assert expected_data.split(b"\n") == result.stdout.split(b"\n")

0 comments on commit ab92a11

Please sign in to comment.