annotate b.sh @ 3:0798045f37b5 default tip

Add blog post link to README.
author Jang Rush <weakish@gmail.com>
date Mon, 05 Sep 2022 10:50:14 +0000
parents 078a17d15fa5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
1 # eval, shift, unset are guaranteed to be in the POSIX shell
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
2 # No local variables are guaranteed, so we use __prefix for local variables
ed67ca9ff4c7 initial commit
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
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
4 # data.
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
5
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
6 #
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
7 # Create temporary directory to keep intermediate test data
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
8 #
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
9 T=$(mktemp -d)
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
10 [ -z $T ] && T=/tmp/bricolagetmp
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
11 export T
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
12 mkdir -p "$T"
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
13
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
14 #
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
15 # Test success/failure reporters, can be overridden
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
16 #
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
17 pass() {
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
18 echo "pass: $*"
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
19 }
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
20
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
21 fail() {
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
22 echo "fail: $*"
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
23 }
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
24
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
25 #
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
26 # Command wrapper. Defines function that behaves like a spied command
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
27 #
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
28 spy() {
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
29 eval "$(cat << EOF
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
30 $1() {
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
31 echo \$* >> "$T/spy.$1.args"
2
078a17d15fa5 Support calling a spied command with arguments.
Jakukyo Friel <weakish@gmail.com>
parents: 0
diff changeset
32 ([ -f "$T/spy.$1" ] && cat "$T/spy.$1" || echo "$1 \$@" | $SHELL -s) >> "$T/spy.$1.stdout"
0
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
33 echo \$? >> "$T/spy.$1.exit"
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
34 }
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
35 __spies="$1 \$__spies"
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
36 EOF
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
37 )"
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
38 rm -f "$T/spy.$1" "$T/spy.$1.stdout" "$T/spy.$1.args"
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
39 }
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
40
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
41 #
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
42 # Test runner
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
43 #
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
44 bricolage() {
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
45 eval "$(cat << EOF
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
46 __spies=""
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
47 ok() {
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
48 if test "\$@" ; then
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
49 pass "$1: \$*"
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
50 else
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
51 fail "$1: \$*"
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
52 fi
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
53 }
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
54 EOF
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
55 )"
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
56 $@
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
57 # eval is needed, because unset breaks in zsh due to a trailing space
2
078a17d15fa5 Support calling a spied command with arguments.
Jakukyo Friel <weakish@gmail.com>
parents: 0
diff changeset
58 [ ! -z "$__spies" ] && eval "unset -f $__spies"
0
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
59 unset -f ok
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
60 }
ed67ca9ff4c7 initial commit
Serge A. Zaitsev <zaitsev.serge@gmail.com>
parents:
diff changeset
61