diff --git a/tests/network/test-lib-self-test.sh b/tests/network/test-lib-self-test.sh new file mode 100755 index 00000000..917e7b33 --- /dev/null +++ b/tests/network/test-lib-self-test.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +# Regression checks for test assertions: matching error output must not pass. +set -euo pipefail +cd "$(dirname "$0")" +source ./test-lib.sh + +LOG_DIR=$(mktemp -d) +trap 'test_cleanup; rm -rf "$LOG_DIR"' EXIT +CURRENT_LOG="$LOG_DIR/assertions.log" +: > "$CURRENT_LOG" + +assert_output_contains "printf 'ready\\n'" ready "successful output matches" +if assert_output_contains "printf 'ready\\n'; exit 7" ready "failed command must not match"; then + echo 'assert_output_contains accepted a failed command' >&2 + exit 1 +fi +if assert_output_contains "printf 'not-ready\\n'" '^ready$' "missing pattern must not match"; then + echo 'assert_output_contains accepted a missing pattern' >&2 + exit 1 +fi +[ "$TESTS_RUN" -eq 3 ] && [ "$TESTS_PASSED" -eq 1 ] && [ "$TESTS_FAILED" -eq 2 ] +echo 'network assertion self-test passed' diff --git a/tests/network/test-lib.sh b/tests/network/test-lib.sh index b513c2c3..e2e829e1 100755 --- a/tests/network/test-lib.sh +++ b/tests/network/test-lib.sh @@ -188,10 +188,12 @@ assert_output_contains() { echo " Expected pattern: $pattern" >> "$CURRENT_LOG" local output - output=$(eval "$cmd" 2>&1) + local command_status=0 + output=$(eval "$cmd" 2>&1) || command_status=$? + echo " Exit status: $command_status" >> "$CURRENT_LOG" echo " Output: $output" >> "$CURRENT_LOG" - - if echo "$output" | grep -q "$pattern"; then + + if [ "$command_status" -eq 0 ] && grep -q "$pattern" <<< "$output"; then TESTS_PASSED=$((TESTS_PASSED + 1)) echo -e " ${GREEN}✓${NC} $msg" echo " Result: PASS" >> "$CURRENT_LOG" @@ -199,7 +201,7 @@ assert_output_contains() { else TESTS_FAILED=$((TESTS_FAILED + 1)) echo -e " ${RED}✗${NC} $msg" - echo " Result: FAIL (pattern not found)" >> "$CURRENT_LOG" + echo " Result: FAIL (exit status $command_status or pattern not found)" >> "$CURRENT_LOG" return 1 fi }