]> git.ipfire.org Git - ipfire-2.x.git/commitdiff
tests/lib.sh: Add check if variable exists to test_value_in_array
authorJonatan Schlag <jonatan.schlag@ipfire.org>
Sun, 16 Jun 2024 16:02:30 +0000 (18:02 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 24 Aug 2024 12:19:56 +0000 (12:19 +0000)
We cannot use [ -v ] here as this does not work. We need to check if the
array is correctly declared.

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

index 716922024219ecaa29be49888eafc57a34a8d14d..4fce151f81cf4975fbdefa97dea2904cfd1a0204 100644 (file)
@@ -28,9 +28,18 @@ var_has_value() {
 
 test_value_in_array() {
        local -n array="${1}"
+       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 
+       # we were not given array.
+       if [[ ! "$(declare  -p "${arrayname}")" =~ "declare -a" && ! "$(declare  -p "${arrayname}")" =~ "declare -A" ]]; then
+               echo -e "${CLR_RED_BG}Test failed: The array '${1}' does not exists. The variable is not set.${CLR_RESET}'"
+               return 1
+       fi
+
        if [[ "${array[${key}]}" == "${value}" ]] ; then
                echo -e "${CLR_GREEN_BG}Test succeded: The array '${1}' contains the value '${value}' under the key '${key}' ${CLR_RESET}"
                return 0