forked from oils-for-unix/oils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdollar0-errexit.sh
executable file
·63 lines (55 loc) · 1016 Bytes
/
dollar0-errexit.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env bash
#
# Pattern for testing if a function has succeeded or failed with set -e:
#
# SUMMARY: Call it through $0.
#
# Useful for test frameworks!
#
# Usage:
# demo/dollar0-errexit.sh <function name>
set -o nounset
set -o pipefail
set -o errexit
# Should produce TWO lines, not THREE.
# And the whole thing FAILS with exit code 1.
myfunc() {
echo one
echo two
false # should FAIL here
echo three
}
mypipeline() { myfunc | wc -l; }
# Prints three lines because errexit is implicitly disabled.
# It also shouldn't succeed.
bad() {
if myfunc | wc -l; then
echo true
else
echo false
fi
echo status=$?
}
# This fixes the behavior. Prints two lines and fails.
good() {
if $0 mypipeline; then
echo true
else
echo false
fi
echo status=$?
}
# This is pretty much what 'bad' is dong.
not-a-fix() {
set +o errexit
mypipeline
local status=$?
set -o errexit
if test $? -eq 0; then
echo true
else
echo false
fi
echo status=$?
}
"$@"