Skip to content

feat(snapshot): add named snapshots and better mismatch guidance #986

Description

@Chemaclass

Summary

The default snapshot path is derived only from the test file and function name.
Two assert_match_snapshot calls in the same test therefore resolve to the same
file unless the author manually constructs explicit paths.

The suite already records this limitation in TODOs such as
tests/acceptance/bashunit_fail_test.sh: "option to choose snapshot name?".

A mismatch also prints a diff without naming the snapshot file or reminding the
author about --snapshot-update. Missing-snapshot failures provide the path, so
the two failure modes have inconsistent guidance.

Proposal

Named snapshots

Add a backward-compatible named API rather than changing the existing second
argument, which is already a public explicit-file path:

assert_match_named_snapshot "verbose" "$output"
assert_match_named_snapshot_ignore_colors "simple" "$colored_output"

Derive names beside the existing default snapshot using a normalized suffix,
for example:

<test-file>.<test-function>.verbose.snapshot

Names must be normalized through the existing helper so spaces and punctuation
are safe and path traversal is impossible. Explicit paths on the existing
assertions remain unchanged.

Mismatch guidance

On failure, print:

  • the resolved snapshot path
  • a concise hint to re-record deliberately with --snapshot-update

Keep the readable diff as the primary content.

Constraints

  • Bash 3.0+
  • Existing assertion signatures and snapshot filenames remain unchanged
  • New assertions stay fork-free apart from the snapshot comparison behavior
  • Named and default snapshots can coexist in one test
  • Both completion scripts and assertion documentation remain in sync
  • Machine-report contents must remain valid

Acceptance criteria

  • One test can use two independently named snapshots
  • Names with spaces/punctuation resolve deterministically and cannot escape snapshots/
  • Named ignore-colors snapshots strip ANSI exactly like the existing assertion
  • Existing default and explicit-path assertions are unchanged
  • Snapshot mismatch output includes the resolved path
  • Snapshot mismatch output mentions --snapshot-update
  • Replace the existing named-snapshot TODO coverage with real assertions
  • Update docs/snapshots.md, docs/assertions.md, generated doc snapshots and both completion scripts
  • Update CHANGELOG.md under ## Unreleased
  • make sa · make lint · ./bashunit --parallel --simple --strict tests/ · bash build.sh bin -v

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions