Skip to content

feat(snapshot): add --snapshot-update to re-record snapshots #900

Description

@Chemaclass

Context

There is no way to re-record snapshots. The documented procedure is to delete the
snapshot file and re-run, since assert_match_snapshot writes the file when it is
missing (src/assert_snapshot.sh:24-27). Both agent-facing docs say so
explicitly:

  • docs/ai-agents.md:119 — "There is no --update-snapshots flag"
  • docs/public/bashunit-skill.md:175 — same

The path to delete is derived, not stated in the test source
(bashunit::snapshot::resolve_file, src/assert_snapshot.sh:91-116): the test
file's basename and the test function name are each passed through
normalize_variable_name, then joined as
./<dir>/snapshots/<file>.<function>.snapshot. For example:

snapshots/snap_test_sh.test_new_snapshot_is_silently_recorded.snapshot

So re-recording means reproducing that normalization by hand, per test.

Proposal

Add --snapshot-update (plus BASHUNIT_SNAPSHOT_UPDATE for .env, following the
existing flag/env pairing in src/env.sh) that makes assert_match_snapshot and
assert_match_snapshot_ignore_colors overwrite the snapshot with the actual value
and count it as a recorded snapshot rather than a pass.

Details worth settling:

  • it must compose with a filter so a single test can be re-recorded, not the whole
    suite — --snapshot-update together with the existing --filter
  • interaction with placeholders (::ignore::, BASHUNIT_SNAPSHOT_PLACEHOLDER):
    overwriting a snapshot destroys its placeholders. Either refuse to overwrite
    a snapshot containing the placeholder, or warn loudly. This needs a decision
    before implementation.
  • run summary must make the rewrite visible (reuse the existing
    Some snapshots created block, wording adjusted)

Why this matters for agentic coding

Deliberate output changes are routine — an agent adjusts a message and a dozen
acceptance snapshots need re-recording. Today the agent must derive each path
through a normalization function it has to read the source to understand, then
rm files it cannot verify it chose correctly. Deletion is also the one operation
with no undo: .claude/skills/gh-issues/SKILL.md and the skill docs both already
warn that a deleted snapshot never fails, so a wrong rm silently converts a real
assertion into a rubber stamp. A flag makes the intended operation the easy one
and takes rm off the agent's path entirely.

Acceptance criteria

  • --snapshot-update rewrites existing snapshots from the actual value
  • BASHUNIT_SNAPSHOT_UPDATE equivalent, documented in docs/configuration.md
  • Composes with --filter for single-test re-recording
  • Placeholder-bearing snapshots are protected or loudly warned about
  • Rewritten snapshots are reported in the summary, not counted as passes
  • Docs updated: docs/snapshots.md, docs/command-line.md,
    docs/configuration.md, and the "there is no such flag" notes in
    docs/ai-agents.md + docs/public/bashunit-skill.md
  • Acceptance tests in tests/acceptance/
  • Bash 3.0+ compatible, make sa + make lint green

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions