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

137 lines, 70 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
7: ${LIB_OSH=stdlib/osh}
8source $LIB_OSH/two.sh
9
10nq-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
37nq-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
54nq-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
74nq-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# 'byo test' can set this?
97: ${NQ_TEST_TEMP=/tmp}
98
99nq-redir() {
100 ### capture status and stdout
101
102 local -n out_status=$1
103 local -n out_stdout_file=$2
104 shift 2
105
106 local __status
107 local __stdout_file=$NQ_TEST_TEMP/nq-redir-$$.txt
108
109 # Tricky: turn errexit off so we can capture it, but turn it on against
110 set +o errexit
111 ( set -o errexit; "$@" ) > $__stdout_file
112 __status=$?
113 set -o errexit
114
115 out_status=$__status
116 out_stdout_file=$__stdout_file
117}
118
119nq-redir-2() {
120 ### capture status and stdout
121
122 local -n out_status=$1
123 local -n out_stderr_file=$2
124 shift 2
125
126 local __status
127 local __stderr_file=$NQ_TEST_TEMP/nq-redir-$$.txt
128
129 # Tricky: turn errexit off so we can capture it, but turn it on against
130 set +o errexit
131 ( set -o errexit; "$@" ) 2> $__stderr_file
132 __status=$?
133 set -o errexit
134
135 out_status=$__status
136 out_stderr_file=$__stderr_file
137}