-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
load.nix
82 lines (71 loc) · 1.59 KB
/
load.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
{ root, lib, haumea }:
let
inherit (builtins)
attrNames
concatStringsSep
filter
mapAttrs
pathExists
readFile
storeDir
tail
toJSON
trace
tryEval
;
inherit (lib)
filterAttrs
flip
hasPrefix
id
pipe
removePrefix
splitString
warn
;
in
args:
let
src = toString (
args.src or (warn
"namaka.load: `flake` and `dir` have been deprecated, use `src` directly instead"
(args.flake + "/${args.dir or "tests"}"))
);
tests = haumea.load (removeAttrs args [ "flake" "dir" ] // {
inherit src;
});
results = flip mapAttrs tests (name: { format ? "json", expr }:
assert hasPrefix "." name
-> throw "invalid snapshot '${name}', names should not start with '.'";
let
path = "${src}/_snapshots/${name}";
old = pathExists path;
snap = readFile path;
prefix = "#${format}\n";
f = root.formats.${format};
value = (f.serialize or id) expr;
expected = (f.parse or id) (removePrefix prefix snap);
in
if old && hasPrefix prefix snap
&& tryEval expected == { inherit value; success = true; } then
true
else {
inherit format value old;
});
msg = {
dir = pipe src [
(removePrefix storeDir)
(splitString "/")
(filter (x: x != ""))
tail
(concatStringsSep "/")
];
inherit results;
};
failures = attrNames (filterAttrs (_: res: res ? value) results);
in
assert trace "namaka=${toJSON msg}" true;
if failures == [ ] then
{ }
else
throw "the following tests failed: ${concatStringsSep "," failures}"