Permalink
Browse files

Test empty arrays combined with set -u (nounset).

This design error was pointed out on help-bash.  mksh and bash both
agree on the same behavior.

Also, put empty array tests close together.
  • Loading branch information...
Andy Chu
Andy Chu committed Sep 5, 2017
1 parent ff95c8a commit 76af99b7f46383c3dc3303bd45397020eb2a2e3b
Showing with 22 additions and 11 deletions.
  1. +22 −11 spec/array.test.sh
View
@@ -15,10 +15,31 @@ a=(1 '2 3')
argv.py ${a[@]} ${a[*]}
# stdout: ['1', '2', '3', '1', '2', '3']
### Empty array tests
### 4 ways to interpolate empty array
argv.py 1 "${a[@]}" 2 ${a[@]} 3 "${a[*]}" 4 ${a[*]} 5
# stdout: ['1', '2', '3', '', '4', '5']
### empty array
empty=()
argv.py "${empty[@]}"
# stdout: []
### Empty array with :-
empty=()
argv.py ${empty[@]:-not one} "${empty[@]:-not one}"
# stdout: ['not', 'one', 'not one']
### nounset with empty array (design bug, makes it hard to use arrays)
# http://lists.gnu.org/archive/html/help-bash/2017-09/msg00005.html
# TODO: sane-arrays should get rid of this problem.
set -o nounset
empty=()
argv.py "${empty[@]}"
echo status=$?
# stdout-json: "[]\nstatus=0\n"
# BUG bash/mksh stdout-json: ""
# BUG bash/mksh status: 1
### local array
# mksh support local variables, but not local arrays, oddly.
f() {
@@ -63,11 +84,6 @@ argv.py "${a[@]}"
# status: 2
# OK mksh status: 1
### empty array
empty=()
argv.py "${empty[@]}"
# stdout: []
### array with empty string
empty=('')
argv.py "${empty[@]}"
@@ -266,11 +282,6 @@ ls foo=(1 2)
# status: 2
# OK mksh status: 1
### Empty array with :-
empty=()
argv.py ${empty[@]:-not one} "${empty[@]:-not one}"
# stdout: ['not', 'one', 'not one']
### Single array with :-
# bash does EMPTY ELISION here, unless it's double quoted. mksh has
# more sane behavior. OSH is better.

0 comments on commit 76af99b

Please sign in to comment.