From 66573bcb7f9a116332795a27cc837af373eaf475 Mon Sep 17 00:00:00 2001 From: Robert Stoll Date: Thu, 7 Mar 2024 16:40:05 +0100 Subject: [PATCH] fix parse_commands, make sure only full matches pass --- README.md | 9 ++- spec/utility/parse-args_spec.sh | 2 +- spec/utility/parse-commands_spec.sh | 117 ++++++++++++++++++++++++++++ src/utility/parse-commands.doc.sh | 9 ++- src/utility/parse-commands.sh | 21 +++-- 5 files changed, 147 insertions(+), 11 deletions(-) create mode 100644 spec/utility/parse-commands_spec.sh diff --git a/README.md b/README.md index a2aeb354..afd8a677 100644 --- a/README.md +++ b/README.md @@ -1305,7 +1305,14 @@ function sourceCommand() { sourceOnce "my-lib-$command.sh" } -parseCommands commands "$MY_LIB_VERSION" sourceCommand my_lib_ "$@" +# pass: +# 1. supported commands +# 2. version which shall be shown in --version and --help +# 3. source command, responsible to load the files +# 4. the prefix used for the commands. e.g. command show with prefix my_lib_ results in calling a +# function my_lib_show if the users wants to execute command show +# 5. arguments passed to the corresponding function +parseCommands commands "$MY_LIB_VERSION" sourceCommand "my_lib_" "$@" ``` diff --git a/spec/utility/parse-args_spec.sh b/spec/utility/parse-args_spec.sh index c5fcfcec..84b3b104 100644 --- a/spec/utility/parse-args_spec.sh +++ b/spec/utility/parse-args_spec.sh @@ -8,7 +8,7 @@ # # -Describe 'parse_arg.sh' +Describe 'parse-arg.sh' Include src/utility/parse-args.sh Describe 'parseArguments' diff --git a/spec/utility/parse-commands_spec.sh b/spec/utility/parse-commands_spec.sh new file mode 100644 index 00000000..429fbd29 --- /dev/null +++ b/spec/utility/parse-commands_spec.sh @@ -0,0 +1,117 @@ +# shellcheck shell=bash +# +# __ __ +# / /____ ___ ____ ___ ___ _/ / This script is provided to you by https://github.com/tegonal/scripts +# / __/ -_) _ `/ _ \/ _ \/ _ `/ / It is licensed under Apache License 2.0 +# \__/\__/\_, /\___/_//_/\_,_/_/ Please report bugs and contribute back your improvements +# /___/ +# +# +# the function which is responsible to load the corresponding file which contains the function of this particular command +function sourceCommand() { + local -r command=$1 + shift + echo "would source file for command $command" +} +function my_lib_login() { + echo "called my_lib_login" +} +function my_lib_self_update() { + echo "called my_lib_self_updatelogin" +} + +Describe 'parse-commands.sh' + Include src/utility/parse-commands.sh + + Describe 'parseCommands' + Describe '--help' + It 'shows command and help text' + declare commands=( + add 'command to add people to your list' + config 'manage configuration' + login '' + ) + When call parseCommands commands "v1.0.0" sourceCommand "my_lib_" --help + The status should be successful + The output line 1 should include 'Commands' + The output line 2 should include 'add' + The output line 2 should include 'command to add people to your list' + The output line 3 should include 'config' + The output line 3 should include 'manage configuration' + The output line 4 should include 'login' + The output line 6 should include '--help' + The output line 6 should include 'prints this help' + The output line 7 should include '--version' + The output line 7 should include 'prints the version of this script' + The output line 9 should include 'Version of evaluation.sh is:' + The output line 10 should include 'v1.0.0' + End + End + It '--version' + declare commands=(login '') + When call parseCommands commands "v1.2.0-RC2" sourceCommand "my_lib_" --version + The status should be successful + The output line 1 should include 'Version of dsl.sh is:' + The output line 2 should include '1.2.0-RC2' + End + Describe 'happy cases' + It 'tries to source the corresponding file and call the function' + declare commands=(login '') + When call parseCommands commands "v1.2.0-RC2" sourceCommand "my_lib_" login + The status should be successful + The output should include 'would source file for command login' + The output should include 'called my_lib_login' + End + It 'same same also if command has hyphens in it' + declare commands=(self-update '') + When call parseCommands commands "v1.2.0-RC2" sourceCommand "my_lib_" self-update + The status should be successful + The output should include 'would source file for command self-update' + The output should include 'called my_lib_self_update' + End + End + Describe 'errors' + It 'errors if no command is passed and shows help' + declare commands=(login '') + When run parseCommands commands "v1.2.0-RC2" sourceCommand "my_lib_" + The status should be failure + The stderr should include 'no command passed to evaluation.sh' + The stderr should include 'Commands' + The stderr should include 'login' + End + It 'errors if non-existing command and shows help' + # shellcheck disable=SC2034 # is passed by name to parseCommands + declare commands=(login '') + When run parseCommands commands "v1.2.0-RC2" sourceCommand "my_lib_" non-existing + The status should be failure + The stderr should include "$(printf "unknown command \033[0;36mnon-existing\033[0m")" + The stderr should include 'Commands' + The stderr should include 'login' + End + It 'errors if command differs regarding - in name' + # shellcheck disable=SC2034 # is passed by name to parseCommands + declare commands=(do-login '') + When run parseCommands commands "v1.2.0-RC2" sourceCommand "my_lib_" do-lo-gin + The status should be failure + The stderr should include "$(printf "unknown command \033[0;36mdo-lo-gin\033[0m")" + The stderr should include 'Commands' + The stderr should include 'do-login' + End + It 'errors if command is not fully matched' + # shellcheck disable=SC2034 # is passed by name to parseCommands + declare commands=( + reset "" + update "" + self-update "" # matches update but not self-update + ) + When run parseCommands commands "v1.2.0-RC2" sourceCommand "my_lib_" "selfupdate" + The status should be failure + The stderr should include "$(printf "unknown command \033[0;36mselfupdate\033[0m")" + The stderr line 3 should include 'Command' + The stderr line 4 should include 'reset' + The stderr line 5 should include 'update' + The stderr line 6 should include 'self-update' + End + End + End +End diff --git a/src/utility/parse-commands.doc.sh b/src/utility/parse-commands.doc.sh index 82167146..6c9aab1f 100644 --- a/src/utility/parse-commands.doc.sh +++ b/src/utility/parse-commands.doc.sh @@ -26,5 +26,12 @@ function sourceCommand() { sourceOnce "my-lib-$command.sh" } -parseCommands commands "$MY_LIB_VERSION" sourceCommand my_lib_ "$@" +# pass: +# 1. supported commands +# 2. version which shall be shown in --version and --help +# 3. source command, responsible to load the files +# 4. the prefix used for the commands. e.g. command show with prefix my_lib_ results in calling a +# function my_lib_show if the users wants to execute command show +# 5. arguments passed to the corresponding function +parseCommands commands "$MY_LIB_VERSION" sourceCommand "my_lib_" "$@" diff --git a/src/utility/parse-commands.sh b/src/utility/parse-commands.sh index 0be997f2..adbaf6da 100644 --- a/src/utility/parse-commands.sh +++ b/src/utility/parse-commands.sh @@ -45,7 +45,14 @@ # sourceOnce "my-lib-$command.sh" # } # -# parseCommands commands "$MY_LIB_VERSION" sourceCommand my_lib_ "$@" +# # pass: +# # 1. supported commands +# # 2. version which shall be shown in --version and --help +# # 3. source command, responsible to load the files +# # 4. the prefix used for the commands. e.g. command show with prefix my_lib_ results in calling a +# # function my_lib_show if the users wants to execute command show +# # 5. arguments passed to the corresponding function +# parseCommands commands "$MY_LIB_VERSION" sourceCommand "my_lib_" "$@" # ####### Limitations ############# # @@ -113,9 +120,8 @@ function parseCommands { shift 4 || die "could not shift by 4" if (($# < 1 )); then - logError "no command passed to %s, following the output of --help" "$(basename "${BASH_SOURCE[1]}")" - echo "" - parse_commands_printHelp parseCommands_paramArr "$version" + logError "no command passed to %s, following the output of --help\n" "$(basename "${BASH_SOURCE[1]}")" + >&2 parse_commands_printHelp parseCommands_paramArr "$version" exit 9 fi @@ -128,7 +134,7 @@ function parseCommands { arrTakeEveryX parseCommands_paramArr commandNames 2 0 local tmpRegex regex tmpRegex=$(joinByChar "|" "${commandNames[@]}") - regex="^$tmpRegex\$" + regex="^($tmpRegex)\$" local -r tmpRegex regex if [[ "$command" =~ $regex ]]; then @@ -139,9 +145,8 @@ function parseCommands { elif [[ "$command" == "--version" ]]; then printVersion "$version" else - logError "unknown command \033[0;36m%s\033[0m, following the output of --help" "$command" - echo "" - parse_commands_printHelp parseCommands_paramArr "$version" + logError "unknown command \033[0;36m%s\033[0m, following the output of --help\n" "$command" + >&2 parse_commands_printHelp parseCommands_paramArr "$version" return 1 fi }