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

588 lines, 363 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#### xtrace_details 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#### xtrace_details AND xtrace_rich on
39shopt -s ysh: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 ysh:upgrade
60set -x
61
62shfunc() {
63 : $1
64}
65
66proc p (x) {
67 : $x
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 ysh: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 ysh: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 ysh: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 ysh: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 ysh: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 ysh:upgrade
208shopt --unset errexit
209set -x
210
211# we wait() for them all at the end
212
213{
214 : begin
215 tac <(seq 3 4) <(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:
2254
2263
2271
228## END
229
230## STDERR:
231 . 12345 builtin echo 1
232 . 12345 exec seq 3 4
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: tac /dev/fd/N /dev/fd/N
239| proc sub 12345
240| proc sub 12345
241## END
242
243#### pipeline (nondeterministic)
244shopt --set ysh: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 ysh: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 ysh: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| part 12345
349| part 12345
350| part 12345
351## END
352
353#### Background process with fork and & (nondeterministic)
354shopt --set ysh:upgrade
355set -x
356
357{
358 sleep 0.1 &
359 wait
360
361 shopt -s ysh:upgrade
362
363 fork {
364 sleep 0.1
365 }
366 wait
367 set +x
368} 2>err.txt
369
370# SORT for determinism
371sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt |
372 LC_ALL=C sort >&2
373
374## stdout-json: ""
375## STDERR:
376 . 12345 exec sleep 0.1
377 . 12345 exec sleep 0.1
378 ; process 12345: status 0
379 ; process 12345: status 0
380. builtin fork
381. builtin set '+x'
382. builtin shopt -s 'ysh:upgrade'
383< wait
384< wait
385> wait
386> wait
387| fork 12345
388| fork 12345
389## END
390
391# others: redirects?
392
393#### Here doc
394shopt --set ysh:upgrade
395shopt --unset errexit
396set -x
397
398{
399 : begin
400 tac <<EOF
4013
4022
403EOF
404
405 set +x
406} 2>err.txt
407
408sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt >&2
409
410## STDOUT:
4112
4123
413## END
414## STDERR:
415. builtin ':' begin
416| command 12345: tac
417; process 12345: status 0
418. builtin set '+x'
419## END
420
421#### Two here docs
422
423# BUG: This trace shows an extra process?
424
425shopt --set ysh:upgrade
426shopt --unset errexit
427set -x
428
429{
430 tac - /dev/fd/3 <<EOF 3<<EOF2
431xx
432yy
433EOF
434zz
435EOF2
436
437 set +x
438} 2>err.txt
439
440sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt >&2
441
442## STDOUT:
443yy
444xx
445zz
446## END
447## STDERR:
448| command 12345: tac - /dev/fd/3
449; process 12345: status 0
450. builtin set '+x'
451## END
452
453#### Here doc greater than 4096 bytes
454
455{
456 echo 'wc -l <<EOF'
457 seq 2000
458 echo 'EOF'
459} > big-here.sh
460
461wc -l big-here.sh
462
463$SH -o ysh:upgrade -x big-here.sh 2>err.txt
464
465sed --regexp-extended 's/[[:digit:]]{2,}/12345/g' err.txt >&2
466
467## STDOUT:
4682002 big-here.sh
4692000
470## END
471## STDERR:
472| here doc 12345
473| command 12345: wc -l
474; process 12345: status 0
475; process 12345: status 0
476## END
477
478#### Control Flow
479shopt --set ysh:upgrade
480set -x
481
482for i in 1 2 3 {
483 echo $i
484 if (i === '2') {
485 break
486 }
487}
488
489for j in a b {
490 for k in y z {
491 echo $j $k
492 if (k === 'y') {
493 continue
494 }
495 }
496}
497
498proc zero {
499 return 0
500}
501
502proc one {
503 return 1
504}
505
506zero
507# one
508
509## STDOUT:
5101
5112
512a y
513a z
514b y
515b z
516## END
517## STDERR:
518. builtin echo 1
519. builtin echo 2
520+ break 1
521. builtin echo a y
522+ continue 1
523. builtin echo a z
524. builtin echo b y
525+ continue 1
526. builtin echo b z
527> proc zero
528 + return 0
529< proc zero
530## END
531
532#### use builtin and invokable module
533shopt --set ysh:upgrade
534
535# make the trace deterministic
536cp $REPO_ROOT/spec/testdata/module2/for-xtrace.ysh .
537
538set -x
539
540source for-xtrace.ysh
541echo
542
543# problem with PS4 here
544use for-xtrace.ysh # --all-provided
545
546for-xtrace increment foo bar
547
548## STDOUT:
549[for-xtrace]
550counter = 5
551
552[for-xtrace]
553counter = 5
554counter = 6
555## END
556
557## STDERR:
558> source for-xtrace.ysh
559 . builtin echo '[for-xtrace]'
560 > proc increment
561 . builtin echo 'counter = 5'
562 < proc increment
563< source for-xtrace.ysh
564. builtin echo
565> use for-xtrace.ysh
566 . builtin echo '[for-xtrace]'
567 > proc increment
568 . builtin echo 'counter = 5'
569 < proc increment
570< use for-xtrace.ysh
571> module-invoke for-xtrace increment foo bar
572 . builtin echo 'counter = 6'
573< module-invoke for-xtrace
574## END
575
576#### Encoded argv uses shell encoding, not J8
577
578shopt --set ysh:upgrade
579set -x
580
581echo $'one two\n' $'\u03bc'
582## STDOUT:
583one two
584 μ
585## END
586## STDERR:
587. builtin echo $'one two\n' 'μ'
588## END