OILS / spec / var-ref.test.sh View on Github | oils.pub

768 lines, 464 significant
1## compare_shells: bash
2## oils_failures_allowed: 1
3
4# Var refs are done with ${!a}
5#
6# local/declare -n is tested in spec/named-ref.test.sh.
7#
8# http://stackoverflow.com/questions/16461656/bash-how-to-pass-array-as-an-argument-to-a-function
9
10#### var ref ${!a}
11a=b
12b=c
13echo ref ${!a} ${a}
14## stdout: ref c b
15
16#### ${!ref-default}
17ref=x
18echo x=${!ref-default}
19
20x=''
21echo x=${!ref-default}
22
23x=foo
24echo x=${!ref-default}
25
26## STDOUT:
27x=default
28x=
29x=foo
30## END
31
32#### ${!undef:-}
33# bash 4.4 gives empty string, but I feel like this could be an error
34echo undef=${!undef-'default'}
35echo undef=${!undef}
36
37set -u
38echo NOUNSET
39echo undef=${!undef-'default'}
40echo undef=${!undef}
41
42## status: 1
43## STDOUT:
44NOUNSET
45## END
46
47# Bash 4.4 had been generating an empty string, but it was fixed in Bash 5.0.
48#
49# ## BUG bash STDOUT:
50# undef=default
51# undef=
52# NOUNSET
53# undef=default
54# ## END
55
56#### comparison to ${!array[@]} keys (similar SYNTAX)
57
58declare -a a=(x y)
59argv.py "${!a[@]}"
60echo a_keys=$?
61
62argv.py "${!a}" # missing [] is equivalent to ${!a[0]} ?
63echo a_nobrackets=$?
64
65echo ---
66declare -A A=([A]=a [B]=b)
67
68argv.py $(printf '%s\n' ${!A[@]} | sort)
69echo A_keys=$?
70
71(argv.py "${!A}") # missing [] is equivalent to ${!A[0]} ?
72echo A_nobrackets=$?
73
74## STDOUT:
75['0', '1']
76a_keys=0
77['']
78a_nobrackets=0
79---
80['A', 'B']
81A_keys=0
82A_nobrackets=1
83## END
84
85## BUG bash STDOUT:
86['0', '1']
87a_keys=0
88['']
89a_nobrackets=0
90---
91['A', 'B']
92A_keys=0
93['']
94A_nobrackets=0
95## END
96
97#### ${!a[@]-'default'} is legal but fails with more than one element
98
99# bash allows this construct, but the indirection fails when the array has more
100# than one element because the variable name contains a space. OSH originally
101# made it an error unconditionally because [@] implies it's an array, so the
102# behavior has been different from Bash when the array has a single element.
103# We now changed it to follow Bash even when the array has a single element.
104
105(argv.py "${!a[@]-default}")
106echo status=$?
107
108a=(x y z)
109(argv.py "${!a[@]-default}")
110echo status=$?
111## status: 0
112## STDOUT:
113status=1
114status=1
115## END
116
117# Bash 4.4 had been generating an empty string for ${!undef[@]-}, but this was
118# fixed in Bash 5.0.
119#
120# ## BUG bash status: 0
121# ## BUG bash STDOUT:
122# ['default']
123# status=0
124# status=1
125# ## END
126
127
128#### var ref to $@ with @
129set -- one two
130ref='@'
131echo ref=${!ref}
132## STDOUT:
133ref=one two
134## END
135
136#### var ref to $1 and $2 with 1 and 2
137set -- one two
138ref1='1'
139echo ref1=${!ref1}
140ref2='2'
141echo ref2=${!ref2}
142
143## STDOUT:
144ref1=one
145ref2=two
146## END
147
148#### var ref: 1, @, *
149set -- x y
150ref=1; argv.py "${!ref}"
151ref=@; argv.py "${!ref}"
152ref=*; argv.py "${!ref}" # maybe_decay_array bug?
153
154## STDOUT:
155['x']
156['x', 'y']
157['x y']
158## END
159
160#### var ref to special var BASH_SOURCE
161ref='LINENO'
162echo lineno=${!ref}
163## STDOUT:
164lineno=2
165## END
166
167#### var ref to $? with '?'
168myfunc() {
169 local ref=$1
170 echo ${!ref}
171}
172myfunc FUNCNAME
173myfunc '?'
174## STDOUT:
175myfunc
1760
177## END
178
179
180#### Var ref, then assignment with ${ := }
181z=zz
182zz=
183echo ${!z:=foo}
184echo ${!z:=bar}
185## STDOUT:
186foo
187foo
188## END
189
190#### Var ref, then error with ${ ? }
191w=ww
192ww=
193echo ${!w:?'my message'}
194echo done
195## status: 1
196## STDOUT:
197## END
198
199#### Indirect expansion, THEN suffix operators
200
201check_eq() {
202 [ "$1" = "$2" ] || { echo "$1 vs $2"; }
203}
204check_expand() {
205 val=$(eval "echo \"$1\"")
206 [ "$val" = "$2" ] || { echo "$1 -> expected $2, got $val"; }
207}
208check_err() {
209 e="$1"
210 msg=$(eval "$e" 2>&1) && echo "bad success: $e"
211 if test -n "$2"; then
212 if [[ "$msg" != $2 ]]; then
213 echo "Expected error: $e"
214 echo "Got error : $msg"
215 fi
216 fi
217}
218# Nearly everything in manual section 3.5.3 "Shell Parameter Expansion"
219# is allowed after a !-indirection.
220#
221# Not allowed: any further prefix syntax.
222x=xx; xx=aaabcc
223xd=x
224check_err '${!!xd}'
225check_err '${!!x*}'
226a=(asdf x)
227check_err '${!!a[*]}'
228check_err '${!#x}'
229check_err '${!#a[@]}'
230# And an array reference binds tighter in the syntax, so goes first;
231# there's no way to spell "indirection, then array reference".
232check_expand '${!a[1]}' xx
233b=(aoeu a)
234check_expand '${!b[1]}' asdf # i.e. like !(b[1]), not (!b)[1]
235#
236# Allowed: apparently everything else.
237y=yy; yy=
238check_expand '${!y:-foo}' foo
239check_expand '${!x:-foo}' aaabcc
240
241check_expand '${!x:?oops}' aaabcc
242
243check_expand '${!y:+foo}' ''
244check_expand '${!x:+foo}' foo
245
246check_expand '${!x:2}' abcc
247check_expand '${!x:2:2}' ab
248
249check_expand '${!x#*a}' aabcc
250check_expand '${!x%%c*}' aaab
251check_expand '${!x/a*b/d}' dcc
252
253# ^ operator not fully implemented in OSH
254#check_expand '${!x^a}' Aaabcc
255
256p=pp; pp='\$ '
257check_expand '${!p@P}' '$ '
258echo ok
259## stdout: ok
260
261#### var ref OF array var -- silent a[0] decay
262declare -a a=(ale bean)
263echo first=${!a}
264
265ale=zzz
266echo first=${!a}
267
268## status: 0
269## STDOUT:
270first=
271first=zzz
272## END
273
274#### array ref
275
276declare -a array=(ale bean)
277ref='array[0]'
278echo ${!ref}
279## status: 0
280## STDOUT:
281ale
282## END
283
284#### array ref with strict_array
285shopt -s strict_array
286
287declare -a array=(ale bean)
288ref='array'
289echo ${!ref}
290## status: 1
291## stdout-json: ""
292## N-I bash status: 0
293## N-I bash STDOUT:
294ale
295## END
296
297#### var ref TO array var
298shopt -s compat_array
299
300declare -a array=(ale bean)
301
302ref='array' # when compat_array is on, this is like array[0]
303ref_AT='array[@]'
304
305echo ${!ref}
306echo ${!ref_AT}
307
308## STDOUT:
309ale
310ale bean
311## END
312
313#### var ref TO array var, with subscripts
314f() {
315 argv.py "${!1}"
316}
317f 'nonexistent[0]'
318array=(x y z)
319f 'array[0]'
320f 'array[1+1]'
321f 'array[@]'
322f 'array[*]'
323# Also associative arrays.
324## STDOUT:
325['']
326['x']
327['z']
328['x', 'y', 'z']
329['x y z']
330## END
331
332#### var ref TO assoc array a[key]
333shopt -s compat_array
334
335declare -A assoc=([ale]=bean [corn]=dip)
336ref=assoc
337#ref_AT='assoc[@]'
338
339# UNQUOTED doesn't work with Oil's parser
340#ref_SUB='assoc[ale]'
341ref_SUB='assoc["ale"]'
342
343ref_SUB_QUOTED='assoc["al"e]'
344
345ref_SUB_BAD='assoc["bad"]'
346
347echo ref=${!ref} # compat_array: assoc is equivalent to assoc[0]
348#echo ref_AT=${!ref_AT}
349echo ref_SUB=${!ref_SUB}
350echo ref_SUB_QUOTED=${!ref_SUB_QUOTED}
351echo ref_SUB_BAD=${!ref_SUB_BAD}
352
353## STDOUT:
354ref=
355ref_SUB=bean
356ref_SUB_QUOTED=bean
357ref_SUB_BAD=
358## END
359
360#### var ref TO array with arbitrary subscripts
361shopt -s eval_unsafe_arith compat_array
362
363f() {
364 local val=$(echo "${!1}")
365 if test "$val" = y; then
366 echo "works: $1"
367 fi
368}
369# Warmup: nice plain array reference
370a=(x y)
371f 'a[1]'
372#
373# Not allowed:
374# no brace expansion
375f 'a[{1,0}]' # operand expected
376# no process substitution (but see command substitution below!)
377f 'a[<(echo x)]' # operand expected
378# TODO word splitting seems interesting
379aa="1 0"
380f 'a[$aa]' # 1 0: syntax error in expression (error token is "0")
381# no filename globbing
382f 'a[b*]' # operand expected
383f 'a[1"]' # bad substitution
384#
385# Allowed: most everything else in section 3.5 "Shell Expansions".
386# shell parameter expansion
387b=1
388f 'a[$b]'
389f 'a[${c:-1}]'
390# (... and presumably most of the other features there)
391# command substitution, yikes!
392f 'a[$(echo 1)]'
393# arithmetic expansion
394f 'a[$(( 3 - 2 ))]'
395
396# All of these are undocumented and probably shouldn't exist,
397# though it's always possible some will turn up in the wild and
398# we'll end up implementing them.
399
400## STDOUT:
401works: a[1]
402works: a[$b]
403works: a[${c:-1}]
404works: a[$(echo 1)]
405works: a[$(( 3 - 2 ))]
406## END
407
408#### Bizarre tilde expansion in array index
409a=(x y)
410PWD=1
411ref='a[~+]'
412echo ${!ref}
413## status: 1
414
415# Bash 4.4 had a bug, which was fixed in Bash 5.0.
416#
417# ## BUG bash status: 0
418# ## BUG bash STDOUT:
419# y
420# ## END
421
422#### Indirect expansion TO fancy expansion features bash disallows
423
424check_indir() {
425 result="${!1}"
426 desugared_result=$(eval 'echo "${'"$1"'}"')
427 [ "$2" = "$desugared_result" ] || { echo "$1 $desugared_result"; }
428}
429x=y
430y=a
431a=(x y)
432declare -A aa
433aa=([k]=r [l]=s)
434# malformed array indexing
435check_indir "a[0"
436check_indir "aa[k"
437# double indirection
438check_indir "!x" a
439check_indir "!a[0]" y
440# apparently everything else in the manual under "Shell Parameter Expansion"
441check_indir "x:-foo" y
442check_indir "x:=foo" y
443check_indir "x:?oops" y
444check_indir "x:+yy" yy
445check_indir "x:0" y
446check_indir "x:0:1" y
447check_indir "!a@" "a aa"
448# (!a[@] is elsewhere)
449check_indir "#x" 1
450check_indir "x#y"
451check_indir "x/y/foo" foo
452check_indir "x@Q" y
453echo done
454## status: 0
455## stdout: done
456
457#### Bad var ref
458a='bad var name'
459echo ref ${!a}
460echo status=$?
461
462## STDOUT:
463status=1
464## END
465
466#### Bad var ref 2
467b='/' # really bad
468echo ref ${!b}
469echo status=$?
470## STDOUT:
471status=1
472## END
473
474#### ${!OPTIND} (used by bash completion
475set -- a b c
476echo ${!OPTIND}
477f() {
478 local OPTIND=1
479 echo ${!OPTIND}
480 local OPTIND=2
481 echo ${!OPTIND}
482}
483f x y z
484## STDOUT:
485a
486x
487y
488## END
489
490#### var ref doesn't need cycle detection
491x=y
492y=x
493echo cycle=${!x}
494
495typeset -n a=b
496typeset -n b=a
497echo cycle=${a}
498## status: 1
499## STDOUT:
500cycle=x
501## END
502## OK bash status: 0
503## OK bash STDOUT:
504cycle=x
505cycle=
506## END
507
508#### Var Ref Code Injection $(tee PWNED)
509
510typeset -a a
511a=(42)
512
513x='a[$(echo 0 | tee PWNED)]'
514
515echo ${!x}
516
517if test -f PWNED; then
518 echo PWNED
519 cat PWNED
520else
521 echo NOPE
522fi
523
524## status: 1
525## STDOUT:
526## END
527
528## BUG bash status: 0
529## BUG bash STDOUT:
53042
531PWNED
5320
533## END
534
535#### ${!array_ref:-set} and ${!array_ref:=assign}
536
537ref='a[@]'
538a=('' '' '')
539
540echo "==== check ===="
541
542argv.py "${!ref:-set}"
543argv.py "${a[@]:-set}"
544
545echo "==== assign ===="
546
547argv.py "${!ref:=assign}"
548argv.py "${!ref}"
549a=('' '' '') # revert the state in case it is modified
550
551argv.py "${a[@]:=assign}"
552argv.py "${a[@]}"
553
554## STDOUT:
555==== check ====
556['', '', '']
557['', '', '']
558==== assign ====
559['', '', '']
560['', '', '']
561['', '', '']
562['', '', '']
563## END
564
565#### Array indirect expansion with suffix operators
566
567declare -A ref=(['dummy']=v1)
568function test-suffixes {
569 echo "==== $1 ===="
570 ref['dummy']=$1
571 argv.py "${!ref[@]:2}"
572 argv.py "${!ref[@]:1:2}"
573 argv.py "${!ref[@]:-empty}"
574 argv.py "${!ref[@]:+set}"
575 argv.py "${!ref[@]:=assign}"
576}
577
578v1=value
579test-suffixes v1
580echo "v1=$v1"
581
582v2=
583test-suffixes v2
584echo "v2=$v2"
585
586a1=()
587test-suffixes a1
588argv.py "${a1[@]}"
589
590a2=(element)
591test-suffixes 'a2[0]'
592argv.py "${a2[@]}"
593
594a3=(1 2 3)
595test-suffixes 'a3[@]'
596argv.py "${a3[@]}"
597
598## STDOUT:
599==== v1 ====
600['lue']
601['al']
602['value']
603['set']
604['value']
605v1=value
606==== v2 ====
607['']
608['']
609['empty']
610['']
611['assign']
612v2=assign
613==== a1 ====
614['']
615['']
616['empty']
617['']
618['assign']
619['assign']
620==== a2[0] ====
621['ement']
622['le']
623['element']
624['set']
625['element']
626['element']
627==== a3[@] ====
628['3']
629['2', '3']
630['1', '2', '3']
631['set']
632['1', '2', '3']
633['1', '2', '3']
634## END
635
636#### Array indirect expansion with replacements
637
638declare -A ref=(['dummy']=v1)
639function test-rep {
640 echo "==== $1 ===="
641 ref['dummy']=$1
642 argv.py "${!ref[@]#?}"
643 argv.py "${!ref[@]%?}"
644 argv.py "${!ref[@]//[a-f]}"
645 argv.py "${!ref[@]//[a-f]/x}"
646}
647
648v1=value
649test-rep v1
650
651v2=
652test-rep v2
653
654a1=()
655test-rep a1
656
657a2=(element)
658test-rep 'a2[0]'
659
660a3=(1 2 3)
661test-rep 'a3[@]'
662
663## STDOUT:
664==== v1 ====
665['alue']
666['valu']
667['vlu']
668['vxlux']
669==== v2 ====
670['']
671['']
672['']
673['']
674==== a1 ====
675['']
676['']
677['']
678['']
679==== a2[0] ====
680['lement']
681['elemen']
682['lmnt']
683['xlxmxnt']
684==== a3[@] ====
685['', '', '']
686['', '', '']
687['1', '2', '3']
688['1', '2', '3']
689## END
690
691#### Array indirect expansion with @? conversion
692
693declare -A ref=(['dummy']=v1)
694function test-op0 {
695 echo "==== $1 ===="
696 ref['dummy']=$1
697 argv.py "${!ref[@]@Q}"
698 argv.py "${!ref[@]@P}"
699 argv.py "${!ref[@]@a}"
700}
701
702v1=value
703test-op0 v1
704
705v2=
706test-op0 v2
707
708a1=()
709test-op0 a1
710
711a2=(element)
712test-op0 'a2[0]'
713
714a3=(1 2 3)
715test-op0 'a3[@]'
716
717## STDOUT:
718==== v1 ====
719['value']
720['value']
721['']
722==== v2 ====
723["''"]
724['']
725['']
726==== a1 ====
727['']
728['']
729['a']
730==== a2[0] ====
731['element']
732['element']
733['a']
734==== a3[@] ====
735['1', '2', '3']
736['1', '2', '3']
737['a', 'a', 'a']
738## END
739
740# Bash 4.4 had a bug in the section "==== a3[@] ====":
741#
742# ==== a3[@] ====
743# []
744# []
745# []
746
747## BUG bash STDOUT:
748==== v1 ====
749["'value'"]
750['value']
751['']
752==== v2 ====
753["''"]
754['']
755['']
756==== a1 ====
757['']
758['']
759['a']
760==== a2[0] ====
761["'element'"]
762['element']
763['a']
764==== a3[@] ====
765["'1'", "'2'", "'3'"]
766['1', '2', '3']
767['a', 'a', 'a']
768## END