We add some shell helper functions to test the expected output of sbase
tools.

In addition to the helper functions themselves we add some tests for
'dirname'.
---
Changes compared to v1:

* use "ls" instead of "find" in subshell
* return number of failed tests in "runalltests" script
* use lowercase variable names more consistently
* print error message on cmp error condition
* make testing Make target depend on all


 Makefile             |  6 +++++-
 tests/dirname.test   | 20 ++++++++++++++++++++
 tests/runalltests    | 12 ++++++++++++
 tests/test-common.sh | 42 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 79 insertions(+), 1 deletion(-)
 create mode 100755 tests/dirname.test
 create mode 100755 tests/runalltests
 create mode 100644 tests/test-common.sh

diff --git a/Makefile b/Makefile
index 0e421e7..2395b50 100644
--- a/Makefile
+++ b/Makefile
@@ -274,4 +274,8 @@ clean:
        rm -f $(BIN) $(OBJ) $(LIB) sbase-box sbase-$(VERSION).tar.gz
        rm -f getconf.h
 
-.PHONY: all install uninstall dist sbase-box sbase-box-install 
sbase-box-uninstall clean
+testing: all
+       @cd tests/; \
+       ./runalltests
+
+.PHONY: all install uninstall dist sbase-box sbase-box-install 
sbase-box-uninstall clean testing
diff --git a/tests/dirname.test b/tests/dirname.test
new file mode 100755
index 0000000..3bc45ca
--- /dev/null
+++ b/tests/dirname.test
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+. ./test-common.sh
+
+#            "test name"       "command to execute" "expected output"
+check_stdout "dirname-noarg"   "../dirname"         "" && \
+check_stderr "dirname-noarg-stderr" "../dirname" "usage: ../dirname path\n" && 
\
+check_stdout "dirname-non-existing" "../dirname a b c" "" && \
+check_stdout "dirname-slash" "../dirname /" "/\n" && \
+check_stdout "dirname-dashes-slash" "../dirname -- /" "/\n" && \
+check_stdout "dirname-dashes-slash-a" "../dirname -- /a" "/\n"  && \
+check_stdout "dirname-doublequotes" "../dirname \"\"" ".\n" && \
+check_stdout "dirname-slashes" "../dirname ///" "/\n" && \
+check_stdout "dirname-a/b" "../dirname a/b" "a\n" && \
+check_stdout "dirname-a/b/" "../dirname a/b/" "a\n" && \
+check_stdout "dirname-a/b//" "../dirname a/b//" "a\n" && \
+check_stdout "dirname-a" "../dirname a" ".\n" && \
+check_stdout "dirname-a/" "../dirname a/" ".\n" && \
+check_stdout "dirname-/a/b/c" "../dirname /a/b/c" "/a/b\n" && \
+check_stdout "dirname-//a/b/c" "../dirname //a/b/c" "//a/b\n"
diff --git a/tests/runalltests b/tests/runalltests
new file mode 100755
index 0000000..b6d5b5d
--- /dev/null
+++ b/tests/runalltests
@@ -0,0 +1,12 @@
+#!/bin/sh
+
+for testfile in $(ls *.test); do
+       ./$testfile
+       ret=$(expr $ret + $?)
+done
+
+if [ $ret -gt 0 ]; then
+       printf "%d tests failed\n" $ret
+fi
+
+exit $ret
diff --git a/tests/test-common.sh b/tests/test-common.sh
new file mode 100644
index 0000000..b1d7572
--- /dev/null
+++ b/tests/test-common.sh
@@ -0,0 +1,42 @@
+check_output() {
+       testname=$1
+       cmdtorun=$2
+       expectedoutput=$3
+       usestdout=$4
+       expoutfile=$(mktemp)
+       actualoutfile=$(mktemp)
+       ret=0
+
+       printf "$expectedoutput" > $expoutfile
+       if [ $usestdout -eq 1 ]; then
+               eval $cmdtorun > $actualoutfile 2> /dev/null
+       else
+               eval $cmdtorun 2> $actualoutfile 1> /dev/null
+       fi
+
+       cmp $expoutfile $actualoutfile  2>&1 > /dev/null
+       if [ $? -eq 1 ]; then
+               printf "$testname:\n"
+               printf "\tWanted:\n"
+               cat $expoutfile
+               printf "\n\tGot:\n"
+               cat $actualoutfile
+               printf "\n\n"
+               ret=1
+       fi
+       if [ $? -eq 2 ]; then
+               printf "cmp error\n"
+               ret=1
+       fi
+
+       rm $expoutfile $actualoutfile
+       return $ret
+}
+
+check_stdout() {
+       check_output "$1" "$2" "$3" 1
+}
+
+check_stderr() {
+       check_output "$1" "$2" "$3" 0
+}
-- 
2.19.0


Reply via email to