OILS / spec / builtin-special.test.sh View on Github | oils.pub

237 lines, 118 significant
1## oils_failures_allowed: 3
2## compare_shells: bash dash mksh zsh ash
3
4# POSIX rule about special builtins pointed at:
5#
6# https://www.reddit.com/r/oilshell/comments/5ykpi3/oildev_is_alive/
7
8#### Prefix assignments persist after special builtins, like : (set -o posix)
9case $SH in
10 bash)
11 set -o posix
12 ;;
13esac
14
15foo=bar :
16echo foo=$foo
17
18# Not true when you use 'builtin'
19z=Z builtin :
20echo z=$Z
21
22## STDOUT:
23foo=bar
24z=
25## END
26
27## BUG zsh STDOUT:
28foo=
29z=
30## END
31
32#### readonly is special and prefix assignments persist (set -o posix)
33
34# Bash only implements it behind the posix option
35case $SH in
36 bash)
37 # for bash
38 set -o posix
39 ;;
40esac
41foo=bar readonly spam=eggs
42echo foo=$foo
43echo spam=$spam
44
45# should NOT be exported
46printenv.py foo
47printenv.py spam
48
49## STDOUT:
50foo=bar
51spam=eggs
52None
53None
54## END
55
56## OK bash/osh STDOUT:
57foo=bar
58spam=eggs
59bar
60None
61## END
62
63#### Special builtins can't be redefined as shell functions : (set -o posix)
64case $SH in
65 bash)
66 set -o posix
67 ;;
68esac
69
70eval 'echo hi'
71
72eval() {
73 echo 'sh func' "$@"
74}
75
76eval 'echo hi'
77
78## status: 2
79## STDOUT:
80hi
81## END
82
83## BUG mksh status: 0
84## BUG mksh STDOUT:
85hi
86hi
87## END
88
89## BUG zsh status: 0
90## BUG zsh STDOUT:
91hi
92sh func echo hi
93## END
94
95#### Non-special builtins CAN be redefined as functions
96test -n "$BASH_VERSION" && set -o posix
97true() {
98 echo 'true func'
99}
100true hi
101echo status=$?
102## STDOUT:
103true func
104status=0
105## END
106
107#### true is not special; prefix assignments don't persist
108foo=bar true
109echo $foo
110## stdout:
111
112#### Shift is special and the whole script exits if it returns non-zero
113$SH -c '
114if test -n "$BASH_VERSION"; then
115 set -o posix
116fi
117set -- a b
118shift 3
119echo status=$?
120'
121if test "$?" != 0; then
122 echo 'non-zero status'
123fi
124
125## STDOUT:
126non-zero status
127## END
128
129## BUG bash/zsh/ash status: 0
130## BUG bash/zsh/ash STDOUT:
131status=1
132## END
133
134#### set is special and fails, even if using || true
135$SH -c '
136shopt -s invalid_ || true
137echo ok
138set -o invalid_ || true
139echo should not get here
140'
141if test "$?" != 0; then
142 echo 'non-zero status'
143fi
144
145## STDOUT:
146ok
147non-zero status
148## END
149
150## BUG bash/ash status: 0
151## BUG bash/ash STDOUT:
152ok
153should not get here
154## END
155
156#### bash 'type' gets confused - says 'function', but runs builtin
157case $SH in dash|mksh|zsh|ash) exit ;; esac
158
159echo TRUE
160type -t true # builtin
161true() { echo true func; }
162type -t true # now a function
163echo ---
164
165echo EVAL
166
167type -t eval # builtin
168# define function before set -o posix
169eval() { echo "$1"; }
170# bash runs the FUNCTION, but OSH finds the special builtin
171# OSH doesn't need set -o posix
172eval 'echo before posix'
173
174if test -n "$BASH_VERSION"; then
175 # this makes the eval definition invisible!
176 set -o posix
177fi
178
179eval 'echo after posix' # this is the builtin eval
180# it claims it's a function, but it's a builtin
181type -t eval
182
183# it finds the function and the special builtin
184#type -a eval
185
186## OK bash STDOUT:
187TRUE
188builtin
189function
190---
191EVAL
192builtin
193echo before posix
194after posix
195function
196## END
197
198## OK osh STDOUT:
199TRUE
200builtin
201function
202---
203EVAL
204builtin
205before posix
206after posix
207function
208## END
209
210## N-I dash/mksh/zsh/ash STDOUT:
211## END
212
213#### command, builtin - both can be redefined, not special (regression)
214case $SH in dash|ash) exit ;; esac
215
216builtin echo b
217command echo c
218
219builtin() {
220 echo builtin-redef "$@"
221}
222
223command() {
224 echo command-redef "$@"
225}
226
227builtin echo b
228command echo c
229
230## STDOUT:
231b
232c
233builtin-redef echo b
234command-redef echo c
235## END
236## N-I dash/ash STDOUT:
237## END