Skip to content

assert_exec cannot assert a non-zero exit code under --strict #1207

Description

@Chemaclass

Problem

function test_x() { assert_exec "./fails.sh" --exit 1; }   # ./fails.sh exits 1
mode result
default ✓ Passed
--strict ✗ Error

A successful command is fine in both modes, which is why this went unnoticed:
only the failing case breaks — and checking a failing command is the whole
point of --exit 1.

Cause

eval "$cmd" >"$stdout_file" 2>"$stderr_file"
local exit_code=$?

--strict enables set -e, so a non-zero eval aborts the test function
right there and local exit_code=$? never runs. Both the stdin and non-stdin
branches have it.

Found via the docs

docs/common-patterns.md → "Testing Failure Cases" shows two forms, and under
--strict both failed:

assert_exec "./src/validate_email.sh invalid-email" --exit 1   # ✗ Error
./src/validate_email.sh invalid-email; assert_general_error     # ✗ Error

The second is the $?-capture trap already documented in #1170. The first was
this bug. With it fixed, the recommended form works under --strict, so the
guide's advice stands as written.

Fix

local exit_code=0 then eval … || exit_code=$?, in both branches. Declaring
and assigning together would mask the status behind local's own.

Activity

  1. self-assigned this
    on Aug 14, 2026
  2. Chemaclass commented on Aug 14, 2026

    @Chemaclass
    MemberAuthor

    Done in #1208 (merged).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions