From 487c9a185bd3c7f91f544707e73d4029db4b4a70 Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Mon, 5 Jul 2021 08:48:59 +0900 Subject: [PATCH] test: Use shellspec as the default test framework There are issues with bats when accessing device files such as /dev/urandom, making it hard to implement tests that require randomness. This commit modifies test.sh to use shellspec as the default test framework. --- test.sh | 53 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/test.sh b/test.sh index ad0cf81..5c2f4c1 100755 --- a/test.sh +++ b/test.sh @@ -1,5 +1,37 @@ #!/bin/bash +get_module_names() { + local path="$1" + + local candidate + + while read -r candidate; do + local filename + + # No constructor -> no module + if ! grep "^__init\(\)" < "$candidate" &>/dev/null; then + continue + fi + + filename="${candidate#$path/}" + echo "${filename%.sh}" + done < <(find "$path" -type f -iname "*.sh") + + return 0 +} + +get_test_names() { + local path="$1" + + local candidate + + while read -r candidate; do + echo "${candidate#$path/}" + done < <(find "$path" -type f -iname "*_spec.sh") + + return 0 +} + main() { local tests local missing @@ -14,15 +46,14 @@ main() { modules=() failed=() - while read -r module; do - test="test/${module#*include/}" + readarray -t modules < <(get_module_names "include") + readarray -t tests < <(get_test_names "test") - if ! [ -f "$test" ]; then + for module in "${modules[@]}"; do + if ! array_contains "${module}_spec.sh" "${tests[@]}"; then missing+=("$module") - else - tests+=("$test") fi - done < <(find "include" -type f -iname "*.sh") + done if (( ${#missing[@]} > 0 )); then echo "There are no tests for these modules:" @@ -35,7 +66,7 @@ main() { fi for test in "${tests[@]}"; do - if ! "$test"; then + if ! shellspec --shell bash --format d "test/$test"; then failed+=("$test") fi done @@ -55,6 +86,14 @@ main() { } { + if ! . toolbox.sh; then + exit 1 + fi + + if ! include "array"; then + exit 1 + fi + main "$@" exit "$?" } -- 2.47.3