OILS / spec / toysh-posix.test.sh View on Github | oils.pub

395 lines, 174 significant
1## oils_failures_allowed: 5
2## compare_shells: bash dash mksh zsh ash yash
3
4#### Fatal error
5# http://landley.net/notes.html#20-06-2020
6
7abc=${a?bc} echo hello; echo blah
8## status: 1
9## OK yash/dash/ash status: 2
10## stdout-json: ""
11
12#### setting readonly var (bash is only one where it's non-fatal)
13# http://landley.net/notes.html#20-06-2020
14
15readonly abc=123
16abc=def
17echo status=$?
18## status: 2
19## stdout-json: ""
20## OK osh/zsh status: 1
21## BUG bash status: 0
22## BUG bash STDOUT:
23status=1
24## END
25
26#### readonly with temp binding
27# http://landley.net/notes.html#20-06-2020
28
29# temp binding
30readonly abc=123
31abc=def echo one
32echo status=$?
33
34echo potato < /does/not/exist || echo hello
35
36## status: 2
37## stdout-json: ""
38## OK osh/bash status: 0
39## OK osh/bash STDOUT:
40one
41status=0
42hello
43## END
44## OK zsh status: 1
45
46#### Failed redirect in assignment, vs. export
47
48abc=def > /does/not/exist1
49echo abc=$abc
50
51export abc=def > /does/not/exist2
52echo abc=$abc
53
54## STDOUT:
55abc=
56abc=
57## END
58## BUG bash STDOUT:
59abc=def
60abc=def
61## END
62## OK dash/ash/mksh STDOUT:
63abc=
64## END
65## OK dash status: 2
66## OK mksh status: 1
67## OK ash status: 1
68
69#### Evaluation order of redirect and ${undef?error}
70# http://landley.net/notes.html#12-06-2020
71
72rm -f walrus
73$SH -c 'X=${x?bc} > walrus'
74if test -f walrus; then echo 'exists1'; fi
75
76rm -f walrus
77$SH -c '>walrus echo ${a?bc}'
78test -f walrus
79if test -f walrus; then echo 'exists2'; fi
80## STDOUT:
81exists1
82## END
83## OK bash stdout-json: ""
84
85#### Function def in pipeline
86# http://landley.net/notes.html#26-05-2020
87
88echo hello | potato() { echo abc; } | echo ha
89
90## STDOUT:
91ha
92## END
93
94#### dynamic glob - http://landley.net/notes.html#08-05-2020
95touch foo
96X='*'; echo $X
97echo "*"*".?z"
98## STDOUT:
99foo
100**.?z
101## END
102## BUG zsh status: 1
103## BUG zsh STDOUT:
104*
105## END
106
107#### no shebang
108cat > snork << 'EOF'
109echo hello $BLAH
110EOF
111
112chmod +x snork
113$SH -c 'BLAH=123; ./snork'
114$SH -c 'BLAH=123; exec ./snork'
115$SH -c 'BLAH=123 exec ./snork'
116## STDOUT:
117hello
118hello
119hello 123
120## END
121
122
123#### IFS
124
125IFS=x; X=abxcd; echo ${X/bxc/g}
126
127X=a=\"\$a\"; echo ${X//a/{x,y,z}}
128
129## STDOUT:
130agd
131{ ,y,z="${ ,y,z"}
132## END
133## BUG zsh STDOUT:
134agd
135{x,y,z}="${x,y,z}"
136## END
137## N-I dash status: 2
138## N-I dash stdout-json: ""
139
140#### shift is fatal at top level?
141# http://landley.net/notes.html#08-04-2020
142
143# This makes a difference for zsh, but not for bash?
144#set -o posix
145
146$SH -c 'shift; echo hello'
147## STDOUT:
148hello
149## END
150## OK dash status: 2
151## OK mksh status: 1
152## OK dash/mksh stdout-json: ""
153
154#### var and func - http://landley.net/notes.html#19-03-2020
155potato() { echo hello; }
156potato=42
157echo $potato
158
159potato
160
161## STDOUT:
16242
163hello
164## END
165
166
167#### IFS - http://landley.net/notes.html#05-03-2020
168case $SH in zsh) exit ;; esac
169
170IFS=x
171chicken() { for i in "$@"; do echo =$i=; done;}
172chicken one abc dxf ghi
173
174echo ---
175myfunc() { "$SH" -c 'IFS=x; for i in $@; do echo =$i=; done' blah "$@"; }
176myfunc one "" two
177
178## STDOUT:
179=one=
180=abc=
181=d f=
182=ghi=
183---
184=one=
185==
186=two=
187## END
188
189## BUG dash/ash STDOUT:
190=one=
191=abc=
192=d f=
193=ghi=
194---
195=one=
196=two=
197## END
198
199## N-I zsh STDOUT:
200## END
201
202#### IFS=x and '' and unquoted $@ - reduction of case above - copied into spec/word-split
203
204setopt SH_WORD_SPLIT
205#set -x
206
207set -- one "" two
208
209IFS=x
210
211argv.py $@
212
213for i in $@; do
214 echo -$i-
215done
216
217## STDOUT:
218['one', '', 'two']
219-one-
220--
221-two-
222## END
223
224## BUG dash/ash/zsh STDOUT:
225['one', 'two']
226-one-
227-two-
228## END
229
230
231#### for loop parsing - http://landley.net/notes.html#04-03-2020
232
233$SH -c '
234for i
235in one two three
236do echo $i;
237done
238'
239echo $?
240
241$SH -c 'for i; in one two three; do echo $i; done'
242test $? -ne 0 && echo cannot-parse
243
244## STDOUT:
245one
246two
247three
2480
249cannot-parse
250## END
251
252#### Parsing $(( ))
253# http://landley.net/notes.html#15-03-2020
254$SH -c 'echo $((echo hello))'
255if test $? -ne 0; then echo fail; fi
256## stdout: fail
257
258#### IFS - http://landley.net/notes.html#15-02-2020 (TODO: osh)
259
260IFS=x
261A=xabcxx
262for i in $A; do echo =$i=; done
263echo
264
265unset IFS
266A=" abc def "
267for i in ""$A""; do echo =$i=; done
268
269## STDOUT:
270==
271=abc=
272==
273
274==
275=abc=
276=def=
277==
278## END
279## BUG zsh status: 1
280## BUG zsh stdout-json: ""
281
282#### IFS 2 - copied into spec/word-split
283# this one appears different between osh and bash
284A=" abc def "; for i in ""x""$A""; do echo =$i=; done
285
286## STDOUT:
287=x=
288=abc=
289=def=
290==
291## END
292## BUG zsh status: 1
293## BUG zsh stdout-json: ""
294
295#### IFS 3
296IFS=x; X="onextwoxxthree"; y=$X; echo $y
297## STDOUT:
298one two three
299## END
300## BUG zsh STDOUT:
301onextwoxxthree
302## END
303
304#### IFS 4
305
306setopt SH_WORD_SPLIT
307
308IFS=x
309
310func1() {
311 echo /$*/
312 for i in $*; do echo -$i-; done
313}
314func1 "" ""
315
316echo
317
318func2() {
319 echo /"$*"/
320 for i in =$*=; do echo -$i-; done
321}
322func2 "" ""
323
324## STDOUT:
325/ /
326
327/x/
328-=-
329-=-
330## END
331## BUG bash STDOUT:
332/ /
333--
334
335/x/
336-=-
337-=-
338## END
339## BUG yash/zsh STDOUT:
340/ /
341--
342--
343
344/x/
345-=-
346-=-
347## END
348
349#### IFS 5
350cc() { for i in $*; do echo -$i-; done;}; cc "" "" "" "" ""
351cc() { echo =$1$2=;}; cc "" ""
352## STDOUT:
353==
354## END
355## BUG yash STDOUT:
356--
357--
358--
359--
360--
361==
362## END
363## BUG zsh status: 1
364## BUG zsh stdout-json: ""
365
366#### Can't parse extra }
367
368$SH -c 'for i in a"$@"b;do echo =$i=;done;}' 123 456 789
369## status: 2
370## OK mksh/zsh status: 1
371## STDOUT:
372## END
373
374#### Command Sub Syntax Error
375# http://landley.net/notes.html#28-01-2020
376
377echo $(if true)
378echo $?
379echo $(false)
380echo $?
381
382## status: 2
383## stdout-json: ""
384
385## OK mksh/zsh status: 1
386
387
388#### Pipeline - http://landley.net/notes-2019.html#16-12-2019
389echo hello | { read i; echo $i;} | { read i; echo $i;} | cat
390echo hello | while read i; do echo -=$i=- | sed s/=/@/g ; done | cat
391## STDOUT:
392hello
393-@hello@-
394## END
395