This repository has been archived by the owner on Aug 13, 2022. It is now read-only.
forked from styx-static/styx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.nix
149 lines (127 loc) · 3.67 KB
/
lib.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* Library tests for Styx
To see the tests reports (failures & coverage) run:
nix run .#report
*/
{ pkgs, lib }: with lib;
let
namespaces = [
"conf"
"data"
"generation"
"pages"
"proplist"
"template"
"themes"
"utils"
];
libs = map (x:
mapAttrs' (k: v:
{ name = "lib.${x}.${k}"; value = v; }
) lib."${x}"
) namespaces;
functions = fold (x: acc:
acc // x
) {} libs;
tests =
let
ex = mapAttrsToList (name: fn:
let
extract = imap (index: ex:
optionalAttrs (ex ? code && ex ? expected)
(ex // { inherit name index; })
) fn.examples;
in if fn ? examples then extract else {}) functions;
in (filter (x: x != {}) (flatten ex) ++ customTests);
missingTests =
let
missing = mapAttrsToList (name: fn:
let
hasTest = any (ex: ex ? expected);
in if isDocFunction fn
then if fn ? examples && hasTest fn.examples
then null
else name
else null) functions;
in filter (x: x != null) (flatten missing);
runTests = fold (test: acc:
if test.code == test.expected
then (acc // { success = acc.success ++ [ test ]; })
else (acc // { failures = acc.failures ++ [ test ]; })
) { success = []; failures = []; };
results = runTests tests;
successNb = length results.success;
failuresNb = length results.failures;
lsep = "====================\n";
sep = "---\n";
inSep = x: sep + x + sep;
report = pkgs.writeText "lib-tests-report.md" ''
# Styx Library Test Report
---
${toString (successNb + failuresNb)} tests run.
- ${toString successNb} success(es).
- ${toString failuresNb} failure(s).
${optionalString (failuresNb > 0) ''
### Failures details:
${lsep}${mapTemplate (failure:
let
header = "${failure.name}${optionalString (failure ? index) ", example number ${toString failure.index}"}:\n";
code = optionalString (failure ? literalCode) ("\ncode:\n" + inSep failure.literalCode);
expected = "\nexpected:\n" + inSep "${prettyNix failure.expected}\n";
got = "\ngot:\n" + inSep "${prettyNix failure.code}\n";
in
header + code + expected + got + lsep
) results.failures}''}
---
'';
coverage = pkgs.writeText "lib-tests-coverage.md" ''
# Styx Library Test Report
---
${toString (length missingTests)} functions missing tests:
${mapTemplate (f: " - ${f}") missingTests}
---
'';
mkLoadFileTest = file:
let data = loadFile { inherit file; env = { inherit lib; foo = "bar"; bar = [1 2 3]; buz = 2; }; };
cleanData = removeAttrs data [ "fileData" ];
in mapAttrs (k: v:
if k == "pages"
then map (x: removeAttrs x [ "fileData" ]) v
else v
) cleanData;
customTests = [ {
name = "loadFile - markdown";
function = "lib.data.loadFile";
code = mkLoadFileTest ./data/markdown.md;
expected = {
bar = "answer";
baz = 40;
foo = "The answer";
intro = ''
<p>Intro text.</p>
'';
pages = [
{
content = ''
<p>First page</p>
<p>The answer is 42.</p>
'';
}
{
content = ''
<p>Second page.</p>
<p>{{ non evaluated nix }}</p>
<p>this }} is not evaluated</p>
'';
}
{
content = ''
<p>Third page.</p>
'';
}
];
};
} ];
in {
inherit report results functions tests coverage;
success = failuresNb == 0;
}