From 1802f18339395d74d0317fed53dff4f3d632a0a9 Mon Sep 17 00:00:00 2001 From: Martin Winkler Date: Fri, 18 Mar 2022 21:48:39 +0100 Subject: [PATCH] Outsourced test definition to separate file Add cmd options --, --run-all --- test/test_rdlink.sh | 94 +++++++++++++++++++-------------------------- test/totest.sh | 70 +++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 55 deletions(-) create mode 100644 test/totest.sh diff --git a/test/test_rdlink.sh b/test/test_rdlink.sh index 226b515..1fb748d 100755 --- a/test/test_rdlink.sh +++ b/test/test_rdlink.sh @@ -5,62 +5,24 @@ # * `tocompete` : competition between readlink and rdlink # * `toassert` : assert expected path and input path -# test_rdlink.sh [PATH TO COMPETE] -# +# 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 -test_dir="/opt/rdlink/test" -tool_rdlink="/opt/rdlink/rdlink.sh" -tool_readlink="$(command -v readlink) -f" +readonly test_dir="/opt/rdlink/test" +readonly tool_rdlink="/opt/rdlink/rdlink.sh" +readonly tool_readlink="$(command -v readlink) -f" +flag_runall=0 -########## 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" - "$test_dir/tmp/a"* - "$test_dir/tmp/a 3" - "/bin/adb" -) +toassert=() +tocompete=() -compete_all=( "Test everything starting from /" - /**/* -) - -compete_args=( "Tests from command line" - "$@" -) - -# Lists for competition test between rdlink and readlink -f -tocompete=( - compete_links -) - -########## 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=() +. totest.sh # rl::testcmp +# Compare results and print summary rl::testcmp() { local testnum= local expect= @@ -79,6 +41,7 @@ rl::testcmp() { # Print debug output of tool_a printf "\n Debug:\n" ( "${tool_rdlink}" -d "${input}" ) + return 1 fi return 0 @@ -90,6 +53,24 @@ rl::test() { 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 @@ -99,15 +80,17 @@ rl::test() { 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 + # Run all tests if option -a is pressend + (( ! $flag_runall )) && testend=1 && break fi firstelement= @@ -126,12 +109,13 @@ rl::test() { 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 - testend=1 - break + # Run all tests if option -a is pressend + (( ! $flag_runall )) && testend=1 && break fi fi @@ -141,4 +125,4 @@ rl::test() { done } -time rl::test +time rl::test "$@" diff --git a/test/totest.sh b/test/totest.sh new file mode 100644 index 0000000..0bef7b3 --- /dev/null +++ b/test/totest.sh @@ -0,0 +1,70 @@ +#!/usr/bin/env bash + +########## 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=( "Test - Valid links" + "/dev/stdin" + "$test_dir/tmp/a"* + "$test_dir/tmp/a 3" + "/bin/adb" +) + +compete_all=( "Test - everything starting from /" + /**/* +) + +tocompete_init() { + # TODO initialize custom test structure + + # Add tests to global test array from test_rdlink + tocompete+=( + compete_links + ) +} + +tocompete_clean() { + # TODO clean custom test structure + echo "tocompete_clean" +} + +tocompete_init + +########## 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" +) + +toassert_init() { + # TODO initilaize custom test structure + + # Add test arrays to global test array from test_rdlink + toassert+=( + assert_string + ) +} + +toassert_clean() { + # TODO clean + echo "toassert_clean" +} + +totest_cleanall() { + echo + tocompete_clean + toassert_clean +} +trap totest_cleanall EXIT + +toassert_init