From 562d75c42b367282ed426eb1247022103d853c60 Mon Sep 17 00:00:00 2001 From: Dmytro Konstantinov Date: Sat, 14 Dec 2024 17:52:09 +0000 Subject: [PATCH 1/2] Added "exit status" label --- test/test_helper.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_helper.bash b/test/test_helper.bash index 0190178..8d78fc6 100644 --- a/test/test_helper.bash +++ b/test/test_helper.bash @@ -63,5 +63,5 @@ compare() { fi expect "${getopts_long_output}" == "${bash_getopts_output}" - expect "${getopts_long[0]}" == "${bash_getopts[0]}" + expect "Exit status: ${getopts_long[0]}" == "Exit status: ${bash_getopts[0]}" } From 3d04ca1447d882657b7dc771e3005a4eeeb498a0 Mon Sep 17 00:00:00 2001 From: Dmytro Konstantinov Date: Sat, 14 Dec 2024 17:52:23 +0000 Subject: [PATCH 2/2] Added missing tests to ensure `-` can be used as a long option --- test/bats/github_26a.bats | 28 ++++++++++++++ test/bats/github_26b.bats | 30 +++++++++++++++ test/bats/github_26c.bats | 22 +++++++++++ test/bin/getopts_long-github_26-silent | 38 +++++++++++++++++++ test/bin/getopts_long-github_26-verbose | 35 +++++++++++++++++ ...topts_long-longspec_with_dash_colon-silent | 38 +++++++++++++++++++ ...opts_long-longspec_with_dash_colon-verbose | 35 +++++++++++++++++ 7 files changed, 226 insertions(+) create mode 100644 test/bats/github_26a.bats create mode 100644 test/bats/github_26b.bats create mode 100644 test/bats/github_26c.bats create mode 100755 test/bin/getopts_long-github_26-silent create mode 100755 test/bin/getopts_long-github_26-verbose create mode 100755 test/bin/getopts_long-longspec_with_dash_colon-silent create mode 100755 test/bin/getopts_long-longspec_with_dash_colon-verbose diff --git a/test/bats/github_26a.bats b/test/bats/github_26a.bats new file mode 100644 index 0000000..53adac8 --- /dev/null +++ b/test/bats/github_26a.bats @@ -0,0 +1,28 @@ +#!/usr/bin/env bats + +load ../test_helper +export GETOPTS_LONG_TEST_BIN='getopts_long-longspec_with_dash' + +@test "${FEATURE}: toggle, silent" { + compare '-t' \ + '---' +} +@test "${FEATURE}: toggle, verbose" { + compare '-t' \ + '---' +} + +@test "${FEATURE}: double toggle, silent" { + compare '-tt' \ + '--- --toggle' \ + '/^OPTIND: /d' + expect "${bash_getopts[6]}" == 'OPTIND: 2' + expect "${getopts_long[6]}" == 'OPTIND: 3' +} +@test "${FEATURE}: double toggle, verbose" { + compare '-tt' \ + '--- --toggle' \ + '/^OPTIND: /d' + expect "${bash_getopts[6]}" == 'OPTIND: 2' + expect "${getopts_long[6]}" == 'OPTIND: 3' +} diff --git a/test/bats/github_26b.bats b/test/bats/github_26b.bats new file mode 100644 index 0000000..21da9eb --- /dev/null +++ b/test/bats/github_26b.bats @@ -0,0 +1,30 @@ +#!/usr/bin/env bats + +load ../test_helper +export GETOPTS_LONG_TEST_BIN='getopts_long-longspec_with_dash_colon' + +@test "${FEATURE}: option, silent" { + compare '-o user_val' \ + '--- user_val' +} +@test "${FEATURE}: option, verbose" { + compare '-o user_val' \ + '--- user_val' +} + +# geopts_long does not support option-value adjoined syntax for long options. +# Thus, let's compare it to bash getopts command that uses an invalid option. +@test "${FEATURE}: option with adjacent value, silent" { + compare '-z' \ + '---zz' \ + '/^INVALID OPTION/d' + expect "${bash_getopts[1]}" == 'INVALID OPTION -- OPTARG=z' + expect "${getopts_long[1]}" == 'INVALID OPTION -- OPTARG=-zz' +} +@test "${FEATURE}: option with adjacent value, verbose" { + compare '-z' \ + '---zz' \ + '/-verbose: illegal option --/d' + expect "${bash_getopts[1]}" =~ '-verbose: illegal option -- z' + expect "${getopts_long[1]}" =~ '-verbose: illegal option -- -zz' +} diff --git a/test/bats/github_26c.bats b/test/bats/github_26c.bats new file mode 100644 index 0000000..f1e6515 --- /dev/null +++ b/test/bats/github_26c.bats @@ -0,0 +1,22 @@ +#!/usr/bin/env bats + +load ../test_helper +export GETOPTS_LONG_TEST_BIN='getopts_long-github_26' + +@test "${FEATURE}: toggle, silent" { + compare '-t user_arg' \ + '---toggle user_arg' +} +@test "${FEATURE}: toggle, verbose" { + compare '-t user_arg' \ + '---toggle user_arg' +} + +@test "${FEATURE}: option, silent" { + compare '-o user_val' \ + '---option user_val' +} +@test "${FEATURE}: option, verbose" { + compare '-o user_val' \ + '---option user_val' +} diff --git a/test/bin/getopts_long-github_26-silent b/test/bin/getopts_long-github_26-silent new file mode 100755 index 0000000..d5d9da6 --- /dev/null +++ b/test/bin/getopts_long-github_26-silent @@ -0,0 +1,38 @@ +#!/usr/bin/env bash + +TOPDIR="$(cd "$(dirname "${0}")"/../.. && pwd)" +# shellcheck disable=SC1090 +source "${TOPDIR}/lib/getopts_long.bash" + +while getopts_long ':to:v: toggle option: variable: -toggle -option:' OPTKEY; do + case ${OPTKEY} in + 't'|'toggle'|'-toggle') + printf 'toggle triggered' + ;; + '-'|'o'|'option'|'-option') + printf 'option supplied' + ;; + 'v'|'variable') + printf 'value supplied' + ;; + '?') + printf "INVALID OPTION" + ;; + ':') + printf "MISSING ARGUMENT" + ;; + *) + printf "NEVER REACHED" + ;; + esac + printf ' -- ' + [[ -z "${OPTARG+SET}" ]] && echo 'OPTARG is unset' || echo "OPTARG=${OPTARG}" +done + +shift $(( OPTIND - 1 )) + +echo "OPTERR: ${OPTERR}" +echo "OPTKEY: ${OPTKEY}" +echo "OPTARG: ${OPTARG}" +echo "OPTIND: ${OPTIND}" +echo "\$@: ${*}" diff --git a/test/bin/getopts_long-github_26-verbose b/test/bin/getopts_long-github_26-verbose new file mode 100755 index 0000000..10f6c11 --- /dev/null +++ b/test/bin/getopts_long-github_26-verbose @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +TOPDIR="$(cd "$(dirname "${0}")"/../.. && pwd)" +# shellcheck disable=SC1090 +source "${TOPDIR}/lib/getopts_long.bash" + +while getopts_long 'to:v: toggle option: variable: -toggle -option:' OPTKEY; do + case ${OPTKEY} in + 't'|'toggle'|'-toggle') + printf 'toggle triggered' + ;; + '-'|'o'|'option'|'-option') + printf 'option supplied' + ;; + 'v'|'variable') + printf 'value supplied' + ;; + '?') + printf "INVALID OPTION or MISSING ARGUMENT" + ;; + *) + printf "NEVER REACHED" + ;; + esac + printf ' -- ' + [[ -z "${OPTARG+SET}" ]] && echo 'OPTARG is unset' || echo "OPTARG=${OPTARG}" +done + +shift $(( OPTIND - 1 )) + +echo "OPTERR: ${OPTERR}" +echo "OPTKEY: ${OPTKEY}" +echo "OPTARG: ${OPTARG}" +echo "OPTIND: ${OPTIND}" +echo "\$@: ${*}" diff --git a/test/bin/getopts_long-longspec_with_dash_colon-silent b/test/bin/getopts_long-longspec_with_dash_colon-silent new file mode 100755 index 0000000..74d1114 --- /dev/null +++ b/test/bin/getopts_long-longspec_with_dash_colon-silent @@ -0,0 +1,38 @@ +#!/usr/bin/env bash + +TOPDIR="$(cd "$(dirname "${0}")"/../.. && pwd)" +# shellcheck disable=SC1090 +source "${TOPDIR}/lib/getopts_long.bash" + +while getopts_long ':to:v: -: toggle option: variable:' OPTKEY; do + case ${OPTKEY} in + 't'|'toggle') + printf 'toggle triggered' + ;; + '-'|'o'|'option') + printf 'option supplied' + ;; + 'v'|'variable') + printf 'value supplied' + ;; + '?') + printf "INVALID OPTION" + ;; + ':') + printf "MISSING ARGUMENT" + ;; + *) + printf "NEVER REACHED" + ;; + esac + printf ' -- ' + [[ -z "${OPTARG+SET}" ]] && echo 'OPTARG is unset' || echo "OPTARG=${OPTARG}" +done + +shift $(( OPTIND - 1 )) + +echo "OPTERR: ${OPTERR}" +echo "OPTKEY: ${OPTKEY}" +echo "OPTARG: ${OPTARG}" +echo "OPTIND: ${OPTIND}" +echo "\$@: ${*}" diff --git a/test/bin/getopts_long-longspec_with_dash_colon-verbose b/test/bin/getopts_long-longspec_with_dash_colon-verbose new file mode 100755 index 0000000..b3477b3 --- /dev/null +++ b/test/bin/getopts_long-longspec_with_dash_colon-verbose @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +TOPDIR="$(cd "$(dirname "${0}")"/../.. && pwd)" +# shellcheck disable=SC1090 +source "${TOPDIR}/lib/getopts_long.bash" + +while getopts_long 'to:v: -: toggle option: variable:' OPTKEY; do + case ${OPTKEY} in + 't'|'toggle') + printf 'toggle triggered' + ;; + '-'|'o'|'option') + printf 'option supplied' + ;; + 'v'|'variable') + printf 'value supplied' + ;; + '?') + printf "INVALID OPTION or MISSING ARGUMENT" + ;; + *) + printf "NEVER REACHED" + ;; + esac + printf ' -- ' + [[ -z "${OPTARG+SET}" ]] && echo 'OPTARG is unset' || echo "OPTARG=${OPTARG}" +done + +shift $(( OPTIND - 1 )) + +echo "OPTERR: ${OPTERR}" +echo "OPTKEY: ${OPTKEY}" +echo "OPTARG: ${OPTARG}" +echo "OPTIND: ${OPTIND}" +echo "\$@: ${*}"