Refactor to support more tests
Extend testcases: assertion test and competition tests
This commit is contained in:
@@ -1,71 +1,143 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
|
||||
tool_a="/opt/rdlink/rdlink.sh"
|
||||
tool_b="$(command -v readlink) -f"
|
||||
# Automatic tests for rdlink
|
||||
# Two kinds of tests are supported
|
||||
# * `tocompete` : competition between readlink and rdlink
|
||||
# * `toassert` : assert expected path and input path
|
||||
|
||||
test_links=( "Valid links"
|
||||
# test_rdlink.sh [PATH TO COMPETE]
|
||||
#
|
||||
# Internal tests will be executed if no arguments are found
|
||||
|
||||
test_dir="/opt/rdlink/test"
|
||||
tool_rdlink="/opt/rdlink/rdlink.sh"
|
||||
tool_readlink="$(command -v readlink) -f"
|
||||
|
||||
########## Competition Block ##########
|
||||
# Each competition link is given to both tools
|
||||
# and the output is compared
|
||||
# [0] = Title
|
||||
# [1] = path for competition
|
||||
# [2] = path for competition
|
||||
# ...
|
||||
compete_links=( "Valid links"
|
||||
"/dev/stdin"
|
||||
"$HOME/tmp/a"*
|
||||
"$HOME/tmp/a 3"
|
||||
"$test_dir/tmp/a"*
|
||||
"$test_dir/tmp/a 3"
|
||||
"/bin/adb"
|
||||
)
|
||||
|
||||
test_all=( "Test everything starting from /"
|
||||
compete_all=( "Test everything starting from /"
|
||||
/**/*
|
||||
)
|
||||
|
||||
test_args=( "Tests from command line"
|
||||
compete_args=( "Tests from command line"
|
||||
"$@"
|
||||
)
|
||||
|
||||
totest=(
|
||||
test_links
|
||||
# Lists for competition test between rdlink and readlink -f
|
||||
tocompete=(
|
||||
compete_links
|
||||
)
|
||||
|
||||
[[ "$@" ]] && totest=(test_args)
|
||||
########## Assertion Block ##########
|
||||
# Assertion string compare test
|
||||
# [0] = Title
|
||||
# [1] expected == [2] input
|
||||
# [3] expected == [4] input
|
||||
# ...
|
||||
assert_string=( "Assert invalid files"
|
||||
"/opt/rdlink/test/a" "a"
|
||||
"/opt/rdlink/test/b" "b"
|
||||
"/opt/rdlink/test/c" "c"
|
||||
)
|
||||
|
||||
# List of assertian test arrays
|
||||
toassert=(
|
||||
assert_string
|
||||
)
|
||||
|
||||
# Only run `compete_args` if arguments are available
|
||||
[[ "$@" ]] && tocompete=(compete_args) && toassert=()
|
||||
|
||||
# rl::testcmp <TEST NUMBER> <TEST INPUT> <EXPECTED RESULT> <ACTUAL RESULT>
|
||||
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 ra=
|
||||
local rb=
|
||||
local path=
|
||||
local testarray=
|
||||
local arraywalker=
|
||||
local firstelement=
|
||||
local testend=0
|
||||
|
||||
for testarray in "${totest[@]}"; do
|
||||
# Compare against expected result
|
||||
for testarray in "${toassert[@]}"; do
|
||||
i=0
|
||||
testarray="$testarray"[@]
|
||||
for path in "${!testarray}"; do
|
||||
|
||||
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
|
||||
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
|
||||
printf -- "%-3d --- %-30s ---" $i "$path"
|
||||
ra="$(${tool_a} "$path")"
|
||||
rb="$(${tool_b} "$path")"
|
||||
if [[ "$ra" = "$rb" ]]; then
|
||||
# Test successful
|
||||
printf " OK\n"
|
||||
else
|
||||
# Test failed
|
||||
printf " FAIL\n"
|
||||
printf "\n Result:\n%s\n%s\n" "$ra" "$rb"
|
||||
printf "\n Subject:\n"
|
||||
ls -al "$path"
|
||||
|
||||
# Print debug output of tool_a
|
||||
printf "\n Debug:\n"
|
||||
( "${tool_a}" -d "${path}" )
|
||||
|
||||
testend=1 && break
|
||||
if ! rl::testcmp "${i}" "${path}" \
|
||||
"$(${tool_readlink} "$path")" "$(${tool_rdlink} "$path")"; then
|
||||
testend=1
|
||||
break
|
||||
fi
|
||||
fi
|
||||
|
||||
((i++))
|
||||
done
|
||||
(( testend )) && break
|
||||
(( testend )) && return 1
|
||||
done
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user