Skip to content

Commit e6651ce

Browse files
committed
fix(tests): separate awk errors from match results
Signed-off-by: Ahmed Kamal <[email protected]> Assisted-by: LLM
1 parent f128876 commit e6651ce

2 files changed

Lines changed: 33 additions & 19 deletions

File tree

‎hack/test-helper_test.bats‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ load test_helper
66
# the function directly through Bats' run helper.
77
run_lacks_pattern() {
88
run bash -eu -c '
9-
. hack/test_helper.bash
9+
. "$3"
1010
assert_file_lacks_pattern "$1" "$2"
1111
printf "checked\n"
12-
' _ "$@"
12+
' _ "$@" "$BATS_TEST_DIRNAME/test_helper.bash"
1313
}
1414

1515
@test "a valid non-matching pattern passes under errexit" {
@@ -83,3 +83,20 @@ run_lacks_pattern() {
8383
[ "$status" -eq 1 ]
8484
[[ "$output" == *'could not evaluate pattern secret:'* ]]
8585
}
86+
87+
@test "a matcher error with status one cannot pass as no match" {
88+
printf 'safe content\n' >"$BATS_TEST_TMPDIR/artifact"
89+
mkdir "$BATS_TEST_TMPDIR/bin"
90+
cat >"$BATS_TEST_TMPDIR/bin/awk" <<'SH'
91+
#!/bin/sh
92+
printf '0\n'
93+
printf 'awk: evaluation failed\n' >&2
94+
exit 1
95+
SH
96+
chmod +x "$BATS_TEST_TMPDIR/bin/awk"
97+
98+
PATH="$BATS_TEST_TMPDIR/bin:$PATH" run_lacks_pattern 'secret:' "$BATS_TEST_TMPDIR/artifact"
99+
100+
[ "$status" -eq 1 ]
101+
[[ "$output" == *'could not evaluate pattern secret:'* ]]
102+
}

‎hack/test_helper.bash‎

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,25 @@ setup() {
1313
assert_file_lacks_pattern() {
1414
local pattern="$1"
1515
local file="$2"
16-
local _rc=0
16+
local found
1717

1818
if [ ! -f "${file}" ]; then
1919
printf 'expected %s to exist so it could be checked for: %s\n' "${file}" "${pattern}" >&2
2020
return 1
2121
fi
22-
# Validate the regex even for an empty file. Only status 1 means no match;
23-
# keeping awk in an OR-list lets that normal result survive errexit.
24-
awk -v pattern="${pattern}" '
22+
# Validate even an empty file, and keep the match result separate from awk's
23+
# status: some implementations also use status 1 for evaluation errors.
24+
if ! found=$(awk -v pattern="${pattern}" '
2525
BEGIN { match("", pattern) }
2626
$0 ~ pattern { found = 1 }
27-
END { exit found ? 0 : 1 }
28-
' "${file}" || _rc=$?
29-
case "${_rc}" in
30-
0)
31-
printf 'expected %s not to match: %s\n' "${file}" "${pattern}" >&2
32-
return 1
33-
;;
34-
1) return 0 ;;
35-
*)
36-
printf 'awk could not evaluate pattern %s against %s\n' "${pattern}" "${file}" >&2
37-
return 1
38-
;;
39-
esac
27+
END { print found ? 1 : 0 }
28+
' "${file}"); then
29+
printf 'awk could not evaluate pattern %s against %s\n' "${pattern}" "${file}" >&2
30+
return 1
31+
fi
32+
if [ "${found}" != 0 ]; then
33+
printf 'expected %s not to match: %s\n' "${file}" "${pattern}" >&2
34+
return 1
35+
fi
36+
return 0
4037
}

0 commit comments

Comments
 (0)