-
Notifications
You must be signed in to change notification settings - Fork 45
/
test.wls
executable file
·237 lines (199 loc) · 8.29 KB
/
test.wls
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env wolframscript
(* Disable messages about outdated cloud. *)
Off[CloudConnect::clver];
$redColor = "\033[0;31m";
$greenColor = "\033[0;32m";
$orangeColor = "\033[0;33m";
$yellowColor = "\033[1;33m";
$endColor = "\033[0m";
$commandLineArgs = Rest[$ScriptCommandLine];
tryPopFlag[longFlag_String, shortFlag_String] := If[
MemberQ[$commandLineArgs, longFlag | shortFlag],
$commandLineArgs = DeleteCases[$commandLineArgs, longFlag | shortFlag];
True
,
False
];
If[tryPopFlag["--load-installed-paclet", "-lip"],
$localSetReplaceEntryPoint = "SetReplace`";
,
$localSetReplaceEntryPoint = FileNameJoin[{FileNameDrop[$InputFileName], "Kernel", "init.m"}];
];
Print["Loading SetReplace from ", $yellowColor, FindFile[$localSetReplaceEntryPoint], $endColor];
Check[
Get[$localSetReplaceEntryPoint];
,
Print[$redColor, "Messages occurred during loading of SetReplace, aborting tests.", $endColor];
Exit[1];
];
(* make it possible to disable parallelization, which for very short tests saves quite a lot of time. *)
If[tryPopFlag["--disable-parallelization", "-dp"],
$allowParallelization = False;
,
$allowParallelization = True;
(* This hack fixes a bug in Mathematica 12.1.1: if any messages or Prints occur in a subkernel, unformatted
KernelObject is printed, obscuring any relevant information. It has been reported upstream. *)
LaunchKernels; (* trigger autoload *)
Unprotect[System`KernelObject];
System`KernelObject /: StringForm["From `1`:", ko_KernelObject] := $orangeColor <> "From subkernel:" <> $endColor;
(* Launch kernels preemptively to avoid "Launching kernels..." interrupting test log *)
CloseKernels[];
LaunchKernels[];
With[{entryPoint = $localSetReplaceEntryPoint}, ParallelEvaluate[Get[entryPoint]]];
];
Needs["GeneralUtilities`"];
$successQ = True;
(* Get test files *)
$testFiles = If[Length @ $commandLineArgs >= 1,
Replace[$commandLineArgs, {
args : {"-e", ___} :> Select[
FileNames[FileNameJoin[{AbsoluteFileName["."], "Tests", "*.wlt"}]],
!MatchQ[FileBaseName @ #, Alternatives @@ Rest[args]] &
],
args_List :> (FileNameJoin[{AbsoluteFileName["."], "Tests", # <> ".wlt"}] & /@ args)}
]
,
FileNames[FileNameJoin[{AbsoluteFileName["."], "Tests", "*.wlt"}]]
];
If[!FileExistsQ[#],
Print["Test file ", #, " does not exist."];
Exit[1];
] & /@ $testFiles;
(* Read tests *)
printFileLineError[FileLine[file_, line_] -> error_] := Print[file <> ":" <> IntegerString[line] <> "\t" <> error];
importAsHeldExpression[file_] := Module[{exprs},
exprs = Quiet @ (DeleteCases[#, Null] &) @ ToExpression[Import[file, "Text"], InputForm, Hold];
If[!$allowParallelization, exprs = exprs /. HoldPattern[ParallelMap] -> Map];
If[FailureQ[exprs],
WriteString["stdout", $redColor <> "There were syntax errors in \"" <> file <> "\"" <> $endColor, ":"];
(* this will cause a message that will print out the syntax error as Get would, but without evaluating any
expressions. The Options override is because apparently someone changed Get in the kernel and forget to change
FullGet, so it is missing an option! *)
Unprotect[Language`FullGet];
Options[Language`FullGet] = Options[Get];
Language`FullGet[file, Null, HoldComplete];
Exit[1];
];
exprs
];
$testGroups = Join @@ (
KeyMap[ReleaseHold, #] & /@
ReleaseHold @ Map[Hold, importAsHeldExpression[#], {5}] & /@ $testFiles);
Attributes[test] = Attributes[constrainedTest] = {HoldAll};
$singleTestTimeConstraint = 300;
$singleTestMemoryConstraint = 1*^9;
constrainedTest[args___] := With[{
timeConstraintOpt =
If[FreeQ[Hold[{args}], TimeConstraint], {TimeConstraint -> $singleTestTimeConstraint}, {}],
memoryConstraintOpt =
If[FreeQ[Hold[{args}], MemoryConstraint], {MemoryConstraint -> $singleTestMemoryConstraint}, {}]},
test[args, ##] & @@ Join[timeConstraintOpt, memoryConstraintOpt]
];
removeHoldFormFromInputString[input_String] := StringReplace[
input,
StartOfString ~~ "HoldForm[" ~~ expr__ ~~ "]" ~~ EndOfString :> expr
];
$results = Association[KeyValueMap[Function[{testGroupName, testGroup}, Module[{
testList, testResults, testReport, options, parallelQ, runInit, failedTests,
requiresFrontEnd, probablyRequiresFrontEnd, bodyWrapper},
(* Notify the user which test group we are running *)
WriteString["stdout",
testGroupName,
StringJoin[ConstantArray[" ", Max[40 - StringLength[testGroupName], 1]]]];
probablyRequiresFrontEnd = !FreeQ[testGroup,
SetReplace`PackageScope`checkGraphics | SetReplace`PackageScope`graphicsQ | UsingFrontEnd];
(* Read options *)
options = Association[ReleaseHold[Lookup[testGroup, "options", <||>]]];
parallelQ = $allowParallelization && Lookup[options, "Parallel", True];
requiresFrontEnd = Lookup[options, "RequiresFrontEnd", probablyRequiresFrontEnd];
(* if a frontend is required, better to start it once than again and again in each test *)
bodyWrapper = If[requiresFrontEnd, UsingFrontEnd, Identity];
bodyWrapper[
(* Run init, changing VerificationTest to test in all definitions *)
runInit[] := ReleaseHold[testGroup["init"] /. VerificationTest -> constrainedTest];
runCleanup[] := ReleaseHold[testGroup["cleanup"] /. VerificationTest -> constrainedTest];
runInit[];
If[parallelQ, ParallelEvaluate[runInit[]]];
(* Make a list of tests, but don't run them yet *)
testList = Flatten[ReleaseHold[testGroup["tests"] /. VerificationTest -> constrainedTest]];
(* Run tests in parallel *)
testResults = If[parallelQ, ParallelMap, Map][# /. test -> VerificationTest &, testList];
runCleanup[];
];
testReport = TestReport[testResults];
(* Print the summery (green if ok, red if failed) *)
WriteString[
"stdout",
If[testReport["AllTestsSucceeded"],
$greenColor <> "[ok]" <> $endColor
,
$successQ = False;
StringJoin[
$redColor <> "[",
ToString @ testReport["TestsFailedCount"],
"/",
ToString @ Length @ testReport["TestResults"],
" failed]" <> $endColor]
],
"\n"];
(* If tests failed, print why *)
failedTests = Join @@ testReport["TestsFailed"];
Switch[#["Outcome"],
"Failure",
WriteString["stdout",
$redColor <> "Input" <> $endColor <> "\n",
"> ", removeHoldFormFromInputString[ToString[#["Input"], InputForm]], "\n\n",
$redColor <> "evaluated to" <> $endColor <> "\n",
"> ", ToString[#["ActualOutput"], OutputForm], "\n\n",
$redColor <> "instead of expected " <> $endColor <> "\n",
"> ", ToString[#["ExpectedOutput"], OutputForm], "\n\n\n"],
"MessagesFailure",
WriteString["stdout",
$orangeColor <> "Input" <> $endColor <> "\n",
"> ", removeHoldFormFromInputString[ToString[#["Input"], InputForm]], "\n\n",
$orangeColor <> "generated messages" <> $endColor <> "\n",
"> ", ToString[#["ActualMessages"], OutputForm], "\n\n",
$orangeColor <> "instead of expected" <> $endColor <> "\n",
"> ", ToString[#["ExpectedMessages"], OutputForm], "\n\n\n"],
"Error",
WriteString["stdout",
$yellowColor <> "Error while evaluating the test with input" <> $endColor <> "\n",
"> ", removeHoldFormFromInputString[ToString[#["Input"], InputForm]], "\n\n\n"]
] & /@ failedTests[[1 ;; UpTo[3]]];
(* If too many tests have failed, print how many remain *)
If[Length[failedTests] > 3,
WriteString["stdout",
"Omitting remaining ",
Length[failedTests] - 3,
" " <> testGroupName,
" test failures.\n\n"]
];
(* Return the report, we'll need it later *)
testGroupName -> testReport
]], $testGroups]];
(* Create a notebook with results *)
$reportFile = UsingFrontEnd[
$NotebookInlineStorageLimit = Infinity;
Export[
FileNameJoin[Join[FileNameSplit[CreateDirectory[]], {"testReport.nb"}]],
Notebook @ Catenate @ Prepend[KeyValueMap[
{Cell[Last @ FileNameSplit @ #1, "Section"],
Cell[
BoxData[RowBox[{"TestReport", "[", "\"" <> #1 <> "\"", "]"}]],
"Input"],
Cell[BoxData[ToBoxes[#2]], "Output"]} &,
$results], {Cell["SetReplace Test Report", "Title"]}]
]
];
Print["Report file: ", $reportFile];
If[$MessageList =!= {}, $successQ = False];
If[Environment["STATUS_FILE"] === "1",
Export["exit_status.txt", If[$successQ, 0, 1]];
];
If[$successQ,
Print["Tests passed."];
Exit[0]
,
Print["Tests failed."];
Exit[1]
]