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

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