1 | #!/usr/bin/env bash
|
2 | #
|
3 | # Testing library for bash and OSH.
|
4 | #
|
5 | # Capture status/stdout/stderr, and nq-assert those values.
|
6 |
|
7 | : ${LIB_OSH=stdlib/osh}
|
8 | source $LIB_OSH/two.sh
|
9 |
|
10 | nq-assert() {
|
11 | ### Assertion with same syntax as shell 'test'
|
12 |
|
13 | if ! test "$@"; then
|
14 | die "line ${BASH_LINENO[0]}: nq-assert $(printf '%q ' "$@") failed"
|
15 | fi
|
16 | }
|
17 |
|
18 | # Problem: we want to capture status and stdout at the same time
|
19 | #
|
20 | # We use:
|
21 | #
|
22 | # __stdout=$(set -o errexit; "$@")
|
23 | # __status=$?
|
24 | #
|
25 | # However, we lose the trailing \n, since that's how command subs work.
|
26 |
|
27 | # Here is another possibility:
|
28 | #
|
29 | # shopt -s lastpipe # need this too
|
30 | # ( set -o errexit; "$@" ) | read -r -d __stdout
|
31 | # __status=${PIPESTATUS[0]}
|
32 | # shopt -u lastpipe
|
33 | #
|
34 | # But this feels complex for just the \n issue, which can be easily worked
|
35 | # around.
|
36 |
|
37 | nq-run() {
|
38 | ### capture status only
|
39 |
|
40 | local -n out_status=$1
|
41 | shift
|
42 |
|
43 | local __status
|
44 |
|
45 | # Tricky: turn errexit off so we can capture it, but turn it on against
|
46 | set +o errexit
|
47 | ( set -o errexit; "$@" )
|
48 | __status=$?
|
49 | set -o errexit
|
50 |
|
51 | out_status=$__status
|
52 | }
|
53 |
|
54 | nq-capture() {
|
55 | ### capture status and stdout
|
56 |
|
57 | local -n out_status=$1
|
58 | local -n out_stdout=$2
|
59 | shift 2
|
60 |
|
61 | local __status
|
62 | local __stdout
|
63 |
|
64 | # Tricky: turn errexit off so we can capture it, but turn it on against
|
65 | set +o errexit
|
66 | __stdout=$(set -o errexit; "$@")
|
67 | __status=$?
|
68 | set -o errexit
|
69 |
|
70 | out_status=$__status
|
71 | out_stdout=$__stdout
|
72 | }
|
73 |
|
74 | nq-capture-2() {
|
75 | ### capture status and stderr
|
76 |
|
77 | # This is almost identical to the above
|
78 |
|
79 | local -n out_status=$1
|
80 | local -n out_stderr=$2
|
81 | shift 2
|
82 |
|
83 | local __status
|
84 | local __stderr
|
85 |
|
86 | # Tricky: turn errexit off so we can capture it, but turn it on against
|
87 | set +o errexit
|
88 | __stderr=$(set -o errexit; "$@" 2>&1)
|
89 | __status=$?
|
90 | set -o errexit
|
91 |
|
92 | out_status=$__status
|
93 | out_stderr=$__stderr
|
94 | }
|
95 |
|
96 | _demo-stderr() {
|
97 | echo zzz "$@" >& 2
|
98 | return 99
|
99 | }
|
100 |
|
101 | test-nq-capture() {
|
102 | local status stdout
|
103 | nq-capture status stdout \
|
104 | echo -n hi
|
105 | nq-assert 0 = "$status"
|
106 | nq-assert 'hi' = "$stdout"
|
107 |
|
108 | nq-capture status stdout \
|
109 | echo hi
|
110 | nq-assert 0 = "$status"
|
111 | # Note that we LOSE the last newline!
|
112 | #nq-assert $'hi\n' = "$stdout"
|
113 |
|
114 | local stderr
|
115 | nq-capture-2 status stderr \
|
116 | _demo-stderr yyy
|
117 |
|
118 | #echo "stderr: [$stderr]"
|
119 |
|
120 | nq-assert 99 = "$status"
|
121 | nq-assert 'zzz yyy' = "$stderr"
|
122 |
|
123 | nq-capture status stdout \
|
124 | _demo-stderr aaa
|
125 |
|
126 | #echo "stderr: [$stderr]"
|
127 |
|
128 | nq-assert 99 = "$status"
|
129 | nq-assert '' = "$stdout"
|
130 | }
|
131 |
|
132 | name=$(basename $0)
|
133 | if test "$name" = 'no-quotes.sh'; then
|
134 | "$@"
|
135 | fi
|