/
assign.test.sh
471 lines (422 loc) · 7.11 KB
/
assign.test.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#!/usr/bin/env bash
#### Env value doesn't persist
FOO=foo printenv.py FOO
echo -$FOO-
## STDOUT:
foo
--
## END
#### Env value with equals
FOO=foo=foo printenv.py FOO
## stdout: foo=foo
#### Env binding can use preceding bindings, but not subsequent ones
# This means that for ASSIGNMENT_WORD, on the RHS you invoke the parser again!
# Could be any kind of quoted string.
FOO="foo" BAR="[$FOO][$BAZ]" BAZ=baz printenv.py FOO BAR BAZ
## STDOUT:
foo
[foo][]
baz
## BUG mksh STDOUT:
foo
[][]
baz
## END
#### Env value with two quotes
FOO='foo'"adjacent" printenv.py FOO
## stdout: fooadjacent
#### Env value with escaped <
FOO=foo\<foo printenv.py FOO
## stdout: foo<foo
#### FOO=foo echo [foo]
FOO=foo echo "[$foo]"
## stdout: []
#### FOO=foo func
func() {
echo "[$FOO]"
}
FOO=foo func
## stdout: [foo]
#### Multiple temporary envs on the stack
g() {
echo "$F" "$G1" "$G2"
echo '--- g() ---'
P=p printenv.py F G1 G2 A P
}
f() {
# NOTE: G1 doesn't pick up binding f, but G2 picks up a.
# I don't quite understand why this is, but bash and OSH agree!
G1=[$f] G2=[$a] g
echo '--- f() ---'
printenv.py F G1 G2 A P
}
a=A
F=f f
## STDOUT:
f [] [A]
--- g() ---
f
[]
[A]
None
p
--- f() ---
f
None
None
None
None
## END
## OK mksh STDOUT:
# G1 and G2 somehow persist. I think that is a bug. They should be local to
# the G call.
f [] [A]
--- g() ---
f
[]
[A]
None
p
--- f() ---
f
[]
[A]
None
None
## END
## BUG dash STDOUT:
# dash sets even less stuff. Doesn't appear correct.
f [] [A]
--- g() ---
None
None
None
None
p
--- f() ---
None
None
None
None
None
## END
#### Escaped = in command name
# foo=bar is in the 'spec/bin' dir.
foo\=bar
## stdout: HI
#### Env binding not allowed before compound command
# bash gives exit code 2 for syntax error, because of 'do'.
# dash gives 0 because there is stuff after for? Should really give an error.
# mksh gives acceptable error of 1.
FOO=bar for i in a b; do printenv.py $FOO; done
## BUG dash status: 0
## OK mksh/zsh status: 1
## status: 2
#### Trying to run keyword 'for'
FOO=bar for
## status: 127
## OK zsh status: 1
#### Empty env binding
EMPTY= printenv.py EMPTY
## stdout:
#### Assignment doesn't do word splitting
words='one two'
a=$words
argv.py "$a"
## stdout: ['one two']
#### Assignment doesn't do glob expansion
touch _tmp/z.Z _tmp/zz.Z
a=_tmp/*.Z
argv.py "$a"
## stdout: ['_tmp/*.Z']
#### Env binding in readonly/declare disallowed
# I'm disallowing this in the oil shell, because it doesn't work in bash!
# (v=None vs v=foo)
# assert status 2 for parse error, but allow stdout v=None/status 0 for
# existing implementations.
FOO=foo readonly v=$(printenv.py FOO)
echo "v=$v"
## OK bash/dash/mksh/zsh stdout: v=None
## OK bash/dash/mksh/zsh status: 0
## status: 2
#### assignments / array assignments not interpreted after 'echo'
a=1 echo b[0]=2 c=3
## stdout: b[0]=2 c=3
# zsh interprets [0] as some kind of glob
## OK zsh stdout-json: ""
## OK zsh status: 1
#### dynamic local variables
f() {
local "$1" # Only x is assigned here
echo -$x-
echo -$a-
local $1 # x and a are assigned here
echo -$x-
echo -$a-
}
f 'x=y a=b'
## STDOUT:
-y a=b-
--
-y-
-b-
## END
# zsh doesn't do word splitting
## OK zsh STDOUT:
-y a=b-
--
-y a=b-
--
## END
#### 'local x' does not set variable
set -o nounset
f() {
local x
echo $x
}
f
## status: 1
## OK dash status: 2
## BUG zsh status: 0
#### 'local -a x' does not set variable
set -o nounset
f() {
local -a x
echo $x
}
f
## status: 1
## OK dash status: 2
## BUG zsh status: 0
#### 'local x' and then array assignment
f() {
local x
x[3]=foo
echo ${x[3]}
}
f
## status: 0
## stdout: foo
## N-I dash status: 2
## N-I dash stdout-json: ""
## BUG zsh stdout: o
#### 'declare -A' and then dict assignment
declare -A foo
key=bar
foo["$key"]=value
echo ${foo["bar"]}
## status: 0
## stdout: value
## N-I dash status: 2
## N-I dash stdout-json: ""
## N-I mksh status: 1
## N-I mksh stdout-json: ""
#### declare in an if statement
# bug caught by my feature detection snippet in bash-completion
if ! foo=bar; then
echo BAD
fi
echo $foo
if ! eval 'spam=eggs'; then
echo BAD
fi
echo $spam
## STDOUT:
bar
eggs
## END
#### Modify a temporary binding
# (regression for bug found by Michael Greenberg)
f() {
echo "x before = $x"
x=$((x+1))
echo "x after = $x"
}
x=5 f
## STDOUT:
x before = 5
x after = 6
## END
#### Reveal existence of "temp frame" (All shells disagree here!!!)
f() {
echo "x=$x"
x=mutated-temp # mutate temp frame
echo "x=$x"
# Declare a new local
local x='local'
echo "x=$x"
# Unset it
unset x
echo "x=$x"
}
x=global
x=temp-binding f
echo "x=$x"
## STDOUT:
x=temp-binding
x=mutated-temp
x=local
x=
x=global
## END
## BUG dash STDOUT:
x=temp-binding
x=mutated-temp
x=local
x=
x=mutated-temp
## END
## BUG bash STDOUT:
x=temp-binding
x=mutated-temp
x=local
x=global
x=global
## END
## BUG mksh STDOUT:
x=temp-binding
x=mutated-temp
x=local
x=mutated-temp
x=mutated-temp
## END
## BUG yash STDOUT:
# yash has no locals
x=temp-binding
x=mutated-temp
x=mutated-temp
x=
x=
## END
#### Test above without 'local' (which is not POSIX)
f() {
echo "x=$x"
x=mutated-temp # mutate temp frame
echo "x=$x"
# Unset it
unset x
echo "x=$x"
}
x=global
x=temp-binding f
echo "x=$x"
## STDOUT:
x=temp-binding
x=mutated-temp
x=
x=global
## END
## BUG dash/mksh/yash STDOUT:
x=temp-binding
x=mutated-temp
x=
x=
## END
## BUG bash STDOUT:
x=temp-binding
x=mutated-temp
x=global
x=global
## END
#### Using ${x-default} after unsetting local shadowing a global
f() {
echo "x=$x"
local x='local'
echo "x=$x"
unset x
echo "- operator = ${x-default}"
echo ":- operator = ${x:-default}"
}
x=global
f
## STDOUT:
x=global
x=local
- operator = default
:- operator = default
## END
## BUG mksh STDOUT:
x=global
x=local
- operator = global
:- operator = global
## END
#### Using ${x-default} after unsetting a temp binding shadowing a global
f() {
echo "x=$x"
local x='local'
echo "x=$x"
unset x
echo "- operator = ${x-default}"
echo ":- operator = ${x:-default}"
}
x=global
x=temp-binding f
## STDOUT:
x=temp-binding
x=local
- operator = default
:- operator = default
## END
## BUG mksh STDOUT:
x=temp-binding
x=local
- operator = temp-binding
:- operator = temp-binding
## END
## BUG bash STDOUT:
x=temp-binding
x=local
- operator = global
:- operator = global
## END
#### static assignment doesn't split
words='a b c'
export ex=$words
glo=$words
readonly ro=$words
argv.py "$ex" "$glo" "$ro"
## STDOUT:
['a b c', 'a b c', 'a b c']
## END
## BUG dash STDOUT:
['a', 'a b c', 'a']
## END
#### aliased assignment doesn't split
shopt -s expand_aliases || true
words='a b c'
alias e=export
alias r=readonly
e ex=$words
r ro=$words
argv.py "$ex" "$ro"
## BUG dash STDOUT:
['a', 'a']
## END
## STDOUT:
['a b c', 'a b c']
## END
#### assignment using dynamic keyword splits
words='a b c'
e=export
r=readonly
$e ex=$words
$r ro=$words
argv.py "$ex" "$ro"
## STDOUT:
['a', 'a']
## END
# zsh is smart?? How does it do this?
## BUG zsh STDOUT:
['a b c', 'a b c']
## END
#### assignment using dynamic var names doesn't split
words='a b c'
arg_ex=ex=$words
arg_ro=ro=$words
export "$arg_ex"
readonly "$arg_ro"
argv.py "$ex" "$ro"
## STDOUT:
['a b c', 'a b c']
## END