-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'topic/bbannier/snapshot-testing'
- Loading branch information
Showing
15 changed files
with
158 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,37 @@ | ||
exclude: tests/data | ||
exclude: tests/(data|__snapshots__|samples) | ||
|
||
repos: | ||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v4.5.0 | ||
rev: v4.6.0 | ||
hooks: | ||
- id: trailing-whitespace | ||
- id: end-of-file-fixer | ||
- id: check-yaml | ||
- id: check-added-large-files | ||
|
||
- repo: https://github.com/PyCQA/pylint | ||
rev: v3.0.1 | ||
rev: v3.2.5 | ||
hooks: | ||
- id: pylint | ||
additional_dependencies: | ||
- "pytest==8.2.2" | ||
- "syrupy==4.6.1" | ||
- "setuptools" | ||
- "tree-sitter>=0.21.3" | ||
- "tree-sitter-zeek" | ||
- "tree-sitter==0.22.3" | ||
- "tree-sitter-zeek==0.1.1" | ||
|
||
- repo: https://github.com/psf/black | ||
rev: 23.10.1 | ||
rev: 24.4.2 | ||
hooks: | ||
- id: black | ||
|
||
- repo: https://github.com/asottile/pyupgrade | ||
rev: v3.15.0 | ||
rev: v3.16.0 | ||
hooks: | ||
- id: pyupgrade | ||
args: ["--py37-plus"] | ||
|
||
- repo: https://github.com/igorshubovych/markdownlint-cli | ||
rev: v0.37.0 | ||
rev: v0.41.0 | ||
hooks: | ||
- id: markdownlint-fix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
"""Installation setup.""" | ||
|
||
from setuptools import setup | ||
|
||
|
||
|
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
"""Snapshot test of formatting of sample files. | ||
Code samples are located in tests/samples. To add a new sample create a new | ||
file and create a snapshot of the formatting result with | ||
$ pytest --snapshot-update | ||
Commit both the sample as well as the updated snapshot. | ||
""" | ||
|
||
import io | ||
from pathlib import Path | ||
|
||
import pytest | ||
from syrupy.assertion import SnapshotAssertion | ||
from syrupy.extensions.single_file import SingleFileSnapshotExtension | ||
|
||
from testutils import zeekscript | ||
|
||
SAMPLES_DIR = Path(__file__).parent / "samples" | ||
|
||
|
||
# Use a custom snapshot fixture so we emit one file per generated test case | ||
# instead of one per module. | ||
@pytest.fixture | ||
def snapshot(snapshot: SnapshotAssertion): # pylint: disable=redefined-outer-name | ||
return snapshot.use_extension(SingleFileSnapshotExtension) | ||
|
||
|
||
def _format(script: zeekscript.Script): | ||
"""Formats a given `Script`""" | ||
buf = io.BytesIO() | ||
script.format(buf) | ||
return buf.getvalue() | ||
|
||
|
||
def _get_samples(): | ||
"""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: | ||
return [sample for sample in SAMPLES_DIR.iterdir() if sample.is_file()] | ||
except FileNotFoundError: | ||
return [] | ||
|
||
|
||
# For each file in `SAMPLES_DIR` test formatting of the file. | ||
@pytest.mark.parametrize("sample", _get_samples()) | ||
# pylint: disable-next=redefined-outer-name | ||
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(sample.relative_to(SAMPLES_DIR.parent.parent)) | ||
|
||
output = _format(input_) | ||
assert output == snapshot( | ||
name=name | ||
), f"formatted {sample} inconsistent with snapshot" | ||
|
||
output2 = _format(input_) | ||
assert output2 == output, f"idempotency violation for {sample}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
"""This module provides reusable command line parsers and tooling.""" | ||
|
||
import argparse | ||
import io | ||
import os | ||
|
Oops, something went wrong.