[07/21] tests/lib.sh: Fix check if array is defined

Message ID 20240520090611.10406-8-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
  -v does a bad job here. We need to use declare here.

Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
---
 tests/lib.sh | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
  

Comments

Michael Tremer May 31, 2024, 9:51 a.m. UTC | #1
> On 20 May 2024, at 10:05, Jonatan Schlag <jonatan.schlag@ipfire.org> wrote:
> 
> -v does a bad job here. We need to use declare here.
> 
> Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
> ---
> tests/lib.sh | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/tests/lib.sh b/tests/lib.sh
> index 079755410..0f4de8e43 100644
> --- a/tests/lib.sh
> +++ b/tests/lib.sh
> @@ -29,10 +29,11 @@ var_has_value() {
> 
> test_that_key_in_arry_has_value() {
> local array="${!1}"
> + local arrayname="${1}"
> local key="${2}"
> local value="${3}"
> 
> - if [[ ! -v "${array}" ]]; then
> + 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

Please comment code like this. It is hard to read and not obvious to understand.

> fi
> -- 
> 2.39.2
>
  

Patch

diff --git a/tests/lib.sh b/tests/lib.sh
index 079755410..0f4de8e43 100644
--- a/tests/lib.sh
+++ b/tests/lib.sh
@@ -29,10 +29,11 @@  var_has_value() {
 
 test_that_key_in_arry_has_value() {
 	local array="${!1}"
+	local arrayname="${1}"
 	local key="${2}"
 	local value="${3}"
 
-	if [[ ! -v "${array}" ]]; then
+	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
 	fi