From e7d60ea17b98db2b1314d7eee7bdf3a3b80722d2 Mon Sep 17 00:00:00 2001 From: Sylvain Viart Date: Wed, 15 Sep 2021 16:17:32 +0200 Subject: [PATCH] fix #53 handle bash empty array correctly --- Makefile | 4 ++-- common_input_test.json | 5 ++++- deployment.yml | 1 + docopts.go | 15 ++++++++++----- docs/release.md | 2 ++ tests/functional_tests_docopts.bats | 17 +++++++++++++++++ tests/get_doctops.bats | 7 +++---- 7 files changed, 39 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index ac3431c..336256e 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/common_input_test.json b/common_input_test.json index bd0b6bd..3279624 100644 --- a/common_input_test.json +++ b/common_input_test.json @@ -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')" ] }, diff --git a/deployment.yml b/deployment.yml index 244a1c2..d98dc98 100644 --- a/deployment.yml +++ b/deployment.yml @@ -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 diff --git a/docopts.go b/docopts.go index 7124577..667247d 100644 --- a/docopts.go +++ b/docopts.go @@ -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: diff --git a/docs/release.md b/docs/release.md index d677080..e28ebdd 100644 --- a/docs/release.md +++ b/docs/release.md @@ -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 diff --git a/tests/functional_tests_docopts.bats b/tests/functional_tests_docopts.bats index 7f3f456..739fd8e 100644 --- a/tests/functional_tests_docopts.bats +++ b/tests/functional_tests_docopts.bats @@ -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 ]] +} diff --git a/tests/get_doctops.bats b/tests/get_doctops.bats index a3d1bd6..9157d45 100644 --- a/tests/get_doctops.bats +++ b/tests/get_doctops.bats @@ -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 ]] }