Skip to content

feat(console): render a unified diff for multiline assert_equals/assert_same failures #777

Description

@Chemaclass

Why

assert_equals/assert_same failures print the full expected and actual values on single quoted lines (bashunit::console_results::print_failed_test, src/console_results.sh:305). For multiline strings (command output, file contents) this is unreadable — you get two blobs and hunt for the difference by eye.

Snapshot failures already solve this: print_failed_snapshot_test (src/console_results.sh:332) renders a git diff of expected vs actual when git is available (bashunit::dependencies::has_git), falling back to plain output otherwise. Reuse that machinery.

Proposal

When both of these hold in print_failed_test:

  • expected or actual contains a newline, and
  • bashunit::dependencies::has_git (same gate as snapshots),

render a unified diff (same temp-file + git diff --no-index approach as the snapshot path — extract the shared logic into one helper instead of duplicating) after the existing Expected/but got header, possibly truncating the inline quoted values to their first line + when the diff is shown.

Single-line values keep the current output byte-identical — a large body of acceptance/snapshot tests depends on it, and so do users parsing output.

Design notes

  • Extract the diff-rendering block from print_failed_snapshot_test into e.g. bashunit::console_results::render_diff expected_text actual_text and use it from both call sites (one commit for the extraction, no behavior change; next commit adds the multiline hook).
  • Respect --no-color / BASHUNIT_NO_COLOR (git diff must be called with color flags consistent with src/colors.sh state).
  • Machine formats (JUnit/TAP/JSON reports) must keep receiving the raw expected/actual values, not the diff — only the console renderer changes.
  • Add an opt-out env (BASHUNIT_NO_DIFF=true) for users who scrape console output.

Tests first (TDD)

Acceptance test: multiline assert_equals failure shows a diff hunk; with BASHUNIT_NO_DIFF=true or without git shows current format; single-line failure output unchanged (snapshot test). Unit test the extracted render_diff helper.

Constraints

  • Bash 3.0+; git remains optional (graceful fallback).
  • CHANGELOG.md entry (user-visible).

Acceptance criteria

  • ./bashunit tests/ and ./bashunit --parallel tests/ pass.
  • make sa, make lint pass; shfmt -w . produces no diff.
  • Existing failure-output snapshot tests untouched or deliberately updated only for the multiline case.

Line references valid at commit 1ae3e82.

Metadata

Metadata

Assignees

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