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