OILS / stdlib / osh / no-quotes.sh View on Github | oilshell.org

93 lines, 55 significant
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
7source stdlib/osh/two.sh
8
9nq-assert() {
10 ### Must be run with errexit off
11
12 if ! test "$@"; then
13 die "line ${BASH_LINENO[0]}: nq-assert '$@' failed"
14 fi
15}
16
17nq-capture() {
18 ### capture status and stdout
19
20 local -n out_status=$1
21 local -n out_stdout=$2
22 shift 2
23
24 local __status
25 local __stdout
26
27 # Tricky: turn errexit off so we can capture it, but turn it on against
28 set +o errexit
29 __stdout=$(set -o errexit; "$@")
30 __status=$?
31 set -o errexit
32
33 out_status=$__status
34 out_stdout=$__stdout
35}
36
37nq-capture-2() {
38 ### capture status and stderr
39
40 # This is almost identical to the above
41
42 local -n out_status=$1
43 local -n out_stderr=$2
44 shift 2
45
46 local __status
47 local __stderr
48
49 # Tricky: turn errexit off so we can capture it, but turn it on against
50 set +o errexit
51 __stderr=$(set -o errexit; "$@" 2>&1)
52 __status=$?
53 set -o errexit
54
55 out_status=$__status
56 out_stderr=$__stderr
57}
58
59_demo-stderr() {
60 echo zzz "$@" >& 2
61 return 99
62}
63
64test-nq-capture() {
65 local status stdout
66 nq-capture status stdout \
67 echo hi
68
69 nq-assert 0 = "$status"
70 nq-assert 'hi' = "$stdout"
71
72 local stderr
73 nq-capture-2 status stderr \
74 _demo-stderr yyy
75
76 #echo "stderr: [$stderr]"
77
78 nq-assert 99 = "$status"
79 nq-assert 'zzz yyy' = "$stderr"
80
81 nq-capture status stdout \
82 _demo-stderr aaa
83
84 #echo "stderr: [$stderr]"
85
86 nq-assert 99 = "$status"
87 nq-assert '' = "$stdout"
88}
89
90name=$(basename $0)
91if test "$name" = 'testing.sh'; then
92 "$@"
93fi