Skip to content

Commit 9f89d74

Browse files
authored
[GH-13] Support for starting OPTSPEC with a space (#21)
* Refactored test_helper to support better error reporting * Added support for executing custom bin scripts by tests. * Fixed github #13
1 parent f8e8e89 commit 9f89d74

File tree

5 files changed

+186
-19
lines changed

5 files changed

+186
-19
lines changed

lib/getopts_long.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ getopts_long() {
1717
set -- "${args[@]}"
1818
fi
1919

20-
builtin getopts "${optspec_short}" "${optvar}" "${@}" || return 1
20+
builtin getopts -- "${optspec_short}" "${optvar}" "${@}" || return 1
2121
[[ "${!optvar}" == '-' ]] || return 0
2222

2323
if [[ "${OPTARG}" == *=* ]]; then

test/bats/github_13.bats

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/env bats
2+
3+
load ../test_helper
4+
export GETOPTS_LONG_TEST_BIN='getopts_long-github_13'
5+
6+
@test "${FEATURE}: long option, silent" {
7+
compare '-o user_val' \
8+
'--option user_val'
9+
}
10+
@test "${FEATURE}: long option, verbose" {
11+
compare '-o user_val' \
12+
'--option user_val'
13+
}
14+
15+
@test "${FEATURE}: long variable, silent" {
16+
compare '-v user_val' \
17+
'--variable=user_val' \
18+
'/^OPTIND: /d'
19+
expect "${bash_getopts_lines[4]}" == 'OPTIND: 3'
20+
expect "${getopts_long_lines[4]}" == 'OPTIND: 2'
21+
}
22+
@test "${FEATURE}: long variable, verbose" {
23+
compare '-v user_val' \
24+
'--variable=user_val' \
25+
'/^OPTIND: /d'
26+
expect "${bash_getopts_lines[4]}" == 'OPTIND: 3'
27+
expect "${getopts_long_lines[4]}" == 'OPTIND: 2'
28+
}
29+
30+
@test "${FEATURE}: toggle followed by long variable, silent" {
31+
compare '-t -v user_val' \
32+
'--toggle --variable=user_val' \
33+
'/^OPTIND: /d'
34+
expect "${bash_getopts_lines[5]}" == 'OPTIND: 4'
35+
expect "${getopts_long_lines[5]}" == 'OPTIND: 3'
36+
}
37+
@test "${FEATURE}: toggle followed by long variable, verbose" {
38+
compare '-t -v user_val' \
39+
'--toggle --variable=user_val' \
40+
'/^OPTIND: /d'
41+
expect "${bash_getopts_lines[5]}" == 'OPTIND: 4'
42+
expect "${getopts_long_lines[5]}" == 'OPTIND: 3'
43+
}
44+
45+
@test "${FEATURE}: long variable followed by toggle, silent" {
46+
compare '-v user_val -t' \
47+
'--variable=user_val --toggle' \
48+
'/^OPTIND: /d'
49+
expect "${bash_getopts_lines[5]}" == 'OPTIND: 4'
50+
expect "${getopts_long_lines[5]}" == 'OPTIND: 3'
51+
}
52+
@test "${FEATURE}: long variable followed by toggle, verbose" {
53+
compare '-v user_val -t' \
54+
'--variable=user_val --toggle' \
55+
'/^OPTIND: /d'
56+
expect "${bash_getopts_lines[5]}" == 'OPTIND: 4'
57+
expect "${getopts_long_lines[5]}" == 'OPTIND: 3'
58+
}
59+
60+
@test "${FEATURE}: terminator followed by long variable, silent" {
61+
compare '-t -- -v user_val' \
62+
'--toggle -- --variable=user_val' \
63+
'/^\$@: /d'
64+
expect "${bash_getopts_lines[5]}" == '$@: -v user_val'
65+
expect "${getopts_long_lines[5]}" == '$@: --variable=user_val'
66+
}
67+
@test "${FEATURE}: terminator followed by long variable, verbose" {
68+
compare '-t -- -v user_val' \
69+
'--toggle -- --variable=user_val' \
70+
'/^\$@: /d'
71+
expect "${bash_getopts_lines[5]}" == '$@: -v user_val'
72+
expect "${getopts_long_lines[5]}" == '$@: --variable=user_val'
73+
}
74+
75+
@test "${FEATURE}: long variable followed by terminator, silent" {
76+
compare '-v user_val -- -t' \
77+
'--variable=user_val -- --toggle' \
78+
'/^(OPTIND|\$@): /d'
79+
expect "${bash_getopts_lines[4]}" == 'OPTIND: 4'
80+
expect "${getopts_long_lines[4]}" == 'OPTIND: 3'
81+
expect "${bash_getopts_lines[5]}" == '$@: -t'
82+
expect "${getopts_long_lines[5]}" == '$@: --toggle'
83+
}
84+
@test "${FEATURE}: long variable followed by terminator, verbose" {
85+
compare '-v user_val -- -t' \
86+
'--variable=user_val -- --toggle' \
87+
'/^(OPTIND|\$@): /d'
88+
expect "${bash_getopts_lines[4]}" == 'OPTIND: 4'
89+
expect "${getopts_long_lines[4]}" == 'OPTIND: 3'
90+
expect "${bash_getopts_lines[5]}" == '$@: -t'
91+
expect "${getopts_long_lines[5]}" == '$@: --toggle'
92+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env bash
2+
3+
TOPDIR="$(cd "$(dirname "${0}")"/../.. && pwd)"
4+
# shellcheck disable=SC1090
5+
source "${TOPDIR}/lib/getopts_long.bash"
6+
7+
while getopts_long ': toggle option: variable:' OPTKEY; do
8+
case ${OPTKEY} in
9+
'toggle')
10+
printf 'toggle triggered'
11+
;;
12+
'option')
13+
printf 'option supplied'
14+
;;
15+
'variable')
16+
printf 'value supplied'
17+
;;
18+
'?')
19+
printf "INVALID OPTION"
20+
;;
21+
':')
22+
printf "MISSING ARGUMENT"
23+
;;
24+
*)
25+
printf "NEVER REACHED"
26+
;;
27+
esac
28+
printf ' -- '
29+
[[ -z "${OPTARG+SET}" ]] && echo 'OPTARG is unset' || echo "OPTARG=${OPTARG}"
30+
done
31+
32+
shift $(( OPTIND - 1 ))
33+
[[ "${1}" == "--" ]] && shift
34+
35+
echo "OPTERR: ${OPTERR}"
36+
echo "OPTKEY: ${OPTKEY}"
37+
echo "OPTARG: ${OPTARG}"
38+
echo "OPTIND: ${OPTIND}"
39+
echo "\$@: ${*}"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env bash
2+
3+
TOPDIR="$(cd "$(dirname "${0}")"/../.. && pwd)"
4+
# shellcheck disable=SC1090
5+
source "${TOPDIR}/lib/getopts_long.bash"
6+
7+
while getopts_long ' toggle option: variable:' OPTKEY; do
8+
case ${OPTKEY} in
9+
'toggle')
10+
printf 'toggle triggered'
11+
;;
12+
'option')
13+
printf 'option supplied'
14+
;;
15+
'variable')
16+
printf 'value supplied'
17+
;;
18+
'?')
19+
printf "INVALID OPTION or MISSING ARGUMENT"
20+
;;
21+
*)
22+
printf "NEVER REACHED"
23+
;;
24+
esac
25+
printf ' -- '
26+
[[ -z "${OPTARG+SET}" ]] && echo 'OPTARG is unset' || echo "OPTARG=${OPTARG}"
27+
done
28+
29+
shift $(( OPTIND - 1 ))
30+
[[ "${1}" == "--" ]] && shift
31+
32+
echo "OPTERR: ${OPTERR}"
33+
echo "OPTKEY: ${OPTKEY}"
34+
echo "OPTARG: ${OPTARG}"
35+
echo "OPTIND: ${OPTIND}"
36+
echo "\$@: ${*}"

test/test_helper.bash

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,29 @@ PATH="${TOPDIR}/bin:${PATH}"
99
debug() {
1010
cat >&3 <<END_OF_MESSAGE
1111
12-
EXPECTED (bash getopts)
13-
––––––––––––––––––––––––
12+
EXPECTED (eg: bash getopts)
13+
––––––––––––––---––––––––––
1414
${2}
1515
16-
RETURNED (getopts_long)
17-
––––––––––––––––––––––––
16+
RECEIVED (eg: getopts_long)
17+
––––––––––––––---––––––––––
1818
${1}
1919
2020
END_OF_MESSAGE
2121
}
2222

2323
expect() {
24-
if ! test "${@}"; then
25-
case ${1} in
26-
-[[:alpha:]])
27-
debug "[[ ${1} ACTUAL ]]" "${!#}"
28-
;;
29-
*)
30-
debug "${!#}" "${1}"
31-
;;
32-
esac
33-
return 1
34-
fi
24+
if ! test "${@}"; then
25+
case ${1} in
26+
-[[:alpha:]])
27+
debug "[[ ${1} ACTUAL ]]" "${!#}"
28+
;;
29+
*)
30+
debug "${!#}" "${1}"
31+
;;
32+
esac
33+
return 1
34+
fi
3535
}
3636

3737
compare() {
@@ -44,7 +44,7 @@ compare() {
4444
bash_getopts_status=${status}
4545
export bash_getopts_output bash_getopts_lines bash_getopts_status
4646

47-
run "getopts_long-${BATS_TEST_DESCRIPTION##* }" ${2}
47+
run "${GETOPTS_LONG_TEST_BIN:-getopts_long}-${BATS_TEST_DESCRIPTION##* }" ${2}
4848
getopts_long_output="${output}"
4949
getopts_long_lines=( "${lines[@]}" )
5050
getopts_long_status=${status}
@@ -55,6 +55,6 @@ compare() {
5555
getopts_long_output="$(echo "${getopts_long_output}" | sed -E "${3}")"
5656
fi
5757

58-
expect "${bash_getopts_output}" == "${getopts_long_output}"
59-
expect "${bash_getopts_status}" == "${getopts_long_status}"
58+
expect "${getopts_long_output}" == "${bash_getopts_output}"
59+
expect "${getopts_long_status}" == "${bash_getopts_status}"
6060
}

0 commit comments

Comments
 (0)