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

341 lines, 181 significant
1## compare_shells: bash dash mksh zsh
2## oils_failures_allowed: 0
3
4#### can continue after unknown option
5#
6# TODO: this is the posix special builtin logic?
7# dash and mksh make this a fatal error no matter what.
8
9set -o errexit
10set -o STRICT || true # unknown option
11echo hello
12## stdout: hello
13## status: 0
14## BUG dash/mksh/zsh stdout-json: ""
15## BUG dash status: 2
16## BUG mksh/zsh status: 1
17
18#### set with both options and argv
19set -o errexit a b c
20echo "$@"
21false
22echo done
23## stdout: a b c
24## status: 1
25
26#### nounset with "$@"
27set a b c
28set -u # shouldn't touch argv
29echo "$@"
30## stdout: a b c
31
32#### set -u -- clears argv
33set a b c
34set -u -- # shouldn't touch argv
35echo "$@"
36## stdout:
37
38#### set -u -- x y z
39set a b c
40set -u -- x y z
41echo "$@"
42## stdout: x y z
43
44#### reset option with long flag
45set -o errexit
46set +o errexit
47echo "[$unset]"
48## stdout: []
49## status: 0
50
51#### reset option with short flag
52set -u
53set +u
54echo "[$unset]"
55## stdout: []
56## status: 0
57
58#### set -eu (flag parsing)
59set -eu
60echo "[$unset]"
61echo status=$?
62## stdout-json: ""
63## status: 1
64## OK dash status: 2
65
66#### set -o lists options
67# NOTE: osh doesn't use the same format yet.
68set -o | grep -o noexec
69## STDOUT:
70noexec
71## END
72
73#### 'set' and 'eval' round trip
74
75# NOTE: not testing arrays and associative arrays!
76_space='[ ]'
77_whitespace=$'[\t\r\n]'
78_sq="'single quotes'"
79_backslash_dq="\\ \""
80_unicode=$'[\u03bc]'
81
82# Save the variables
83varfile=$TMP/vars-$(basename $SH).txt
84
85set | grep '^_' > "$varfile"
86
87# Unset variables
88unset _space _whitespace _sq _backslash_dq _unicode
89echo [ $_space $_whitespace $_sq $_backslash_dq $_unicode ]
90
91# Restore them
92
93. $varfile
94echo "Code saved to $varfile" 1>&2 # for debugging
95
96test "$_space" = '[ ]' && echo OK
97test "$_whitespace" = $'[\t\r\n]' && echo OK
98test "$_sq" = "'single quotes'" && echo OK
99test "$_backslash_dq" = "\\ \"" && echo OK
100test "$_unicode" = $'[\u03bc]' && echo OK
101
102## STDOUT:
103[ ]
104OK
105OK
106OK
107OK
108OK
109## END
110
111## BUG zsh status: 1
112## BUG zsh STDOUT:
113[ ]
114## END
115
116#### set - - and so forth
117set a b
118echo "$@"
119
120set - a b
121echo "$@"
122
123set -- a b
124echo "$@"
125
126set - -
127echo "$@"
128
129set -- --
130echo "$@"
131
132# note: zsh is different, and yash is totally different
133## STDOUT:
134a b
135a b
136a b
137-
138--
139## END
140## N-I yash STDOUT:
141a b
142- a b
143a b
144- -
145--
146## END
147## BUG zsh STDOUT:
148a b
149a b
150a b
151
152--
153## END
154
155#### set - leading single dash is ignored, turns off xtrace verbose (#2364)
156
157show_options() {
158 case $- in
159 *v*) echo verbose-on ;;
160 esac
161 case $- in
162 *x*) echo xtrace-on ;;
163 esac
164}
165
166set -x -v
167show_options
168echo
169
170set - a b c
171echo "$@"
172show_options
173echo
174
175# dash that's not leading is not special
176set x - y z
177echo "$@"
178
179## STDOUT:
180verbose-on
181xtrace-on
182
183a b c
184
185x - y z
186## END
187
188## BUG zsh STDOUT:
189verbose-on
190xtrace-on
191
192a b c
193verbose-on
194xtrace-on
195
196x - y z
197## END
198
199#### set - stops option processing like set --
200case $SH in zsh) exit ;; esac
201
202show_options() {
203 case $- in
204 *v*) echo verbose-on ;;
205 esac
206 case $- in
207 *x*) echo xtrace-on ;;
208 esac
209}
210
211set -x - -v
212
213show_options
214echo argv "$@"
215
216## STDOUT:
217argv -v
218## END
219
220## N-I zsh STDOUT:
221## END
222
223#### A single + is an ignored flag; not an argument
224case $SH in zsh) exit ;; esac
225
226show_options() {
227 case $- in
228 *v*) echo verbose-on ;;
229 esac
230 case $- in
231 *x*) echo xtrace-on ;;
232 esac
233}
234
235set +
236echo plus "$@"
237
238set -x + -v x y
239show_options
240echo plus "$@"
241
242## STDOUT:
243plus
244verbose-on
245xtrace-on
246plus x y
247## END
248
249## BUG mksh STDOUT:
250plus
251xtrace-on
252plus -v x y
253## END
254
255## N-I zsh STDOUT:
256## END
257
258#### set - + and + -
259set - +
260echo "$@"
261
262set + -
263echo "$@"
264
265## STDOUT:
266+
267+
268## END
269
270## BUG mksh STDOUT:
271+
272-
273## END
274
275## OK zsh/osh STDOUT:
276+
277
278## END
279
280#### set -a exports variables
281set -a
282FOO=bar
283BAZ=qux
284printenv.py FOO BAZ
285## STDOUT:
286bar
287qux
288## END
289
290#### set +a stops exporting
291set -a
292FOO=exported
293set +a
294BAR=not_exported
295printenv.py FOO BAR
296## STDOUT:
297exported
298None
299## END
300
301#### set -o allexport (long form)
302set -o allexport
303VAR1=value1
304set +o allexport
305VAR2=value2
306printenv.py VAR1 VAR2
307## STDOUT:
308value1
309None
310## END
311
312#### variables set before set -a are not exported
313BEFORE=before_value
314set -a
315AFTER=after_value
316printenv.py BEFORE AFTER
317## STDOUT:
318None
319after_value
320## END
321
322#### set -a exports local variables
323set -a
324f() {
325 local ZZZ=zzz
326 printenv.py ZZZ
327}
328f
329## STDOUT:
330zzz
331## END
332## BUG mksh stdout: None
333
334#### set -a exports declare variables
335set -a
336declare ZZZ=zzz
337printenv.py ZZZ
338## STDOUT:
339zzz
340## END
341## N-I dash/mksh stdout: None