Progress on tests definition, init. and clean

Update test result output mainly -o and integrated -oo into it
This commit is contained in:
2022-03-19 19:29:00 +01:00
parent 1802f18339
commit c50bdefba5
2 changed files with 138 additions and 43 deletions

View File

@@ -7,41 +7,72 @@
# test_rdlink.sh [OPTIONS] [PATH TO COMPETE] # test_rdlink.sh [OPTIONS] [PATH TO COMPETE]
# [OPTIONS] # [OPTIONS]
# -a, --run-all : Run all tests (failed and successful) # -a, --run-all : Run all tests (failed and successful)
# -o, --only-result : Show only result (input <> expected)
# #
# Internal tests will be executed if no arguments are found # Internal tests will be executed if no arguments are found
readonly test_dir="/opt/rdlink/test" readonly test_dir="$(cd "$(dirname -- "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
readonly tool_rdlink="/opt/rdlink/rdlink.sh" readonly tool_rdlink="${test_dir}/../rdlink.sh"
readonly tool_readlink="$(command -v readlink) -f" readonly tool_readlink="$(command -v readlink) -f"
readonly config_path_width=45
readonly color_red='\033[1;31m'
readonly color_yellow='\033[1;33m'
readonly color_green='\033[1;32m'
readonly color_less='\033[0m'
flag_runall=0 flag_runall=0
flag_onlyresult=0
flag_verbose=0
toassert=() toassert=()
tocompete=() tocompete=()
. totest.sh . "${test_dir}/totest.sh"
# rl::testcmp <TEST NUMBER> <TEST INPUT> <EXPECTED RESULT> <ACTUAL RESULT> # rl::testcmp <TEST NUMBER> <TEST INPUT> <EXPECTED RESULT> <ACTUAL RESULT>
# Compare results and print summary # Compare results and print summary
rl::testcmp() { rl::testcmp() {
local testnum= local testnum="${1:-0}"
local expect= local input="${2:-"-"}"
local actual= local expect="${3:-"-"}"
local input= local actual="${4:-"-"}"
printf -- "\n%-3d --- %-30s ---" ${testnum:=${1:-0}} "${input:=${2:-"-"}}" local testresult=0 #failed
if [[ "${expect:=${3:-"-"}}" == "${actual:=${4:-"-"}}" ]]; then local testresult_graphic="${color_red}${color_less}" # alt. symbol ≠
# Test successful
printf " OK\n" [[ "${expect}" == "${actual}" ]] && testresult=1 && testresult_graphic="${color_green}${color_less}" # alt. symbol ✓
else
# Show only reslult as quick overview
if (( flag_onlyresult )); then
# Change result graphic if test failed
(( ! testresult )) && testresult_graphic="→" # "${color_green}→${color_less}"
# Truncate input string on the left if longer than $config_path_width
(( ${#input} > ${config_path_width} )) && input="<-${input:$(( ${#input} - ${config_path_width} + 2 ))}"
# Print input and expected
printf "%${config_path_width}s %b %s\n" "${input}" "${testresult_graphic}" "${expect}"
# Print actual result if test failed
if (( ! testresult )); then
printf "%$((config_path_width))s %b %s\n" " " "${color_red}${color_less}" "${actual}"
return 1
fi
return 0
fi
printf -- "%-3d Inp: %-${config_path_width}s ---" ${testnum} "${input}"
printf " %b\n" "${testresult_graphic}"
if (( ! testresult )); then
# Test failed # Test failed
printf " FAIL\n" printf " Result: %s\n Exp: %s\n" "${actual}" "${expect}"
printf " Result:\nExp: %s\nAct: %s\n" "${expect}" "${actual}"
[ -e "${input}" ] && printf "\n Subject:\n" && ls -al "${input}"
# Print debug output of tool_a if (( flag_verbose )); then
printf "\n Debug:\n" [ -e "${input}" ] && printf "\n Subject:\n" && ls -al "${input}"
( "${tool_rdlink}" -d "${input}" )
# Print debug output of tool_a
printf "\n Debug:\n"
( "${tool_rdlink}" -d "${input}" )
fi
return 1 return 1
fi fi
return 0 return 0
@@ -56,12 +87,18 @@ rl::test() {
local arg= local arg=
for arg in "$@"; do for arg in "$@"; do
case "$arg" in case "$1" in
--) ## End of options --) ## End of options
shift && break ;; shift && break ;;
-a|--run-all) ## Run all even if tests fail -a|--run-all) ## Run all even if tests fail
flag_runall=1 flag_runall=1
shift ;; shift ;;
-o|--only-result)
flag_onlyresult=1
shift ;;
-v|--verbose)
flag_verbose=1
shift ;;
esac esac
done done
@@ -73,13 +110,15 @@ rl::test() {
fi fi
# Compare against expected result # Compare against expected result
printf "\n# Assertion tests (\"Expected path\" == rdlink)\n"
for testarray in "${toassert[@]}"; do for testarray in "${toassert[@]}"; do
i=0 i=0
arraywalker="$testarray"[@] arraywalker="$testarray"[@]
for path in "${!arraywalker}"; do for path in "${!arraywalker}"; do
if (( ! i )); then if (( ! i )); then
# Print title in array element 0 # Print title in array element 0
printf " ### %s ###\n" "$path" printf " ### %b ###\n" "${color_yellow}${path}${color_less}"
(( flag_onlyresult )) && printf "%${config_path_width}s %s %s\n" "<INPUT>" "<EXPECTED>" "✗ [ACTUAL]"
elif [ -z "$firstelement" ]; then elif [ -z "$firstelement" ]; then
# Save first element for string compare # Save first element for string compare
@@ -87,8 +126,8 @@ rl::test() {
else else
# Do the compare between two following elements # Do the compare between two following elements
if ! rl::testcmp "$((i-1))" "${path}" \ if ! rl::testcmp "$((i-1))" "${firstelement}" \
"${firstelement}" "$("$tool_rdlink" "${path}")"; then "${path}" "$("$tool_rdlink" "${firstelement}")"; then
# Run all tests if option -a is pressend # Run all tests if option -a is pressend
(( ! $flag_runall )) && testend=1 && break (( ! $flag_runall )) && testend=1 && break
fi fi
@@ -101,6 +140,7 @@ rl::test() {
done done
# Compare output of rdlink and readlink -f # Compare output of rdlink and readlink -f
printf "\n# Competition tests (readlink -f == rdlink)\n"
for testarray in "${tocompete[@]}"; do for testarray in "${tocompete[@]}"; do
i=0 i=0
arraywalker="$testarray"[@] arraywalker="$testarray"[@]
@@ -108,7 +148,8 @@ rl::test() {
if (( ! i )); then if (( ! i )); then
# Print title in array element 0 # Print title in array element 0
printf " ### %s ###\n" "$path" printf " ### %b ###\n" "${color_yellow}${path}${color_less}"
(( flag_onlyresult )) && printf "%${config_path_width}s %s %s\n" "<INPUT>" "<EXPECTED>" "✗ [ACTUAL]"
else else
# Execute tests # Execute tests

View File

@@ -1,65 +1,120 @@
#!/usr/bin/env bash #!/usr/bin/env bash
########## Competition Block ########## ########## Competition Block ##########
# tc = test compete
_tc_tmp="${test_dir:-"/tmp"}/tmp_compete"
# Each competition link is given to both tools # Each competition link is given to both tools
# and the output is compared # and the output is compared
#
# Array format:
# [0] = Title # [0] = Title
# [1] = path for competition # [1] = path for competition
# [2] = path for competition # [2] = path for competition
# ... # ...
compete_links=( "Test - Valid links"
"/dev/stdin"
"$test_dir/tmp/a"*
"$test_dir/tmp/a 3"
"/bin/adb"
)
# Compete test suites (arrays)
compete_canonicalize=(
"Canonicalize invalid path"
"///tmp//./b"
"//tmp//./b/.."
"//tmp//./b/."
)
compete_all=( "Test - everything starting from /" compete_all=( "Test - everything starting from /"
/**/* /**/*
) )
tocompete_init() { tocompete_init() {
# TODO initialize custom test structure # initialize custom test structure
{
mkdir -p "${_tc_tmp}"
# compete_links
touch "${_tc_tmp}/a"
ln -s "${_tc_tmp}/a" "${_tc_tmp}/a 2"
ln -s "${_tc_tmp}/a 2" "${_tc_tmp}/a 3"
}
# Base directory for the test
cd "${_tc_tmp}"
# Compete test arrays with "dynamic" cases need to be inside the init function
# e.g. "$(cd ../test && pwd)/file" - base directory must be set first
# e.g. "${_tc_tmp}/"* - _tc_tmp must be populated first
compete_links=( "Test - Valid links"
"${_tc_tmp}/a 3" # slink chain a3 -> a2 -> a
"${_tc_tmp}/a"*
"/dev/stdin"
"/bin/adb"
)
# Add tests to global test array from test_rdlink # Add tests to global test array from test_rdlink
tocompete+=( tocompete+=(
compete_links compete_links
compete_canonicalize
#compete_all
) )
} }
tocompete_clean() { tocompete_clean() {
# TODO clean custom test structure # TODO clean custom test structure
echo "tocompete_clean" rm -rf "${_tc_tmp}"
} }
tocompete_init tocompete_init
########## Assertion Block ########## ########## Assertion Block ##########
# Assertion string compare test
# ta = test assert
_ta_tmp="${test_dir:-"/tmp"}/tmp_assert"
# Assertion string compare test arrays
#
# Array format
# [0] = Title # [0] = Title
# [1] expected == [2] input # [1] input == [2] expected
# [3] expected == [4] input # [3] input == [4] expected
# ... # ...
assert_string=( "Assert - invalid files"
"/opt/rdlink/test/a" "a"
"/opt/rdlink/test/b" "b"
"/opt/rdlink/test/c" "c"
)
toassert_init() { toassert_init() {
# TODO initilaize custom test structure # TODO initilaize custom test structure
{
mkdir -p "${_ta_tmp}"
}
# Base directory for the test
cd "${_ta_tmp}"
# Assert test arrays with "dynamic" cases need to be inside the init function
# e.g. "$(cd ../test && pwd)/file" - base directory must be set first
# e.g. "${_tc_tmp}/"* - _tc_tmp must be populated first
assert_invalid_files=( "Assert - invalid files"
"${_ta_tmp}/missing_file" "${_ta_tmp}/missing_file"
"${_ta_tmp}/missd/missf" "${_ta_tmp}/missd/missf"
"${_ta_tmp}/miss c" "${_ta_tmp}/miss c"
"rel_a" "${_ta_tmp}/rel_a"
"../rel_b" "$(cd ".." && pwd)/rel_b"
"/a/very/long/path/whthMustExceed/fourtyfive/character" "a"
)
# Add test arrays to global test array from test_rdlink # Add test arrays to global test array from test_rdlink
toassert+=( toassert+=(
assert_string assert_invalid_files
) )
} }
toassert_clean() { toassert_clean() {
# TODO clean # TODO clean
echo "toassert_clean" rm -rf "${_ta_tmp}"
} }
toassert_init
########## Clean custom test data ##########
#
totest_cleanall() { totest_cleanall() {
echo echo
tocompete_clean tocompete_clean
@@ -67,4 +122,3 @@ totest_cleanall() {
} }
trap totest_cleanall EXIT trap totest_cleanall EXIT
toassert_init