OILS / spec / ysh-xtrace.test.sh View on Github | oils.pub

589 lines, 364 significant
1## oils_failures_allowed: 0
2
3#### Customize PS4
4shopt -s ysh: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#### no_xtrace_osh doesn't show [[ ]] etc.
24shopt -s ysh: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#### no_xtrace_osh UNSET, and xtrace_rich set
39shopt --set ysh:upgrade
40shopt --unset no_xtrace_osh
41shopt --unset errexit
42set -x
43
44{
45 env false
46 set +x
47} 2>err.txt
48
49sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt >&2
50
51## STDOUT:
52## END
53## STDERR:
54| command 12345: env 'false'
55; process 12345: status 1
56. builtin set '+x'
57## END
58
59#### proc and shell function
60shopt --set ysh:upgrade
61set -x
62
63shfunc() {
64 : $1
65}
66
67proc p (x) {
68 : $x
69}
70
71shfunc 1
72p 2
73## stdout-json: ""
74## STDERR:
75> proc shfunc 1
76 . builtin ':' 1
77< proc shfunc
78> proc p 2
79 . builtin ':' 2
80< proc p
81## END
82
83#### eval
84shopt --set ysh:upgrade
85set -x
86
87eval 'echo 1; echo 2'
88## STDOUT:
891
902
91## END
92## STDERR:
93> eval
94 . builtin echo 1
95 . builtin echo 2
96< eval
97## END
98
99#### source
100echo 'echo "\$1 = $1"' > lib.sh
101
102shopt --set ysh:upgrade
103set -x
104
105source lib.sh a b c
106
107# Test the quoting style. TODO: Don't use bash style in YSH.
108
109source lib.sh x $'\xfe' $'\xff'
110
111## STDOUT:
112$1 = a
113$1 = x
114## END
115## STDERR:
116> source lib.sh a b c
117 . builtin echo '$1 = a'
118< source lib.sh
119> source lib.sh x $'\xfe' $'\xff'
120 . builtin echo '$1 = x'
121< source lib.sh
122## END
123
124#### external and builtin
125shopt --set ysh:upgrade
126shopt --unset errexit
127set -x
128
129{
130 env false
131 true
132 set +x
133} 2>err.txt
134
135# normalize PIDs, assumed to be 2 or more digits
136sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt >&2
137
138## stdout-json: ""
139## STDERR:
140| command 12345: env 'false'
141; process 12345: status 1
142. builtin 'true'
143. builtin set '+x'
144## END
145
146#### subshell
147shopt --set ysh:upgrade
148shopt --unset errexit
149set -x
150
151proc p {
152 : p
153}
154
155{
156 : begin
157 (
158 : 1
159 p
160 exit 3
161 )
162 set +x
163} 2>err.txt
164
165# Hack: sort for determinism
166sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt | LANG=C sort >&2
167
168## stdout-json: ""
169## STDERR:
170 . 12345 builtin ':' p
171 + 12345 exit 3
172 . 12345 builtin ':' 1
173 < 12345 proc p
174 > 12345 proc p
175. builtin ':' begin
176. builtin set '+x'
177; process 12345: status 3
178| forkwait 12345
179## END
180
181#### command sub
182shopt --set ysh:upgrade
183set -x
184
185{
186 echo foo=$(echo bar)
187 set +x
188
189} 2>err.txt
190
191# HACK: sort because xtrace output has non-determinism.
192# This is arguably a bug -- see issue #995.
193# The real fix might be to sys.stderr.flush() in few places?
194sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt | LANG=C sort >&2
195
196## STDOUT:
197foo=bar
198## END
199## STDERR:
200 . 12345 builtin echo bar
201. builtin echo 'foo=bar'
202. builtin set '+x'
203; process 12345: status 0
204| command sub 12345
205## END
206
207#### process sub (nondeterministic)
208shopt --set ysh:upgrade
209shopt --unset errexit
210set -x
211
212# we wait() for them all at the end
213
214{
215 : begin
216 tac <(seq 3 4) <(echo 1)
217 set +x
218} 2>err.txt
219
220# SORT for determinism
221sed --regexp-extended 's/[[:digit:]]{2,}/12345/g; s|/fd/.|/fd/N|g' err.txt |
222 LC_ALL=C sort >&2
223#cat err.txt >&2
224
225## STDOUT:
2264
2273
2281
229## END
230
231## STDERR:
232 . 12345 builtin echo 1
233 . 12345 exec seq 3 4
234. builtin ':' begin
235. builtin set '+x'
236; process 12345: status 0
237; process 12345: status 0
238; process 12345: status 0
239| command 12345: tac /dev/fd/N /dev/fd/N
240| proc sub 12345
241| proc sub 12345
242## END
243
244#### pipeline (nondeterministic)
245shopt --set ysh:upgrade
246set -x
247
248myfunc() {
249 echo 1
250 echo 2
251}
252
253{
254 : begin
255 myfunc | sort | wc -l
256 set +x
257} 2>err.txt
258
259do_sort=1
260
261if test -n $do_sort; then
262 # SORT for determinism
263 sed --regexp-extended 's/[[:digit:]]{2,}/12345/g; s|/fd/.|/fd/N|g' err.txt |
264 LC_ALL=C sort >&2
265else
266 cat err.txt
267fi
268
269## STDOUT:
2702
271## END
272## STDERR:
273 . 12345 builtin echo 1
274 . 12345 builtin echo 2
275 . 12345 exec sort
276 < 12345 proc myfunc
277 > 12345 proc myfunc
278 ; process 12345: status 0
279 ; process 12345: status 0
280 ; process 12345: status 0
281 | command 12345: wc -l
282 | part 12345
283 | part 12345
284. builtin ':' begin
285. builtin set '+x'
286< pipeline
287> pipeline
288## END
289
290#### singleton pipeline
291
292# Hm extra tracing
293
294shopt --set ysh:upgrade
295set -x
296
297: begin
298! false
299: end
300
301## stdout-json: ""
302## STDERR:
303. builtin ':' begin
304. builtin 'false'
305. builtin ':' end
306## END
307
308#### Background pipeline (separate code path)
309
310shopt --set ysh:upgrade
311shopt --unset errexit
312set -x
313
314myfunc() {
315 echo 2
316 echo 1
317}
318
319{
320 : begin
321 myfunc | sort | grep ZZZ &
322 wait
323 echo status=$?
324 set +x
325} 2>err.txt
326
327# SORT for determinism
328sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt |
329 LC_ALL=C sort >&2
330
331## STDOUT:
332status=0
333## END
334## STDERR:
335 . 12345 builtin echo 1
336 . 12345 builtin echo 2
337 . 12345 exec grep ZZZ
338 . 12345 exec sort
339 ; process 12345: status 0
340 ; process 12345: status 0
341 ; process 12345: status 1
342 < 12345 proc myfunc
343 > 12345 proc myfunc
344. builtin ':' begin
345. builtin echo 'status=0'
346. builtin set '+x'
347< wait
348> wait
349| part 12345
350| part 12345
351| part 12345
352## END
353
354#### Background process with fork and & (nondeterministic)
355shopt --set ysh:upgrade
356set -x
357
358{
359 sleep 0.1 &
360 wait
361
362 shopt -s ysh: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 'ysh:upgrade'
384< wait
385< wait
386> wait
387> wait
388| fork 12345
389| fork 12345
390## END
391
392# others: redirects?
393
394#### Here doc
395shopt --set ysh:upgrade
396shopt --unset errexit
397set -x
398
399{
400 : begin
401 tac <<EOF
4023
4032
404EOF
405
406 set +x
407} 2>err.txt
408
409sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt >&2
410
411## STDOUT:
4122
4133
414## END
415## STDERR:
416. builtin ':' begin
417| command 12345: tac
418; process 12345: status 0
419. builtin set '+x'
420## END
421
422#### Two here docs
423
424# BUG: This trace shows an extra process?
425
426shopt --set ysh:upgrade
427shopt --unset errexit
428set -x
429
430{
431 tac - /dev/fd/3 <<EOF 3<<EOF2
432xx
433yy
434EOF
435zz
436EOF2
437
438 set +x
439} 2>err.txt
440
441sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt >&2
442
443## STDOUT:
444yy
445xx
446zz
447## END
448## STDERR:
449| command 12345: tac - /dev/fd/3
450; process 12345: status 0
451. builtin set '+x'
452## END
453
454#### Here doc greater than 4096 bytes
455
456{
457 echo 'wc -l <<EOF'
458 seq 2000
459 echo 'EOF'
460} > big-here.sh
461
462wc -l big-here.sh
463
464$SH -o ysh:upgrade -x big-here.sh 2>err.txt
465
466sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt >&2
467
468## STDOUT:
4692002 big-here.sh
4702000
471## END
472## STDERR:
473| here doc 12345
474| command 12345: wc -l
475; process 12345: status 0
476; process 12345: status 0
477## END
478
479#### Control Flow
480shopt --set ysh:upgrade
481set -x
482
483for i in 1 2 3 {
484 echo $i
485 if (i === '2') {
486 break
487 }
488}
489
490for j in a b {
491 for k in y z {
492 echo $j $k
493 if (k === 'y') {
494 continue
495 }
496 }
497}
498
499proc zero {
500 return 0
501}
502
503proc one {
504 return 1
505}
506
507zero
508# one
509
510## STDOUT:
5111
5122
513a y
514a z
515b y
516b z
517## END
518## STDERR:
519. builtin echo 1
520. builtin echo 2
521+ break 1
522. builtin echo a y
523+ continue 1
524. builtin echo a z
525. builtin echo b y
526+ continue 1
527. builtin echo b z
528> proc zero
529 + return 0
530< proc zero
531## END
532
533#### use builtin and invokable module
534shopt --set ysh:upgrade
535
536# make the trace deterministic
537cp $REPO_ROOT/spec/testdata/module2/for-xtrace.ysh .
538
539set -x
540
541source for-xtrace.ysh
542echo
543
544# problem with PS4 here
545use for-xtrace.ysh # --all-provided
546
547for-xtrace increment foo bar
548
549## STDOUT:
550[for-xtrace]
551counter = 5
552
553[for-xtrace]
554counter = 5
555counter = 6
556## END
557
558## STDERR:
559> source for-xtrace.ysh
560 . builtin echo '[for-xtrace]'
561 > proc increment
562 . builtin echo 'counter = 5'
563 < proc increment
564< source for-xtrace.ysh
565. builtin echo
566> use for-xtrace.ysh
567 . builtin echo '[for-xtrace]'
568 > proc increment
569 . builtin echo 'counter = 5'
570 < proc increment
571< use for-xtrace.ysh
572> module-invoke for-xtrace increment foo bar
573 . builtin echo 'counter = 6'
574< module-invoke for-xtrace
575## END
576
577#### Encoded argv uses shell encoding, not J8
578
579shopt --set ysh:upgrade
580set -x
581
582echo $'one two\n' $'\u03bc'
583## STDOUT:
584one two
585 μ
586## END
587## STDERR:
588. builtin echo $'one two\n' 'μ'
589## END