OILS / test / gold / errexit.sh View on Github | oilshell.org

121 lines, 85 significant
1#!/usr/bin/env bash
2#
3# Usage:
4# ./16-errexit.sh <function name>
5
6set -o nounset
7set -o pipefail
8set -o errexit
9
10fail() {
11 echo FAIL
12 false
13}
14
15ok() {
16 echo OK
17 true
18}
19
20_func() {
21 fail
22 ok
23}
24
25# Hm this is behavior is odd. Usually errexit stops at first failed command.
26# || suppresses this functions and blocks!
27#
28# I guess it is sort of like "if { grep foo; grep bar } ?
29
30test-func-or() {
31 _func || echo "Test function FAILED"
32
33 echo DONE # We get here
34}
35
36test-brace-or() {
37 { fail
38 ok
39 } || {
40 echo "Test block FAILED"
41 }
42
43 echo DONE
44}
45
46test-func-if() {
47 if _func; then
48 echo THEN # shouldn't succeed!
49 else
50 echo ELSE
51 fi
52
53 echo DONE
54}
55
56test-brace-if() {
57 if { fail; ok; }; then
58 echo THEN # shouldn't succeed!
59 else
60 echo ELSE
61 fi
62
63 echo DONE
64}
65
66# This behaves as expected
67test-func-pipe() {
68 _func | tee /dev/null
69 echo PIPE
70}
71
72test-brace-pipe() {
73 { fail; ok; } | tee /dev/null
74 echo PIPE
75}
76
77# We get ELSE
78test-fail-if() {
79 if fail; then
80 echo THEN
81 else
82 echo ELSE
83 fi
84
85 echo DONE
86}
87
88# We get ELSE
89test-fail-or() {
90 fail || echo FAILED
91
92 echo DONE
93}
94
95succeed() {
96 return 0
97}
98
99fail() {
100 return 1
101}
102
103test-func-with-and() {
104 succeed && echo "OK 1"
105 fail && echo "OK 2" # Swallos the error because of errexit, not good!
106 succeed && echo "OK 3"
107}
108
109all() {
110 compgen -A function | egrep '^test-' | while read func; do
111 echo
112 echo "--- $func ---"
113 echo
114 set +o errexit
115 $0 $func
116 echo status=$?
117 set -o errexit
118 done
119}
120
121"$@"