]> git.ipfire.org Git - ipfire-2.x.git/commitdiff
test_value_in_array: Check if the key is defined
authorJonatan Schlag <jonatan.schlag@ipfire.org>
Sun, 16 Jun 2024 16:02:33 +0000 (18:02 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 24 Aug 2024 12:19:56 +0000 (12:19 +0000)
Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
tests/lib.sh

index f49a9474835750e9a1d36174072c8d212f7d2721..af8c632cf1bb86492e13e844fbce11e68b9054f7 100644 (file)
@@ -48,6 +48,14 @@ test_value_in_array() {
                return 1
        fi
 
+       # If key is not defined we return _
+       # If the key is defined we return nothing
+       # See also https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
+       if [[ "${array["${key}"]+_}" == ""  ]]; then
+               log_test_failed "The array does not contain the key '${key}', valid keys are: ${!array[*]}"
+               return 1
+       fi
+
        if [[ "${array[${key}]}" == "${value}" ]] ; then
                log_test_succeded "The array '${1}' contains the value '${value}' under the key '${key}'"
                return 0