From 37f62dd0f389105a94324158a7b1d2ca1cdfea12 Mon Sep 17 00:00:00 2001 From: Dmytro Konstantinov Date: Sun, 8 Dec 2024 19:53:46 +0000 Subject: [PATCH 1/6] Refactored internal variable names used in tests --- test/bats/invalid_arguments.bats | 28 +++++------ test/bats/no_arguments.bats | 4 +- test/bats/option_supplied.bats | 36 +++++++------- test/bats/toggle_triggered.bats | 32 ++++++------ test/bats/value_supplied.bats | 84 ++++++++++++++++---------------- test/test_helper.bash | 37 ++++++++------ 6 files changed, 115 insertions(+), 106 deletions(-) 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..b5cda42 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' \ 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 < Date: Sun, 8 Dec 2024 20:24:20 +0000 Subject: [PATCH 2/6] Fixed github #15 --- lib/getopts_long.bash | 12 +++++++++++- test/bats/github_15.bats | 22 ++++++++++++++++++++++ test/bats/option_supplied.bats | 21 +++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 test/bats/github_15.bats diff --git a/lib/getopts_long.bash b/lib/getopts_long.bash index b99ae04..9e0f469 100644 --- a/lib/getopts_long.bash +++ b/lib/getopts_long.bash @@ -20,7 +20,17 @@ 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}"}" diff --git a/test/bats/github_15.bats b/test/bats/github_15.bats new file mode 100644 index 0000000..11aa1f5 --- /dev/null +++ b/test/bats/github_15.bats @@ -0,0 +1,22 @@ +#!/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/option_supplied.bats b/test/bats/option_supplied.bats index b5cda42..658329f 100644 --- a/test/bats/option_supplied.bats +++ b/test/bats/option_supplied.bats @@ -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' +} From 91a37ef01b3e2bcd3706f03426fc2bf91f6d6a0f Mon Sep 17 00:00:00 2001 From: Dmytro Konstantinov Date: Tue, 10 Dec 2024 11:53:35 +0000 Subject: [PATCH 3/6] Replaced regex with globing to improve performance --- lib/getopts_long.bash | 4 ++-- test/bats/github_15.bats | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/getopts_long.bash b/lib/getopts_long.bash index 9e0f469..99c2f79 100644 --- a/lib/getopts_long.bash +++ b/lib/getopts_long.bash @@ -32,7 +32,7 @@ getopts_long() { [[ "${!optvar}" == '-' ]] && printf -v "${optvar}" "%s" "${OPTARG}" fi - if [[ "${optspec_long}" =~ (^|[[:space:]])${!optvar}:([[:space:]]|$) ]]; then + if [[ " ${optspec_long} " == *" ${!optvar}: "* ]]; then OPTARG="${OPTARG#"${!optvar}"}" OPTARG="${OPTARG#=}" @@ -49,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 index 11aa1f5..71cd9aa 100644 --- a/test/bats/github_15.bats +++ b/test/bats/github_15.bats @@ -15,7 +15,6 @@ load ../test_helper compare '-o-- user_arg' \ '--option-- user_arg' } - @test "${FEATURE}: long option, verbose" { compare '-o-- user_arg' \ '--option-- user_arg' From 53c6aac7876f7434b9152abfc37e16e4a8295ad2 Mon Sep 17 00:00:00 2001 From: Dmytro Konstantinov Date: Tue, 10 Dec 2024 17:02:39 +0000 Subject: [PATCH 4/6] Refactored test_helper to support better error reporting --- test/test_helper.bash | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/test_helper.bash b/test/test_helper.bash index bf7abdc..6646898 100644 --- a/test/test_helper.bash +++ b/test/test_helper.bash @@ -9,12 +9,12 @@ PATH="${TOPDIR}/bin:${PATH}" debug() { cat >&3 < Date: Tue, 10 Dec 2024 17:05:38 +0000 Subject: [PATCH 5/6] Added support for executing custom bin scripts by tests This allows us to customize spec definition for different scenarios. --- test/bin/getopts_long-github_13-silent | 39 +++++++++++++++++++++++++ test/bin/getopts_long-github_13-verbose | 36 +++++++++++++++++++++++ test/test_helper.bash | 2 +- 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100755 test/bin/getopts_long-github_13-silent create mode 100755 test/bin/getopts_long-github_13-verbose diff --git a/test/bin/getopts_long-github_13-silent b/test/bin/getopts_long-github_13-silent new file mode 100755 index 0000000..13b178f --- /dev/null +++ b/test/bin/getopts_long-github_13-silent @@ -0,0 +1,39 @@ +#!/usr/bin/env bash + +TOPDIR="$(cd "$(dirname "${0}")"/../.. && pwd)" +# shellcheck disable=SC1090 +source "${TOPDIR}/lib/getopts_long.bash" + +while getopts_long ': toggle option: variable:' OPTKEY; do + case ${OPTKEY} in + 'toggle') + printf 'toggle triggered' + ;; + 'option') + printf 'option supplied' + ;; + '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 )) +[[ "${1}" == "--" ]] && shift + +echo "OPTERR: ${OPTERR}" +echo "OPTKEY: ${OPTKEY}" +echo "OPTARG: ${OPTARG}" +echo "OPTIND: ${OPTIND}" +echo "\$@: ${*}" diff --git a/test/bin/getopts_long-github_13-verbose b/test/bin/getopts_long-github_13-verbose new file mode 100755 index 0000000..9bc2841 --- /dev/null +++ b/test/bin/getopts_long-github_13-verbose @@ -0,0 +1,36 @@ +#!/usr/bin/env bash + +TOPDIR="$(cd "$(dirname "${0}")"/../.. && pwd)" +# shellcheck disable=SC1090 +source "${TOPDIR}/lib/getopts_long.bash" + +while getopts_long ' toggle option: variable:' OPTKEY; do + case ${OPTKEY} in + 'toggle') + printf 'toggle triggered' + ;; + 'option') + printf 'option supplied' + ;; + '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 )) +[[ "${1}" == "--" ]] && shift + +echo "OPTERR: ${OPTERR}" +echo "OPTKEY: ${OPTKEY}" +echo "OPTARG: ${OPTARG}" +echo "OPTIND: ${OPTIND}" +echo "\$@: ${*}" diff --git a/test/test_helper.bash b/test/test_helper.bash index 6646898..54e8cd0 100644 --- a/test/test_helper.bash +++ b/test/test_helper.bash @@ -44,7 +44,7 @@ compare() { bash_getopts_status=${status} export bash_getopts_output bash_getopts_lines bash_getopts_status - run "getopts_long-${BATS_TEST_DESCRIPTION##* }" ${2} + run "${GETOPTS_LONG_TEST_BIN:-getopts_long}-${BATS_TEST_DESCRIPTION##* }" ${2} getopts_long_output="${output}" getopts_long_lines=( "${lines[@]}" ) getopts_long_status=${status} From 47f8f071f4e9633e3e0cc1e0eecbf0a1683062a5 Mon Sep 17 00:00:00 2001 From: Dmytro Konstantinov Date: Tue, 10 Dec 2024 17:09:09 +0000 Subject: [PATCH 6/6] Fixed github #13 Thanks @AndrewDDavis for suggesting [the fix](https://github.com/UrsaDK/getopts_long/issues/13#issuecomment-2463839302)! --- lib/getopts_long.bash | 2 +- test/bats/github_13.bats | 92 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 test/bats/github_13.bats diff --git a/lib/getopts_long.bash b/lib/getopts_long.bash index 99c2f79..1a9187a 100644 --- a/lib/getopts_long.bash +++ b/lib/getopts_long.bash @@ -17,7 +17,7 @@ getopts_long() { set -- "${args[@]}" fi - builtin getopts "${optspec_short}" "${optvar}" "${@}" || return 1 + builtin getopts -- "${optspec_short}" "${optvar}" "${@}" || return 1 [[ "${!optvar}" == '-' ]] || return 0 if [[ "${OPTARG}" == *=* ]]; then diff --git a/test/bats/github_13.bats b/test/bats/github_13.bats new file mode 100644 index 0000000..16a5d3d --- /dev/null +++ b/test/bats/github_13.bats @@ -0,0 +1,92 @@ +#!/usr/bin/env bats + +load ../test_helper +export GETOPTS_LONG_TEST_BIN='getopts_long-github_13' + +@test "${FEATURE}: long option, silent" { + compare '-o user_val' \ + '--option user_val' +} +@test "${FEATURE}: long option, verbose" { + compare '-o user_val' \ + '--option user_val' +} + +@test "${FEATURE}: long variable, silent" { + compare '-v user_val' \ + '--variable=user_val' \ + '/^OPTIND: /d' + expect "${bash_getopts_lines[4]}" == 'OPTIND: 3' + expect "${getopts_long_lines[4]}" == 'OPTIND: 2' +} +@test "${FEATURE}: long variable, verbose" { + compare '-v user_val' \ + '--variable=user_val' \ + '/^OPTIND: /d' + expect "${bash_getopts_lines[4]}" == 'OPTIND: 3' + expect "${getopts_long_lines[4]}" == 'OPTIND: 2' +} + +@test "${FEATURE}: toggle followed by long variable, silent" { + compare '-t -v user_val' \ + '--toggle --variable=user_val' \ + '/^OPTIND: /d' + expect "${bash_getopts_lines[5]}" == 'OPTIND: 4' + expect "${getopts_long_lines[5]}" == 'OPTIND: 3' +} +@test "${FEATURE}: toggle followed by long variable, verbose" { + compare '-t -v user_val' \ + '--toggle --variable=user_val' \ + '/^OPTIND: /d' + expect "${bash_getopts_lines[5]}" == 'OPTIND: 4' + expect "${getopts_long_lines[5]}" == 'OPTIND: 3' +} + +@test "${FEATURE}: long variable followed by toggle, silent" { + compare '-v user_val -t' \ + '--variable=user_val --toggle' \ + '/^OPTIND: /d' + expect "${bash_getopts_lines[5]}" == 'OPTIND: 4' + expect "${getopts_long_lines[5]}" == 'OPTIND: 3' +} +@test "${FEATURE}: long variable followed by toggle, verbose" { + compare '-v user_val -t' \ + '--variable=user_val --toggle' \ + '/^OPTIND: /d' + expect "${bash_getopts_lines[5]}" == 'OPTIND: 4' + expect "${getopts_long_lines[5]}" == 'OPTIND: 3' +} + +@test "${FEATURE}: terminator followed by long variable, silent" { + compare '-t -- -v user_val' \ + '--toggle -- --variable=user_val' \ + '/^\$@: /d' + expect "${bash_getopts_lines[5]}" == '$@: -v user_val' + expect "${getopts_long_lines[5]}" == '$@: --variable=user_val' +} +@test "${FEATURE}: terminator followed by long variable, verbose" { + compare '-t -- -v user_val' \ + '--toggle -- --variable=user_val' \ + '/^\$@: /d' + expect "${bash_getopts_lines[5]}" == '$@: -v user_val' + expect "${getopts_long_lines[5]}" == '$@: --variable=user_val' +} + +@test "${FEATURE}: long variable followed by terminator, silent" { + compare '-v user_val -- -t' \ + '--variable=user_val -- --toggle' \ + '/^(OPTIND|\$@): /d' + expect "${bash_getopts_lines[4]}" == 'OPTIND: 4' + expect "${getopts_long_lines[4]}" == 'OPTIND: 3' + expect "${bash_getopts_lines[5]}" == '$@: -t' + expect "${getopts_long_lines[5]}" == '$@: --toggle' +} +@test "${FEATURE}: long variable followed by terminator, verbose" { + compare '-v user_val -- -t' \ + '--variable=user_val -- --toggle' \ + '/^(OPTIND|\$@): /d' + expect "${bash_getopts_lines[4]}" == 'OPTIND: 4' + expect "${getopts_long_lines[4]}" == 'OPTIND: 3' + expect "${bash_getopts_lines[5]}" == '$@: -t' + expect "${getopts_long_lines[5]}" == '$@: --toggle' +}