commit: 3cb2261328c1f8e46a4ecf2a7516c226f1c9f9d7
Author: Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Thu Feb 9 02:52:56 2023 +0000
Commit: Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Thu Feb 9 03:14:41 2023 +0000
URL:
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=3cb22613
test-functions: Add a test for the esyslog() function
In order to facilitate the appropriate tests, the test suite was
adjusted slightly. Now, iterate_tests() will compose the code to be
evaluated for each test being conducted. Consequently, it is able to
recognise "N/A" as a special word to indicate that no further parameters
should be specified. In turn, that means that the callback() function
no longer needs to assume responsibility for recognising said word and
can instead work with the positional parameters it receives in a natural
fashion. Further, a print_args() function has been added that is used to
compose the test descriptions.
Also, further clarify the runtime requirements in the opening comment.
Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>
Signed-off-by: Sam James <sam <AT> gentoo.org>
test-functions | 120 +++++++++++++++++++++++++++++++++++++++------------------
1 file changed, 83 insertions(+), 37 deletions(-)
diff --git a/test-functions b/test-functions
index d6471ed..8db2191 100755
--- a/test-functions
+++ b/test-functions
@@ -1,7 +1,9 @@
#!/bin/sh
# shellcheck disable=2015
-# Requires mktemp(1) and touch(1). The implementations provided by GNU
-# coreutils are compatible, as are the builtins provided by busybox.
+
+# Requires mktemp(1), which is not a standard utility, but is commonly
+# available. The implementations provided by GNU coreutils, busybox and toybox
+# are known to be compatible.
bailout() {
printf 'Bail out! %s.\n' "$1"
@@ -88,13 +90,9 @@ test_is_older_than() {
done
callback() {
- if [ "$2" != "N/A" ] && [ "$3" != "N/A" ]; then
- description="is_older_than $2 $3"
- is_older_than "$2" "$3"
- else
- description="is_older_than"
- is_older_than
- fi
+ shift
+ test_description="is_older_than $(print_args "$@")"
+ is_older_than "$@"
}
iterate_tests 3 "$@"
@@ -103,59 +101,106 @@ test_is_older_than() {
test_get_bootparam() {
cmdline="foo gentoo=bar,baz quux"
set -- \
- 1 N/A "${cmdline}" \
- 1 '' "${cmdline}" \
- 1 '' "gentoo=" \
- 1 foo "${cmdline}" \
- 0 bar "${cmdline}" \
- 0 bar "foo gentoo=gentoo=1,bar baz" \
- 0 bar "foo gentoo=bar,gentoo=1 baz" \
- 0 baz "${cmdline}" \
- 1 bar,baz "${cmdline}" \
- 0 gentoo=1 "foo gentoo=bar,gentoo=1 baz" \
- 0 gentoo=1 "foo gentoo=gentoo=1,bar baz" \
- 1 quux "${cmdline}"
+ 1 "${cmdline}" N/A \
+ 1 "${cmdline}" '' \
+ 1 "gentoo=" '' \
+ 1 "${cmdline}" foo \
+ 0 "${cmdline}" bar \
+ 0 "foo gentoo=gentoo=1,bar baz" bar \
+ 0 "foo gentoo=bar,gentoo=1 baz" bar \
+ 0 "${cmdline}" baz \
+ 1 "${cmdline}" bar,baz \
+ 0 "foo gentoo=bar,gentoo=1 baz" gentoo=1 \
+ 0 "foo gentoo=gentoo=1,bar baz" gentoo=1 \
+ 1 "${cmdline}" quux
callback() {
- if [ "$2" = "N/A" ]; then
- description="get_bootparam"
- printf '%s\n' "$3" | get_bootparam
- else
- description="get_bootparam \"$2\""
- printf '%s\n' "$3" | get_bootparam "$2"
- fi
+ cmdline=$2
+ shift 2
+ test_description="get_bootparam $(print_args "$@")"
+ printf '%s\n' "${cmdline}" | get_bootparam "$@"
}
iterate_tests 3 "$@"
}
+test_esyslog() {
+ set -- \
+ 1 0 N/A N/A N/A \
+ 1 0 debug N/A N/A \
+ 0 0 debug user N/A \
+ 0 0 debug user '' \
+ 0 1 debug user message
+
+ logger() {
+ # esyslog() ignores empty messages. By overriding logger(1), it
+ # can be determined whether a message would have been logged.
+ logged=$((logged + 1))
+ }
+
+ callback() {
+ should_log=$2
+ shift 2
+ logged=0
+ test_description="esyslog $(print_args "$@")"
+ EINFO_LOG=1 esyslog "$@" 2>/dev/null
+ case $? in
+ 0)
+ test "${logged}" -eq "${should_log}"
+ ;;
+ *)
+ return "$?"
+ esac
+ }
+
+ iterate_tests 5 "$@"
+}
+
iterate_tests() {
slice_width=$1
shift
total=$(( $# / slice_width ))
printf '1..%d\n' "${total}"
- code="callback"
- i=0
- while [ "$((i += 1))" -le "${slice_width}" ]; do
- code="${code} \"\$${i}\""
- done
passed=0
- i=1
- while [ "${i}" -le "${total}" ]; do
+ i=0
+ while [ "$((i += 1))" -le "${total}" ]; do
+ code="callback"
+ j=0
+ while [ "$((j += 1))" -le "${slice_width}" ]; do
+ if eval "[ \"\$${j}\" = N/A ]"; then
+ break
+ else
+ code="${code} \"\$${j}\""
+ fi
+ done
eval "${code}"
if [ "$?" -eq "$1" ]; then
passed=$((passed + 1))
else
printf 'not '
fi
- printf 'ok %d - %s\n' "${i}" "${description} (expecting $1)"
- i=$((i + 1))
+ printf 'ok %d - %s (expecting %d)\n' "${i}"
"${test_description}" "$1"
shift "${slice_width}"
done
return "$(( passed < total ))"
}
+print_args() {
+ i=0
+ for arg; do
+ if [ "$((i += 1))" -eq 1 ]; then
+ set --
+ fi
+ if [ -n "${arg}" ]; then
+ set -- "$@" "${arg}"
+ else
+ set -- "$@" "''"
+ fi
+ done
+ printf '%s\n' "$*"
+}
+
printf 'TAP version 14\n'
unset -v dir
@@ -172,5 +217,6 @@ export TZ=UTC
rc=0
test_is_older_than || rc=1
test_get_bootparam || rc=1
+test_esyslog || rc=1
cleanup_tmpdir
exit "${rc}"