push sheeet
Some checks failed
Periodic Merges (6h) / master → staging-nixos (push) Failing after 12m50s
Periodic Merges (6h) / master → staging-next (push) Failing after 12m54s
Periodic Merges (24h) / merge-base(master,staging) → haskell-updates (push) Failing after 11m54s
Periodic Merges (6h) / staging-next → staging (push) Failing after 12m13s
Periodic Merges (24h) / staging-next-25.05 → staging-25.05 (push) Failing after 13m24s
Periodic Merges (24h) / release-25.05 → staging-next-25.05 (push) Failing after 14m28s

This commit is contained in:
Dark Steveneq
2025-10-09 14:15:47 +02:00
commit 646b892680
49168 changed files with 5897842 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
{
callPackages,
isDeclaredArray,
makeSetupHook,
}:
makeSetupHook {
name = "sortArray";
propagatedBuildInputs = [ isDeclaredArray ];
passthru.tests = callPackages ./tests.nix { };
meta.description = "Sorts an array";
} ./sortArray.bash

View File

@@ -0,0 +1,53 @@
# shellcheck shell=bash
# sortArray
# Sorts the indexed array referenced by inputArrRef and stores the result in the indexed array referenced by
# outputArrRef.
#
# Arguments:
# - inputArrRef: a reference to an indexed array (not mutated, may alias outputArrRef)
# - outputArrRef: a reference to an indexed array (contents are replaced entirely, may alias inputArrRef)
#
# Returns 0.
sortArray() {
if (($# != 2)); then
nixErrorLog "expected two arguments!"
nixErrorLog "usage: sortArray inputArrRef outputArrRef"
exit 1
fi
local -rn inputArrRef="$1"
local -rn outputArrRef="$2"
if ! isDeclaredArray "${!inputArrRef}"; then
nixErrorLog "first argument inputArrRef must be a reference to an indexed array"
exit 1
elif ! isDeclaredArray "${!outputArrRef}"; then
nixErrorLog "second argument outputArrRef must be a reference to an indexed array"
exit 1
fi
local -a sortedArray=()
# Guard on the length of the input array, as empty array will expand to nothing, but printf will still see it as an
# argument, producing an empty string.
if ((${#inputArrRef[@]} > 0)); then
# NOTE from Bash's printf documentation:
# The format is reused as necessary to consume all of the arguments. If the format requires more arguments than
# are supplied, the extra format specifications behave as if a zero value or null string, as appropriate, had
# been supplied.
# - https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html#index-printf
# NOTE from sort manpage:
# If you use a non-POSIX locale (e.g., by setting LC_ALL to 'en_US'), then sort may produce output that is sorted
# differently than you're accustomed to. In that case, set the LC_ALL environment variable to 'C'. Setting only
# LC_COLLATE has two problems. First, it is ineffective if LC_ALL is also set. Second, it has undefined behavior
# if LC_CTYPE (or LANG, if LC_CTYPE is unset) is set to an incompatible value. For example, you get undefined
# behavior if LC_CTYPE is ja_JP.PCK but LC_COLLATE is en_US.UTF-8.
# - https://www.gnu.org/software/coreutils/manual/html_node/sort-invocation.html#FOOT1
mapfile -d $'\0' -t sortedArray < <(printf '%s\0' "${inputArrRef[@]}" | LC_ALL=C sort --stable --zero-terminated)
fi
outputArrRef=("${sortedArray[@]}")
return 0
}

View File

@@ -0,0 +1,178 @@
# NOTE: Tests related to sortArray go here.
{
lib,
sortArray,
testers,
}:
let
inherit (lib.attrsets) recurseIntoAttrs;
inherit (testers) shellcheck shfmt testEqualArrayOrMap;
check =
{
name,
valuesArray,
expectedArray,
}:
(testEqualArrayOrMap {
inherit name valuesArray expectedArray;
script = ''
set -eu
nixLog "running sortArray with valuesArray to populate actualArray"
sortArray valuesArray actualArray
'';
}).overrideAttrs
(prevAttrs: {
nativeBuildInputs = prevAttrs.nativeBuildInputs or [ ] ++ [ sortArray ];
});
checkInPlace =
{
name,
valuesArray,
expectedArray,
}:
(testEqualArrayOrMap {
inherit name valuesArray expectedArray;
script = ''
set -eu
nixLog "running sortArray with valuesArray as input and output"
sortArray valuesArray valuesArray
nixLog "copying valuesArray to actualArray"
actualArray=("''${valuesArray[@]}")
'';
}).overrideAttrs
(prevAttrs: {
nativeBuildInputs = prevAttrs.nativeBuildInputs or [ ] ++ [ sortArray ];
});
in
recurseIntoAttrs {
shellcheck = shellcheck {
name = "sortArray";
src = ./sortArray.bash;
};
shfmt = shfmt {
name = "sortArray";
src = ./sortArray.bash;
};
empty = check {
name = "empty";
valuesArray = [ ];
expectedArray = [ ];
};
singleton = check {
name = "singleton";
valuesArray = [ "apple" ];
expectedArray = [ "apple" ];
};
oneDuplicate = check {
name = "oneDuplicate";
valuesArray = [
"apple"
"apple"
];
expectedArray = [
"apple"
"apple"
];
};
oneUnique = check {
name = "oneUnique";
valuesArray = [
"bee"
"apple"
"bee"
];
expectedArray = [
"apple"
"bee"
"bee"
];
};
duplicatesWithSpacesAndLineBreaks = check {
name = "duplicatesWithSpacesAndLineBreaks";
valuesArray = [
"dog"
"bee"
''
line
break
''
"cat"
"zebra"
"bee"
"cat"
"elephant"
"dog with spaces"
''
line
break
''
];
expectedArray = [
"bee"
"bee"
"cat"
"cat"
"dog"
"dog with spaces"
"elephant"
# NOTE: lead whitespace is removed, so the following entries start with `l`.
''
line
break
''
''
line
break
''
"zebra"
];
};
duplicatesWithSpacesAndLineBreaksInPlace = checkInPlace {
name = "duplicatesWithSpacesAndLineBreaksInPlace";
valuesArray = [
"dog"
"bee"
''
line
break
''
"cat"
"zebra"
"bee"
"cat"
"elephant"
"dog with spaces"
''
line
break
''
];
expectedArray = [
"bee"
"bee"
"cat"
"cat"
"dog"
"dog with spaces"
"elephant"
# NOTE: lead whitespace is removed, so the following entries start with `l`.
''
line
break
''
''
line
break
''
"zebra"
];
};
}