Files
rdlink/test/test_rdlink.sh
2022-03-18 21:48:39 +01:00

129 lines
3.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# Automatic tests for rdlink
# Two kinds of tests are supported
# * `tocompete` : competition between readlink and rdlink
# * `toassert` : assert expected path and input path
# test_rdlink.sh [OPTIONS] [PATH TO COMPETE]
# [OPTIONS]
# -a, --run-all : Run all tests (failed and successful)
#
# Internal tests will be executed if no arguments are found
readonly test_dir="/opt/rdlink/test"
readonly tool_rdlink="/opt/rdlink/rdlink.sh"
readonly tool_readlink="$(command -v readlink) -f"
flag_runall=0
toassert=()
tocompete=()
. totest.sh
# rl::testcmp <TEST NUMBER> <TEST INPUT> <EXPECTED RESULT> <ACTUAL RESULT>
# Compare results and print summary
rl::testcmp() {
local testnum=
local expect=
local actual=
local input=
printf -- "\n%-3d --- %-30s ---" ${testnum:=${1:-0}} "${input:=${2:-"-"}}"
if [[ "${expect:=${3:-"-"}}" == "${actual:=${4:-"-"}}" ]]; then
# Test successful
printf " OK\n"
else
# Test failed
printf " FAIL\n"
printf " Result:\nExp: %s\nAct: %s\n" "${expect}" "${actual}"
[ -e "${input}" ] && printf "\n Subject:\n" && ls -al "${input}"
# Print debug output of tool_a
printf "\n Debug:\n"
( "${tool_rdlink}" -d "${input}" )
return 1
fi
return 0
}
rl::test() {
local i=0
local path=
local arraywalker=
local firstelement=
local testend=0
local arg=
for arg in "$@"; do
case "$arg" in
--) ## End of options
shift && break ;;
-a|--run-all) ## Run all even if tests fail
flag_runall=1
shift ;;
esac
done
# Only run `compete_args` if arguments are available
if [[ "$@" ]]; then
compete_args=( "Tests from command line" "$@" )
(( flag_runall )) && tocompete+=(compete_args) || tocompete=(compete_args)
toassert=()
fi
# Compare against expected result
for testarray in "${toassert[@]}"; do
i=0
arraywalker="$testarray"[@]
for path in "${!arraywalker}"; do
if (( ! i )); then
# Print title in array element 0
printf " ### %s ###\n" "$path"
elif [ -z "$firstelement" ]; then
# Save first element for string compare
firstelement="${path}"
else
# Do the compare between two following elements
if ! rl::testcmp "$((i-1))" "${path}" \
"${firstelement}" "$("$tool_rdlink" "${path}")"; then
# Run all tests if option -a is pressend
(( ! $flag_runall )) && testend=1 && break
fi
firstelement=
fi
((i++))
done
(( testend )) && return 1
done
# Compare output of rdlink and readlink -f
for testarray in "${tocompete[@]}"; do
i=0
arraywalker="$testarray"[@]
for path in "${!arraywalker}"; do
if (( ! i )); then
# Print title in array element 0
printf " ### %s ###\n" "$path"
else
# Execute tests
if ! rl::testcmp "${i}" "${path}" \
"$(${tool_readlink} "$path")" "$(${tool_rdlink} "$path")"; then
# Run all tests if option -a is pressend
(( ! $flag_runall )) && testend=1 && break
fi
fi
((i++))
done
(( testend )) && return 1
done
}
time rl::test "$@"