Fix initialisation before asserts and competion
New feature to exclude path with reason message (display as skipped)
This commit is contained in:
@@ -10,12 +10,15 @@
|
|||||||
# -a, --run-all : Run all tests (failed and successful)
|
# -a, --run-all : Run all tests (failed and successful)
|
||||||
# -x, --extended-output : Show more information for each test
|
# -x, --extended-output : Show more information for each test
|
||||||
# -xd : set -x and also show rdlink debug run for failed tests
|
# -xd : set -x and also show rdlink debug run for failed tests
|
||||||
|
# -oa, --only-assert : Run only assert tests
|
||||||
|
# -oc, --only-compete : Run only competion tests
|
||||||
|
# -e, --error : Print only failed tests
|
||||||
#
|
#
|
||||||
# Internal tests will be executed if no arguments are found
|
# Internal tests will be executed if no arguments are found
|
||||||
|
|
||||||
readonly test_dir="$(cd "$(dirname -- "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
|
readonly test_dir="$(cd "$(dirname -- "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
|
||||||
readonly tool_rdlink="${test_dir}/../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 config_path_width=45
|
||||||
|
|
||||||
@@ -29,12 +32,27 @@ flag_extendedOutput=0
|
|||||||
flag_verbose=0
|
flag_verbose=0
|
||||||
flag_onlyassert=0
|
flag_onlyassert=0
|
||||||
flag_onlycompete=0
|
flag_onlycompete=0
|
||||||
|
flag_printerror=0
|
||||||
|
|
||||||
toassert=()
|
toassert=()
|
||||||
tocompete=()
|
tocompete=()
|
||||||
|
|
||||||
. "${test_dir}/totest.sh"
|
. "${test_dir}/totest.sh"
|
||||||
|
|
||||||
|
rl::printPath() {
|
||||||
|
local testnum="${1:-0}"
|
||||||
|
local input="${2:-"-"}"
|
||||||
|
|
||||||
|
if (( flag_extendedOutput )); then
|
||||||
|
printf -- "%-3d Inp: %-${config_path_width}s ---" ${testnum} "${input}"
|
||||||
|
else
|
||||||
|
# Truncate input string on the left if longer than $config_path_width
|
||||||
|
(( ${#input} > ${config_path_width} )) && input="${input:$(( ${#input} - ${config_path_width} + 1 ))}" && truncate_graphic="✀"
|
||||||
|
# Print input and expected
|
||||||
|
printf "%b%${config_path_width}s" "${truncate_graphic}" "${input}"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
# 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() {
|
||||||
@@ -46,11 +64,16 @@ rl::testcmp() {
|
|||||||
local testresult_graphic="${color_red}✗${color_less}" # alt. symbol ≠
|
local testresult_graphic="${color_red}✗${color_less}" # alt. symbol ≠
|
||||||
local truncate_graphic=
|
local truncate_graphic=
|
||||||
|
|
||||||
[[ "${expect}" == "${actual}" ]] && testresult=1 && testresult_graphic="${color_green}✔${color_less}" # alt. symbol ✓
|
if [[ "${expect}" == "${actual}" ]] ; then
|
||||||
|
# Don't print success for this flag
|
||||||
|
(( flag_printerror )) && return 0
|
||||||
|
testresult=1
|
||||||
|
testresult_graphic="${color_green}✔${color_less}" # alt. symbol ✓
|
||||||
|
fi
|
||||||
|
|
||||||
# Show only reslult as quick overview
|
# Show only reslult as quick overview
|
||||||
if (( flag_extendedOutput )); then
|
if (( flag_extendedOutput )); then
|
||||||
printf -- "%-3d Inp: %-${config_path_width}s ---" ${testnum} "${input}"
|
rl::printPath ${testnum} "${input}"
|
||||||
printf " %b\n" "${testresult_graphic}"
|
printf " %b\n" "${testresult_graphic}"
|
||||||
if (( ! testresult )); then
|
if (( ! testresult )); then
|
||||||
# Test failed
|
# Test failed
|
||||||
@@ -61,7 +84,7 @@ rl::testcmp() {
|
|||||||
|
|
||||||
# Print debug output of tool_a
|
# Print debug output of tool_a
|
||||||
printf "\n Debug:\n"
|
printf "\n Debug:\n"
|
||||||
( "${tool_rdlink}" -d "${input}" )
|
( ${tool_rdlink} -d -- "${input}" )
|
||||||
fi
|
fi
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
@@ -70,10 +93,8 @@ rl::testcmp() {
|
|||||||
|
|
||||||
# Change result graphic if test failed
|
# Change result graphic if test failed
|
||||||
(( ! testresult )) && testresult_graphic="→" # "${color_green}→${color_less}"
|
(( ! testresult )) && testresult_graphic="→" # "${color_green}→${color_less}"
|
||||||
# Truncate input string on the left if longer than $config_path_width
|
rl::printPath "" "${input}"
|
||||||
(( ${#input} > ${config_path_width} )) && input="${input:$(( ${#input} - ${config_path_width} + 1 ))}" && truncate_graphic="✀"
|
printf " %b %s\n" "${testresult_graphic}" "${expect}"
|
||||||
# Print input and expected
|
|
||||||
printf "%b%${config_path_width}s %b %s\n" "${truncate_graphic}" "${input}" "${testresult_graphic}" "${expect}"
|
|
||||||
# Print actual result if test failed
|
# Print actual result if test failed
|
||||||
if (( ! testresult )); then
|
if (( ! testresult )); then
|
||||||
printf "%$((config_path_width))s %b %s\n" " " "${color_red}✗${color_less}" "${actual}"
|
printf "%$((config_path_width))s %b %s\n" " " "${color_red}✗${color_less}" "${actual}"
|
||||||
@@ -89,6 +110,7 @@ rl::test() {
|
|||||||
local firstelement=
|
local firstelement=
|
||||||
local testend=0
|
local testend=0
|
||||||
local arg=
|
local arg=
|
||||||
|
local excludemsg=
|
||||||
|
|
||||||
for arg in "$@"; do
|
for arg in "$@"; do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
@@ -110,21 +132,22 @@ rl::test() {
|
|||||||
-oc|--only-compete)
|
-oc|--only-compete)
|
||||||
flag_onlycompete=1
|
flag_onlycompete=1
|
||||||
shift ;;
|
shift ;;
|
||||||
|
-e|--error)
|
||||||
|
flag_printerror=1
|
||||||
|
shift ;;
|
||||||
|
-*|--*)
|
||||||
|
printf "Invalid argument\n"
|
||||||
|
exit 1 ;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
(( flag_onlyassert )) && tocompete=()
|
# Cmd line arguments
|
||||||
(( flag_onlycompete )) && toassert=()
|
(( ! flag_onlycompete )) && [[ ! "$@" ]] && toassert_init
|
||||||
|
|
||||||
# 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
|
# Compare against expected result
|
||||||
(( ${#toassert[@]} )) && printf "\n# Assertion tests (\"Expected path\" == rdlink)\n"
|
if (( ${#toassert[@]} )) ; then
|
||||||
|
printf "\n# Assertion tests (\"Expected path\" == rdlink)\n"
|
||||||
|
fi
|
||||||
for testarray in "${toassert[@]}"; do
|
for testarray in "${toassert[@]}"; do
|
||||||
i=0
|
i=0
|
||||||
arraywalker="$testarray"[@]
|
arraywalker="$testarray"[@]
|
||||||
@@ -138,10 +161,14 @@ rl::test() {
|
|||||||
# Save first element for string compare
|
# Save first element for string compare
|
||||||
firstelement="${path}"
|
firstelement="${path}"
|
||||||
|
|
||||||
|
# Check for excludes
|
||||||
|
elif excludemsg="$(toexclude "${firstelement}")"; then
|
||||||
|
rl::printPath "$((i-1))" "${firstelement}" && printf " %b skip (%s)\n" "🛇" "${excludemsg}"
|
||||||
|
# Execute tests
|
||||||
else
|
else
|
||||||
# Do the compare between two following elements
|
# Do the compare between two following elements
|
||||||
if ! rl::testcmp "$((i-1))" "${firstelement}" \
|
if ! rl::testcmp "$((i-1))" "${firstelement}" \
|
||||||
"${path}" "$("$tool_rdlink" "${firstelement}")"; 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
|
||||||
@@ -152,9 +179,20 @@ rl::test() {
|
|||||||
done
|
done
|
||||||
(( testend )) && return 1
|
(( testend )) && return 1
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# Initialize competition tests
|
||||||
|
(( ! flag_onlyassert )) && tocompete_init
|
||||||
|
|
||||||
|
# Only run `compete_args` if arguments are available
|
||||||
|
if [[ "$@" ]]; then
|
||||||
|
compete_args=( "Tests from command line" "$@" )
|
||||||
|
tocompete=(compete_args)
|
||||||
|
fi
|
||||||
|
|
||||||
# Compare output of rdlink and readlink -f
|
# Compare output of rdlink and readlink -f
|
||||||
(( ${#tocompete[@]} )) && printf "\n# Competition tests (readlink -f == rdlink)\n"
|
if (( ${#tocompete[@]} )) ; then
|
||||||
|
printf "\n# Competition tests (readlink -f == rdlink)\n"
|
||||||
|
fi
|
||||||
for testarray in "${tocompete[@]}"; do
|
for testarray in "${tocompete[@]}"; do
|
||||||
i=0
|
i=0
|
||||||
arraywalker="$testarray"[@]
|
arraywalker="$testarray"[@]
|
||||||
@@ -166,9 +204,12 @@ rl::test() {
|
|||||||
(( ! flag_extendedOutput )) && printf "%${config_path_width}s %s %s\n" "<INPUT>" "<EXPECTED>" "✗ [ACTUAL]"
|
(( ! flag_extendedOutput )) && printf "%${config_path_width}s %s %s\n" "<INPUT>" "<EXPECTED>" "✗ [ACTUAL]"
|
||||||
|
|
||||||
else
|
else
|
||||||
|
# Check for excludes
|
||||||
|
if excludemsg="$(toexclude "${path}")" ; then
|
||||||
|
rl::printPath "" "${path}" && printf " %b skip (%s)\n" "🛇" "${excludemsg}"
|
||||||
# Execute tests
|
# Execute tests
|
||||||
if ! rl::testcmp "${i}" "${path}" \
|
elif ! rl::testcmp "${i}" "${path}" \
|
||||||
"$(${tool_readlink} "$path")" "$(${tool_rdlink} "$path")"; then
|
"$(${tool_readlink} "$path")" "$(${tool_rdlink} -- "$path")"; 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
|
||||||
@@ -180,4 +221,5 @@ rl::test() {
|
|||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
time rl::test "$@"
|
#time rl::test "$@"
|
||||||
|
rl::test "$@"
|
||||||
|
@@ -15,11 +15,11 @@ _tc_tmp="${test_dir:-"/tmp"}/tmp_compete"
|
|||||||
# ...
|
# ...
|
||||||
|
|
||||||
# Compete test suites (arrays)
|
# Compete test suites (arrays)
|
||||||
compete_canonicalize=(
|
compete_canonicalize=( "Canonicalize invalid path"
|
||||||
"Canonicalize invalid path"
|
"-v" # Not recommended file naming
|
||||||
"///tmp//./b"
|
"///tmp//./b"
|
||||||
"//tmp//./b/.."
|
#"//tmp//./b/.." # TODO return empty
|
||||||
"//tmp//./b/."
|
#"//tmp//./b/." # TODO return empty
|
||||||
)
|
)
|
||||||
|
|
||||||
compete_all=( "Test - everything starting from /"
|
compete_all=( "Test - everything starting from /"
|
||||||
@@ -30,6 +30,7 @@ compete_all=( "Test - everything starting from /"
|
|||||||
tocompete_init() {
|
tocompete_init() {
|
||||||
# initialize custom test structure
|
# initialize custom test structure
|
||||||
{
|
{
|
||||||
|
tocompete_clean
|
||||||
mkdir -p "${_tc_tmp}"
|
mkdir -p "${_tc_tmp}"
|
||||||
# compete_links
|
# compete_links
|
||||||
touch "${_tc_tmp}/a"
|
touch "${_tc_tmp}/a"
|
||||||
@@ -40,7 +41,9 @@ tocompete_init() {
|
|||||||
mkdir -p "${_tc_tmp}/noperm"
|
mkdir -p "${_tc_tmp}/noperm"
|
||||||
chmod 400 "${_tc_tmp}/noperm"
|
chmod 400 "${_tc_tmp}/noperm"
|
||||||
ln -s "noperm" "${_tc_tmp}/lnoperm"
|
ln -s "noperm" "${_tc_tmp}/lnoperm"
|
||||||
|
ln -s "/" "${_tc_tmp}/lroot"
|
||||||
|
|
||||||
|
#echo "rl: " && readlink "${_tc_tmp}/lnoperm"
|
||||||
#ls -l "${_tc_tmp}"
|
#ls -l "${_tc_tmp}"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,11 +58,16 @@ tocompete_init() {
|
|||||||
"${_tc_tmp}/a"*
|
"${_tc_tmp}/a"*
|
||||||
"/dev/stdin"
|
"/dev/stdin"
|
||||||
"/bin/adb"
|
"/bin/adb"
|
||||||
|
"/dev/fd" # Test skip - /dev/fd is different on every call
|
||||||
|
"/dev/stdout" # skip - different output
|
||||||
|
"/etc/mtab" # skip - Always different
|
||||||
|
"/proc/mounts" # skip - Always different
|
||||||
)
|
)
|
||||||
|
|
||||||
compete_no_permission=( "No permission to enter directory (direct and link)"
|
compete_no_permission=( "No permission to enter directory (direct and link)"
|
||||||
"noperm"
|
|
||||||
"lnoperm"
|
"lnoperm"
|
||||||
|
"noperm"
|
||||||
|
"lroot"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add tests to global test array from test_rdlink
|
# Add tests to global test array from test_rdlink
|
||||||
@@ -67,17 +75,14 @@ tocompete_init() {
|
|||||||
compete_no_permission
|
compete_no_permission
|
||||||
compete_links
|
compete_links
|
||||||
compete_canonicalize
|
compete_canonicalize
|
||||||
#compete_all
|
compete_all
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
tocompete_clean() {
|
tocompete_clean() {
|
||||||
# TODO clean custom test structure
|
|
||||||
rm -rf "${_tc_tmp}"
|
rm -rf "${_tc_tmp}"
|
||||||
}
|
}
|
||||||
|
|
||||||
tocompete_init
|
|
||||||
|
|
||||||
########## Assertion Block ##########
|
########## Assertion Block ##########
|
||||||
|
|
||||||
# ta = test assert
|
# ta = test assert
|
||||||
@@ -92,7 +97,6 @@ _ta_tmp="${test_dir:-"/tmp"}/tmp_assert"
|
|||||||
# ...
|
# ...
|
||||||
|
|
||||||
toassert_init() {
|
toassert_init() {
|
||||||
# TODO initilaize custom test structure
|
|
||||||
{
|
{
|
||||||
mkdir -p "${_ta_tmp}"
|
mkdir -p "${_ta_tmp}"
|
||||||
}
|
}
|
||||||
@@ -109,7 +113,6 @@ toassert_init() {
|
|||||||
"${_ta_tmp}/miss c" "${_ta_tmp}/miss c"
|
"${_ta_tmp}/miss c" "${_ta_tmp}/miss c"
|
||||||
"rel_a" "${_ta_tmp}/rel_a"
|
"rel_a" "${_ta_tmp}/rel_a"
|
||||||
"../rel_b" "$(cd ".." && pwd)/rel_b"
|
"../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
|
||||||
@@ -119,17 +122,41 @@ toassert_init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
toassert_clean() {
|
toassert_clean() {
|
||||||
# TODO clean
|
|
||||||
rm -rf "${_ta_tmp}"
|
rm -rf "${_ta_tmp}"
|
||||||
}
|
}
|
||||||
|
|
||||||
toassert_init
|
########## Common data and functions ##########
|
||||||
|
#
|
||||||
|
# [0] path to exclude [1] Reason for exclution
|
||||||
|
# [2] path to exclude [3] Reason for exclution
|
||||||
|
exclude_path=(
|
||||||
|
"/dev/fd" "Always different"
|
||||||
|
"/dev/stdout" "Always different in pipes"
|
||||||
|
"/etc/mtab" "Always different"
|
||||||
|
"/proc/mounts" "Always different"
|
||||||
|
)
|
||||||
|
|
||||||
|
toexclude() {
|
||||||
|
local path=
|
||||||
|
local exclude=
|
||||||
|
for path in "${exclude_path[@]}" ; do
|
||||||
|
if [[ ! "${exclude}" ]] ; then
|
||||||
|
exclude="${path}"
|
||||||
|
else
|
||||||
|
# return reason for exclution
|
||||||
|
if [[ "${exclude}" == "${1:-}" ]] ; then
|
||||||
|
printf "${path}"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
exclude=
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
########## Clean custom test data ##########
|
########## Clean custom test data ##########
|
||||||
#
|
#
|
||||||
totest_cleanall() {
|
totest_cleanall() {
|
||||||
echo
|
|
||||||
tocompete_clean
|
tocompete_clean
|
||||||
toassert_clean
|
toassert_clean
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user