Skip to content

Commit

Permalink
[spec/var-op-test] Enhance tests for ${array[@]+foo}
Browse files Browse the repository at this point in the history
Part of issue #1884.
  • Loading branch information
Andy C committed Mar 28, 2024
1 parent 1ad82d6 commit 6fad299
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
1 change: 1 addition & 0 deletions osh/word_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ def _ApplyTestOp(
is_falsey = False
elif case(value_e.BashArray):
val = cast(value.BashArray, UP_val)
# TODO: allow undefined
is_falsey = len(val.strs) == 0
elif case(value_e.BashAssoc):
val = cast(value.BashAssoc, UP_val)
Expand Down
5 changes: 3 additions & 2 deletions spec/array.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ 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)
#### nounset / set -u with empty array (bug in bash 4.3, fixed in 4.4)

# http://lists.gnu.org/archive/html/help-bash/2017-09/msg00005.html
# NOTE: This used to be a bug in bash 4.3, but is fixed in bash 4.4.

set -o nounset
empty=()
argv.py "${empty[@]}"
Expand Down
43 changes: 37 additions & 6 deletions spec/var-op-test.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ argv.py "${with_icc+set}" = set
['', '=', 'set']
## END

#### ${v+foo} and ${v:+foo} when set -u
#### ${s+foo} and ${s:+foo} when set -u
set -u
v=v
echo v=${v:+foo}
Expand All @@ -118,18 +118,49 @@ v=
v=
## END

#### "${array[@]} with set -u (bash is outlier)
case $SH in dash) exit ;; esac

set -u

typeset -a empty
empty=()

echo empty /"${empty[@]}"/
echo undefined /"${undefined[@]}"/

## status: 1
## STDOUT:
empty //
## END

## BUG bash status: 0
## BUG bash STDOUT:
empty //
undefined //
## END

# empty array is unset in mksh
## BUG mksh status: 1
## BUG mksh STDOUT:
## END

## N-I dash status: 0
## N-I dash STDOUT:
## END


#### "${a[@]+foo}" and "${a[@]:+foo}" undefined, with set -u
case $SH in dash) exit ;; esac

set -u

echo array="${array[@]+foo}"
echo array="${array[@]:+foo}"
## status: 0
echo plus /"${array[@]+foo}"/
echo plus colon /"${array[@]:+foo}"/

## STDOUT:
array=
array=
plus //
plus colon //
## END

## N-I dash STDOUT:
Expand Down

0 comments on commit 6fad299

Please sign in to comment.