comparison b.sh @ 0:ed67ca9ff4c7

initial commit
author Serge A. Zaitsev <zaitsev.serge@gmail.com>
date Thu, 18 Dec 2014 23:49:30 +0200
parents
children 078a17d15fa5
comparison
equal deleted inserted replaced
-1:000000000000 0:ed67ca9ff4c7
1 # eval, shift, unset are guaranteed to be in the POSIX shell
2 # No local variables are guaranteed, so we use __prefix for local variables
3 # We try to use mktemp, but we actually can use any directory to keep our tests
4 # data.
5
6 #
7 # Create temporary directory to keep intermediate test data
8 #
9 T=$(mktemp -d)
10 [ -z $T ] && T=/tmp/bricolagetmp
11 export T
12 mkdir -p "$T"
13
14 #
15 # Test success/failure reporters, can be overridden
16 #
17 pass() {
18 echo "pass: $*"
19 }
20
21 fail() {
22 echo "fail: $*"
23 }
24
25 #
26 # Command wrapper. Defines function that behaves like a spied command
27 #
28 spy() {
29 eval "$(cat << EOF
30 $1() {
31 echo \$* >> "$T/spy.$1.args"
32 ([ -f "$T/spy.$1" ] && cat "$T/spy.$1" || $SHELL -c $1 \$@) >> "$T/spy.$1.stdout"
33 echo \$? >> "$T/spy.$1.exit"
34 }
35 __spies="$1 \$__spies"
36 EOF
37 )"
38 rm -f "$T/spy.$1" "$T/spy.$1.stdout" "$T/spy.$1.args"
39 }
40
41 #
42 # Test runner
43 #
44 bricolage() {
45 eval "$(cat << EOF
46 __spies=""
47 ok() {
48 if test "\$@" ; then
49 pass "$1: \$*"
50 else
51 fail "$1: \$*"
52 fi
53 }
54 EOF
55 )"
56 $@
57 # eval is needed, because unset breaks in zsh due to a trailing space
58 [ ! -z $__spies ] && eval "unset -f $__spies"
59 unset -f ok
60 }
61