Skip to content

feat(snapshot): --snapshot-prune to delete the unused snapshot files #1030

Description

@Chemaclass

Problem

--snapshot-report-unused (added in #902) identifies snapshot files that no test resolved, and its help text is explicit that it "deletes nothing" (bashunit test --help). So the workflow stops halfway: bashunit tells you which files are dead, and you delete them by hand, one path at a time, hoping you did not miss one or delete one that was merely filtered out of that run.

The detection logic is already there — bashunit::snapshot::report_unused (src/assert/snapshot.sh:96).

Proposal

--snapshot-prune      Delete the snapshot files no test resolved
  • Only valid on a full run. --filter, --tag, --shard, --rerun-failed or --changed narrow the set of tests, which makes untouched snapshots look unused — combining prune with any of them must be a hard error, not a data-loss surprise. --snapshot-report-unused already restricts itself to full runs; apply the same rule and make it explicit.
  • Prints every deleted path.
  • Deletes nothing when the run has failures, on the same reasoning: a failing run may not have reached the assertions that resolve those snapshots.
  • Exit code unaffected by deletions.

Consider a --snapshot-prune-dry-run only if it does not duplicate --snapshot-report-unused, which is already the dry run.

Where to change

  • src/assert/snapshot.sh:96 report_unused — factor the discovery so both flags share it.
  • src/main/run.sh:126-128 (the report-unused call site), src/main/test.sh, src/main/validate.sh:60 for the mutual-exclusion check.

Acceptance criteria

Repo checklist (agent)

  • TDD: RED → GREEN → REFACTOR. Write the failing test first.
  • Bash 3.0+ only: no printf -v, no += append, no declare -A, no [[ ]], no ${var,,}, no &>>, no ${arr[-1]}. Expanding a possibly-empty array under set -u needs ${arr[@]+"${arr[@]}"}.
  • A new CLI flag must be wired in all of these or a parity test fails:
  • Gates: make sa, make lint, ./bashunit tests/, ./bashunit --parallel tests/. Never run shfmt -w.
  • Docs: update docs/command-line.md. Editing docs/assertions.md invalidates the bashunit doc acceptance snapshot — regenerate it.
  • CHANGELOG.md: add one line under ## Unreleased.
  • Fixtures under tests/acceptance/fixtures/ must not end in *test.sh.
  • One issue = one PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    Status
    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions