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

394 lines, 192 significant
1# xtrace test. Test PS4 and line numbers, etc.
2
3## oils_failures_allowed: 1
4## compare_shells: bash dash mksh
5
6#### unset PS4
7case $SH in dash) echo 'weird bug'; exit ;; esac
8
9set -x
10echo 1
11unset PS4
12echo 2
13## STDOUT:
141
152
16## STDERR:
17+ echo 1
18+ unset PS4
19echo 2
20## END
21
22## BUG dash STDOUT:
23weird bug
24## END
25## BUG dash STDERR:
26## END
27
28#### set -o verbose prints unevaluated code
29set -o verbose
30x=foo
31y=bar
32echo $x
33echo $(echo $y)
34## STDOUT:
35foo
36bar
37## STDERR:
38x=foo
39y=bar
40echo $x
41echo $(echo $y)
42## OK bash STDERR:
43x=foo
44y=bar
45echo $x
46echo $(echo $y)
47## END
48
49#### xtrace with unprintable chars
50case $SH in (dash) exit ;; esac
51
52s=$'a\x03b\004c\x00d'
53set -o xtrace
54echo "$s"
55## stdout-repr: 'a\x03b\x04c\x00d\n'
56## STDERR:
57+ echo $'a\u0003b\u0004c\u0000d'
58## END
59## OK bash stdout-repr: 'a\x03b\x04c\n'
60## OK bash stderr-repr: "+ echo $'a\\003b\\004c'\n"
61
62# nonsensical output?
63## BUG mksh stdout-repr: 'a;\x04c\r\n'
64## BUG mksh stderr-repr: "+ echo $'a;\\004c\\r'\n"
65## N-I dash stdout-json: ""
66## N-I dash stderr-json: ""
67
68#### xtrace with unicode chars
69case $SH in (dash) exit ;; esac
70
71mu1='[μ]'
72mu2=$'[\u03bc]'
73
74set -o xtrace
75echo "$mu1" "$mu2"
76
77## STDOUT:
78[μ] [μ]
79## END
80## STDERR:
81+ echo '[μ]' '[μ]'
82## END
83## N-I dash stdout-json: ""
84## N-I dash stderr-json: ""
85
86#### xtrace with paths
87set -o xtrace
88echo my-dir/my_file.cc
89## STDOUT:
90my-dir/my_file.cc
91## END
92## STDERR:
93+ echo my-dir/my_file.cc
94## END
95
96#### xtrace with tabs
97case $SH in (dash) exit ;; esac
98
99set -o xtrace
100echo $'[\t]'
101## stdout-json: "[\t]\n"
102## STDERR:
103+ echo $'[\t]'
104## END
105# this is a bug because it's hard to see
106## BUG bash stderr-json: "+ echo '[\t]'\n"
107## N-I dash stdout-json: ""
108## N-I dash stderr-json: ""
109
110#### xtrace with whitespace, quotes, and backslash
111set -o xtrace
112echo '1 2' \' \" \\
113## STDOUT:
1141 2 ' " \
115## END
116
117# YSH is different because backslashes require $'\\' and not '\', but that's OK
118## STDERR:
119+ echo '1 2' $'\'' '"' $'\\'
120## END
121
122## OK bash/mksh STDERR:
123+ echo '1 2' \' '"' '\'
124## END
125
126## BUG dash STDERR:
127+ echo 1 2 ' " \
128## END
129
130#### xtrace with newlines
131# bash and dash trace this badly. They print literal newlines, which I don't
132# want.
133set -x
134echo $'[\n]'
135## STDOUT:
136[
137]
138## STDERR:
139+ echo $'[\n]'
140## END
141# bash has ugly output that spans lines
142## OK bash STDERR:
143+ echo '[
144]'
145## END
146## N-I dash stdout-json: "$[\n]\n"
147## N-I dash stderr-json: "+ echo $[\\n]\n"
148
149#### xtrace written before command executes
150set -x
151echo one >&2
152echo two >&2
153## stdout-json: ""
154## STDERR:
155+ echo one
156one
157+ echo two
158two
159## OK mksh STDERR:
160# mksh traces redirects!
161+ >&2
162+ echo one
163one
164+ >&2
165+ echo two
166two
167## END
168
169#### Assignments and assign builtins
170set -x
171x=1 x=2; echo $x; readonly x=3
172## STDOUT:
1732
174## END
175## STDERR:
176+ x=1
177+ x=2
178+ echo 2
179+ readonly x=3
180## END
181## OK dash STDERR:
182+ x=1 x=2
183+ echo 2
184+ readonly x=3
185## END
186## OK bash STDERR:
187+ x=1
188+ x=2
189+ echo 2
190+ readonly x=3
191+ x=3
192## END
193## OK mksh STDERR:
194+ x=1 x=2
195+ echo 2
196+ readonly 'x=3'
197## END
198
199#### [[ ]]
200case $SH in (dash|mksh) exit ;; esac
201
202set -x
203
204dir=/
205if [[ -d $dir ]]; then
206 (( a = 42 ))
207fi
208## stdout-json: ""
209## STDERR:
210+ dir=/
211+ [[ -d $dir ]]
212+ (( a = 42 ))
213## END
214## OK bash STDERR:
215+ dir=/
216+ [[ -d / ]]
217+ (( a = 42 ))
218## END
219## N-I dash/mksh stderr-json: ""
220
221#### PS4 is scoped
222set -x
223echo one
224f() {
225 local PS4='- '
226 echo func;
227}
228f
229echo two
230## STDERR:
231+ echo one
232+ f
233+ local 'PS4=- '
234- echo func
235+ echo two
236## END
237## OK osh STDERR:
238+ echo one
239+ f
240+ local PS4='- '
241- echo func
242+ echo two
243## END
244## OK dash STDERR:
245# dash loses information about spaces! There is a trailing space, but you
246# can't see it.
247+ echo one
248+ f
249+ local PS4=-
250- echo func
251+ echo two
252## END
253## OK mksh STDERR:
254# local gets turned into typeset
255+ echo one
256+ f
257+ typeset 'PS4=- '
258- echo func
259+ echo two
260## END
261
262#### xtrace with variables in PS4
263PS4='+$x:'
264set -o xtrace
265x=1
266echo one
267x=2
268echo two
269## STDOUT:
270one
271two
272## END
273
274## STDERR:
275+:x=1
276+1:echo one
277+1:x=2
278+2:echo two
279## END
280
281## OK mksh STDERR:
282# mksh has trailing spaces
283+:x=1
284+1:echo one
285+1:x=2
286+2:echo two
287## END
288
289## OK osh/dash STDERR:
290# the PS4 string is evaluated AFTER the variable is set. That's OK
291+1:x=1
292+1:echo one
293+2:x=2
294+2:echo two
295## END
296
297#### PS4 with unterminated ${
298# osh shows inline error; maybe fail like dash/mksh?
299x=1
300PS4='+${x'
301set -o xtrace
302echo one
303echo status=$?
304## STDOUT:
305one
306status=0
307## END
308# mksh and dash both fail. bash prints errors to stderr.
309## OK dash stdout-json: ""
310## OK dash status: 2
311## OK mksh stdout-json: ""
312## OK mksh status: 1
313
314#### PS4 with unterminated $(
315# osh shows inline error; maybe fail like dash/mksh?
316x=1
317PS4='+$(x'
318set -o xtrace
319echo one
320echo status=$?
321## STDOUT:
322one
323status=0
324## END
325# mksh and dash both fail. bash prints errors to stderr.
326## OK dash stdout-json: ""
327## OK dash status: 2
328## OK mksh stdout-json: ""
329## OK mksh status: 1
330
331#### PS4 with runtime error
332# osh shows inline error; maybe fail like dash/mksh?
333x=1
334PS4='+oops $(( 1 / 0 )) \$'
335set -o xtrace
336echo one
337echo status=$?
338## STDOUT:
339one
340status=0
341## END
342# mksh and dash both fail. bash prints errors to stderr.
343## OK dash stdout-json: ""
344## OK dash status: 2
345## OK mksh stdout-json: ""
346## OK mksh status: 1
347
348
349#### Reading $? in PS4
350PS4='[last=$?] '
351set -x
352false
353echo ok
354## STDOUT:
355ok
356## END
357## STDERR:
358[last=0] false
359[last=1] echo ok
360## END
361## OK osh STDERR:
362[last=0] 'false'
363[last=1] echo ok
364## END
365
366
367#### Regression: xtrace for "declare -a a+=(v)"
368case $SH in dash|mksh) exit ;; esac
369
370a=(1)
371set -x
372declare a+=(2)
373## STDERR:
374+ declare a+=(2)
375## END
376## OK bash STDERR:
377+ a+=('2')
378+ declare a
379## END
380## N-I dash/mksh STDERR:
381## END
382
383
384#### Regression: xtrace for "a+=(v)"
385case $SH in dash|mksh) exit ;; esac
386
387a=(1)
388set -x
389a+=(2)
390## STDERR:
391+ a+=(2)
392## END
393## N-I dash/mksh STDERR:
394## END