]> git.corax.cc Git - toolbox/commitdiff
test: Add script and Makefile target for test-suite execution
authorMatthias Kruk <m@m10k.eu>
Sat, 24 Apr 2021 01:10:49 +0000 (10:10 +0900)
committerMatthias Kruk <m@m10k.eu>
Sat, 24 Apr 2021 01:10:49 +0000 (10:10 +0900)
This commit adds the "test" target to the Makefile and adds a script
that executes all test cases.

Makefile
test.sh [new file with mode: 0755]

index 1d779a7b09e85d2dc0cd90ff44cbb9430e28007f..4ff0c7ad4fc9d45527b916d492d74aca6a8b56f8 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-PHONY = install uninstall
+PHONY = install uninstall test
 
 ifeq ($(PREFIX), )
        PREFIX = /usr
@@ -8,6 +8,9 @@ all:
 
 clean:
 
+test:
+       ./test.sh
+
 install:
        mkdir -p $(DESTDIR)/$(PREFIX)/share/toolbox
        mkdir -p $(DESTDIR)/$(PREFIX)/bin
diff --git a/test.sh b/test.sh
new file mode 100755 (executable)
index 0000000..ad0cf81
--- /dev/null
+++ b/test.sh
@@ -0,0 +1,60 @@
+#!/bin/bash
+
+main() {
+       local tests
+       local missing
+       local modules
+       local failed
+       local module
+       local test
+       local err
+
+       tests=()
+       missing=()
+       modules=()
+       failed=()
+
+       while read -r module; do
+               test="test/${module#*include/}"
+
+               if ! [ -f "$test" ]; then
+                       missing+=("$module")
+               else
+                       tests+=("$test")
+               fi
+       done < <(find "include" -type f -iname "*.sh")
+
+       if (( ${#missing[@]} > 0 )); then
+               echo "There are no tests for these modules:"
+
+               for module in "${missing[@]}"; do
+                       echo "  $module"
+               done
+
+               return 1
+       fi
+
+       for test in "${tests[@]}"; do
+               if ! "$test"; then
+                       failed+=("$test")
+               fi
+       done
+
+       if (( ${#failed[@]} > 0 )); then
+               echo "The following unit tests failed:"
+
+               for test in "${failed[@]}"; do
+                       echo "  $test"
+               done
+
+               return 1
+       fi
+
+       echo "All unit tests passed"
+       return 0
+}
+
+{
+       main "$@"
+       exit "$?"
+}