Skip to content

Commit

Permalink
exitIfVariablesNotDefined and use in parse-(fn-)args
Browse files Browse the repository at this point in the history
  • Loading branch information
robstoll committed Oct 17, 2024
1 parent ec5003e commit 061d015
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,10 @@ exitIfCommandDoesNotExist "git" "please install it via https://git-scm.com/downl
# meant to be used in a file which is sourced where a contract exists between the file which `source`s and the sourced file
exitIfVarsNotAlreadySetBySource myVar1 var2 var3
declare myVar4
exitIfVariablesNotDefined myVar4 myVar5 # would exit because myVar5 is not set
echo "myVar4 $myVar4"
```
</utility-checks>
Expand Down
24 changes: 21 additions & 3 deletions spec/utility/parse-args_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Describe 'parse-arg.sh'
Describe 'parseArguments'
Describe '--help'
It 'without examples'
# shellcheck disable=SC2034 # withoutHelp is never used but that's fine
declare version withoutHelp
declare params=(
version -v 'The version'
withoutHelp -wv ''
Expand All @@ -22,11 +24,12 @@ Describe 'parse-arg.sh'
The status should be successful
The output should include 'Parameters'
The output should include '-v'
The output should include '-wv'
The output should include 'The version'
The output should include '-wv'
The output should not include 'Examples'
End
It 'with examples'
declare version
declare params=(version -v 'The version')
When call parseArguments params "example code\non multiple lines" 'v1.0.0' --help
The status should be successful
Expand All @@ -38,14 +41,15 @@ Describe 'parse-arg.sh'
End
End
It '--version'
declare version
declare params=(version -v 'The version')
When call parseArguments params "example code\non multiple lines" "v1.2.0-RC2" --version
The status should be successful
The output should include 'v1.2.0-RC2'
End
Describe 'happy cases'
It 'does not fail if not for all parameters an argument is passed'
declare version
declare version asdf
declare params=(
version -v ''
asdf -a ''
Expand All @@ -56,7 +60,8 @@ Describe 'parse-arg.sh'
End
It 'does not pollute parent "scope"'
function foo() {
declare version
# shellcheck disable=SC2034 # asdf is not used but that's fine
declare version asdf
declare params=(
version -v ''
asdf -a ''
Expand All @@ -71,13 +76,15 @@ Describe 'parse-arg.sh'
End
Describe 'errors'
It 'not enough arguments passed'
declare version
declare params=(version -v '')
When run parseArguments params
The status should be failure
The stderr should include 'At least three arguments need to be passed to parseArguments'
End
Describe 'wrong number in params'
It 'one leftover'
declare version
declare params=(version -v '' leftOver1)
When run parseArguments params '' 'v1.0.0'--help
The status should be failure
Expand All @@ -88,6 +95,7 @@ Describe 'parse-arg.sh'
The stderr should include 'leftOver1'
End
It 'two leftovers'
declare version
declare params=(version -v '' leftOver1 leftOver2)
When run parseArguments params '' 'v1.0.0' --help
The status should be failure
Expand All @@ -106,6 +114,16 @@ Describe 'parse-arg.sh'
The stderr should include "$(printf "array \033[0;36massociativeParams\033[0m is broken")"
The stderr should include 'The first argument to parse_args_exitIfParameterDefinitionIsNotTriple needs to be a non-associative array containing parameter definitions'
End
It 'fails if variable not defined in scope'
declare version
declare params=(
version -v ''
asdf -a ''
)
When run parseArguments params 'example' 'v1.0.0' -v v0.1.0
The status should be failure
The stderr should include "$(printf "you need to define the variable \033[0;36masdf\033[0m")"
End
End
End

Expand Down
4 changes: 4 additions & 0 deletions src/utility/checks.doc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ exitIfCommandDoesNotExist "git" "please install it via https://git-scm.com/downl

# meant to be used in a file which is sourced where a contract exists between the file which `source`s and the sourced file
exitIfVarsNotAlreadySetBySource myVar1 var2 var3

declare myVar4
exitIfVariablesNotDefined myVar4 myVar5 # would exit because myVar5 is not set
echo "myVar4 $myVar4"
15 changes: 15 additions & 0 deletions src/utility/checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
# # meant to be used in a file which is sourced where a contract exists between the file which `source`s and the sourced file
# exitIfVarsNotAlreadySetBySource myVar1 var2 var3
#
# declare myVar4
# exitIfVariablesNotDefined myVar4 myVar5 # would exit because myVar5 is not set
# echo "myVar4 $myVar4"
#
###################################
set -euo pipefail
shopt -s inherit_errexit
Expand Down Expand Up @@ -295,3 +299,14 @@ function exitIfVarsNotAlreadySetBySource() {
fi
done
}

function exitIfVariablesNotDefined() {
shift 1 || die "could not shift by 1"
for variableName in "$@"; do
if ! declare -p "$variableName" 2>/dev/null | grep -q 'declare --'; then
logError "you need to define the variable \033[0;36m%s\033[0m otherwise we write to the global scope" "$variableName"
printStackTrace
exit 1
fi
done
}
5 changes: 5 additions & 0 deletions src/utility/parse-args.sh
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ function parseArgumentsInternal {

parse_args_exitIfParameterDefinitionIsNotTriple parseArguments_paramArr

# shellcheck disable=SC2034 # passed by name to exitIfVariablesNotDefined
local -a parseArguments_variableNames
arrTakeEveryX parseArguments_paramArr parseArguments_variableNames 3 0
exitIfVariablesNotDefined "${parseArguments_variableNames[@]}"

local -ri parseArguments_arrLength="${#parseArguments_paramArr[@]}"

local -i parseArguments_numOfArgumentsParsed=0
Expand Down
2 changes: 2 additions & 0 deletions src/utility/parse-fn-args.sh
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ function parseFnArgs() {
exit 9
fi

exitIfVariablesNotDefined "${parseFnArgs_paramArr1[@]}"

for ((parseFnArgs_i = 0; parseFnArgs_i < parseFnArgs_minExpected; ++parseFnArgs_i)); do
local parseFnArgs_name=${parseFnArgs_paramArr1[parseFnArgs_i]}
# assign arguments to specified variables
Expand Down

0 comments on commit 061d015

Please sign in to comment.