Skip to content

Commit

Permalink
Add a failing test for dynamic parsing of assignments.
Browse files Browse the repository at this point in the history
This came up in the bash-completion project, with 'local $2 $3 $4'.  But
wrapping it in 'eval' is an easy workaround.
  • Loading branch information
Andy Chu committed Sep 22, 2018
1 parent 7f361ff commit b8c896f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
8 changes: 7 additions & 1 deletion osh/cmd_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,14 @@ def _AppendMoreEnv(preparsed_list, more_env):


def _MakeAssignment(parse_ctx, assign_kw, suffix_words):
"""Create an ast.Assignment node from a keyword and a list of words."""
"""Create an ast.Assignment node from a keyword and a list of words.
NOTE: We don't allow dynamic assignments like:
local $1
This can be replaced with eval 'local $1'
"""
# First parse flags, e.g. -r -x -a -A. None of the flags have arguments.
flags = []
n = len(suffix_words)
Expand Down
17 changes: 17 additions & 0 deletions spec/assign.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,20 @@ echo $a $b $c
## OK mksh stdout-json: ""
## OK mksh 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
2 changes: 1 addition & 1 deletion test/spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ word-eval() {
}

assign() {
sh-spec spec/assign.test.sh --osh-failures-allowed 2 \
sh-spec spec/assign.test.sh --osh-failures-allowed 3 \
${REF_SHELLS[@]} $OSH_LIST "$@"
}

Expand Down

0 comments on commit b8c896f

Please sign in to comment.