summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Pursehouse <david.pursehouse@sonymobile.com>2012-07-24 15:31:14 +0900
committerDavid Pursehouse <david.pursehouse@sonymobile.com>2012-07-24 16:55:55 +0900
commit05307c038028cad71059f48b2b852d663cbd03ed (patch)
treeb500a626943a0a21c9b4977e1a4bff0823ea2b50
parentf48eed76cc12f9e802afe5a150d4b8f7f9a13ec6 (diff)
downloadpygerrit-05307c038028cad71059f48b2b852d663cbd03ed.tar.gz
Add Makefile and script for unit tests
Change-Id: Ib12d9c22c507dff58fabfc6cf80092bc3fd60e3d
-rw-r--r--tests/Makefile47
-rwxr-xr-xtests/test_category.sh73
2 files changed, 120 insertions, 0 deletions
diff --git a/tests/Makefile b/tests/Makefile
new file mode 100644
index 0000000..d1a1f40
--- /dev/null
+++ b/tests/Makefile
@@ -0,0 +1,47 @@
+#
+# This makefile executes all tests found in the current directory. The
+# tests are divided in categories, typically connected to a particular
+# script, and the actual testcases are defined in the directory
+# <category>/<testcase>.
+#
+# Each testcase directory can contain the following special files:
+#
+# command A shell script (doesn't have to be executable) that
+# will be invoked by sh to run the testcase.
+# input Will be fed to the stdin stream of the command script.
+# output The expected stdout output of the command script.
+# If present, the actual output will be compared to the
+# contents of this file.
+# status The expected exit status of the command script,
+# expressed as an integer.
+#
+# In addition to this, users are encouraged to include a file README that
+# explains the purpose of the testcase in question.
+#
+# Command scripts will be run with the parent directory of the
+# makefile as current directory (should be the top directory of the
+# git). To access files stored in the testcase directory the $TESTDIR
+# variable is available.
+#
+# In addition to the `all' default goal, each test category gets its
+# own make goal, i.e. if you have a category (and subdirectory) named
+# `mytests', you can run just these tests with `make mytests'.
+#
+
+# Only testcases with a `command' file are considered.
+ALL_CATEGORIES := \
+ $(sort $(foreach t,$(wildcard */*/command),$(firstword $(subst /, ,$(t)))))
+
+.PHONY: all
+all: $(ALL_CATEGORIES)
+ @echo All tests successful
+
+define create-category
+$(eval .PHONY: $(1)
+$(1):
+ @./test_category.sh $(1) ..
+)
+endef
+
+$(foreach category,$(ALL_CATEGORIES),\
+ $(call create-category,$(category)))
diff --git a/tests/test_category.sh b/tests/test_category.sh
new file mode 100755
index 0000000..8f8b982
--- /dev/null
+++ b/tests/test_category.sh
@@ -0,0 +1,73 @@
+#!/bin/sh
+
+ERROUTPUT=`mktemp`
+TESTOUTPUT=`mktemp`
+
+cleanup()
+{
+ rm -f $ERROUTPUT 2> /dev/null
+ rm -f $TESTOUTPUT 2> /dev/null
+ return 0
+}
+
+fatal()
+{
+ echo '***' "$@" >&2
+ cleanup
+ exit 1
+}
+
+CATEGORY=$1
+ROOTDIR=$2
+TESTS=`ls $CATEGORY/*/command 2> /dev/null | sed 's|/command||; s|.*/||' | sort`
+
+if [ -z "$CATEGORY" ] ; then
+ fatal No test directory specified.
+fi
+if [ -z "$ROOTDIR" -o ! -d "$ROOTDIR" ] ; then
+ fatal No valid test root directory specified.
+fi
+
+echo Running tests for $CATEGORY
+for TEST in $TESTS ; do
+ echo " $TEST"
+ export TESTDIR=`pwd`/$CATEGORY/$TEST
+
+ if [ -f $TESTDIR/input -a -f $TESTDIR/output ] ; then
+ ( cd $ROOTDIR && sh $TESTDIR/command ) \
+ < $TESTDIR/input 2> $ERROUTPUT > $TESTOUTPUT
+ EXITSTATUS=$?
+ elif [ -f $TESTDIR/input ] ; then
+ ( cd $ROOTDIR && sh $TESTDIR/command ) \
+ < $TESTDIR/input 2> $ERROUTPUT > /dev/null
+ EXITSTATUS=$?
+ elif [ -f $TESTDIR/output ] ; then
+ ( cd $ROOTDIR && sh $TESTDIR/command ) 2> $ERROUTPUT > $TESTOUTPUT
+ EXITSTATUS=$?
+ elif [ -f $TESTDIR/status ] ; then
+ ( cd $ROOTDIR && sh $TESTDIR/command ) 2> $ERROUTPUT > /dev/null
+ EXITSTATUS=$?
+ fi
+
+ if [ ! -f $TESTDIR/status -a ! -f $TESTDIR/output ] ; then
+ fatal "Bad test case; no expected output or exit status provided."
+ fi
+
+ if [ -f $TESTDIR/status ] ; then
+ EXPECTED_STATUS=`cat $TESTDIR/status`
+ if [ "$EXITSTATUS" != "$EXPECTED_STATUS" ] ; then
+ cat $ERROUTPUT >&2
+ fatal "Bad exit status; expected $EXPECTED_STATUS, got $EXITSTATUS."
+ fi
+ fi
+
+ if [ -f $TESTDIR/output ] ; then
+ if ! cmp -s $TESTDIR/output $TESTOUTPUT ; then
+ diff -u $TESTDIR/output $TESTOUTPUT | \
+ sed -e '1s/--- [^ ][^ ]*/--- expected-output/' \
+ -e '2s/+++ [^ ][^ ]*/+++ actual-output/'
+ fatal "Unexpected command output (see diff above)."
+ fi
+ fi
+done
+cleanup