From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 8CC5215800F for ; Tue, 7 Feb 2023 01:08:55 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id BF83BE0891; Tue, 7 Feb 2023 01:08:54 +0000 (UTC) Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id A49B0E0891 for ; Tue, 7 Feb 2023 01:08:54 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id B6CD5340A9F for ; Tue, 7 Feb 2023 01:08:53 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 6A61F89C for ; Tue, 7 Feb 2023 01:08:51 +0000 (UTC) From: "Sam James" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Sam James" Message-ID: <1675731764.91ceadf4b0e1b2d1e5c8a45ee7abc38005ec0f2a.sam@gentoo> Subject: [gentoo-commits] proj/gentoo-functions:master commit in: / X-VCS-Repository: proj/gentoo-functions X-VCS-Files: functions.sh test-functions X-VCS-Directories: / X-VCS-Committer: sam X-VCS-Committer-Name: Sam James X-VCS-Revision: 91ceadf4b0e1b2d1e5c8a45ee7abc38005ec0f2a X-VCS-Branch: master Date: Tue, 7 Feb 2023 01:08:51 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: 62aba88c-1fec-4b68-9cf0-b19a37dcdd72 X-Archives-Hash: 5345472dee0eed5185c61ed402cdcc4e commit: 91ceadf4b0e1b2d1e5c8a45ee7abc38005ec0f2a Author: Kerin Millar plushkava net> AuthorDate: Tue Feb 7 00:41:16 2023 +0000 Commit: Sam James gentoo 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 plushkava.net> Signed-off-by: Sam James 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}"