flake-utils/check-utils.nix
oxalica 7e2a3b3dfd
check-utils: use the same success derivation (#75)
A successful check doesn't need reporting, so we use the same derivation
to reduce building jobs.
2022-07-04 12:00:14 +02:00

52 lines
1.9 KiB
Nix

systemOrPkgs:
let
inherit (builtins) foldl' unsafeDiscardStringContext elemAt match split concatStringsSep isList substring stringLength length attrNames;
system = systemOrPkgs.system or systemOrPkgs;
pipe = val: functions: foldl' (x: f: f x) val functions;
max = x: y: if x > y then x else y;
# Minimized copy-paste https://github.com/NixOS/nixpkgs/blob/master/lib/strings.nix#L746-L762
sanitizeDerivationName = string: pipe (toString string) [
# Get rid of string context. This is safe under the assumption that the
# resulting string is only used as a derivation name
unsafeDiscardStringContext
# Strip all leading "."
(x: elemAt (match "\\.*(.*)" x) 0)
# Split out all invalid characters
# https://github.com/NixOS/nix/blob/2.3.2/src/libstore/store-api.cc#L85-L112
# https://github.com/NixOS/nix/blob/2242be83c61788b9c0736a92bb0b5c7bbfc40803/nix-rust/src/store/path.rs#L100-L125
(split "[^[:alnum:]+._?=-]+")
# Replace invalid character ranges with a "-"
(map (s: if isList s then "-" else s))
(concatStringsSep "")
# Limit to 211 characters (minus 4 chars for ".drv")
(x: substring (max (stringLength x - 207) 0) (-1) x)
# If the result is empty, replace it with "?EMPTY?"
(x: if stringLength x == 0 then "?EMPTY?" else x)
];
# Minimized version of 'sanitizeDerivationName' function
str = it: if it == null then "null" else (sanitizeDerivationName it);
test = name: command: derivation {
inherit system;
name = str name;
builder = "/bin/sh";
args = [ "-c" command ];
};
success = test "SUCCESS" "echo success > $out";
in
{
isEqual = a: b:
if a == b
then success
else test "FAILURE__${str a}__NOT_EQUAL__${str b}" "exit 1";
hasKey = attrset: key:
if attrset ? ${str key}
then success
else test "FAILURE__${str key}__DOES_NOT_EXISTS_IN_ATTRSET_SIZE_${str(length (attrNames attrset))}" "exit 1";
}