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

167 lines, 83 significant
1## oils_failures_allowed: 3
2## compare_shells: bash dash mksh zsh ash
3
4
5# POSIX rule about special builtins pointed at:
6#
7# https://www.reddit.com/r/oilshell/comments/5ykpi3/oildev_is_alive/
8
9#### : is special and prefix assignments persist after special builtins (set -o posix)
10case $SH in
11 dash|zsh)
12 ;;
13 *)
14 # for bash
15 set -o posix
16 ;;
17esac
18
19foo=bar :
20echo foo=$foo
21
22# Not true when you use 'builtin'
23z=Z builtin :
24echo z=$Z
25
26## STDOUT:
27foo=bar
28z=
29## END
30
31## BUG zsh STDOUT:
32foo=
33z=
34## END
35
36#### readonly is special and prefix assignments persist (set -o posix)
37
38# Bash only implements it behind the posix option
39case $SH in
40 dash|zsh)
41 ;;
42 *)
43 # for bash
44 set -o posix
45 ;;
46esac
47foo=bar readonly spam=eggs
48echo foo=$foo
49echo spam=$spam
50
51# should NOT be exported
52printenv.py foo
53printenv.py spam
54
55## STDOUT:
56foo=bar
57spam=eggs
58None
59None
60## END
61
62## OK bash/osh STDOUT:
63foo=bar
64spam=eggs
65bar
66None
67## END
68
69#### true is not special
70foo=bar true
71echo $foo
72## stdout:
73
74#### Shift is special and the whole script exits if it returns non-zero
75if test -n "$BASH_VERSION"; then
76 set -o posix
77fi
78set -- a b
79shift 3
80echo status=$?
81
82## status: 1
83## STDOUT:
84## END
85
86## OK dash status: 2
87
88## BUG bash/zsh/ash status: 0
89## BUG bash/zsh/ash STDOUT:
90status=1
91## END
92
93#### set is special and fails, even if using || true
94shopt -s invalid_ || true
95echo ok
96set -o invalid_ || true
97echo should not get here
98
99## status: 1
100## STDOUT:
101ok
102## END
103
104## OK dash status: 2
105
106## BUG bash/ash status: 0
107## BUG bash/ash STDOUT:
108ok
109should not get here
110## END
111
112#### Special builtins can't be redefined as functions (set -o posix)
113
114# bash manual says they are 'found before' functions.
115if test -n "$BASH_VERSION"; then
116 set -o posix
117fi
118
119export() {
120 echo 'export func'
121}
122export hi
123echo status=$?
124## status: 2
125## BUG mksh/zsh status: 0
126## BUG mksh/zsh STDOUT:
127status=0
128## END
129
130#### Non-special builtins CAN be redefined as functions
131test -n "$BASH_VERSION" && set -o posix
132true() {
133 echo 'true func'
134}
135true hi
136echo status=$?
137## STDOUT:
138true func
139status=0
140## END
141
142
143#### command, builtin - both can be redefined, not special (regression)
144case $SH in dash|ash) exit ;; esac
145
146builtin echo b
147command echo c
148
149builtin() {
150 echo builtin-redef "$@"
151}
152
153command() {
154 echo command-redef "$@"
155}
156
157builtin echo b
158command echo c
159
160## STDOUT:
161b
162c
163builtin-redef echo b
164command-redef echo c
165## END
166## N-I dash/ash STDOUT:
167## END