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

396 lines, 175 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
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
109cat > snork << 'EOF'
110echo hello $BLAH
111EOF
112
113chmod +x snork
114$SH -c 'BLAH=123; ./snork'
115$SH -c 'BLAH=123; exec ./snork'
116$SH -c 'BLAH=123 exec ./snork'
117## STDOUT:
118hello
119hello
120hello 123
121## END
122
123
124#### IFS
125
126IFS=x; X=abxcd; echo ${X/bxc/g}
127
128X=a=\"\$a\"; echo ${X//a/{x,y,z}}
129
130## STDOUT:
131agd
132{ ,y,z="${ ,y,z"}
133## END
134## BUG zsh STDOUT:
135agd
136{x,y,z}="${x,y,z}"
137## END
138## N-I dash status: 2
139## N-I dash stdout-json: ""
140
141#### shift is fatal at top level?
142# http://landley.net/notes.html#08-04-2020
143
144# This makes a difference for zsh, but not for bash?
145#set -o posix
146
147$SH -c 'shift; echo hello'
148## STDOUT:
149hello
150## END
151## OK dash status: 2
152## OK mksh status: 1
153## OK dash/mksh stdout-json: ""
154
155#### var and func - http://landley.net/notes.html#19-03-2020
156potato() { echo hello; }
157potato=42
158echo $potato
159
160potato
161
162## STDOUT:
16342
164hello
165## END
166
167
168#### IFS - http://landley.net/notes.html#05-03-2020
169case $SH in zsh) exit ;; esac
170
171IFS=x
172chicken() { for i in "$@"; do echo =$i=; done;}
173chicken one abc dxf ghi
174
175echo ---
176myfunc() { "$SH" -c 'IFS=x; for i in $@; do echo =$i=; done' blah "$@"; }
177myfunc one "" two
178
179## STDOUT:
180=one=
181=abc=
182=d f=
183=ghi=
184---
185=one=
186==
187=two=
188## END
189
190## BUG dash/ash STDOUT:
191=one=
192=abc=
193=d f=
194=ghi=
195---
196=one=
197=two=
198## END
199
200## N-I zsh STDOUT:
201## END
202
203#### IFS=x and '' and unquoted $@ - reduction of case above - copied into spec/word-split
204
205setopt SH_WORD_SPLIT
206#set -x
207
208set -- one "" two
209
210IFS=x
211
212argv.py $@
213
214for i in $@; do
215 echo -$i-
216done
217
218## STDOUT:
219['one', '', 'two']
220-one-
221--
222-two-
223## END
224
225## BUG dash/ash/zsh STDOUT:
226['one', 'two']
227-one-
228-two-
229## END
230
231
232#### for loop parsing - http://landley.net/notes.html#04-03-2020
233
234$SH -c '
235for i
236in one two three
237do echo $i;
238done
239'
240echo $?
241
242$SH -c 'for i; in one two three; do echo $i; done'
243test $? -ne 0 && echo cannot-parse
244
245## STDOUT:
246one
247two
248three
2490
250cannot-parse
251## END
252
253#### Parsing $(( ))
254# http://landley.net/notes.html#15-03-2020
255$SH -c 'echo $((echo hello))'
256if test $? -ne 0; then echo fail; fi
257## stdout: fail
258
259#### IFS - http://landley.net/notes.html#15-02-2020 (TODO: osh)
260
261IFS=x
262A=xabcxx
263for i in $A; do echo =$i=; done
264echo
265
266unset IFS
267A=" abc def "
268for i in ""$A""; do echo =$i=; done
269
270## STDOUT:
271==
272=abc=
273==
274
275==
276=abc=
277=def=
278==
279## END
280## BUG zsh status: 1
281## BUG zsh stdout-json: ""
282
283#### IFS 2 - copied into spec/word-split
284# this one appears different between osh and bash
285A=" abc def "; for i in ""x""$A""; do echo =$i=; done
286
287## STDOUT:
288=x=
289=abc=
290=def=
291==
292## END
293## BUG zsh status: 1
294## BUG zsh stdout-json: ""
295
296#### IFS 3
297IFS=x; X="onextwoxxthree"; y=$X; echo $y
298## STDOUT:
299one two three
300## END
301## BUG zsh STDOUT:
302onextwoxxthree
303## END
304
305#### IFS 4
306
307setopt SH_WORD_SPLIT
308
309IFS=x
310
311func1() {
312 echo /$*/
313 for i in $*; do echo -$i-; done
314}
315func1 "" ""
316
317echo
318
319func2() {
320 echo /"$*"/
321 for i in =$*=; do echo -$i-; done
322}
323func2 "" ""
324
325## STDOUT:
326/ /
327
328/x/
329-=-
330-=-
331## END
332## BUG bash STDOUT:
333/ /
334--
335
336/x/
337-=-
338-=-
339## END
340## BUG yash/zsh STDOUT:
341/ /
342--
343--
344
345/x/
346-=-
347-=-
348## END
349
350#### IFS 5
351cc() { for i in $*; do echo -$i-; done;}; cc "" "" "" "" ""
352cc() { echo =$1$2=;}; cc "" ""
353## STDOUT:
354==
355## END
356## BUG yash STDOUT:
357--
358--
359--
360--
361--
362==
363## END
364## BUG zsh status: 1
365## BUG zsh stdout-json: ""
366
367#### Can't parse extra }
368
369$SH -c 'for i in a"$@"b;do echo =$i=;done;}' 123 456 789
370## status: 2
371## OK mksh/zsh status: 1
372## STDOUT:
373## END
374
375#### Command Sub Syntax Error
376# http://landley.net/notes.html#28-01-2020
377
378echo $(if true)
379echo $?
380echo $(false)
381echo $?
382
383## status: 2
384## stdout-json: ""
385
386## OK mksh/zsh status: 1
387
388
389#### Pipeline - http://landley.net/notes-2019.html#16-12-2019
390echo hello | { read i; echo $i;} | { read i; echo $i;} | cat
391echo hello | while read i; do echo -=$i=- | sed s/=/@/g ; done | cat
392## STDOUT:
393hello
394-@hello@-
395## END
396