testers.testEqualArrayOrMap: use arrayUtilities where possible

Signed-off-by: Connor Baker <ConnorBaker01@gmail.com>

Changed files
+14 -27
pkgs
build-support
+2 -8
pkgs/build-support/testers/testEqualArrayOrMap/assert-equal-array.sh
···
# shellcheck shell=bash
-
# Tests if an array is declared.
-
isDeclaredArray() {
-
# shellcheck disable=SC2034
-
local -nr arrayRef="$1" && [[ ${!arrayRef@a} =~ a ]]
-
}
-
# Asserts that two arrays are equal, printing out differences if they are not.
# Does not short circuit on the first difference.
assertEqualArray() {
···
local -nr actualArrayRef="$2"
if ! isDeclaredArray "${!expectedArrayRef}"; then
-
nixErrorLog "first arugment expectedArrayRef must be an array reference to a declared array"
+
nixErrorLog "first argument expectedArrayRef must be a reference to an indexed array"
exit 1
fi
if ! isDeclaredArray "${!actualArrayRef}"; then
-
nixErrorLog "second arugment actualArrayRef must be an array reference to a declared array"
+
nixErrorLog "second argument actualArrayRef must be a reference to an indexed array"
exit 1
fi
+8 -19
pkgs/build-support/testers/testEqualArrayOrMap/assert-equal-map.sh
···
# shellcheck shell=bash
-
# Tests if a map is declared.
-
isDeclaredMap() {
-
# shellcheck disable=SC2034
-
local -nr mapRef="$1" && [[ ${!mapRef@a} =~ A ]]
-
}
-
# Asserts that two maps are equal, printing out differences if they are not.
# Does not short circuit on the first difference.
assertEqualMap() {
···
local -nr actualMapRef="$2"
if ! isDeclaredMap "${!expectedMapRef}"; then
-
nixErrorLog "first arugment expectedMapRef must be an associative array reference to a declared associative array"
+
nixErrorLog "first argument expectedMapRef must be a reference to an associative array"
exit 1
fi
if ! isDeclaredMap "${!actualMapRef}"; then
-
nixErrorLog "second arugment actualMapRef must be an associative array reference to a declared associative array"
+
nixErrorLog "second argument actualMapRef must be a reference to an associative array"
exit 1
fi
-
# NOTE:
-
# From the `sort` manpage: "The locale specified by the environment affects sort order. Set LC_ALL=C to get the
-
# traditional sort order that uses native byte values."
-
# We specify the environment variable in a subshell to avoid polluting the caller's environment.
-
-
local -a sortedExpectedKeys
-
mapfile -d '' -t sortedExpectedKeys < <(printf '%s\0' "${!expectedMapRef[@]}" | LC_ALL=C sort --stable --zero-terminated)
-
-
local -a sortedActualKeys
-
mapfile -d '' -t sortedActualKeys < <(printf '%s\0' "${!actualMapRef[@]}" | LC_ALL=C sort --stable --zero-terminated)
-
local -ir expectedLength=${#expectedMapRef[@]}
local -ir actualLength=${#actualMapRef[@]}
···
nixErrorLog "maps differ in length: expectedMap has length $expectedLength but actualMap has length $actualLength"
hasDiff=1
fi
+
+
local -a sortedExpectedKeys=()
+
getSortedMapKeys "${!expectedMapRef}" sortedExpectedKeys
+
+
local -a sortedActualKeys=()
+
getSortedMapKeys "${!actualMapRef}" sortedActualKeys
local -i expectedKeyIdx=0
local expectedKey
+4
pkgs/build-support/testers/testEqualArrayOrMap/default.nix
···
{
+
arrayUtilities,
lib,
stdenvNoCC,
}:
···
inherit name;
nativeBuildInputs = [
+
arrayUtilities.isDeclaredArray
./assert-equal-array.sh
+
arrayUtilities.isDeclaredMap
+
arrayUtilities.getSortedMapKeys
./assert-equal-map.sh
];