-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntests.d
355 lines (318 loc) · 10.2 KB
/
runtests.d
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import std.algorithm;
import std.array;
import std.conv;
import std.exception;
import std.file;
import std.path;
import std.process;
import std.regex;
import std.stdio;
import std.string;
void runCommand(string[] args, string workDir = null, Appender!string *app = null)
{
if (app !is null)
app.put(text("Running: ", escapeShellCommand(args), "\n"));
else
writeln("Running: ", escapeShellCommand(args));
Redirect redirect = Redirect.stdin;
if (app !is null)
redirect |= Redirect.stdout | Redirect.stderrToStdout;
auto pipes = pipeProcess(args, redirect, null, Config.none, workDir);
pipes.stdin.close();
if (app !is null)
{
foreach (ubyte[] chunk; pipes.stdout.byChunk(4096))
app.put(chunk);
}
auto status = pipes.pid.wait();
if (status)
{
throw new Exception(text("Command ", args[0], " failed with status ", status));
}
}
class Test
{
string name;
string workDir;
string[] translationUnits;
string[] includedFiles;
string[] testDefines;
string[] expectedOutputFiles;
string[] extraArgs;
}
int cmpNumberStrings(string a, string b)
{
while (1)
{
if (a.length == 0 && b.length == 0)
return 0;
if (a.length == 0)
return -1;
if (b.length == 0)
return 1;
size_t nlena;
while (nlena < a.length && a[nlena] >= '0' && a[nlena] <= '9')
nlena++;
size_t nlenb;
while (nlenb < b.length && b[nlenb] >= '0' && b[nlenb] <= '9')
nlenb++;
if (nlena != nlenb)
{
if (nlena == 0)
return 1;
if (nlenb == 0)
return -1;
if (nlena < nlenb)
return -1;
if (nlena > nlenb)
return 1;
}
if (a[0] < b[0])
return -1;
if (a[0] > b[0])
return 1;
a = a[1 .. $];
b = b[1 .. $];
}
}
int main(string[] args)
{
Test[] tests;
Test[string] testByName;
bool anyFailure;
bool color;
bool github;
bool updateExpected;
for (size_t i = 1; i < args.length; i++)
{
auto arg = args[i];
if (arg.startsWith("--color"))
{
color = true;
}
else if (arg.startsWith("--github"))
{
github = true;
}
else if (arg.startsWith("--update-expected"))
{
updateExpected = true;
}
else
{
stderr.writeln("Unknown argument ", arg);
return 1;
}
}
foreach (DirEntry e; dirEntries("tests/single", SpanMode.depth))
{
string name = e.name.stripExtension;
string ext = e.name.extension;
if (e.name.endsWith("-output-config.json"))
{
ext = "-output-config.json";
name = e.name[0 .. $ - ext.length];
}
Test test;
if (name in testByName)
{
test = testByName[name];
}
else
{
test = new Test;
test.name = name;
testByName[name] = test;
tests ~= test;
test.workDir = "tests/single";
}
if (ext.among(".c", ".cpp"))
{
test.translationUnits ~= relativePath(absolutePath(e.name), absolutePath(test.workDir));
}
else if (ext == ".d")
{
test.expectedOutputFiles ~= relativePath(absolutePath(e.name), absolutePath(test.workDir));
}
else if (ext == "-output-config.json")
{
test.extraArgs = ["--output-config", relativePath(absolutePath(e.name), absolutePath(test.workDir))];
}
else
{
stderr.writeln("Unexpected file: ", e.name);
anyFailure = true;
continue;
}
}
foreach (DirEntry d; dirEntries("tests/multifile", SpanMode.shallow))
{
Test test = new Test;
test.name = d.name;
test.workDir = d.name;
tests ~= test;
foreach (DirEntry e; dirEntries(d.name, SpanMode.depth))
{
string name = e.name.stripExtension;
string ext = e.name.extension;
if (ext.among(".c", ".cpp"))
{
test.translationUnits ~= relativePath(absolutePath(e.name), absolutePath(test.workDir));
}
else if (ext.among(".h"))
{
test.includedFiles ~= relativePath(absolutePath(e.name), absolutePath(test.workDir));
}
else if (ext.among(".d"))
{
test.expectedOutputFiles ~= relativePath(absolutePath(e.name), absolutePath(test.workDir));
}
else if (baseName(e.name) == "output-config.json")
{
test.extraArgs = ["--output-config", relativePath(absolutePath(e.name), absolutePath(test.workDir))];
}
else
{
stderr.writeln("Unexpected file: ", e.name);
anyFailure = true;
}
}
}
tests.sort!((a, b) {
return cmpNumberStrings(a.name, b.name) < 0;
});
auto regexTestDefine = regex(r"\bDEF[a-zA-Z0-9]*\b");
foreach (test; tests)
{
test.translationUnits.sort!((a, b) {
return cmpNumberStrings(a, b) < 0;
});
test.includedFiles.sort!((a, b) {
return cmpNumberStrings(a, b) < 0;
});
test.expectedOutputFiles.sort!((a, b) {
return cmpNumberStrings(a, b) < 0;
});
foreach (filename; test.translationUnits ~ test.includedFiles)
{
string content = readText(buildPath(test.workDir, filename));
foreach (c; matchAll(content, regexTestDefine))
{
if (!test.testDefines.canFind(c.hit))
test.testDefines ~= c.hit;
}
}
test.testDefines.sort!((a, b) {
return cmpNumberStrings(a, b) < 0;
});
}
if (exists("test_results"))
{
rmdirRecurse("test_results");
}
runCommand(["dub", "build"]);
string[] failedTests;
size_t successfulTests;
foreach (test; tests)
{
auto testDir = absolutePath(buildPath("test_results", test.name));
auto convDir = absolutePath(buildPath("test_results", test.name, "conv"));
Appender!string app;
bool hasError;
try
{
runCommand([relativePath(absolutePath("./cppconv"), absolutePath(test.workDir)),
"--output-dir", relativePath(convDir, absolutePath(test.workDir)),
"--extra-output-dir", relativePath(testDir, absolutePath(test.workDir)),
"-DALWAYS_PREDEFINED_IN_TEST=1", "-UALWAYS_PREUNDEFINED_IN_TEST"]
~ test.extraArgs
~ test.translationUnits, test.workDir, &app);
foreach (tu; test.translationUnits)
{
string[] gccArgs = [tu.extension == ".cpp" ? "g++" : "gcc",
tu, "-c", "-o", "/dev/null"];
if (color)
gccArgs ~= "-fdiagnostics-color";
runCommand(gccArgs, test.workDir, &app);
foreach (testDefine; test.testDefines)
{
runCommand(gccArgs ~ ["-D" ~ testDefine], test.workDir, &app);
}
}
string[] outputFiles;
foreach (DirEntry e; dirEntries(convDir, SpanMode.depth))
{
enforce(e.name.endsWith(".d"));
outputFiles ~= relativePath(absolutePath(e.name), absolutePath(convDir));
string[] dmdArgs = ["dmd", relativePath(absolutePath(e.name), absolutePath(convDir)),
"-I" ~ relativePath(absolutePath("tests/helpers"), absolutePath(convDir)),
"-c", "-of/dev/null"];
if (color)
dmdArgs ~= "-color=on";
runCommand(dmdArgs, convDir, &app);
foreach (testDefine; test.testDefines)
{
runCommand(dmdArgs ~ ["-version=" ~ testDefine], convDir, &app);
}
}
outputFiles.sort!((a, b) {
return cmpNumberStrings(a, b) < 0;
});
if (updateExpected)
{
foreach (name; test.expectedOutputFiles)
{
remove(buildPath(test.workDir, name));
}
foreach (name; outputFiles)
{
copy(buildPath(convDir, name), buildPath(test.workDir, name));
}
test.expectedOutputFiles = outputFiles;
}
if (outputFiles != test.expectedOutputFiles)
{
throw new Exception(text("Wrong set of output files: ", outputFiles, " expected: ", test.expectedOutputFiles));
}
foreach (name; outputFiles)
{
string text1 = readText(buildPath(test.workDir, name)).replace("\r", "");
string text2 = readText(buildPath(convDir, name)).replace("\r", "");
if (text1 != text2)
{
app.put(text("Files differ: ", name, "\n"));
runCommand(["diff", buildPath(test.workDir, name), buildPath(convDir, name)], null, &app);
hasError = true;
}
}
if (!hasError)
successfulTests++;
}
catch (Exception e)
{
app.put(e.msg);
app.put("\n");
hasError = true;
}
if (hasError)
{
failedTests ~= test.name;
anyFailure = true;
if (github)
writeln("::group::Test ", test.name, " failed");
else
writeln("############ Test ", test.name, " failed ############");
writeln(app.data);
if (github)
writeln("::endgroup::");
}
}
if (failedTests.length)
{
writeln("Failed tests:");
foreach (test; failedTests)
writeln(" ", test);
}
writeln("Successful tests: ", successfulTests, " / ", tests.length);
return anyFailure;
}