[14/21] test: Add functions test_that_array_is_defined

Message ID 20240520090611.10406-15-jonatan.schlag@ipfire.org
State New
Headers
Series [01/21] test: Add bash lib for colors |

Commit Message

Jonatan Schlag May 20, 2024, 9:06 a.m. UTC
  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>
---
 tests/lib.sh | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
  

Patch

diff --git a/tests/lib.sh b/tests/lib.sh
index 9ce0201b1..29f4e3b71 100644
--- a/tests/lib.sh
+++ b/tests/lib.sh
@@ -27,17 +27,25 @@  var_has_value() {
 	[[ "${!1}" == "${2}" ]]
 }
 
-test_that_key_in_arry_has_value() {
-	local -n array="${1}"
+test_that_array_is_defined() {
 	local arrayname="${1}"
-	local key="${2}"
-	local value="${3}"
 
 	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_that_key_in_arry_has_value() {
+	local -n array="${1}"
+	local arrayname="${1}"
+	local key="${2}"
+	local value="${3}"
 
+	test_that_array_is_defined "${arrayname}"  || return 1
 
 	if [[ "${array["${key}"]+_}" == ""  ]]; then
 		log_test_failed "The array does not contain the key '${key}', valid keys are: ${!array[*]}"