Skip to content

Commit

Permalink
fix #53 handle bash empty array correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Sylvain Viart committed Sep 15, 2021
1 parent 2844dcd commit e7d60ea
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ install: all
test: docopts
./docopts --version
go test -v
python language_agnostic_tester.py ./testee.sh
cd tests/ && bats .
python3 language_agnostic_tester.py ./testee.sh
cd ./tests/ && bats .

# README.md is composed with external source too
# Markdown hidden markup are used to insert some text form the dependancies
Expand Down
5 changes: 4 additions & 1 deletion common_input_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
"pipo",
"molo",
"toto"
]
],
"EMPTY_ARRAY" : []
},
"expect_args": [
"declare -A args",
"args['EMPTY_ARRAY,#']=0",
"args['FILE,0']='pipo'",
"args['FILE,1']='molo'",
"args['FILE,2']='toto'",
"args['FILE,#']=3"
],
"expect_global": [
"EMPTY_ARRAY=()",
"FILE=('pipo' 'molo' 'toto')"
]
},
Expand Down
1 change: 1 addition & 0 deletions deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ releases:
- fix error refusing mangling double dash in global mode [#52]
- still refuse to mangle single dash `[-]` in global mode (not compatible with v0.6.1)
- now can mangle single-dash and double-dash in `-G` mode
- fix output bash empty array #53
internal changes:
- use Go 1.17.1 for compiling
Expand Down
15 changes: 10 additions & 5 deletions docopts.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,18 @@ func To_bash(v interface{}) string {
case string:
s = fmt.Sprintf("'%s'", Shellquote(v.(string)))
case []string:
// escape all strings
arr := v.([]string)
arr_out := make([]string, len(arr))
for i, e := range arr {
arr_out[i] = Shellquote(e)
if len(arr) == 0 {
// bash emtpy array
s = "()"
} else {
// escape all strings
arr_out := make([]string, len(arr))
for i, e := range arr {
arr_out[i] = Shellquote(e)
}
s = fmt.Sprintf("('%s')", strings.Join(arr_out[:], "' '"))
}
s = fmt.Sprintf("('%s')", strings.Join(arr_out[:], "' '"))
case nil:
s = ""
default:
Expand Down
2 changes: 2 additions & 0 deletions docs/release.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Step to produce a release.
## 1. prepare the release

copy the previous `./VERSION` to `./tests/VERSION`
This is for functional test `get_doctops.bats` so it can fetch a
published release version.

```
cp ./VERSION ./tests/VERSION
Expand Down
17 changes: 17 additions & 0 deletions tests/functional_tests_docopts.bats
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,20 @@ Usage: prog dump [-]
[[ "$output" =~ $expected_regexp ]]
[[ $(echo "$output" | wc -l) -eq 9 ]]
}

# bug #53 non empty array
@test "multiple length argument returns a 0 sized array" {
# Global mode
run $DOCOPTS_BIN -h 'Usage: prog [NAME...]' :
[[ $status -eq 0 ]]
expected_regexp='NAME=\(\)'
[[ "$output" =~ $expected_regexp ]]
[[ ${#lines[@]} -eq 1 ]]

# also with -G
run $DOCOPTS_BIN -G ARGS -h 'Usage: prog [NAME...]' :
[[ $status -eq 0 ]]
expected_regexp='ARGS_NAME=\(\)'
[[ "$output" =~ $expected_regexp ]]
[[ ${#lines[@]} -eq 1 ]]
}
7 changes: 3 additions & 4 deletions tests/get_doctops.bats
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
#

_run_get_docopts() {
# go to repository basepath
cd ..
[[ $(basename $PWD) == 'docopts' ]]
# ensure we are still in tests sub-folder
[[ $(basename $PWD) == 'tests' ]]
[[ -x docopts ]] && rm docopts
run ./get_docopts.sh
run ../get_docopts.sh
echo "$output"
[[ $status -eq 0 ]]
}
Expand Down

0 comments on commit e7d60ea

Please sign in to comment.