blob: dc8e40f8ad4b3dc83be8300e66decee9247f9575 (
plain) (
blame)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
{ lib, pkgs, depot, ... }:
# Run a nix testsuite.
#
# The tests are simple assertions on the nix level,
# and can use derivation outputs if IfD is enabled.
#
# You build a testsuite by bundling assertions into
# “it”s and then bundling the “it”s into a testsuite.
#
# Running the testsuite will abort evaluation if
# any assertion fails.
#
# Example:
#
# runTestsuite "myFancyTestsuite" [
# (it "does an assertion" [
# (assertEq "42 is equal to 42" "42" "42")
# (assertEq "also 23" 23 23)
# ])
# (it "frmbls the brlbr" [
# (assertEq true false)
# ])
# ]
#
# will fail the second it group because true is not false.
let
inherit (depot.nix.yants)
sum
struct
string
any
defun
list
drv
bool
;
bins = depot.nix.getBins pkgs.coreutils [ "printf" ]
// depot.nix.getBins pkgs.s6-portable-utils [ "s6-touch" "s6-false" ];
# Returns true if the given expression throws when `deepSeq`-ed
throws = expr:
!(builtins.tryEval (builtins.deepSeq expr {})).success;
# rewrite the builtins.partition result
# to use `ok` and `err` instead of `right` and `wrong`.
partitionTests = pred: xs:
let res = builtins.partition pred xs;
in {
ok = res.right;
err = res.wrong;
};
AssertErrorContext =
sum "AssertErrorContext" {
not-equal = struct "not-equal" {
left = any;
right = any;
};
should-throw = struct "should-throw" {
expr = any;
};
unexpected-throw = struct "unexpected-throw" { };
};
# The result of an assert,
# either it’s true (yep) or false (nope).
# If it's nope we return an additional context
# attribute which gives details on the failure
# depending on the type of assert performed.
AssertResult =
sum "AssertResult" {
yep = struct "yep" {
test = string;
};
nope = struct "nope" {
test = string;
context = AssertErrorContext;
};
};
# Result of an it. An it is a bunch of asserts
# bundled up with a good description of what is tested.
ItResult =
struct "ItResult" {
it-desc = string;
asserts = list AssertResult;
};
# If the given boolean is true return a positive AssertResult.
# If the given boolean is false return a negative AssertResult
# with the provided AssertErrorContext describing the failure.
#
# This function is intended as a generic assert to implement
# more assert types and is not exposed to the user.
assertBoolContext = defun [ AssertErrorContext string bool AssertResult ]
(context: desc: res:
if res
then { yep = { test = desc; }; }
else { nope = {
test = desc;
inherit context;
};
});
# assert that left and right values are equal
assertEq = defun [ string any any AssertResult ]
(desc: left: right:
let
context = { not-equal = { inherit left right; }; };
in
assertBoolContext context desc (left == right));
# assert that the expression throws when `deepSeq`-ed
assertThrows = defun [ string any AssertResult ]
(desc: expr:
let
context = { should-throw = { inherit expr; }; };
in
assertBoolContext context desc (throws expr));
# assert that the expression does not throw when `deepSeq`-ed
assertDoesNotThrow = defun [ string any AssertResult ]
(desc: expr:
assertBoolContext { unexpected-throw = { }; } desc (!(throws expr)));
# Annotate a bunch of asserts with a descriptive name
it = desc: asserts: {
it-desc = desc;
inherit asserts;
};
# Run a bunch of its and check whether all asserts are yep.
# If not, abort evaluation with `throw`
# and print the result of the test suite.
#
# Takes a test suite name as first argument.
runTestsuite = defun [ string (list ItResult) drv ]
(name: itResults:
let
goodAss = ass: {
good = AssertResult.match ass {
yep = _: true;
nope = _: false;
};
x = ass;
};
goodIt = it: {
inherit (it) it-desc;
asserts = partitionTests (ass:
AssertResult.match ass {
yep = _: true;
nope = _: false;
}) it.asserts;
};
goodIts = partitionTests (it: (goodIt it).asserts.err == []);
res = goodIts itResults;
prettyRes = lib.generators.toPretty {} res;
in
if res.err == []
then depot.nix.runExecline.local "testsuite-${name}-successful" {} [
"importas" "out" "out"
# force derivation to rebuild if test case list changes
"ifelse" [ bins.s6-false ] [ bins.printf "" prettyRes ]
"if" [ bins.printf "%s\n" "testsuite ${name} successful!" ]
bins.s6-touch "$out"
]
else depot.nix.runExecline.local "testsuite-${name}-failed" {} [
"importas" "out" "out"
"if" [
bins.printf "%s\n%s\n"
"testsuite ${name} failed!"
prettyRes
]
"exit" "1"
]);
in {
inherit
assertEq
assertThrows
assertDoesNotThrow
it
runTestsuite
;
}
|