Skip to content

perf(snapshot): remove success-path forks from assert_match_snapshot #775

Description

@Chemaclass

Part of #761.

Problem

assert_match_snapshot forks ~6-8 times even when the snapshot matches (the common case):

  • src/assert_snapshot.sh:5$(echo -n "$1" | tr -d '\r'): 2 forks to strip carriage returns.
  • src/assert_snapshot.sh:7$(bashunit::helper::find_test_function_name): capture fork around a pure-bash helper.
  • src/assert_snapshot.sh:8$(bashunit::snapshot::resolve_file ...), which internally (src/assert_snapshot.sh:62-64) forks dirname once, basename once, and normalize_variable_name twice.
  • src/assert_snapshot.sh:83snapshot=$(tr -d '\r' <"$snapshot_path"): subshell + external tr per compare.

assert_match_snapshot_ignore_colors (src/assert_snapshot.sh:19) adds a sed on top.

Task — checklist, one commit each

  • Replace \r stripping with pure-bash ${var//$'\r'/} at src/assert_snapshot.sh:5 and for the file content at :83 (read with $(<"$snapshot_path") — no external exec — then strip in bash).
  • Use return-slot variants of find_test_function_name and normalize_test_function_name/normalize_variable_name (add slots if they don't exist yet; coordinate with the assert hot-path issue which adds some of the same slots).
  • In resolve_file (src/assert_snapshot.sh:55), replace dirname/basename with parameter expansion (${path%/*}, ${path##*/}) and convert the function itself to a return slot. Watch the edge case BASH_SOURCE without a slash.
  • ignore_colors variant: reuse bashunit::str::strip_ansi (src/str.sh:4) instead of a bespoke sed, so plain input takes its pure-bash fast path. Verify the sed patterns match ([mK] suffixes) before swapping; if they differ, align deliberately and cover with a test.

Tests first (TDD)

tests/unit/snapshot_test.sh (or nearest existing snapshot tests — grep first) is the spec. Add cases: content containing \r, snapshot file with \r\n line endings, custom file hint vs derived path, ANSI-colored input for the ignore-colors variant. Snapshot files on disk must remain byte-compatible with ones created by the current version.

Constraints

  • Bash 3.0+ only.
  • Existing .snapshot files must keep matching (no format/normalization change).

Acceptance criteria

  • ./bashunit tests/ and ./bashunit --parallel tests/ pass after every commit.
  • make sa, make lint pass; shfmt -w . produces no diff.

Line references valid at commit 1ae3e82.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions