Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify baseline names in snapshot testing #105

Merged
merged 2 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ repos:
- id: check-added-large-files

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.9.3
rev: v0.9.7
hooks:
- id: ruff
- id: ruff-format

- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.43.0
rev: v0.44.0
hooks:
- id: markdownlint-fix
19 changes: 7 additions & 12 deletions tests/test_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,27 @@ def _format(script: zeekscript.Script):
return buf.getvalue()


def _get_samples():
def _get_samples() -> list[Path]:
"""Helper to enumerate samples"""

# We exclude directories since we store snapshots along with the samples.
# This assumes that there are no tests in subdirectories of `SAMPLES_DIR`.
try:
# Make sample order deterministic.
return sorted([sample for sample in SAMPLES_DIR.iterdir() if sample.is_file()])
except FileNotFoundError:
return []
assert SAMPLES_DIR.exists(), "directory with code samples not found"
return sorted([sample for sample in SAMPLES_DIR.iterdir() if sample.is_file()])


# For each file in `SAMPLES_DIR` test formatting of the file.
@pytest.mark.parametrize("sample", _get_samples())
@pytest.mark.parametrize(
"sample", _get_samples(), ids=[str(os.path.basename(s)) for s in _get_samples()]
)
def test_samples(sample: Path, snapshot: SnapshotAssertion):
input_ = zeekscript.Script(sample)

assert input_.parse(), f"failed to parse input {sample}"
assert not input_.has_error(), f"parse result for {sample} has parse errors"

name = str(os.path.basename(sample))

output = _format(input_)
assert output == snapshot(name=name), (
f"formatted {sample} inconsistent with snapshot"
)
assert output == snapshot(), f"formatted {sample} inconsistent with snapshot"

output2 = _format(input_)
assert output2 == output, f"idempotency violation for {sample}"
Loading