summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKerin Millar <kfm@plushkava.net>2024-08-08 09:10:32 +0100
committerSam James <sam@gentoo.org>2024-08-11 11:10:58 +0100
commitf76dddb6a9e66ba3ffcb44822af0016d2b158b25 (patch)
treee1d601160d6b89bf6173a80e590537bfe0c761bd
parentAvoid unspecified behaviour around simple commands in general (diff)
downloadgentoo-functions-f76dddb6a9e66ba3ffcb44822af0016d2b158b25.tar.gz
gentoo-functions-f76dddb6a9e66ba3ffcb44822af0016d2b158b25.tar.bz2
gentoo-functions-f76dddb6a9e66ba3ffcb44822af0016d2b158b25.zip
test-functions: have three tests employ callback functions
Convert test_local(), test_ebegin() and test_quote_args() so as to declare and use callbacks, just like the other tests. An appreciable code cleanup is the result. Signed-off-by: Kerin Millar <kfm@plushkava.net> Signed-off-by: Sam James <sam@gentoo.org>
-rwxr-xr-xtest-functions99
1 files changed, 48 insertions, 51 deletions
diff --git a/test-functions b/test-functions
index 0b0e127..79c66a9 100755
--- a/test-functions
+++ b/test-functions
@@ -24,26 +24,27 @@ cleanup_tmpdir() {
}
test_local() {
- (
- var=1
- f() {
- local var=2
- g
- test "${var}" = 3 || exit
- }
- g() {
- test "${var}" = 2 || exit
- var=3
- }
- f
- test "${var}" = 1
- ) 2>/dev/null
- retval=$?
- if [ "${retval}" -ne 0 ]; then
- printf 'not '
- fi
- printf 'ok %d - /bin/sh supports local\n' "$((testnum += 1))"
- return "${retval}"
+ set -- eq 0
+
+ callback() {
+ test_description="/bin/sh supports local"
+ (
+ var=1
+ f() {
+ local var=2
+ g
+ test "${var}" = 3 || exit
+ }
+ g() {
+ test "${var}" = 2 || exit
+ var=3
+ }
+ f
+ test "${var}" = 1
+ ) 2>/dev/null
+ }
+
+ iterate_tests 2 "$@"
}
test_chdir() {
@@ -104,21 +105,18 @@ test_die() {
}
test_ebegin() {
- _eprint() {
- shift
- _ends_with_newline "$*"
- }
-
- set -- "message"
- ebegin "$1"
- retval=$?
+ set -- eq 0
- if [ "${retval}" -ne 0 ]; then
- printf 'not '
- fi
- printf 'ok %d - ebegin %s (expecting terminating newline)\n' "$((testnum + 1))" "$1"
+ callback() (
+ test_description="ebegin message (expecting terminating newline)"
+ _eprint() {
+ shift
+ _ends_with_newline "$*"
+ }
+ ebegin "message"
+ )
- return "${retval}"
+ iterate_tests 2 "$@"
}
test_edo() {
@@ -926,25 +924,24 @@ test_contains_any() {
}
test_quote_args() {
- local POSIXLY_CORRECT
+ set -- eq 0
- testnum=$((testnum + 1))
- retval=0
- i=0
- while [ "$(( i += 1 ))" -le 255 ]; do
- fmt=$(printf '\\%o' "$i")
- # shellcheck disable=2059
- str=$(printf "$fmt.")
- quote_args "${str%.}" || break
- done | cksum | {
- read -r cksum _
- if [ "${cksum}" != "380900690" ]; then
- printf 'not '
- retval=1
- fi
- printf 'ok %d - quote_args output test (expected cksum 380900690, got %s)\n' "${testnum}" "${cksum}"
- return "${retval}"
+ callback() {
+ local POSIXLY_CORRECT i
+
+ test_description="quote_args output test (expecting cksum 380900690)"
+ i=0
+ while [ "$((i += 1))" -le 255 ]; do
+ fmt=$(printf '\\%o' "$i")
+ # shellcheck disable=2059
+ str=$(printf "$fmt.")
+ quote_args "${str%.}" || break
+ done \
+ | cksum \
+ | { read -r cksum _ && test "${cksum}" = "380900690"; }
}
+
+ iterate_tests 2 "$@"
}
test_assign() {
@@ -1096,7 +1093,7 @@ elif ! GENFUN_MODULES="portage rc" . ./functions.sh; then
else
assign_tmpdir
test_chdir || rc=1
- ( test_ebegin ) || rc=1; testnum=$((testnum + 1))
+ test_ebegin || rc=1; testnum=$((testnum + 1))
test_is_older_than || rc=1
test_get_bootparam || rc=1
test_esyslog || rc=1