From e6651ce222c43b84749998257ff3ab7cf148b2ea Mon Sep 17 00:00:00 2001 From: Ahmed Kamal Date: Sat, 31 Oct 2026 23:40:51 +0200 Subject: [PATCH] fix(tests): separate awk errors from match results Signed-off-by: Ahmed Kamal Assisted-by: LLM --- hack/test-helper_test.bats | 21 +++++++++++++++++++-- hack/test_helper.bash | 31 ++++++++++++++----------------- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/hack/test-helper_test.bats b/hack/test-helper_test.bats index d02580099..902ed8af9 100644 --- a/hack/test-helper_test.bats +++ b/hack/test-helper_test.bats @@ -6,10 +6,10 @@ load test_helper # the function directly through Bats' run helper. run_lacks_pattern() { run bash -eu -c ' - . hack/test_helper.bash + . "$3" assert_file_lacks_pattern "$1" "$2" printf "checked\n" - ' _ "$@" + ' _ "$@" "$BATS_TEST_DIRNAME/test_helper.bash" } @test "a valid non-matching pattern passes under errexit" { @@ -83,3 +83,20 @@ run_lacks_pattern() { [ "$status" -eq 1 ] [[ "$output" == *'could not evaluate pattern secret:'* ]] } + +@test "a matcher error with status one cannot pass as no match" { + printf 'safe content\n' >"$BATS_TEST_TMPDIR/artifact" + mkdir "$BATS_TEST_TMPDIR/bin" + cat >"$BATS_TEST_TMPDIR/bin/awk" <<'SH' +#!/bin/sh +printf '0\n' +printf 'awk: evaluation failed\n' >&2 +exit 1 +SH + chmod +x "$BATS_TEST_TMPDIR/bin/awk" + + PATH="$BATS_TEST_TMPDIR/bin:$PATH" run_lacks_pattern 'secret:' "$BATS_TEST_TMPDIR/artifact" + + [ "$status" -eq 1 ] + [[ "$output" == *'could not evaluate pattern secret:'* ]] +} diff --git a/hack/test_helper.bash b/hack/test_helper.bash index f5f74a8b1..592676b8c 100644 --- a/hack/test_helper.bash +++ b/hack/test_helper.bash @@ -13,28 +13,25 @@ setup() { assert_file_lacks_pattern() { local pattern="$1" local file="$2" - local _rc=0 + local found if [ ! -f "${file}" ]; then printf 'expected %s to exist so it could be checked for: %s\n' "${file}" "${pattern}" >&2 return 1 fi - # Validate the regex even for an empty file. Only status 1 means no match; - # keeping awk in an OR-list lets that normal result survive errexit. - awk -v pattern="${pattern}" ' + # Validate even an empty file, and keep the match result separate from awk's + # status: some implementations also use status 1 for evaluation errors. + if ! found=$(awk -v pattern="${pattern}" ' BEGIN { match("", pattern) } $0 ~ pattern { found = 1 } - END { exit found ? 0 : 1 } - ' "${file}" || _rc=$? - case "${_rc}" in - 0) - printf 'expected %s not to match: %s\n' "${file}" "${pattern}" >&2 - return 1 - ;; - 1) return 0 ;; - *) - printf 'awk could not evaluate pattern %s against %s\n' "${pattern}" "${file}" >&2 - return 1 - ;; - esac + END { print found ? 1 : 0 } + ' "${file}"); then + printf 'awk could not evaluate pattern %s against %s\n' "${pattern}" "${file}" >&2 + return 1 + fi + if [ "${found}" != 0 ]; then + printf 'expected %s not to match: %s\n' "${file}" "${pattern}" >&2 + return 1 + fi + return 0 }