From: Jonatan Schlag Date: Sun, 16 Jun 2024 16:02:33 +0000 (+0200) Subject: test_value_in_array: Check if the key is defined X-Git-Tag: v2.29-core189~65^2~88 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=721373c3d75c0ba27f6b77e6577163820014f6d4;p=ipfire-2.x.git test_value_in_array: Check if the key is defined Signed-off-by: Jonatan Schlag Signed-off-by: Michael Tremer --- diff --git a/tests/lib.sh b/tests/lib.sh index f49a947483..af8c632cf1 100644 --- a/tests/lib.sh +++ b/tests/lib.sh @@ -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