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

380 lines, 167 significant
1## oils_failures_allowed: 3
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
71rm *
72
73rm -f walrus
74$SH -c 'X=${x?bc} > walrus'
75if test -f walrus; then echo 'exists1'; fi
76
77rm -f walrus
78$SH -c '>walrus echo ${a?bc}'
79test -f walrus
80if test -f walrus; then echo 'exists2'; fi
81## STDOUT:
82exists1
83## END
84## OK bash stdout-json: ""
85
86#### Function def in pipeline
87# http://landley.net/notes.html#26-05-2020
88
89echo hello | potato() { echo abc; } | echo ha
90
91## STDOUT:
92ha
93## END
94
95#### dynamic glob - http://landley.net/notes.html#08-05-2020
96rm * # setup
97X='*'; echo $X
98echo "*"*".?z"
99## STDOUT:
100_tmp
101**.?z
102## END
103## BUG zsh status: 1
104## BUG zsh STDOUT:
105*
106## END
107
108#### no shebang
109rm *
110
111cat > snork << 'EOF'
112echo hello $BLAH
113EOF
114
115chmod +x snork
116$SH -c 'BLAH=123; ./snork'
117$SH -c 'BLAH=123; exec ./snork'
118$SH -c 'BLAH=123 exec ./snork'
119## STDOUT:
120hello
121hello
122hello 123
123## END
124
125
126#### IFS
127
128IFS=x; X=abxcd; echo ${X/bxc/g}
129
130X=a=\"\$a\"; echo ${X//a/{x,y,z}}
131
132## STDOUT:
133agd
134{ ,y,z="${ ,y,z"}
135## END
136## BUG zsh STDOUT:
137agd
138{x,y,z}="${x,y,z}"
139## END
140## N-I dash status: 2
141## N-I dash stdout-json: ""
142
143#### shift is fatal at top level?
144# http://landley.net/notes.html#08-04-2020
145
146# This makes a difference for zsh, but not for bash?
147#set -o posix
148
149$SH -c 'shift; echo hello'
150## STDOUT:
151hello
152## END
153## OK dash status: 2
154## OK mksh status: 1
155## OK dash/mksh stdout-json: ""
156
157#### var and func - http://landley.net/notes.html#19-03-2020
158potato() { echo hello; }
159potato=42
160echo $potato
161
162potato
163
164## STDOUT:
16542
166hello
167## END
168
169
170#### IFS - http://landley.net/notes.html#05-03-2020
171case $SH in zsh) exit ;; esac
172
173IFS=x
174chicken() { for i in "$@"; do echo =$i=; done;}
175chicken one abc dxf ghi
176
177echo ---
178myfunc() { "$SH" -c 'IFS=x; for i in $@; do echo =$i=; done' blah "$@"; }
179myfunc one "" two
180
181## STDOUT:
182=one=
183=abc=
184=d f=
185=ghi=
186---
187=one=
188=two=
189## END
190## BUG bash/mksh/yash STDOUT:
191=one=
192=abc=
193=d f=
194=ghi=
195---
196=one=
197==
198=two=
199## END
200## N-I zsh STDOUT:
201## END
202
203#### Compare $@ in argv to $@ in loop - reduction of case above
204case $SH in zsh) exit ;; esac
205
206set -- one "" two
207argv.py $@
208
209IFS=x
210for i in $@; do
211 echo =$i=
212done
213
214## STDOUT:
215['one', 'two']
216=one=
217=two=
218## END
219
220## BUG bash/mksh STDOUT:
221['one', 'two']
222=one=
223==
224=two=
225## END
226
227## BUG yash STDOUT:
228['one', '', 'two']
229=one=
230==
231=two=
232## END
233
234## N-I zsh STDOUT:
235## END
236
237#### for loop parsing - http://landley.net/notes.html#04-03-2020
238
239$SH -c '
240for i
241in one two three
242do echo $i;
243done
244'
245echo $?
246
247$SH -c 'for i; in one two three; do echo $i; done'
248test $? -ne 0 && echo cannot-parse
249
250## STDOUT:
251one
252two
253three
2540
255cannot-parse
256## END
257
258#### Parsing $(( ))
259# http://landley.net/notes.html#15-03-2020
260$SH -c 'echo $((echo hello))'
261if test $? -ne 0; then echo fail; fi
262## stdout: fail
263
264#### IFS - http://landley.net/notes.html#15-02-2020 (TODO: osh)
265
266IFS=x; A=xabcxx; for i in $A; do echo =$i=; done
267
268unset IFS; A=" abc def "; for i in ""$A""; do echo =$i=; done
269
270## STDOUT:
271==
272=abc=
273==
274==
275=abc=
276=def=
277==
278## END
279## BUG zsh status: 1
280## BUG zsh stdout-json: ""
281
282#### IFS 2 (TODO: osh)
283this 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
305case $SH in zsh) exit ;; esac # buggy
306
307IFS=x
308cc() { echo =$*=; for i in $*; do echo -$i-; done;}; cc "" ""
309cc() { echo ="$*"=; for i in =$*=; do echo -$i-; done;}; cc "" ""
310## STDOUT:
311= =
312=x=
313-=-
314-=-
315## END
316## BUG bash STDOUT:
317= =
318--
319=x=
320-=-
321-=-
322## END
323## BUG yash STDOUT:
324= =
325--
326--
327=x=
328-=-
329-=-
330## END
331## N-I zsh STDOUT:
332## END
333
334#### IFS 5
335cc() { for i in $*; do echo -$i-; done;}; cc "" "" "" "" ""
336cc() { echo =$1$2=;}; cc "" ""
337## STDOUT:
338==
339## END
340## BUG yash STDOUT:
341--
342--
343--
344--
345--
346==
347## END
348## BUG zsh status: 1
349## BUG zsh stdout-json: ""
350
351#### Can't parse extra }
352
353$SH -c 'for i in a"$@"b;do echo =$i=;done;}' 123 456 789
354## status: 2
355## OK mksh/zsh status: 1
356## STDOUT:
357## END
358
359#### Command Sub Syntax Error
360# http://landley.net/notes.html#28-01-2020
361
362echo $(if true)
363echo $?
364echo $(false)
365echo $?
366
367## status: 2
368## stdout-json: ""
369
370## OK mksh/zsh status: 1
371
372
373#### Pipeline - http://landley.net/notes-2019.html#16-12-2019
374echo hello | { read i; echo $i;} | { read i; echo $i;} | cat
375echo hello | while read i; do echo -=$i=- | sed s/=/@/g ; done | cat
376## STDOUT:
377hello
378-@hello@-
379## END
380