blob: 073ddaa12cfa5f1b3425012369eb1339773a9d4f (
plain)
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
150
151
152
153
|
{ lib }:
let
p = import ../../stdenv/generic/problems.nix { inherit lib; };
genHandlerTest =
let
slowReference =
config: package: name: kind:
# Try to find an explicit handler
(config.problems.handlers.${package} or { }).${name}
# Fall back, iterating through the matchers
or (lib.pipe config.problems.matchers [
# Find matches
(lib.filter (
matcher:
(matcher.name != null -> name == matcher.name)
&& (matcher.kind != null -> kind == matcher.kind)
&& (matcher.package != null -> package == matcher.package)
))
# Extract handler level
(map (matcher: matcher.handler))
# Take the strongest matched handler level
(lib.foldl' p.handlers.max "ignore")
]);
genValue =
f:
map
(
package:
map
(
name:
map (kind: f package name kind) [
"k1"
"k2"
"k3"
]
)
[
"n1"
"n2"
"n3"
]
)
[
"p1"
"p2"
"p3"
];
in
v: {
expr = genValue (p.genHandlerSwitch { problems = v; }).handlerForProblem;
expected = genValue (slowReference {
problems = v;
});
};
in
lib.runTests {
testHandlersLessThan =
let
levels = p.handlers.levels;
slowReference =
a: b:
lib.lists.findFirstIndex (v: v == a) (abort "Shouldn't happen") levels
< lib.lists.findFirstIndex (v: v == b) (abort "Shouldn't happen") levels;
genValue =
f:
lib.genList (
i: lib.genList (j: f (lib.elemAt levels i) (lib.elemAt levels j)) (lib.length levels)
) (lib.length levels);
in
{
expr = genValue p.handlers.lessThan;
expected = genValue slowReference;
};
testHandlerEmpty = genHandlerTest {
matchers = [ ];
handlers = { };
};
testHandlerNameSpecificHandlers = genHandlerTest {
matchers = [ ];
handlers.p1.n1 = "error";
handlers.p1.n2 = "warn";
handlers.p1.n3 = "ignore";
};
testHandlerPackageSpecificHandlers = genHandlerTest {
matchers = [ ];
handlers.p1.n1 = "error";
handlers.p2.n1 = "warn";
handlers.p3.n1 = "ignore";
};
testHandlersOverrideMatchers = genHandlerTest {
matchers = [
{
package = "p1";
name = "n1";
kind = null;
handler = "error";
}
];
handlers.p1.n1 = "warn";
};
testMatchersDefault = genHandlerTest {
matchers = [
# Everything should warn by default
{
package = null;
name = null;
kind = null;
handler = "warn";
}
];
handlers = { };
};
testMatchersComplicated = genHandlerTest {
matchers = [
{
package = "p1";
name = null;
kind = null;
handler = "warn";
}
{
package = "p1";
name = "n1";
kind = null;
handler = "error";
}
{
package = "p1";
name = null;
kind = "k1";
handler = "error";
}
{
package = "p1";
name = "n2";
kind = "k2";
handler = "error";
}
];
handlers = { };
};
}
|