]> git.corax.cc Git - toolbox/commitdiff
test: Use shellspec as the default test framework
authorMatthias Kruk <m@m10k.eu>
Sun, 4 Jul 2021 23:48:59 +0000 (08:48 +0900)
committerMatthias Kruk <m@m10k.eu>
Sun, 4 Jul 2021 23:48:59 +0000 (08:48 +0900)
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

diff --git a/test.sh b/test.sh
index ad0cf814bf837261acfc442ce09cf14bad912d71..5c2f4c11c3082aff9145c618d0320775ca53c5cb 100755 (executable)
--- 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 "$?"
 }