logo

utils-std

Collection of commonly available Unix tools
commit: b19215fbc3035ab37a093da219f8cdfc8e6128c9
parent 9f439095960843ec79c6083eb0b489ec05efb7b7
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Thu, 13 Jun 2024 11:28:36 +0200

test-cmd/uname.sh: Extract test functions to test-cmd/tap.sh

Diffstat:

Atest-cmd/tap.sh50++++++++++++++++++++++++++++++++++++++++++++++++++
Mtest-cmd/uname.sh41++++++++---------------------------------
2 files changed, 58 insertions(+), 33 deletions(-)

diff --git a/test-cmd/tap.sh b/test-cmd/tap.sh @@ -0,0 +1,50 @@ +#!/bin/false +# utils-std: Collection of commonly available Unix tools +# SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> +# SPDX-License-Identifier: MPL-2.0 + +count=0 +err=0 + +# $1 -> name +# $2 -> arguments +# $3 -> expected output +t () +{ + count=$((count+1)) + out="$("${target?}" $2 2>&1)" + ret="$?" + if [ "$?" != 0 ]; then + printf 'not ok %s - %s\n' "$count $1" "$out" + elif [ "$out" != "$3" ]; then + printf 'not ok %s - (%s != %s)\n' "$count $1" "$out" "$3" + err=1 + else + printf 'ok %s\n' "$count $1" + fi +} + +t_end () +{ + if [ $count -ne $plans ] + then + printf 'Error: Ran %d instead of the planned %d tests\n' "$count" "$plans" >&2 + err=1 + fi + + exit $err +} + +# $1 -> name +# $2 -> reason for skipping +skip () +{ + count=$((count+1)) + name="$1" + shift + printf 'ok %s # skip %s\n' "$count $name" "$*" +} + +printf '1..%d\n' "$plans" + +trap t_end EXIT diff --git a/test-cmd/uname.sh b/test-cmd/uname.sh @@ -2,44 +2,19 @@ # SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> # SPDX-License-Identifier: MPL-2.0 -uname="$(dirname "$0")/../cmd/uname" - -t () -{ - # $1 -> arguments - # $2 -> expected output - - count=$((count+1)) - out="$("$uname" $1 2>&1)" - ret="$?" - if [ "$?" != 0 ]; then - printf 'not ok %s - %s\n' "$count $1" "$out" - elif [ "$out" != "$2" ]; then - printf 'not ok %s - (%s != %s)\n' "$count $1" "$out" "$2" - else - printf 'ok %s\n' "$count $1" - fi -} +target="$(dirname "$0")/../cmd/uname" has_cmd () { command -v "$@" >/dev/null } -skip () -{ - count=$((count+1)) - name="$1" - shift - printf 'ok %s # skip %s\n' "$count $name" "$*" -} - -count=0 +plans=5 -echo '1..5' +. "$(dirname "$0")/tap.sh" -t '' "$(uname -s)" -t '-s' "$(uname -s)" -t '-a' "$(uname -s) $(uname -n) $(uname -r) $(uname -v) $(uname -m)" -t '-n' "$(hostname)" -if has_cmd arch; then t '-m' "$(arch)"; else skip '-m' "no arch command"; fi +t 'no args' '' "$(uname -s)"$'\n' +t '-s' '-s' "$(uname -s)"$'\n' +t '-a' '-a' "$(uname -s) $(uname -n) $(uname -r) $(uname -v) $(uname -m)"$'\n' +t '-n' '-n' "$(hostname)"$'\n' +if has_cmd arch; then t '-m' '-m' "$(arch)"$'\n'; else skip '-m' "no arch command"; fi