Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[builtin/assign] Do not remove existing arrays by 'declare -a array'. #731

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions osh/builtin_assign.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,15 +377,15 @@ def Run(self, cmd_val):
flags |= state.ClearNameref

for pair in cmd_val.pairs:
if pair.rval is None:
rval = pair.rval
if rval is None and (arg.a or arg.A):
old_val = self.mem.GetVar(pair.var_name)
if arg.a:
rval = value.MaybeStrArray([]) # type: value_t
if old_val.tag_() != value_e.MaybeStrArray:
rval = value.MaybeStrArray([])
elif arg.A:
rval = value.AssocArray({})
else:
rval = None
else:
rval = pair.rval
if old_val.tag_() != value_e.AssocArray:
rval = value.AssocArray({})

rval = _ReconcileTypes(rval, arg, pair.spid)
self.mem.SetVar(lvalue.Named(pair.var_name), rval, lookup_mode, flags=flags)
Expand Down
27 changes: 27 additions & 0 deletions spec/assign.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -643,3 +643,30 @@ declare -- foo=bar
## N-I dash stderr-json: ""
## stdout-json: ""

#### declare -a arr does not remove existing arrays (OSH regression)
case $SH in (dash) exit 99;; esac # dash does not support arrays

declare -a arr
arr=(foo bar baz)
declare -a arr
echo arr:${#arr[@]}
## STDOUT:
arr:3
## END
## N-I dash status: 99
## N-I dash stdout-json: ""

#### declare -A dict does not remove existing arrays (OSH regression)
case $SH in (dash|mksh) exit 99;; esac # dash/mksh does not support associative arrays

declare -A dict
dict['foo']=hello
dict['bar']=oil
dict['baz']=world
declare -A dict
echo dict:${#dict[@]}
## STDOUT:
dict:3
## END
## N-I dash/mksh status: 99
## N-I dash/mksh stdout-json: ""