mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-21 12:13:55 +09:00
fix(tests): separate awk errors from match results
Signed-off-by: Ahmed Kamal <email.ahmedkamal@googlemail.com> Assisted-by: LLM
This commit is contained in:
1 parent
f12887636c
commit
e6651ce222
2 files changed
+33
-19
No files matched your search
@@ -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:'* ]]
|
||||
}
|
||||
+14
-17
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user