diff --git a/Dockerfile b/Dockerfile index 689cffd..2337312 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,6 @@ FROM debian:stable-slim AS base +ARG BUILD_SHA +ARG BUILD_DATE LABEL org.opencontainers.image.vendor="Dmytro Konstantinov" \ org.opencontainers.image.source="https://github.com/UrsaDK/getopts_long" \ org.opencontainers.image.revision="${BUILD_SHA}" \ @@ -103,6 +105,7 @@ RUN TZ=UTC git show --pretty="%H%+ad" | head -2 > ./VERSION \ && rm -Rf \ ./.git \ ./dockerfs \ + && ./bin/bats \ && ./bin/kcov WORKDIR /mnt VOLUME ["/mnt"] diff --git a/bin/bats b/bin/bats index 7ebbe0d..9052454 100755 --- a/bin/bats +++ b/bin/bats @@ -29,5 +29,4 @@ shellcheck --format="tty" --shell="bash" --check-sourced \ # Run bats without generating code coverage echo "Running tests: ${*:-"${TEST_DIR}"}" -/usr/local/bin/bats ${*:+'--tap'} --recursive "${@:-"${TEST_DIR}"}" -[[ -z "${*}" ]] && "$(dirname "${0}")/kcov" +/usr/local/bin/bats --recursive "${*:-"${TEST_DIR}"}" diff --git a/bin/docker b/bin/docker index f15772b..6364980 100755 --- a/bin/docker +++ b/bin/docker @@ -62,6 +62,7 @@ case "${COMMAND}" in ;; 'r'|'run') [[ -t 1 ]] && IT='-it' # Check if output is attached to a TTY + # shellcheck disable=SC2086 docker container run ${GITHUB_ACTIONS:+-u root} \ --rm --init ${IT} -v "${PWD}:/mnt" "${IMAGE}" "${@}" ;; @@ -72,6 +73,7 @@ case "${COMMAND}" in 'e'|'exec') [[ -t 1 ]] && IT='-it' # Check if output is attached to a TTY CONTAINER_ID="$(docker container ls -qlf "ancestor=${IMAGE}")" + #shellcheck disable=SC2086 docker container exec ${IT} "${CONTAINER_ID}" "${@}" ;; 's'|'stop') diff --git a/bin/kcov b/bin/kcov index 1408d78..cad1944 100755 --- a/bin/kcov +++ b/bin/kcov @@ -62,12 +62,14 @@ path_exists "${TEST_DIR}" TEMP_DIR="$(mktemp -d -t kcov.XXXXXXXXX)" trap 'rm -Rf ${TEMP_DIR}' EXIT -# Run kcov -echo "Generating coverage report: ${COVERAGE_DIR}/index.html" -KCOV_CMD="/usr/local/bin/kcov --clean --include-path=${LIB_DIR} ${@} \ +# Run kcov + bats +echo "Generating coverage report: ${TEMP_DIR}/bats/index.html" +echo -n "Running the whole tests suite: " +KCOV_CMD="/usr/local/bin/kcov --clean --include-path=${LIB_DIR} ${*} \ ${TEMP_DIR} /usr/local/bin/bats --recursive ${TEST_DIR}" if [[ ${EUID} -eq 0 ]]; then - KCOV_USER="$(ls -l "${0}" | awk '{print $3}')" + echo + KCOV_USER="$(find "${0}" -maxdepth 0 -printf '%u')" [[ "${KCOV_USER}" == "root" ]] && die_with_kcov_user_error chown -R "${KCOV_USER}" "${TEMP_DIR}" @@ -83,8 +85,10 @@ sed -i -E 's#/(home|mnt)/##g' \ "${TEMP_DIR}"/bats/*.{json,xml} "${TEMP_DIR}"/bats/metadata/* # Present kcov report -rm -Rf "${COVERAGE_DIR}" -cp -LR "${TEMP_DIR}/bats" "${COVERAGE_DIR}" +if rm -Rf "${COVERAGE_DIR}" 2>/dev/null; then + echo "Publishing coverage report: ${COVERAGE_DIR}/index.html"; + cp -LR "${TEMP_DIR}/bats" "${COVERAGE_DIR}" +fi # Total code coverage COVERAGE="$(jq -r .percent_covered "${COVERAGE_DIR}/coverage.json")" diff --git a/lib/getopts_long.bash b/lib/getopts_long.bash index ef4616a..99c2f79 100644 --- a/lib/getopts_long.bash +++ b/lib/getopts_long.bash @@ -20,10 +20,20 @@ getopts_long() { builtin getopts "${optspec_short}" "${optvar}" "${@}" || return 1 [[ "${!optvar}" == '-' ]] || return 0 - printf -v "${optvar}" "%s" "${OPTARG%%=*}" + if [[ "${OPTARG}" == *=* ]]; then + printf -v "${optvar}" "%s" "${OPTARG%%=*}" + else + for optspec in $(echo "${optspec_long}" | tr ' ' '\n' | sort -ur); do + if [[ "${optspec}" == *: && "${OPTARG}" == "${optspec%?}"* ]]; then + printf -v "${optvar}" "%s" "${optspec%?}" + break + fi + done + [[ "${!optvar}" == '-' ]] && printf -v "${optvar}" "%s" "${OPTARG}" + fi - if [[ "${optspec_long}" =~ (^|[[:space:]])${!optvar}:([[:space:]]|$) ]]; then - OPTARG="${OPTARG#${!optvar}}" + if [[ " ${optspec_long} " == *" ${!optvar}: "* ]]; then + OPTARG="${OPTARG#"${!optvar}"}" OPTARG="${OPTARG#=}" # Missing argument @@ -39,7 +49,7 @@ getopts_long() { unset OPTARG && printf -v "${optvar}" '?' fi fi - elif [[ "${optspec_long}" =~ (^|[[:space:]])${!optvar}([[:space:]]|$) ]]; then + elif [[ " ${optspec_long} " == *" ${!optvar} "* ]]; then unset OPTARG else # Invalid option diff --git a/test/bats/github_15.bats b/test/bats/github_15.bats new file mode 100644 index 0000000..71cd9aa --- /dev/null +++ b/test/bats/github_15.bats @@ -0,0 +1,21 @@ +#!/usr/bin/env bats + +load ../test_helper + +@test "${FEATURE}: short option, silent" { + compare '-o-- user_arg' \ + '-o-- user_arg' +} +@test "${FEATURE}: short option, verbose" { + compare '-o-- user_arg' \ + '-o-- user_arg' +} + +@test "${FEATURE}: long option, silent" { + compare '-o-- user_arg' \ + '--option-- user_arg' +} +@test "${FEATURE}: long option, verbose" { + compare '-o-- user_arg' \ + '--option-- user_arg' +} diff --git a/test/bats/invalid_arguments.bats b/test/bats/invalid_arguments.bats index cb4147c..492ecb3 100644 --- a/test/bats/invalid_arguments.bats +++ b/test/bats/invalid_arguments.bats @@ -16,8 +16,8 @@ load ../test_helper compare '-i' \ '--invalid' \ '/^INVALID OPTION -- /d' - expect "${expected_lines[0]}" == 'INVALID OPTION -- OPTARG=i' - expect "${actual_lines[0]}" == 'INVALID OPTION -- OPTARG=invalid' + expect "${bash_getopts_lines[0]}" == 'INVALID OPTION -- OPTARG=i' + expect "${getopts_long_lines[0]}" == 'INVALID OPTION -- OPTARG=invalid' } @test "${FEATURE}: long option, verbose" { compare '-i' \ @@ -41,8 +41,8 @@ load ../test_helper compare '-i user_arg' \ '--invalid user_arg' \ '/^INVALID OPTION -- /d' - expect "${expected_lines[0]}" == 'INVALID OPTION -- OPTARG=i' - expect "${actual_lines[0]}" == 'INVALID OPTION -- OPTARG=invalid' + expect "${bash_getopts_lines[0]}" == 'INVALID OPTION -- OPTARG=i' + expect "${getopts_long_lines[0]}" == 'INVALID OPTION -- OPTARG=invalid' } @test "${FEATURE}: long option, extra arguments, verbose" { compare '-i user_arg' \ @@ -55,28 +55,28 @@ load ../test_helper @test "${FEATURE}: short option, terminator, extra arguments, silent" { compare '-i -- user_arg' \ '-i -- user_arg' - expect "${actual_lines[5]}" == '$@: user_arg' + expect "${getopts_long_lines[5]}" == '$@: user_arg' } @test "${FEATURE}: short option, terminator, extra arguments, verbose" { compare '-i -- user_arg' \ '-i -- user_arg' \ 's/getopts_long-verbose/getopts-verbose/g' - expect "${actual_lines[6]}" == '$@: user_arg' + expect "${getopts_long_lines[6]}" == '$@: user_arg' } @test "${FEATURE}: long option, terminator, extra arguments, silent" { compare '-i -- user_arg' \ '--invalid -- user_arg' \ '/^INVALID OPTION -- /d' - expect "${expected_lines[0]}" == 'INVALID OPTION -- OPTARG=i' - expect "${actual_lines[0]}" == 'INVALID OPTION -- OPTARG=invalid' - expect "${actual_lines[5]}" == '$@: user_arg' + expect "${bash_getopts_lines[0]}" == 'INVALID OPTION -- OPTARG=i' + expect "${getopts_long_lines[0]}" == 'INVALID OPTION -- OPTARG=invalid' + expect "${getopts_long_lines[5]}" == '$@: user_arg' } @test "${FEATURE}: long option, terminator, extra arguments, verbose" { compare '-i -- user_arg' \ '--invalid -- user_arg' \ 's/getopts_long-verbose: (.*) invalid$/getopts-verbose: \1 i/g' - expect "${actual_lines[6]}" == '$@: user_arg' + expect "${getopts_long_lines[6]}" == '$@: user_arg' } # terminator followed by options @@ -84,24 +84,24 @@ load ../test_helper @test "${FEATURE}: terminator, short option, extra arguments, silent" { compare '-- -i user_arg' \ '-- -i user_arg' - expect "${actual_lines[4]}" == '$@: -i user_arg' + expect "${getopts_long_lines[4]}" == '$@: -i user_arg' } @test "${FEATURE}: terminator, short option, extra arguments, verbose" { compare '-- -i user_arg' \ '-- -i user_arg' \ 's/getopts_long-verbose/getopts-verbose/g' - expect "${actual_lines[4]}" == '$@: -i user_arg' + expect "${getopts_long_lines[4]}" == '$@: -i user_arg' } @test "${FEATURE}: terminator, long option, extra arguments, silent" { compare '-- -i user_arg' \ '-- --invalid user_arg' \ '/^\$@: /d' - expect "${actual_lines[4]}" == '$@: --invalid user_arg' + expect "${getopts_long_lines[4]}" == '$@: --invalid user_arg' } @test "${FEATURE}: terminator, long option, extra arguments, verbose" { compare '-- -i user_arg' \ '-- --invalid user_arg' \ '/^\$@: /d' - expect "${actual_lines[4]}" == '$@: --invalid user_arg' + expect "${getopts_long_lines[4]}" == '$@: --invalid user_arg' } diff --git a/test/bats/no_arguments.bats b/test/bats/no_arguments.bats index 6660472..95c29b4 100644 --- a/test/bats/no_arguments.bats +++ b/test/bats/no_arguments.bats @@ -21,10 +21,10 @@ load ../test_helper @test "${FEATURE}: terminator, extra arguments, silent" { compare '-- user_arg' \ '-- user_arg' - expect "${actual_lines[4]}" == '$@: user_arg' + expect "${getopts_long_lines[4]}" == '$@: user_arg' } @test "${FEATURE}: terminator, extra arguments, verbose" { compare '-- user_arg' \ '-- user_arg' - expect "${actual_lines[4]}" == '$@: user_arg' + expect "${getopts_long_lines[4]}" == '$@: user_arg' } diff --git a/test/bats/option_supplied.bats b/test/bats/option_supplied.bats index bccb796..658329f 100644 --- a/test/bats/option_supplied.bats +++ b/test/bats/option_supplied.bats @@ -45,23 +45,23 @@ load ../test_helper @test "${FEATURE}: short option, terminator, extra arguments, silent" { compare '-o user_val -- user_arg' \ '-o user_val -- user_arg' - expect "${actual_lines[5]}" == '$@: user_arg' + expect "${getopts_long_lines[5]}" == '$@: user_arg' } @test "${FEATURE}: short option, terminator, extra arguments, verbose" { compare '-o user_val -- user_arg' \ '-o user_val -- user_arg' - expect "${actual_lines[5]}" == '$@: user_arg' + expect "${getopts_long_lines[5]}" == '$@: user_arg' } @test "${FEATURE}: long option, terminator, extra arguments, silent" { compare '-o user_val -- user_arg' \ '--option user_val -- user_arg' - expect "${actual_lines[5]}" == '$@: user_arg' + expect "${getopts_long_lines[5]}" == '$@: user_arg' } @test "${FEATURE}: long option, terminator, extra arguments, verbose" { compare '-o user_val -- user_arg' \ '--option user_val -- user_arg' - expect "${actual_lines[5]}" == '$@: user_arg' + expect "${getopts_long_lines[5]}" == '$@: user_arg' } # multiple same arguments @@ -69,27 +69,27 @@ load ../test_helper @test "${FEATURE}: short option, multiple same arguments, silent" { compare '-o user_val1 -o user_val2' \ '-o user_val1 -o user_val2' - expect "${actual_lines[0]}" == 'option supplied -- OPTARG=user_val1' - expect "${actual_lines[1]}" == 'option supplied -- OPTARG=user_val2' + expect "${getopts_long_lines[0]}" == 'option supplied -- OPTARG=user_val1' + expect "${getopts_long_lines[1]}" == 'option supplied -- OPTARG=user_val2' } @test "${FEATURE}: short option, multiple same arguments, verbose" { compare '-o user_val1 -o user_val2' \ '-o user_val1 -o user_val2' - expect "${actual_lines[0]}" == 'option supplied -- OPTARG=user_val1' - expect "${actual_lines[1]}" == 'option supplied -- OPTARG=user_val2' + expect "${getopts_long_lines[0]}" == 'option supplied -- OPTARG=user_val1' + expect "${getopts_long_lines[1]}" == 'option supplied -- OPTARG=user_val2' } @test "${FEATURE}: long option, multiple same arguments, silent" { compare '-o user_val1 -o user_val2' \ '--option user_val1 --option user_val2' - expect "${actual_lines[0]}" == 'option supplied -- OPTARG=user_val1' - expect "${actual_lines[1]}" == 'option supplied -- OPTARG=user_val2' + expect "${getopts_long_lines[0]}" == 'option supplied -- OPTARG=user_val1' + expect "${getopts_long_lines[1]}" == 'option supplied -- OPTARG=user_val2' } @test "${FEATURE}: long option, multiple same arguments, verbose" { compare '-o user_val1 -o user_val2' \ '--option user_val1 --option user_val2' - expect "${actual_lines[0]}" == 'option supplied -- OPTARG=user_val1' - expect "${actual_lines[1]}" == 'option supplied -- OPTARG=user_val2' + expect "${getopts_long_lines[0]}" == 'option supplied -- OPTARG=user_val1' + expect "${getopts_long_lines[1]}" == 'option supplied -- OPTARG=user_val2' } # terminator followed by options @@ -97,25 +97,25 @@ load ../test_helper @test "${FEATURE}: terminator, short option, extra arguments, silent" { compare '-- -o user_val user_arg' \ '-- -o user_val user_arg' - expect "${actual_lines[4]}" == '$@: -o user_val user_arg' + expect "${getopts_long_lines[4]}" == '$@: -o user_val user_arg' } @test "${FEATURE}: terminator, short option, extra arguments, verbose" { compare '-- -o user_val user_arg' \ '-- -o user_val user_arg' - expect "${actual_lines[4]}" == '$@: -o user_val user_arg' + expect "${getopts_long_lines[4]}" == '$@: -o user_val user_arg' } @test "${FEATURE}: terminator, long option, extra arguments, silent" { compare '-- -o user_val user_arg' \ '-- --option user_val user_arg' \ '/^\$@: /d' - expect "${actual_lines[4]}" == '$@: --option user_val user_arg' + expect "${getopts_long_lines[4]}" == '$@: --option user_val user_arg' } @test "${FEATURE}: terminator, long option, extra arguments, verbose" { compare '-- -o user_val user_arg' \ '-- --option user_val user_arg' \ '/^\$@: /d' - expect "${actual_lines[4]}" == '$@: --option user_val user_arg' + expect "${getopts_long_lines[4]}" == '$@: --option user_val user_arg' } # option without an argument @@ -134,8 +134,8 @@ load ../test_helper compare '-o' \ '--option' \ '/^MISSING ARGUMENT -- /d' - expect "${expected_lines[0]}" == 'MISSING ARGUMENT -- OPTARG=o' - expect "${actual_lines[0]}" == 'MISSING ARGUMENT -- OPTARG=option' + expect "${bash_getopts_lines[0]}" == 'MISSING ARGUMENT -- OPTARG=o' + expect "${getopts_long_lines[0]}" == 'MISSING ARGUMENT -- OPTARG=option' } @test "${FEATURE}: long option, missing value, verbose" { compare '-o' \ @@ -182,3 +182,24 @@ load ../test_helper compare '-o =user_val' \ '--option =user_val' } + +# option with an adjoined value + +@test "${FEATURE}: short option, adjoined value, silent" { + compare '-ouser_val' \ + '-ouser_val' +} +@test "${FEATURE}: short option, adjoined value, verbose" { + compare '-ouser_val' \ + '-ouser_val' +} + +@test "${FEATURE}: long option, adjoined value, silent" { + compare '-ouser_val' \ + '--optionuser_val' +} + +@test "${FEATURE}: long option, adjoined value, verbose" { + compare '-ouser_val' \ + '--optionuser_val' +} diff --git a/test/bats/toggle_triggered.bats b/test/bats/toggle_triggered.bats index 2738982..a98e287 100644 --- a/test/bats/toggle_triggered.bats +++ b/test/bats/toggle_triggered.bats @@ -45,23 +45,23 @@ load ../test_helper @test "${FEATURE}: short option, terminator, extra arguments, silent" { compare '-t -- user_arg' \ '-t -- user_arg' - expect "${actual_lines[5]}" == '$@: user_arg' + expect "${getopts_long_lines[5]}" == '$@: user_arg' } @test "${FEATURE}: short option, terminator, extra arguments, verbose" { compare '-t -- user_arg' \ '-t -- user_arg' - expect "${actual_lines[5]}" == '$@: user_arg' + expect "${getopts_long_lines[5]}" == '$@: user_arg' } @test "${FEATURE}: long option, terminator, extra arguments, silent" { compare '-t -- user_arg' \ '--toggle -- user_arg' - expect "${actual_lines[5]}" == '$@: user_arg' + expect "${getopts_long_lines[5]}" == '$@: user_arg' } @test "${FEATURE}: long option, terminator, extra arguments, verbose" { compare '-t -- user_arg' \ '--toggle -- user_arg' - expect "${actual_lines[5]}" == '$@: user_arg' + expect "${getopts_long_lines[5]}" == '$@: user_arg' } # multiple same arguments @@ -69,27 +69,27 @@ load ../test_helper @test "${FEATURE}: short option, multiple same arguments, silent" { compare '-t -t' \ '-t -t' - expect "${actual_lines[0]}" == "${actual_lines[1]}" - expect "${actual_lines[0]}" == 'toggle triggered -- OPTARG is unset' + expect "${getopts_long_lines[0]}" == "${getopts_long_lines[1]}" + expect "${getopts_long_lines[0]}" == 'toggle triggered -- OPTARG is unset' } @test "${FEATURE}: short option, multiple same arguments, verbose" { compare '-t -t' \ '-t -t' - expect "${actual_lines[0]}" == "${actual_lines[1]}" - expect "${actual_lines[0]}" == 'toggle triggered -- OPTARG is unset' + expect "${getopts_long_lines[0]}" == "${getopts_long_lines[1]}" + expect "${getopts_long_lines[0]}" == 'toggle triggered -- OPTARG is unset' } @test "${FEATURE}: long option, multiple same arguments, silent" { compare '-t -t' \ '--toggle --toggle' - expect "${actual_lines[0]}" == "${actual_lines[1]}" - expect "${actual_lines[0]}" == 'toggle triggered -- OPTARG is unset' + expect "${getopts_long_lines[0]}" == "${getopts_long_lines[1]}" + expect "${getopts_long_lines[0]}" == 'toggle triggered -- OPTARG is unset' } @test "${FEATURE}: long option, multiple same arguments, verbose" { compare '-t -t' \ '--toggle --toggle' - expect "${actual_lines[0]}" == "${actual_lines[1]}" - expect "${actual_lines[0]}" == 'toggle triggered -- OPTARG is unset' + expect "${getopts_long_lines[0]}" == "${getopts_long_lines[1]}" + expect "${getopts_long_lines[0]}" == 'toggle triggered -- OPTARG is unset' } # terminator followed by options @@ -97,23 +97,23 @@ load ../test_helper @test "${FEATURE}: terminator, short option, extra arguments, silent" { compare '-- -t user_arg' \ '-- -t user_arg' - expect "${actual_lines[4]}" == '$@: -t user_arg' + expect "${getopts_long_lines[4]}" == '$@: -t user_arg' } @test "${FEATURE}: terminator, short option, extra arguments, verbose" { compare '-- -t user_arg' \ '-- -t user_arg' - expect "${actual_lines[4]}" == '$@: -t user_arg' + expect "${getopts_long_lines[4]}" == '$@: -t user_arg' } @test "${FEATURE}: terminator, long option, extra arguments, silent" { compare '-- -t user_arg' \ '-- --toggle user_arg' \ '/^\$@: /d' - expect "${actual_lines[4]}" == '$@: --toggle user_arg' + expect "${getopts_long_lines[4]}" == '$@: --toggle user_arg' } @test "${FEATURE}: terminator, long option, extra arguments, verbose" { compare '-- -t user_arg' \ '-- --toggle user_arg' \ '/^\$@: /d' - expect "${actual_lines[4]}" == '$@: --toggle user_arg' + expect "${getopts_long_lines[4]}" == '$@: --toggle user_arg' } diff --git a/test/bats/value_supplied.bats b/test/bats/value_supplied.bats index 7ef80b4..2ee34d4 100644 --- a/test/bats/value_supplied.bats +++ b/test/bats/value_supplied.bats @@ -15,15 +15,15 @@ load ../test_helper compare '-v user_val' \ '--variable=user_val' \ '/^OPTIND: /d' - expect "${expected_lines[4]}" == 'OPTIND: 3' - expect "${actual_lines[4]}" == 'OPTIND: 2' + expect "${bash_getopts_lines[4]}" == 'OPTIND: 3' + expect "${getopts_long_lines[4]}" == 'OPTIND: 2' } @test "${FEATURE}: long option, verbose" { compare '-v user_val' \ '--variable=user_val' \ '/^OPTIND: /d' - expect "${expected_lines[4]}" == 'OPTIND: 3' - expect "${actual_lines[4]}" == 'OPTIND: 2' + expect "${bash_getopts_lines[4]}" == 'OPTIND: 3' + expect "${getopts_long_lines[4]}" == 'OPTIND: 2' } # extra arguments @@ -41,15 +41,15 @@ load ../test_helper compare '-v user_val user_arg' \ '--variable=user_val user_arg' \ '/^OPTIND: /d' - expect "${expected_lines[4]}" == 'OPTIND: 3' - expect "${actual_lines[4]}" == 'OPTIND: 2' + expect "${bash_getopts_lines[4]}" == 'OPTIND: 3' + expect "${getopts_long_lines[4]}" == 'OPTIND: 2' } @test "${FEATURE}: long option, extra arguments, verbose" { compare '-v user_val user_arg' \ '--variable=user_val user_arg' \ '/^OPTIND: /d' - expect "${expected_lines[4]}" == 'OPTIND: 3' - expect "${actual_lines[4]}" == 'OPTIND: 2' + expect "${bash_getopts_lines[4]}" == 'OPTIND: 3' + expect "${getopts_long_lines[4]}" == 'OPTIND: 2' } # extra arguments with terminator @@ -57,29 +57,29 @@ load ../test_helper @test "${FEATURE}: short option, terminator, extra arguments, silent" { compare '-v user_val -- user_arg' \ '-v user_val -- user_arg' - expect "${actual_lines[5]}" == '$@: user_arg' + expect "${getopts_long_lines[5]}" == '$@: user_arg' } @test "${FEATURE}: short option, terminator, extra arguments, verbose" { compare '-v user_val -- user_arg' \ '-v user_val -- user_arg' - expect "${actual_lines[5]}" == '$@: user_arg' + expect "${getopts_long_lines[5]}" == '$@: user_arg' } @test "${FEATURE}: long option, terminator, extra arguments, silent" { compare '-v user_val -- user_arg' \ '--variable=user_val -- user_arg' \ '/^OPTIND: /d' - expect "${actual_lines[5]}" == '$@: user_arg' - expect "${expected_lines[4]}" == 'OPTIND: 4' - expect "${actual_lines[4]}" == 'OPTIND: 3' + expect "${getopts_long_lines[5]}" == '$@: user_arg' + expect "${bash_getopts_lines[4]}" == 'OPTIND: 4' + expect "${getopts_long_lines[4]}" == 'OPTIND: 3' } @test "${FEATURE}: long option, terminator, extra arguments, verbose" { compare '-v user_val -- user_arg' \ '--variable=user_val -- user_arg' \ '/^OPTIND: /d' - expect "${actual_lines[5]}" == '$@: user_arg' - expect "${expected_lines[4]}" == 'OPTIND: 4' - expect "${actual_lines[4]}" == 'OPTIND: 3' + expect "${getopts_long_lines[5]}" == '$@: user_arg' + expect "${bash_getopts_lines[4]}" == 'OPTIND: 4' + expect "${getopts_long_lines[4]}" == 'OPTIND: 3' } # multiple same arguments @@ -87,33 +87,33 @@ load ../test_helper @test "${FEATURE}: short option, multiple same arguments, silent" { compare '-v user_val1 -v user_val2' \ '-v user_val1 -v user_val2' - expect "${actual_lines[0]}" == 'value supplied -- OPTARG=user_val1' - expect "${actual_lines[1]}" == 'value supplied -- OPTARG=user_val2' + expect "${getopts_long_lines[0]}" == 'value supplied -- OPTARG=user_val1' + expect "${getopts_long_lines[1]}" == 'value supplied -- OPTARG=user_val2' } @test "${FEATURE}: short option, multiple same arguments, verbose" { compare '-v user_val1 -v user_val2' \ '-v user_val1 -v user_val2' - expect "${actual_lines[0]}" == 'value supplied -- OPTARG=user_val1' - expect "${actual_lines[1]}" == 'value supplied -- OPTARG=user_val2' + expect "${getopts_long_lines[0]}" == 'value supplied -- OPTARG=user_val1' + expect "${getopts_long_lines[1]}" == 'value supplied -- OPTARG=user_val2' } @test "${FEATURE}: long option, multiple same arguments, silent" { compare '-v user_val1 -v user_val2' \ '--variable=user_val1 --variable=user_val2' \ '/^OPTIND: /d' - expect "${actual_lines[0]}" == 'value supplied -- OPTARG=user_val1' - expect "${actual_lines[1]}" == 'value supplied -- OPTARG=user_val2' - expect "${expected_lines[5]}" == 'OPTIND: 5' - expect "${actual_lines[5]}" == 'OPTIND: 3' + expect "${getopts_long_lines[0]}" == 'value supplied -- OPTARG=user_val1' + expect "${getopts_long_lines[1]}" == 'value supplied -- OPTARG=user_val2' + expect "${bash_getopts_lines[5]}" == 'OPTIND: 5' + expect "${getopts_long_lines[5]}" == 'OPTIND: 3' } @test "${FEATURE}: long option, multiple same arguments, verbose" { compare '-v user_val1 -v user_val2' \ '--variable=user_val1 --variable=user_val2' \ '/^OPTIND: /d' - expect "${actual_lines[0]}" == 'value supplied -- OPTARG=user_val1' - expect "${actual_lines[1]}" == 'value supplied -- OPTARG=user_val2' - expect "${expected_lines[5]}" == 'OPTIND: 5' - expect "${actual_lines[5]}" == 'OPTIND: 3' + expect "${getopts_long_lines[0]}" == 'value supplied -- OPTARG=user_val1' + expect "${getopts_long_lines[1]}" == 'value supplied -- OPTARG=user_val2' + expect "${bash_getopts_lines[5]}" == 'OPTIND: 5' + expect "${getopts_long_lines[5]}" == 'OPTIND: 3' } # terminator followed by options @@ -121,25 +121,25 @@ load ../test_helper @test "${FEATURE}: terminator, short option, extra arguments, silent" { compare '-- -v user_val user_arg' \ '-- -v user_val user_arg' - expect "${actual_lines[4]}" == '$@: -v user_val user_arg' + expect "${getopts_long_lines[4]}" == '$@: -v user_val user_arg' } @test "${FEATURE}: terminator, short option, extra arguments, verbose" { compare '-- -v user_val user_arg' \ '-- -v user_val user_arg' - expect "${actual_lines[4]}" == '$@: -v user_val user_arg' + expect "${getopts_long_lines[4]}" == '$@: -v user_val user_arg' } @test "${FEATURE}: terminator, long option, extra arguments, silent" { compare '-- -v user_val user_arg' \ '-- --variable=user_val user_arg' \ '/^\$@: /d' - expect "${actual_lines[4]}" == '$@: --variable=user_val user_arg' + expect "${getopts_long_lines[4]}" == '$@: --variable=user_val user_arg' } @test "${FEATURE}: terminator, long option, extra arguments, verbose" { compare '-- -v user_val user_arg' \ '-- --variable=user_val user_arg' \ '/^\$@: /d' - expect "${actual_lines[4]}" == '$@: --variable=user_val user_arg' + expect "${getopts_long_lines[4]}" == '$@: --variable=user_val user_arg' } # variable without an argument @@ -158,8 +158,8 @@ load ../test_helper compare '-v' \ '--variable' \ '/^MISSING ARGUMENT -- /d' - expect "${expected_lines[0]}" == 'MISSING ARGUMENT -- OPTARG=v' - expect "${actual_lines[0]}" == 'MISSING ARGUMENT -- OPTARG=variable' + expect "${bash_getopts_lines[0]}" == 'MISSING ARGUMENT -- OPTARG=v' + expect "${getopts_long_lines[0]}" == 'MISSING ARGUMENT -- OPTARG=variable' } @test "${FEATURE}: long option, missing value, verbose" { compare '-v' \ @@ -182,15 +182,15 @@ load ../test_helper compare '-v -user_val' \ '--variable=-user_val' \ '/^OPTIND: /d' - expect "${expected_lines[4]}" == 'OPTIND: 3' - expect "${actual_lines[4]}" == 'OPTIND: 2' + expect "${bash_getopts_lines[4]}" == 'OPTIND: 3' + expect "${getopts_long_lines[4]}" == 'OPTIND: 2' } @test "${FEATURE}: long option, value starts with -, verbose" { compare '-v -user_val' \ '--variable=-user_val' \ '/^OPTIND: /d' - expect "${expected_lines[4]}" == 'OPTIND: 3' - expect "${actual_lines[4]}" == 'OPTIND: 2' + expect "${bash_getopts_lines[4]}" == 'OPTIND: 3' + expect "${getopts_long_lines[4]}" == 'OPTIND: 2' } # option with a value that start with an equals sign @@ -208,13 +208,13 @@ load ../test_helper compare '-v =user_val' \ '--variable==user_val' \ '/^OPTIND: /d' - expect "${expected_lines[4]}" == 'OPTIND: 3' - expect "${actual_lines[4]}" == 'OPTIND: 2' + expect "${bash_getopts_lines[4]}" == 'OPTIND: 3' + expect "${getopts_long_lines[4]}" == 'OPTIND: 2' } @test "${FEATURE}: long option, value starts with =, verbose" { compare '-v =user_val' \ '--variable==user_val' \ '/^OPTIND: /d' - expect "${expected_lines[4]}" == 'OPTIND: 3' - expect "${actual_lines[4]}" == 'OPTIND: 2' + expect "${bash_getopts_lines[4]}" == 'OPTIND: 3' + expect "${getopts_long_lines[4]}" == 'OPTIND: 2' } diff --git a/test/test_helper.bash b/test/test_helper.bash index 79b6941..bf7abdc 100644 --- a/test/test_helper.bash +++ b/test/test_helper.bash @@ -7,8 +7,17 @@ FEATURE="$(basename "${BATS_TEST_FILENAME}" '.bats' | tr '_' ' ')" PATH="${TOPDIR}/bin:${PATH}" debug() { - printf '\nACTUAL:\n––––––––\n%s\n' "${2}" >&3 - printf '\nEXPECTED:\n––––––––\n%s\n\n' "${1}" >&3 +cat >&3 <