tap.sh (3086B)
- #!/bin/false
- # extracted from https://hacktivis.me/git/utils-std
- # SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
- # SPDX-License-Identifier: MPL-2.0
- count=0
- err=0
- # t [--exit=n] [--input=str] <test_name> <expected_output> <arguments ...>
- t() {
- exp_ret=0
- for i; do
- case "$i" in
- --exit=*)
- exp_ret="${i#*=}"
- shift
- ;;
- --input=*)
- input="${i#*=}"
- shift
- ;;
- --)
- shift
- break
- ;;
- # Note: * is still a wildcard, even with a range before
- -[a-zA-Z0-9]|--[a-zA-Z0-9]*=*)
- printf 'Unknown option: %s\n' "$i"
- exit 2
- ;;
- *)
- break
- ;;
- esac
- done
- name="$1"; shift
- exp_out="$1"; shift
- if [ "${input+set}" = "set" ]; then
- # Append a final slash so sh(1) doesn't trims final newlines
- out="$(printf "${input?}" | "${target?}" "$@" 2>&1;r=$?;printf %s /;exit $r)"
- ret="$?"
- else
- # Append a final slash so sh(1) doesn't trims final newlines
- out="$("${target?}" "$@" 2>&1;r=$?;printf %s /;exit $r)"
- ret="$?"
- fi
- out="${out%/}"
- count=$((count+1))
- if [ "$ret" != "$exp_ret" ]; then
- printf 'not ok %d - %s\n' "$count" "$name"
- printf '# Expected exit code %d, got %d\n' "$exp_ret" "$ret"
- printf '# == Got ==\n'
- printf '# %s\n' "$out" | sed -e 's;^[^#];# ;'
- err=1
- elif [ "$out" != "$exp_out" ]; then
- printf 'not ok %d - %s\n' "$count" "$name"
- printf '# == Expected ==\n'
- printf '# %s\n' "$exp_out" | sed -e 's;^[^#];# ;'
- printf '# == Got ==\n'
- printf '# %s\n' "$out" | sed -e 's;^[^#];# ;'
- err=1
- else
- printf 'ok %d - %s\n' "$count" "$name"
- fi
- }
- # t_cmd [--exit=n] <test_name> <expected_output> <command> [arguments]
- t_cmd() {
- exp_ret=0
- for i; do
- case "$i" in
- --exit=*)
- exp_ret="${i#*=}"
- shift
- ;;
- --)
- shift
- break
- ;;
- # Note: * is still a wildcard, even with a range before
- -[a-zA-Z0-9]|--[a-zA-Z0-9]*=*)
- printf 'Unknown option: %s\n' "$i"
- exit 2
- ;;
- *)
- break
- ;;
- esac
- done
- name="$1"; shift
- exp_out="$1"; shift
- [ -z "${name}" ] && name="$*"
- # Append a final slash so sh(1) doesn't trims final newlines
- out="$("$@" 2>&1;r=$?;printf %s /;exit $r)"
- ret="$?"
- out="${out%/}"
- count=$((count+1))
- if [ "$ret" != "$exp_ret" ]; then
- printf 'not ok %d - %s\n' "$count" "$name"
- printf '# Expected exit code %d, got %d\n' "$exp_ret" "$ret"
- printf '# %s\n' "$out" | sed -e 's;^[^#];# ;'
- err=1
- elif [ "$out" != "$exp_out" ]; then
- printf 'not ok %d - %s\n' "$count" "$name"
- printf '# == Expected ==\n'
- printf '# %s\n' "$exp_out" | sed -e 's;^[^#];# ;'
- printf '# == Got ==\n'
- printf '# %s\n' "$out" | sed -e 's;^[^#];# ;'
- err=1
- else
- printf 'ok %d - %s\n' "$count" "$name"
- 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" "$*"
- }
- if ! test -f "${target?}"; then
- printf '1..0 # SKIP: missing executable: %s\n' "${target?}"
- exit 0
- fi
- printf '1..%d\n' "$plans"
- trap t_end EXIT