diff --git a/pkgs/build-support/testers/testEqualArrayOrMap/assert-equal-array.sh b/pkgs/build-support/testers/testEqualArrayOrMap/assert-equal-array.sh index ef43dedba625..b8c292ffed5e 100644 --- a/pkgs/build-support/testers/testEqualArrayOrMap/assert-equal-array.sh +++ b/pkgs/build-support/testers/testEqualArrayOrMap/assert-equal-array.sh @@ -1,11 +1,5 @@ # 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() { @@ -19,12 +13,12 @@ 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 diff --git a/pkgs/build-support/testers/testEqualArrayOrMap/assert-equal-map.sh b/pkgs/build-support/testers/testEqualArrayOrMap/assert-equal-map.sh index b601f9e424e9..1469f94565dd 100644 --- a/pkgs/build-support/testers/testEqualArrayOrMap/assert-equal-map.sh +++ b/pkgs/build-support/testers/testEqualArrayOrMap/assert-equal-map.sh @@ -1,11 +1,5 @@ # 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() { @@ -19,26 +13,15 @@ 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[@]} @@ -49,6 +32,12 @@ assertEqualMap() { hasDiff=1 fi + local -a sortedExpectedKeys=() + getSortedMapKeys "${!expectedMapRef}" sortedExpectedKeys + + local -a sortedActualKeys=() + getSortedMapKeys "${!actualMapRef}" sortedActualKeys + local -i expectedKeyIdx=0 local expectedKey local expectedValue diff --git a/pkgs/build-support/testers/testEqualArrayOrMap/default.nix b/pkgs/build-support/testers/testEqualArrayOrMap/default.nix index 4dc17d0e2b58..31bbc7fe3f12 100644 --- a/pkgs/build-support/testers/testEqualArrayOrMap/default.nix +++ b/pkgs/build-support/testers/testEqualArrayOrMap/default.nix @@ -1,4 +1,5 @@ { + arrayUtilities, lib, stdenvNoCC, }: @@ -21,7 +22,10 @@ lib.makeOverridable ( inherit name; nativeBuildInputs = [ + arrayUtilities.isDeclaredArray ./assert-equal-array.sh + arrayUtilities.isDeclaredMap + arrayUtilities.getSortedMapKeys ./assert-equal-map.sh ];