Skip to content

Replace hand-rolled SHA redaction in snapshot test with a syrupy matcher #72

Description

@lodyai

Background

tests/test_template/test_snapshots.py normalises 40-hex SHA-pinned uses: refs before comparing against the syrupy snapshot, so routine Dependabot SHA bumps don't churn the snapshot. Today this is done by pre-transforming the data with hand-rolled recursion (_redact_pinned_shas + _redact_mapping_entry).

Since that test already uses syrupy, the redaction could instead be expressed as a syrupy matcher= on the snapshot call. syrupy already walks the structure, so the container recursion collapses to a single leaf transform:

def _redact_uses(*, data: object, path: object) -> object:
    """Redact 40-hex SHA-pinned ``uses`` refs so Dependabot bumps don't churn snapshots."""
    match data:
        case str() if path and path[-1][0] == "uses":
            return _PINNED_USES_RE.sub(r"\g<ref>@<sha>", data)
        case _:
            return data

# in the test:
assert {...} == snapshot(matcher=_redact_uses)

(path is a tuple of (name, type) pairs; path[-1][0] is the current leaf's dict key / list index.)

Why not now

We have a planned overhaul of snapshot testing once AST-aware snapshots for Markdown files are ready. The matcher swap should land as part of that overhaul rather than as an isolated change, to avoid churning the snapshot infrastructure twice.

Scope

  • In scope: tests/test_template/test_snapshots.py — replace _redact_pinned_shas/_redact_mapping_entry with a syrupy matcher; regenerate the snapshot and confirm byte-identical .ambr output.
  • Out of scope: the contract helpers in tests/helpers/tooling_contracts/. They match refs by regex against raw rendered text (_is_pinned_action) with targeted failure messages — the correct tool there — and do not use syrupy.

Notes

  • syrupy 5.5.3 (currently pinned) supports matcher= plus syrupy.matchers.path_type / path_value; a custom matcher is the cleanest fit for this key-based rule.
  • Trade-off accepted: a matcher couples to syrupy's (name, type) path representation and still checks str() at the leaf, versus the current pure, independently-testable function.

Tracked from a # FIXME in tests/test_template/test_snapshots.py.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions