changeset 0:ed67ca9ff4c7

initial commit
author Serge A. Zaitsev <zaitsev.serge@gmail.com>
date Thu, 18 Dec 2014 23:49:30 +0200
parents
children bce8021ad094
files b.sh example.sh runall.sh
diffstat 3 files changed, 93 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/b.sh	Thu Dec 18 23:49:30 2014 +0200
@@ -0,0 +1,61 @@
+# eval, shift, unset are guaranteed to be in the POSIX shell
+# No local variables are guaranteed, so we use __prefix for local variables
+# We try to use mktemp, but we actually can use any directory to keep our tests
+# data.
+
+#
+# Create temporary directory to keep intermediate test data
+#
+T=$(mktemp -d)
+[ -z $T ] && T=/tmp/bricolagetmp
+export T
+mkdir -p "$T"
+
+#
+# Test success/failure reporters, can be overridden
+#
+pass() {
+	echo "pass: $*" 
+}
+
+fail() {
+	echo "fail: $*" 
+}
+
+#
+# Command wrapper. Defines function that behaves like a spied command
+#
+spy() {
+	eval "$(cat << EOF
+$1() {
+	echo \$* >> "$T/spy.$1.args"
+	([ -f "$T/spy.$1" ] && cat "$T/spy.$1" || $SHELL -c $1 \$@) >> "$T/spy.$1.stdout"
+	echo \$? >> "$T/spy.$1.exit"
+}
+__spies="$1 \$__spies"
+EOF
+)"
+	rm -f "$T/spy.$1" "$T/spy.$1.stdout" "$T/spy.$1.args"
+}
+
+#
+# Test runner
+#
+bricolage() {
+	eval "$(cat << EOF
+__spies=""
+ok() {
+	if test "\$@" ; then
+		pass "$1: \$*"
+	else
+		fail "$1: \$*"
+	fi
+}
+EOF
+)"
+	$@
+	# eval is needed, because unset breaks in zsh due to a trailing space
+	[ ! -z $__spies ] && eval "unset -f $__spies"
+	unset -f ok
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example.sh	Thu Dec 18 23:49:30 2014 +0200
@@ -0,0 +1,27 @@
+# Include bricolage library
+. ./b.sh
+
+# Simple test
+mytest() {
+	ok 1 -eq 1
+	ok foo = foo
+	foo="Foo bar"
+	ok "$foo" = "Foo bar"
+}
+
+myspytest() {
+	spy date
+	date
+	ok "$(cat $T/spy.date.stdout)" != "foo"
+	echo foo > $T/spy.date
+	date
+	ok "$(tail -n 1 $T/spy.date.stdout)" = "foo"
+}
+
+# Run tests
+bricolage mytest
+bricolage myspytest
+
+# Remove test data
+rm -r "$T"
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/runall.sh	Thu Dec 18 23:49:30 2014 +0200
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+zsh example.sh
+bash example.sh
+busybox sh example.sh