Skip to content

Commit

Permalink
fix parse_commands, make sure only full matches pass
Browse files Browse the repository at this point in the history
  • Loading branch information
robstoll committed Mar 7, 2024
1 parent 63488a1 commit 66573bc
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 11 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_" "$@"
```
</utility-parse-commands>
Expand Down
2 changes: 1 addition & 1 deletion spec/utility/parse-args_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#
#

Describe 'parse_arg.sh'
Describe 'parse-arg.sh'
Include src/utility/parse-args.sh

Describe 'parseArguments'
Expand Down
117 changes: 117 additions & 0 deletions spec/utility/parse-commands_spec.sh
Original file line number Diff line number Diff line change
@@ -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
9 changes: 8 additions & 1 deletion src/utility/parse-commands.doc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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_" "$@"

21 changes: 13 additions & 8 deletions src/utility/parse-commands.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 #############
#
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit 66573bc

Please sign in to comment.