]> git.ipfire.org Git - ipfire-2.x.git/commitdiff
test: Add functions test_that_array_is_defined
authorJonatan Schlag <jonatan.schlag@ipfire.org>
Sun, 16 Jun 2024 16:02:35 +0000 (18:02 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 24 Aug 2024 12:19:56 +0000 (12:19 +0000)
we need this check in multiple places so it makes sense to move this to
a separate function.

Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
tests/lib.sh

index 4110ed2d8976b18bccbb0caea2b6ead3e47e0803..32481b6a52bb94b6f12fa67f3dd341d6d49c9a41 100644 (file)
@@ -34,11 +34,8 @@ var_has_value() {
        [[ "${!1}" == "${2}" ]]
 }
 
-test_value_in_array() {
-       local -n array="${1}"
+test_that_array_is_defined() {
        local arrayname="${1}"
-       local key="${2}"
-       local value="${3}"
 
        # `declare -p` print out how the variable with the name $arrayname was declared
        # If the array was not declared as indexed or associative array we fail. We cannot check for a value in an array if 
@@ -46,7 +43,19 @@ test_value_in_array() {
        if [[ ! "$(declare  -p "${arrayname}")" =~ "declare -a" && ! "$(declare  -p "${arrayname}")" =~ "declare -A" ]]; then
                log_test_failed "The array '${1}' does not exists. The variable is not set."
                return 1
+       else
+               log_test_succeded "The array '${1}' is defined."
+               return 0
        fi
+}
+
+test_value_in_array() {
+       local -n array="${1}"
+       local arrayname="${1}"
+       local key="${2}"
+       local value="${3}"
+
+       test_that_array_is_defined "${arrayname}"  || return 1
 
        # If key is not defined we return _
        # If the key is defined we return nothing