Skip to content

Commit e7d60ea

Browse files
author
Sylvain Viart
committed
fix #53 handle bash empty array correctly
1 parent 2844dcd commit e7d60ea

7 files changed

Lines changed: 39 additions & 12 deletions

File tree

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ install: all
4848
test: docopts
4949
./docopts --version
5050
go test -v
51-
python language_agnostic_tester.py ./testee.sh
52-
cd tests/ && bats .
51+
python3 language_agnostic_tester.py ./testee.sh
52+
cd ./tests/ && bats .
5353

5454
# README.md is composed with external source too
5555
# Markdown hidden markup are used to insert some text form the dependancies

common_input_test.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@
66
"pipo",
77
"molo",
88
"toto"
9-
]
9+
],
10+
"EMPTY_ARRAY" : []
1011
},
1112
"expect_args": [
1213
"declare -A args",
14+
"args['EMPTY_ARRAY,#']=0",
1315
"args['FILE,0']='pipo'",
1416
"args['FILE,1']='molo'",
1517
"args['FILE,2']='toto'",
1618
"args['FILE,#']=3"
1719
],
1820
"expect_global": [
21+
"EMPTY_ARRAY=()",
1922
"FILE=('pipo' 'molo' 'toto')"
2023
]
2124
},

deployment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ releases:
2525
- fix error refusing mangling double dash in global mode [#52]
2626
- still refuse to mangle single dash `[-]` in global mode (not compatible with v0.6.1)
2727
- now can mangle single-dash and double-dash in `-G` mode
28+
- fix output bash empty array #53
2829
2930
internal changes:
3031
- use Go 1.17.1 for compiling

docopts.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,18 @@ func To_bash(v interface{}) string {
181181
case string:
182182
s = fmt.Sprintf("'%s'", Shellquote(v.(string)))
183183
case []string:
184-
// escape all strings
185184
arr := v.([]string)
186-
arr_out := make([]string, len(arr))
187-
for i, e := range arr {
188-
arr_out[i] = Shellquote(e)
185+
if len(arr) == 0 {
186+
// bash emtpy array
187+
s = "()"
188+
} else {
189+
// escape all strings
190+
arr_out := make([]string, len(arr))
191+
for i, e := range arr {
192+
arr_out[i] = Shellquote(e)
193+
}
194+
s = fmt.Sprintf("('%s')", strings.Join(arr_out[:], "' '"))
189195
}
190-
s = fmt.Sprintf("('%s')", strings.Join(arr_out[:], "' '"))
191196
case nil:
192197
s = ""
193198
default:

docs/release.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Step to produce a release.
55
## 1. prepare the release
66

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

911
```
1012
cp ./VERSION ./tests/VERSION

tests/functional_tests_docopts.bats

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,20 @@ Usage: prog dump [-]
6565
[[ "$output" =~ $expected_regexp ]]
6666
[[ $(echo "$output" | wc -l) -eq 9 ]]
6767
}
68+
69+
# bug #53 non empty array
70+
@test "multiple length argument returns a 0 sized array" {
71+
# Global mode
72+
run $DOCOPTS_BIN -h 'Usage: prog [NAME...]' :
73+
[[ $status -eq 0 ]]
74+
expected_regexp='NAME=\(\)'
75+
[[ "$output" =~ $expected_regexp ]]
76+
[[ ${#lines[@]} -eq 1 ]]
77+
78+
# also with -G
79+
run $DOCOPTS_BIN -G ARGS -h 'Usage: prog [NAME...]' :
80+
[[ $status -eq 0 ]]
81+
expected_regexp='ARGS_NAME=\(\)'
82+
[[ "$output" =~ $expected_regexp ]]
83+
[[ ${#lines[@]} -eq 1 ]]
84+
}

tests/get_doctops.bats

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
#
55

66
_run_get_docopts() {
7-
# go to repository basepath
8-
cd ..
9-
[[ $(basename $PWD) == 'docopts' ]]
7+
# ensure we are still in tests sub-folder
8+
[[ $(basename $PWD) == 'tests' ]]
109
[[ -x docopts ]] && rm docopts
11-
run ./get_docopts.sh
10+
run ../get_docopts.sh
1211
echo "$output"
1312
[[ $status -eq 0 ]]
1413
}

0 commit comments

Comments
 (0)