Skip to content

Commit ceffed0

Browse files
author
Andy Chu
committed
[stdlib/osh] Add nq-run for getting status only
This is necessary so set -o errexit doesn't abort the shell! Use it in benchmarks/time-test.sh
1 parent 0f30fbd commit ceffed0

File tree

2 files changed

+80
-55
lines changed

2 files changed

+80
-55
lines changed

benchmarks/time-test.sh

Lines changed: 63 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -111,53 +111,55 @@ test-append() {
111111
}
112112

113113
test-usage() {
114-
# no args
115-
set +o errexit
116-
117-
time-tool; status=$?
114+
local status
115+
nq-run status \
116+
time-tool
118117
nq-assert $status -eq 2
119118

120-
time-tool --output; status=$?
119+
nq-run status \
120+
time-tool --output
121121
nq-assert $status -eq 2
122122

123-
time-tool sleep 0.1
124-
time-tool --append sleep 0.1; status=$?
123+
nq-run status \
124+
time-tool sleep 0.1
125125
nq-assert $status -eq 0
126126

127-
set -o errexit
127+
nq-run status \
128+
time-tool --append sleep 0.1
129+
nq-assert $status -eq 0
128130
}
129131

130132
test-bad-tsv-chars() {
133+
local status
131134
local out=_tmp/time2.tsv
132135
rm -f $out
133136

134-
set +o errexit
135-
136137
# Newline should fail
137-
time-tool --tsv -o $out --field $'\n' -- sleep 0.001; status=$?
138-
nq-assert $status -eq 1
138+
nq-run status \
139+
time-tool --tsv -o $out --field $'\n' -- sleep 0.001
140+
nq-assert $status = 1
139141

140142
# Tab should fail
141-
time-tool --tsv -o $out --field $'\t' -- sleep 0.001; status=$?
142-
nq-assert $status -eq 1
143+
nq-run status \
144+
time-tool --tsv -o $out --field $'\t' -- sleep 0.001
145+
nq-assert $status = 1
143146

144147
# Quote should fail
145-
time-tool --tsv -o $out --field '"' -- sleep 0.001; status=$?
146-
nq-assert $status -eq 1
148+
nq-run status \
149+
time-tool --tsv -o $out --field '"' -- sleep 0.001
150+
nq-assert $status = 1
147151

148152
# Backslash is OK
149-
time-tool --tsv -o $out --field '\' -- sleep 0.001; status=$?
150-
nq-assert $status -eq 0
153+
nq-run status \
154+
time-tool --tsv -o $out --field '\' -- sleep 0.001
155+
nq-assert $status = 0
151156

152157
# Space is OK, although canonical form would be " "
153-
time-tool --tsv -o $out --field ' ' -- sleep 0.001; status=$?
154-
nq-assert $status -eq 0
155-
156-
set -o errexit
158+
nq-run status \
159+
time-tool --tsv -o $out --field ' ' -- sleep 0.001
160+
nq-assert $status = 0
157161

158162
cat $out
159-
160-
echo $'OK\ttest-bad-tsv-chars'
161163
}
162164

163165
test-stdout() {
@@ -218,34 +220,38 @@ test-maxrss() {
218220
}
219221

220222
test-print-header() {
221-
set +o errexit
223+
local status
222224

223225
# no arguments allowed
224-
time-tool --tsv --print-header foo bar
225-
nq-assert $? -eq 2
226+
nq-run status \
227+
time-tool --tsv --print-header foo bar
228+
nq-assert $status = 2
226229

227-
time-tool --tsv --print-header --field name
228-
nq-assert $? -eq 0
230+
nq-run status \
231+
time-tool --tsv --print-header --field name
232+
nq-assert $status = 0
229233

230-
time-tool --tsv --print-header --rusage --field name
231-
nq-assert $? -eq 0
234+
nq-run status \
235+
time-tool --tsv --print-header --rusage --field name
236+
nq-assert $status = 0
232237

233-
time-tool --print-header --rusage --field foo --field bar
234-
nq-assert $? -eq 0
238+
nq-run status \
239+
time-tool --print-header --rusage --field foo --field bar
240+
nq-assert $status = 0
235241

236-
time-tool -o _tmp/time-test-1 \
242+
nq-run status \
243+
time-tool -o _tmp/time-test-1 \
237244
--print-header --rusage --stdout DUMMY --tsv --field a --field b
238-
nq-assert $? -eq 0
245+
nq-assert $status = 0
239246

240-
#set -x
241247
head _tmp/time-test-1
248+
249+
echo OK
242250
}
243251

244252
test-time-helper() {
245-
set +o errexit
246-
253+
local status
247254
local tmp=_tmp/time-helper.txt
248-
249255
local th=_devbuild/bin/time-helper
250256

251257
# Make some work show up
@@ -254,26 +260,32 @@ test-time-helper() {
254260
echo 'will be overwritten' > $tmp
255261
cat $tmp
256262

257-
$th
258-
nq-assert $? -ne 0 # it's 1, but could be 2
263+
nq-run status \
264+
$th
265+
nq-assert $status != 0 # it's 1, but could be 2
259266

260-
$th /bad
261-
nq-assert $? -eq 1
267+
nq-run status \
268+
$th /bad
269+
nq-assert $status = 1
262270

263-
$th -o $tmp -d $'\t' -x -e -- sh -c "$cmd"
264-
nq-assert $? -eq 42
271+
nq-run status \
272+
$th -o $tmp -d $'\t' -x -e -- sh -c "$cmd"
273+
nq-assert $status = 42
265274
cat $tmp
266275
echo
267276

268277
# Now append
269-
$th -o $tmp -a -d , -x -e -U -S -M -- sh -c "$cmd"
270-
nq-assert $? -eq 42
278+
279+
nq-run status \
280+
$th -o $tmp -a -d , -x -e -U -S -M -- sh -c "$cmd"
281+
nq-assert $status = 42
271282
cat $tmp
272283
echo
273284

274285
# Error case
275-
$th -q
276-
nq-assert $? -eq 2
286+
nq-run status \
287+
$th -q
288+
nq-assert $status -eq 2
277289
}
278290

279291
test-time-tsv() {
@@ -283,12 +295,8 @@ test-time-tsv() {
283295
rm -f -v $out
284296

285297
# Similar to what soil/worker.sh does
286-
set +o errexit
287-
time-tsv -o $out --append -- zz
288-
status=$?
289-
set -o errexit
290-
291-
echo status=$status
298+
nq-run status \
299+
time-tsv -o $out --append -- zz
292300
nq-assert $status -eq 1
293301

294302
cat $out

stdlib/osh/no-quotes.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,23 @@ nq-assert() {
3434
# But this feels complex for just the \n issue, which can be easily worked
3535
# around.
3636

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+
3754
nq-capture() {
3855
### capture status and stdout
3956

0 commit comments

Comments
 (0)