aboutsummaryrefslogtreecommitdiffstats
path: root/test/lib.sh
diff options
context:
space:
mode:
authorWolfgang Müller2021-03-10 17:09:13 +0100
committerWolfgang Müller2021-03-10 17:09:13 +0100
commit3bce19d12d93f1b27e7d2f2acbef0926417d5390 (patch)
tree3700afd21af95a0904be6aaf3378fc75e3955b10 /test/lib.sh
parent1d767c0bdd6476a4ba9a998b8953ec8b9ad9f21c (diff)
downloadbosun-3bce19d12d93f1b27e7d2f2acbef0926417d5390.tar.gz
Add a first draft of the test suite1.3.0
The introduction of a test suite makes future changes easier to troubleshoot and verify. Whilst this is only a first draft, it should be stable enough to be used in normal development and should cover the entirety of bosun's functionality.
Diffstat (limited to 'test/lib.sh')
-rw-r--r--test/lib.sh84
1 files changed, 84 insertions, 0 deletions
diff --git a/test/lib.sh b/test/lib.sh
new file mode 100644
index 0000000..733613c
--- /dev/null
+++ b/test/lib.sh
@@ -0,0 +1,84 @@
+#!/bin/sh
+
+TESTS_FAILED=0
+
+ok() {
+ if ! test -t 1; then
+ printf 'ok - %s\n' "$*"
+ else
+ printf '\e[32mok\e[0m - %s\n' "$*"
+ fi
+}
+
+nok() {
+ TESTS_FAILED=$((TESTS_FAILED + 1))
+ if ! test -t 1; then
+ printf 'not ok - %s\n' "$*" >&2
+ else
+ printf '\e[31mnot ok\e[0m - %s\n' "$*" >&2
+ fi
+}
+
+header() {
+ printf '# %s\n' "$*"
+}
+
+info() {
+ printf '\n'
+ eval "$@"
+ printf '\n'
+}
+
+want() {
+ cat > want
+}
+
+assert_output_matches() {
+ msg=$1
+ if cmp have want >/dev/null 2>&1; then
+ ok "$msg"
+ return 0
+ else
+ nok "$msg"
+ info diff -u have want >&2
+ return 1
+ fi
+}
+
+assert() {
+ if test "$1" = "success"; then
+ assert_return=0
+ elif test "$1" = "failure"; then
+ assert_return=1
+ else
+ assert_return=$1
+ fi
+ shift
+
+ msg=$1
+ shift
+
+ eval "$@" > have 2>&1
+
+ if test "$?" -eq "$assert_return"; then
+ ok "$msg"
+ return 0
+ else
+ nok "$msg"
+ info cat have >&2
+ return 1
+ fi
+}
+
+end() {
+ test $TESTS_FAILED -gt 0 && exit $TESTS_FAILED
+ exit 0
+}
+
+have_dir() {
+ (cd "$BOSUN_DIR/.." && find . -printf '%y %p\n' | sed '/^..\.\/stow/d' | sort) > have
+}
+
+have_links() {
+ (cd "$BOSUN_DIR/.." && find . -type l -printf '%p -> %l\n' | sort) > have
+}