diff options
author | Kerin Millar <kfm@plushkava.net> | 2023-02-07 00:41:16 +0000 |
---|---|---|
committer | Sam James <sam@gentoo.org> | 2023-02-07 01:02:44 +0000 |
commit | 91ceadf4b0e1b2d1e5c8a45ee7abc38005ec0f2a (patch) | |
tree | 16de14c1e9fb55083368216346a674dc5def428d /test-functions | |
parent | Re-implement get_bootparam as pure sh, eliminating the gawk dependency (diff) | |
download | gentoo-functions-91ceadf4b0e1b2d1e5c8a45ee7abc38005ec0f2a.tar.gz gentoo-functions-91ceadf4b0e1b2d1e5c8a45ee7abc38005ec0f2a.tar.bz2 gentoo-functions-91ceadf4b0e1b2d1e5c8a45ee7abc38005ec0f2a.zip |
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@plushkava.net>
Signed-off-by: Sam James <sam@gentoo.org>
Diffstat (limited to 'test-functions')
-rwxr-xr-x | test-functions | 233 |
1 files changed, 136 insertions, 97 deletions
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}" |