OILS / spec / ysh-xtrace.test.sh View on Github | oilshell.org

528 lines, 330 significant
1# Oil xtrace
2
3#### Customize PS4
4shopt -s oil:upgrade
5set -x
6
7# Reuse the default
8PS4='$LINENO '"$PS4"
9echo 1; echo 2
10echo 3
11## STDOUT:
121
132
143
15## END
16## STDERR:
175 . builtin echo 1
185 . builtin echo 2
196 . builtin echo 3
20## END
21
22
23#### xtrace_details doesn't show [[ ]] etc.
24shopt -s oil:upgrade
25set -x
26
27dir=/
28if [[ -d $dir ]]; then
29 (( a = 42 ))
30fi
31cd /
32
33## stdout-json: ""
34## STDERR:
35. builtin cd /
36## END
37
38#### xtrace_details AND xtrace_rich on
39shopt -s oil:upgrade xtrace_details
40shopt --unset errexit
41set -x
42
43{
44 env false
45 set +x
46} 2>err.txt
47
48sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt >&2
49
50## STDOUT:
51## END
52## STDERR:
53| command 12345: env 'false'
54; process 12345: status 1
55. builtin set '+x'
56## END
57
58#### proc and shell function
59shopt --set oil:upgrade
60set -x
61
62shfunc() {
63 : $1
64}
65
66proc p {
67 : $1
68}
69
70shfunc 1
71p 2
72## stdout-json: ""
73## STDERR:
74> proc shfunc 1
75 . builtin ':' 1
76< proc shfunc
77> proc p 2
78 . builtin ':' 2
79< proc p
80## END
81
82#### eval
83shopt --set oil:upgrade
84set -x
85
86eval 'echo 1; echo 2'
87## STDOUT:
881
892
90## END
91## STDERR:
92> eval
93 . builtin echo 1
94 . builtin echo 2
95< eval
96## END
97
98#### source
99echo 'echo "\$1 = $1"' > lib.sh
100
101shopt --set oil:upgrade
102set -x
103
104source lib.sh a b c
105
106# Test the quoting style. TODO: Don't use bash style in YSH.
107
108source lib.sh x $'\xfe' $'\xff'
109
110## STDOUT:
111$1 = a
112$1 = x
113## END
114## STDERR:
115> source lib.sh a b c
116 . builtin echo '$1 = a'
117< source lib.sh
118> source lib.sh x $'\xfe' $'\xff'
119 . builtin echo '$1 = x'
120< source lib.sh
121## END
122
123#### external and builtin
124shopt --set oil:upgrade
125shopt --unset errexit
126set -x
127
128{
129 env false
130 true
131 set +x
132} 2>err.txt
133
134# normalize PIDs, assumed to be 2 or more digits
135sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt >&2
136
137## stdout-json: ""
138## STDERR:
139| command 12345: env 'false'
140; process 12345: status 1
141. builtin 'true'
142. builtin set '+x'
143## END
144
145#### subshell
146shopt --set oil:upgrade
147shopt --unset errexit
148set -x
149
150proc p {
151 : p
152}
153
154{
155 : begin
156 (
157 : 1
158 p
159 exit 3
160 )
161 set +x
162} 2>err.txt
163
164# Hack: sort for determinism
165sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt | LANG=C sort >&2
166
167## stdout-json: ""
168## STDERR:
169 . 12345 builtin ':' p
170 + 12345 exit 3
171 . 12345 builtin ':' 1
172 < 12345 proc p
173 > 12345 proc p
174. builtin ':' begin
175. builtin set '+x'
176; process 12345: status 3
177| forkwait 12345
178## END
179
180#### command sub
181shopt --set oil:upgrade
182set -x
183
184{
185 echo foo=$(echo bar)
186 set +x
187
188} 2>err.txt
189
190# HACK: sort because xtrace output has non-determinism.
191# This is arguably a bug -- see issue #995.
192# The real fix might be to sys.stderr.flush() in few places?
193sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt | LANG=C sort >&2
194
195## STDOUT:
196foo=bar
197## END
198## STDERR:
199 . 12345 builtin echo bar
200. builtin echo 'foo=bar'
201. builtin set '+x'
202; process 12345: status 0
203| command sub 12345
204## END
205
206#### process sub (nondeterministic)
207shopt --set oil:upgrade
208shopt --unset errexit
209set -x
210
211# we wait() for them all at the end
212
213{
214 : begin
215 cat <(seq 2) <(echo 1)
216 set +x
217} 2>err.txt
218
219# SORT for determinism
220sed --regexp-extended 's/[[:digit:]]{2,}/12345/g; s|/fd/.|/fd/N|g' err.txt |
221 LC_ALL=C sort >&2
222#cat err.txt >&2
223
224## STDOUT:
2251
2262
2271
228## END
229
230## STDERR:
231 . 12345 builtin echo 1
232 . 12345 exec seq 2
233. builtin ':' begin
234. builtin set '+x'
235; process 12345: status 0
236; process 12345: status 0
237; process 12345: status 0
238| command 12345: cat /dev/fd/N /dev/fd/N
239| proc sub 12345
240| proc sub 12345
241## END
242
243#### pipeline (nondeterministic)
244shopt --set oil:upgrade
245set -x
246
247myfunc() {
248 echo 1
249 echo 2
250}
251
252{
253 : begin
254 myfunc | sort | wc -l
255 set +x
256} 2>err.txt
257
258do_sort=1
259
260if test -n $do_sort; then
261 # SORT for determinism
262 sed --regexp-extended 's/[[:digit:]]{2,}/12345/g; s|/fd/.|/fd/N|g' err.txt |
263 LC_ALL=C sort >&2
264else
265 cat err.txt
266fi
267
268## STDOUT:
2692
270## END
271## STDERR:
272 . 12345 builtin echo 1
273 . 12345 builtin echo 2
274 . 12345 exec sort
275 < 12345 proc myfunc
276 > 12345 proc myfunc
277 ; process 12345: status 0
278 ; process 12345: status 0
279 ; process 12345: status 0
280 | command 12345: wc -l
281 | part 12345
282 | part 12345
283. builtin ':' begin
284. builtin set '+x'
285< pipeline
286> pipeline
287## END
288
289#### singleton pipeline
290
291# Hm extra tracing
292
293shopt --set oil:upgrade
294set -x
295
296: begin
297! false
298: end
299
300## stdout-json: ""
301## STDERR:
302. builtin ':' begin
303. builtin 'false'
304. builtin ':' end
305## END
306
307#### Background pipeline (separate code path)
308
309shopt --set oil:upgrade
310shopt --unset errexit
311set -x
312
313myfunc() {
314 echo 2
315 echo 1
316}
317
318{
319 : begin
320 myfunc | sort | grep ZZZ &
321 wait
322 echo status=$?
323 set +x
324} 2>err.txt
325
326# SORT for determinism
327sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt |
328 LC_ALL=C sort >&2
329
330## STDOUT:
331status=0
332## END
333## STDERR:
334 . 12345 builtin echo 1
335 . 12345 builtin echo 2
336 . 12345 exec grep ZZZ
337 . 12345 exec sort
338 ; process 12345: status 0
339 ; process 12345: status 0
340 ; process 12345: status 1
341 < 12345 proc myfunc
342 > 12345 proc myfunc
343. builtin ':' begin
344. builtin echo 'status=0'
345. builtin set '+x'
346< wait
347> wait
348[1] Done PGID 12345
349| part 12345
350| part 12345
351| part 12345
352## END
353
354#### Background process with fork and & (nondeterministic)
355shopt --set oil:upgrade
356set -x
357
358{
359 sleep 0.1 &
360 wait
361
362 shopt -s oil:upgrade
363
364 fork {
365 sleep 0.1
366 }
367 wait
368 set +x
369} 2>err.txt
370
371# SORT for determinism
372sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt |
373 LC_ALL=C sort >&2
374
375## stdout-json: ""
376## STDERR:
377 . 12345 exec sleep 0.1
378 . 12345 exec sleep 0.1
379 ; process 12345: status 0
380 ; process 12345: status 0
381. builtin fork
382. builtin set '+x'
383. builtin shopt -s 'oil:upgrade'
384< wait
385< wait
386> wait
387> wait
388[1] Done PID 12345
389[1] Done PID 12345
390| fork 12345
391| fork 12345
392## END
393
394# others: redirects?
395
396#### here doc
397shopt --set oil:upgrade
398shopt --unset errexit
399set -x
400
401{
402 : begin
403 tac <<EOF
4043
4052
406EOF
407
408 set +x
409} 2>err.txt
410
411sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt >&2
412
413## STDOUT:
4142
4153
416## END
417## STDERR:
418. builtin ':' begin
419| here doc 12345
420| command 12345: tac
421; process 12345: status 0
422; process 12345: status 0
423. builtin set '+x'
424## END
425
426#### Two here docs
427
428# BUG: This trace shows an extra process?
429
430shopt --set oil:upgrade
431shopt --unset errexit
432set -x
433
434{
435 cat - /dev/fd/3 <<EOF 3<<EOF2
436xx
437yy
438EOF
439zz
440EOF2
441
442 set +x
443} 2>err.txt
444
445sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt >&2
446
447## STDOUT:
448xx
449yy
450zz
451## END
452## STDERR:
453| here doc 12345
454| here doc 12345
455| command 12345: cat - /dev/fd/3
456; process 12345: status 0
457; process 12345: status 0
458; process 12345: status 0
459. builtin set '+x'
460## END
461
462#### Control Flow
463shopt --set oil:upgrade
464set -x
465
466for i in 1 2 3 {
467 echo $i
468 if (i === '2') {
469 break
470 }
471}
472
473for j in a b {
474 for k in y z {
475 echo $j $k
476 if (k === 'y') {
477 continue
478 }
479 }
480}
481
482proc zero {
483 return 0
484}
485
486proc one {
487 return 1
488}
489
490zero
491# one
492
493## STDOUT:
4941
4952
496a y
497a z
498b y
499b z
500## END
501## STDERR:
502. builtin echo 1
503. builtin echo 2
504+ break 1
505. builtin echo a y
506+ continue 1
507. builtin echo a z
508. builtin echo b y
509+ continue 1
510. builtin echo b z
511> proc zero
512 + return 0
513< proc zero
514## END
515
516#### Encoded argv uses shell encoding, not J8
517
518shopt --set ysh:upgrade
519set -x
520
521echo $'one two\n' $'\u03bc'
522## STDOUT:
523one two
524 μ
525## END
526## STDERR:
527. builtin echo $'one two\n' 'μ'
528## END