mirror of
https://github.com/vincentmli/bpfire.git
synced 2026-04-28 11:43:25 +02:00
tests/lib.sh: Add check if variable exists to test_value_in_array
We cannot use [ -v ] here as this does not work. We need to check if the array is correctly declared. Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org> Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
This commit is contained in:
committed by
Michael Tremer
parent
dae8a08086
commit
035f866d75
@@ -28,9 +28,18 @@ var_has_value() {
|
|||||||
|
|
||||||
test_value_in_array() {
|
test_value_in_array() {
|
||||||
local -n array="${1}"
|
local -n array="${1}"
|
||||||
|
local arrayname="${1}"
|
||||||
local key="${2}"
|
local key="${2}"
|
||||||
local value="${3}"
|
local value="${3}"
|
||||||
|
|
||||||
|
# `declare -p` print out how the variable with the name $arrayname was declared
|
||||||
|
# If the array was not declared as indexed or associative array we fail. We cannot check for a value in an array if
|
||||||
|
# we were not given array.
|
||||||
|
if [[ ! "$(declare -p "${arrayname}")" =~ "declare -a" && ! "$(declare -p "${arrayname}")" =~ "declare -A" ]]; 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
|
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}"
|
echo -e "${CLR_GREEN_BG}Test succeded: The array '${1}' contains the value '${value}' under the key '${key}' ${CLR_RESET}"
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
Reference in New Issue
Block a user