#!/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 [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" "$test_dir/tmp/a"* "$test_dir/tmp/a 3" "/bin/adb" ) 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=() # rl::testcmp 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 # 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 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 testend=1 break fi fi ((i++)) done (( testend )) && return 1 done } time rl::test