From 1bf087faa37ffcf458da9a3f29d4ea7e9eff5820 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Sat, 24 May 2014 11:01:23 +0100 Subject: [PATCH] Basic setup for tests Based on the test setup of stub.sh: https://github.com/jimeh/stub.sh --- .gitignore | 2 ++ .travis.yml | 4 ++++ Makefile | 37 +++++++++++++++++++++++++++++++++++++ test.sh | 34 ++++++++++++++++++++++++++++++++++ test/test-helper.sh | 8 ++++++++ 5 files changed, 85 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 Makefile create mode 100755 test.sh create mode 100644 test/test-helper.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..71ab7b8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +test/assert.sh +test/stub.sh \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..5b02332 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,4 @@ +language: bash +script: make test +before_install: + - sudo apt-get install bc \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..90104af --- /dev/null +++ b/Makefile @@ -0,0 +1,37 @@ +test: prepare + ./test.sh + +prepare: test/assert.sh test/stub.sh + +test/assert.sh: + test -f "test/assert.sh" || ( \ + echo "fetching test/assert.sh..." && \ + curl -s -L -o test/assert.sh \ + https://raw.github.com/lehmannro/assert.sh/v1.0.2/assert.sh \ + ) + +update-assert.sh: remove-assert.sh test/assert.sh + +remove-assert.sh: + test -f "test/assert.sh" && \ + rm "test/assert.sh" && \ + echo "removed test/assert.sh" + +test/stub.sh: + test -f "test/stub.sh" || ( \ + echo "fetching test/stub.sh..." && \ + curl -s -L -o test/stub.sh \ + https://raw.github.com/jimeh/stub.sh/v0.3.0/stub.sh \ + ) + +update-stub.sh: remove-stub.sh test/stub.sh + +remove-stub.sh: + test -f "test/stub.sh" && \ + rm "test/stub.sh" && \ + echo "removed test/stub.sh" + +.SILENT: +.PHONY: test prepare \ + test/assert.sh update-assert.sh remove-assert.sh \ + test/stub.sh update-stub.sh remove-stub.sh diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..656e3ac --- /dev/null +++ b/test.sh @@ -0,0 +1,34 @@ +#! /usr/bin/env bash + +resolve_link() { + $(type -p greadlink readlink | head -1) $1 +} + +abs_dirname() { + local cwd="$(pwd)" + local path="$1" + + while [ -n "$path" ]; do + cd "${path%/*}" + local name="${path##*/}" + path="$(resolve_link "$name" || true)" + done + + pwd + cd "$cwd" +} + + +testdir="$(abs_dirname "$0")/test" +testfiles="$(find "$testdir" -name "*-test.sh")" + +RET=0 +for testfile in $testfiles; do + echo "" + echo "running: ${testfile/#$(dirname "$testdir")\//}" + cd "$(dirname "$testfile")" + "$testfile" + if [ "$?" != "0" ]; then RET=1; fi +done +echo "" +exit $RET diff --git a/test/test-helper.sh b/test/test-helper.sh new file mode 100644 index 0000000..30fbcd9 --- /dev/null +++ b/test/test-helper.sh @@ -0,0 +1,8 @@ +[ -n "$TEST_DEBUG" ] && set -x + +# Set testroot variable. +testroot="$(dirname "$BASH_SOURCE")" + +# Include assert.sh and stub.sh libraries. +source "${testroot}/assert.sh" +source "${testroot}/stub.sh"