From 655a8b3ee30cda0ee8e5ac1acbb700a9c29e2b6e Mon Sep 17 00:00:00 2001 From: Benjamin Bannier Date: Sun, 26 Jan 2025 19:02:16 +0100 Subject: [PATCH 1/2] Simplify baseline names in snapshot testing We previously would include a running number for the specific test parameterization in the snapshot for each sample test. This meant that tests could get renamed whenever a new sample was added. With this patch we now override the default name enumerating each parameterization and instead set it to the name of the sample a test runs on. With that snapshots should be much more stable. --- ...id.zeek].raw => test_samples[id.zeek].raw} | 0 ...eek].raw => test_samples[pragma.zeek].raw} | 0 ...zeek].raw => test_samples[redef.zeek].raw} | 0 ...zeek].raw => test_samples[test1.zeek].raw} | 0 tests/test_samples.py | 19 +++++++------------ 5 files changed, 7 insertions(+), 12 deletions(-) rename tests/__snapshots__/test_samples/{test_samples[sample0][id.zeek].raw => test_samples[id.zeek].raw} (100%) rename tests/__snapshots__/test_samples/{test_samples[sample1][pragma.zeek].raw => test_samples[pragma.zeek].raw} (100%) rename tests/__snapshots__/test_samples/{test_samples[sample2][redef.zeek].raw => test_samples[redef.zeek].raw} (100%) rename tests/__snapshots__/test_samples/{test_samples[sample3][test1.zeek].raw => test_samples[test1.zeek].raw} (100%) diff --git a/tests/__snapshots__/test_samples/test_samples[sample0][id.zeek].raw b/tests/__snapshots__/test_samples/test_samples[id.zeek].raw similarity index 100% rename from tests/__snapshots__/test_samples/test_samples[sample0][id.zeek].raw rename to tests/__snapshots__/test_samples/test_samples[id.zeek].raw diff --git a/tests/__snapshots__/test_samples/test_samples[sample1][pragma.zeek].raw b/tests/__snapshots__/test_samples/test_samples[pragma.zeek].raw similarity index 100% rename from tests/__snapshots__/test_samples/test_samples[sample1][pragma.zeek].raw rename to tests/__snapshots__/test_samples/test_samples[pragma.zeek].raw diff --git a/tests/__snapshots__/test_samples/test_samples[sample2][redef.zeek].raw b/tests/__snapshots__/test_samples/test_samples[redef.zeek].raw similarity index 100% rename from tests/__snapshots__/test_samples/test_samples[sample2][redef.zeek].raw rename to tests/__snapshots__/test_samples/test_samples[redef.zeek].raw diff --git a/tests/__snapshots__/test_samples/test_samples[sample3][test1.zeek].raw b/tests/__snapshots__/test_samples/test_samples[test1.zeek].raw similarity index 100% rename from tests/__snapshots__/test_samples/test_samples[sample3][test1.zeek].raw rename to tests/__snapshots__/test_samples/test_samples[test1.zeek].raw diff --git a/tests/test_samples.py b/tests/test_samples.py index eb09089..207b767 100644 --- a/tests/test_samples.py +++ b/tests/test_samples.py @@ -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}" From aabb00067ebd7749a8bc35383fa415b69a30a01b Mon Sep 17 00:00:00 2001 From: Benjamin Bannier Date: Mon, 24 Feb 2025 15:30:01 +0100 Subject: [PATCH 2/2] Bump pre-commit hooks --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index bed7576..c0dba70 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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