commit:     91ceadf4b0e1b2d1e5c8a45ee7abc38005ec0f2a
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Tue Feb  7 00:41:16 2023 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Tue Feb  7 01:02:44 2023 +0000
URL:        
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=91ceadf4

Add a test for the get_bootparam() function

The changes are not as drastic as they appear. It's mostly on account
of having moved the individual tests into functions.

Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>
Signed-off-by: Sam James <sam <AT> gentoo.org>

 functions.sh   |   6 +-
 test-functions | 233 +++++++++++++++++++++++++++++++++------------------------
 2 files changed, 141 insertions(+), 98 deletions(-)

diff --git a/functions.sh b/functions.sh
index 08950b8..ca43e06 100644
--- a/functions.sh
+++ b/functions.sh
@@ -342,7 +342,11 @@ get_bootparam()
        # Similarly, the empty string must not be allowed to match.
        case $1 in ''|*,*) return 1 ;; esac
 
-       read -r cmdline < /proc/cmdline || return
+       if [ "${TEST_GENFUNCS}" = 1 ]; then
+               read -r cmdline
+       else
+               read -r cmdline < /proc/cmdline
+       fi || return
 
        # Disable pathname expansion. The definition of this function
        # is a compound command that incurs a subshell. Therefore, the

diff --git a/test-functions b/test-functions
index 699cc23..39216b2 100755
--- a/test-functions
+++ b/test-functions
@@ -1,6 +1,5 @@
 #!/bin/bash
 
-# Presently, only the is_older_than() function is tested.
 # Requires mktemp(1) and touch(1) from GNU coreutils.
 
 bailout() {
@@ -8,108 +7,148 @@ bailout() {
        exit 1
 }
 
-printf 'TAP version 14\n'
-
-if ! source ./functions.sh; then
-       bailout "Couldn't source ./functions.sh"
-fi
+assign_tmpdir() {
+       dir=$(mktemp -d) \
+       && CDPATH= cd -- "${dir}" \
+       || bailout "Couldn't create or change to the temp dir"
+}
 
-unset -v dir
-trap '[[ ${dir} ]] && rm -rf -- "${dir}"' EXIT
+test_is_older_than() {
+       local age desc i passed total tstamp
+       local -a tests=(
+               1  N/A           N/A
+               0  newer         newer
+               1  newer         newer-empty
+               0  newer         newer/file
+               1  newer         non-existent
+               1  newer         older
+               1  newer         older-empty
+               1  newer         older/file
+               0  newer-empty   newer
+               1  newer-empty   newer-empty
+               0  newer-empty   newer/file
+               1  newer-empty   non-existent
+               1  newer-empty   older
+               1  newer-empty   older-empty
+               1  newer-empty   older/file
+               1  newer/file    newer
+               1  newer/file    newer-empty
+               1  newer/file    newer/file
+               1  newer/file    non-existent
+               1  newer/file    older
+               1  newer/file    older-empty
+               1  newer/file    older/file
+               0  non-existent  newer
+               0  non-existent  newer-empty
+               0  non-existent  newer/file
+               1  non-existent  non-existent
+               0  non-existent  older
+               0  non-existent  older-empty
+               0  non-existent  older/file
+               0  older         newer
+               0  older         newer-empty
+               0  older         newer/file
+               1  older         non-existent
+               0  older         older
+               1  older         older-empty
+               0  older         older/file
+               0  older-empty   newer
+               0  older-empty   newer-empty
+               0  older-empty   newer/file
+               1  older-empty   non-existent
+               0  older-empty   older
+               1  older-empty   older-empty
+               0  older-empty   older/file
+               0  older/file    newer
+               0  older/file    newer-empty
+               0  older/file    newer/file
+               1  older/file    non-existent
+               1  older/file    older
+               1  older/file    older-empty
+               1  older/file    older/file
+       )
 
-dir=$(mktemp -d) \
-&& CDPATH= cd -- "${dir}" \
-|| bailout "Couldn't create or change to the temp dir"
+       # The mtimes need to be explicitly assigned. Empirical evidence has 
shown
+       # that executing mkdir(1) sequentially, with a single operand each time,
+       # does not guarantee the order of the resulting mtimes. As such, the
+       # implementation of touch(1) from coreutils is required.
+       local -x TZ=UTC
+       tstamp=197001010000
+       for age in older newer; do
+               mkdir "${age}"{,-empty} \
+               && touch -m -t "${tstamp%0}1" "${age}"/file \
+               && touch -m -t "${tstamp}" "${age}"{,-empty} \
+               || bailout "Couldn't create or adjust the mtimes of the sample 
files"
+               tstamp=197001010100 # add an hour
+       done
 
-# The mtimes need to be explicitly assigned. Empirical evidence has shown
-# that executing mkdir(1) sequentially, with a single operand each time,
-# does not guarantee the order of the resulting mtimes. As such, the
-# implementation of touch(1) from coreutils is required.
-export TZ=UTC
-tstamp=197001010000
-for age in older newer; do
-       mkdir "${age}"{,-empty} \
-       && touch -m -t "${tstamp%0}1" "${age}"/file \
-       && touch -m -t "${tstamp}" "${age}"{,-empty} \
-       || bailout "Couldn't create or adjust the mtimes of the sample files"
-       tstamp=197001010100 # add an hour
-done
+       total=$(( ${#tests[@]} / 3 ))
+       passed=0
+       printf '1..%d\n' "${total}"
+       for ((i = 0; i < total; i++)); do
+               set -- "${tests[@]:i*3:3}"
+               if [[ $2 != N/A && $3 != N/A ]]; then
+                       desc="is_older_than $2 $3 (expecting $1)"
+                       is_older_than "$2" "$3"
+               else
+                       desc="is_older_than (expecting $1)"
+                       is_older_than
+               fi
+               if (( $? == $1 )); then
+                       (( ++passed ))
+               else
+                       printf 'not '
+               fi
+               printf 'ok %d - %s\n' "$((i + 1))" "${desc}"
+       done
+       return "$(( passed < total ))"
+}
 
-tests=(
-       1  ''            ''
-       0  newer         newer
-       1  newer         newer-empty
-       0  newer         newer/file
-       1  newer         non-existent
-       1  newer         older
-       1  newer         older-empty
-       1  newer         older/file
-       0  newer-empty   newer
-       1  newer-empty   newer-empty
-       0  newer-empty   newer/file
-       1  newer-empty   non-existent
-       1  newer-empty   older
-       1  newer-empty   older-empty
-       1  newer-empty   older/file
-       1  newer/file    newer
-       1  newer/file    newer-empty
-       1  newer/file    newer/file
-       1  newer/file    non-existent
-       1  newer/file    older
-       1  newer/file    older-empty
-       1  newer/file    older/file
-       0  non-existent  newer
-       0  non-existent  newer-empty
-       0  non-existent  newer/file
-       1  non-existent  non-existent
-       0  non-existent  older
-       0  non-existent  older-empty
-       0  non-existent  older/file
-       0  older         newer
-       0  older         newer-empty
-       0  older         newer/file
-       1  older         non-existent
-       0  older         older
-       1  older         older-empty
-       0  older         older/file
-       0  older-empty   newer
-       0  older-empty   newer-empty
-       0  older-empty   newer/file
-       1  older-empty   non-existent
-       0  older-empty   older
-       1  older-empty   older-empty
-       0  older-empty   older/file
-       0  older/file    newer
-       0  older/file    newer-empty
-       0  older/file    newer/file
-       1  older/file    non-existent
-       1  older/file    older
-       1  older/file    older-empty
-       1  older/file    older/file
-)
+test_get_bootparam() {
+       local desc i input passed total
+       local -a tests=(
+               1  N/A      "${input:=foo gentoo=bar,baz quux}"
+               1  ''       "${input}"
+               1  ''       "gentoo="
+               1  foo      "${input}"
+               0  bar      "${input}"
+               0  baz      "${input}"
+               1  bar,baz  "${input}"
+       )
 
-total=$(( ${#tests[@]} / 3 ))
-passed=0
+       total=$(( ${#tests[@]} / 3 ))
+       printf '1..%d\n' "${total}"
+       for ((i = 0; i < total; i++)); do
+               set -- "${tests[@]:i*3:3}"
+               if [[ $2 == N/A ]]; then
+                       desc="get_bootparam (expecting $1)"
+                       get_bootparam
+               else
+                       desc="get_bootparam \"$2\" (expecting $1)"
+                       get_bootparam "$2"
+               fi <<<"$3"
+               if (( $? == $1 )); then
+                       (( ++passed ))
+               else
+                       printf 'not '
+               fi
+               printf 'ok %d - %s\n' "$((i + 1))" "${desc}"
+       done
+       return "$(( passed < total ))"
+}
 
-printf '1..%d\n' "${total}"
+printf 'TAP version 14\n'
 
-for ((i = 0; i < total; i++)); do
-       set -- "${tests[@]:i*3:3}"
-       if [[ $2 && $3 ]]; then
-               desc="is_older_than $2 $3 (expecting $1)"
-               is_older_than "$2" "$3"
-       else
-               desc="is_older_than (expecting $1)"
-               is_older_than
-       fi
-       if (( $? == $1 )); then
-               (( ++passed ))
-       else
-               printf 'not '
-       fi
-       printf 'ok %d - %s\n' "$((i + 1))" "${desc}"
-done
+if ! source ./functions.sh; then
+       bailout "Couldn't source ./functions.sh"
+fi
 
-printf >&2 '%d/%d tests passed.\n' "${passed}" "${total}"
+unset -v dir
+trap '[[ ${dir} ]] && rm -rf -- "${dir}"' EXIT
+assign_tmpdir
 
-exit "$(( passed < total ))"
+TEST_GENFUNCS=1
+rc=0
+test_is_older_than || rc=1
+test_get_bootparam || rc=1
+exit "${rc}"

Reply via email to