forked from numtide/flake-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-utils.nix
51 lines (45 loc) · 1.89 KB
/
check-utils.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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";
}