[04/21] tests/lib.sh: Add check if variable exists to test_that_key_in_arry_has_value

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

Commit Message

Jonatan Schlag May 20, 2024, 9:05 a.m. UTC
  Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
---
 tests/lib.sh | 5 +++++
 1 file changed, 5 insertions(+)
  

Comments

Michael Tremer May 31, 2024, 9:49 a.m. UTC | #1
> On 20 May 2024, at 10:05, Jonatan Schlag <jonatan.schlag@ipfire.org> wrote:
> 
> Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
> ---
> tests/lib.sh | 5 +++++
> 1 file changed, 5 insertions(+)
> 
> diff --git a/tests/lib.sh b/tests/lib.sh
> index c51257e79..373b7c3a0 100644
> --- a/tests/lib.sh
> +++ b/tests/lib.sh
> @@ -24,6 +24,11 @@ test_that_key_in_arry_has_value() {
> local key="${2}"
> local value="${3}"
> 
> + if [[ ! -v "${array}" ]]; then
> + echo -e "${CLR_RED_BG}Test failed: The array '${1}' does not exists. The variable is not set.${CLR_RESET}'"

Typo.

You never want to use double brackets unless you have something that [ does not understand. As far as I know this is only regular expressions.

> + 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
> -- 
> 2.39.2
>
  

Patch

diff --git a/tests/lib.sh b/tests/lib.sh
index c51257e79..373b7c3a0 100644
--- a/tests/lib.sh
+++ b/tests/lib.sh
@@ -24,6 +24,11 @@  test_that_key_in_arry_has_value() {
 	local key="${2}"
 	local value="${3}"
 
+	if [[ ! -v "${array}" ]]; 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