From 3bce19d12d93f1b27e7d2f2acbef0926417d5390 Mon Sep 17 00:00:00 2001 From: Wolfgang Müller Date: Wed, 10 Mar 2021 17:09:13 +0100 Subject: Add a first draft of the test suite 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. --- test/lib.sh | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 test/lib.sh (limited to 'test/lib.sh') 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 +} -- cgit v1.2.3-2-gb3c3