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

369 lines, 183 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 dash STDERR:
187+ x=1 x=2
188+ echo 2
189+ readonly x=3
190## END
191## OK bash STDERR:
192+ x=1
193+ x=2
194+ echo 2
195+ readonly x=3
196+ x=3
197## END
198## OK mksh STDERR:
199+ x=1 x=2
200+ echo 2
201+ readonly 'x=3'
202## END
203
204#### [[ ]]
205case $SH in (dash|mksh) exit ;; esac
206
207set -x
208
209dir=/
210if [[ -d $dir ]]; then
211 (( a = 42 ))
212fi
213## stdout-json: ""
214## STDERR:
215+ dir=/
216+ [[ -d $dir ]]
217+ (( a = 42 ))
218## END
219## OK bash STDERR:
220+ dir=/
221+ [[ -d / ]]
222+ (( a = 42 ))
223## END
224## N-I dash/mksh stderr-json: ""
225
226#### PS4 is scoped
227set -x
228echo one
229f() {
230 local PS4='- '
231 echo func;
232}
233f
234echo two
235## STDERR:
236+ echo one
237+ f
238+ local 'PS4=- '
239- echo func
240+ echo two
241## END
242## OK osh STDERR:
243+ echo one
244+ f
245+ local PS4='- '
246- echo func
247+ echo two
248## END
249## OK dash STDERR:
250# dash loses information about spaces! There is a trailing space, but you
251# can't see it.
252+ echo one
253+ f
254+ local PS4=-
255- echo func
256+ echo two
257## END
258## OK mksh STDERR:
259# local gets turned into typeset
260+ echo one
261+ f
262+ typeset 'PS4=- '
263- echo func
264+ echo two
265## END
266
267#### xtrace with variables in PS4
268PS4='+$x:'
269set -o xtrace
270x=1
271echo one
272x=2
273echo two
274## STDOUT:
275one
276two
277## END
278
279## STDERR:
280+:x=1
281+1:echo one
282+1:x=2
283+2:echo two
284## END
285
286## OK mksh STDERR:
287# mksh has trailing spaces
288+:x=1
289+1:echo one
290+1:x=2
291+2:echo two
292## END
293
294## OK osh/dash STDERR:
295# the PS4 string is evaluated AFTER the variable is set. That's OK
296+1:x=1
297+1:echo one
298+2:x=2
299+2:echo two
300## END
301
302#### PS4 with unterminated ${
303# osh shows inline error; maybe fail like dash/mksh?
304x=1
305PS4='+${x'
306set -o xtrace
307echo one
308echo status=$?
309## STDOUT:
310one
311status=0
312## END
313# mksh and dash both fail. bash prints errors to stderr.
314## OK dash stdout-json: ""
315## OK dash status: 2
316## OK mksh stdout-json: ""
317## OK mksh status: 1
318
319#### PS4 with unterminated $(
320# osh shows inline error; maybe fail like dash/mksh?
321x=1
322PS4='+$(x'
323set -o xtrace
324echo one
325echo status=$?
326## STDOUT:
327one
328status=0
329## END
330# mksh and dash both fail. bash prints errors to stderr.
331## OK dash stdout-json: ""
332## OK dash status: 2
333## OK mksh stdout-json: ""
334## OK mksh status: 1
335
336#### PS4 with runtime error
337# osh shows inline error; maybe fail like dash/mksh?
338x=1
339PS4='+oops $(( 1 / 0 )) \$'
340set -o xtrace
341echo one
342echo status=$?
343## STDOUT:
344one
345status=0
346## END
347# mksh and dash both fail. bash prints errors to stderr.
348## OK dash stdout-json: ""
349## OK dash status: 2
350## OK mksh stdout-json: ""
351## OK mksh status: 1
352
353
354#### Reading $? in PS4
355PS4='[last=$?] '
356set -x
357false
358echo ok
359## STDOUT:
360ok
361## END
362## STDERR:
363[last=0] false
364[last=1] echo ok
365## END
366## OK osh STDERR:
367[last=0] 'false'
368[last=1] echo ok
369## END