-
-
Notifications
You must be signed in to change notification settings - Fork 145
/
tests_extractor.d
executable file
·359 lines (306 loc) · 11.2 KB
/
tests_extractor.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
356
357
358
359
#!/usr/bin/env dub
/++dub.sdl:
name "tests_extractor"
dependency "libdparse" version="~>0.24.0"
+/
/*
* Parses all public unittests that are visible on dlang.org
* (= annotated with three slashes)
*
* Copyright (C) 2018 by D Language Foundation
*
* Author: Sebastian Wilzbach
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
// Written in the D programming language.
import dparse.ast;
import std.algorithm;
import std.ascii : whitespace;
import std.conv;
import std.exception;
import std.experimental.logger;
import std.file;
import std.path;
import std.range;
import std.stdio;
class TestVisitor : ASTVisitor
{
File outFile;
ubyte[] sourceCode;
string moduleName;
VisitorConfig config;
this(File outFile, ubyte[] sourceCode, VisitorConfig config)
{
this.outFile = outFile;
this.sourceCode = sourceCode;
this.config = config;
}
alias visit = ASTVisitor.visit;
override void visit(const Module m)
{
if (m.moduleDeclaration !is null)
{
moduleName = m.moduleDeclaration.moduleName.identifiers.map!(i => i.text).join(".");
}
else
{
// fallback: convert the file path to its module path, e.g. std/uni.d -> std.uni
moduleName = outFile.name.replace(".d", "").replace(dirSeparator, ".").replace(".package", "");
}
m.accept(this);
// -betterC doesn't run unittests out of the box
if (config.betterCOutput)
{
outFile.writeln(q{extern(C) void main()
{
static foreach(test; __traits(allMembers, mixin(__MODULE__)))
static if (test.length > 8 && test[0..8] == "unittest")
mixin(test)();
}});
}
}
override void visit(const Declaration decl)
{
if (decl.unittest_ !is null && shouldIncludeUnittest(decl))
print(decl.unittest_, decl.attributes);
decl.accept(this);
}
override void visit(const ConditionalDeclaration decl)
{
bool skipTrue;
// Check if it's a version that should be skipped
if (auto vcd = decl.compileCondition.versionCondition)
{
const id = vcd.token.text;
skipTrue = config.ignoredVersions.canFind(id);
}
// search if/version block
if (!skipTrue)
{
foreach (d; decl.trueDeclarations)
visit(d);
}
// Search else block
foreach (d; decl.falseDeclarations)
visit(d);
}
private:
bool shouldIncludeUnittest(const Declaration decl)
{
if (!config.attributes.empty)
return filterForUDAs(decl);
else
return decl.unittest_.comment !is null;
}
bool filterForUDAs(const Declaration decl)
{
foreach (attr; decl.attributes)
{
if (attr.atAttribute is null)
continue;
// check for @myArg
if (config.attributes.canFind(attr.atAttribute.identifier.text))
return true;
// support @("myArg") too
if (auto argList = attr.atAttribute.argumentList)
{
foreach (arg; argList.items)
{
if (auto unaryExp = cast(UnaryExpression) arg)
if (auto primaryExp = unaryExp.primaryExpression)
{
auto attribute = primaryExp.primary.text;
if (attribute.length >= 2)
{
attribute = attribute[1 .. $ - 1];
if (config.attributes.canFind(attribute))
return true;
}
}
}
}
}
return false;
}
void print(const Unittest u, const Attribute[] attributes)
{
/*
Write the origin source code line
u.line is the first line of the unittest block, hence we need to
subtract two lines from it as we add "import <current.module>\n\n" at
the top of the unittest.
*/
const line = u.line > 2 ? u.line - 2 : 0;
outFile.writefln("# line %d", line);
static immutable predefinedAttributes = ["nogc", "system", "nothrow", "safe", "trusted", "pure"];
// write system attributes
foreach (attr; attributes)
{
// pure and nothrow
if (attr.attribute.type != 0)
{
import dparse.lexer : str;
const attrText = attr.attribute.type.str;
outFile.write(text(attrText, " "));
}
const atAttribute = attr.atAttribute;
if (atAttribute is null)
continue;
const atText = atAttribute.identifier.text;
// ignore custom attributes (@myArg)
if (!predefinedAttributes.canFind(atText))
continue;
outFile.write(text("@", atText, " "));
}
// write the unittest block
if (config.betterCOutput)
outFile.writef("void unittest_line_%s()\n{\n", line);
else
outFile.write("unittest\n{\n");
scope(exit) outFile.writeln("}\n");
// add an import to the current module
outFile.writefln(" import %s;", moduleName);
// write the content of the unittest block (but skip the first brace)
auto k = cast(immutable(char)[]) sourceCode[u.blockStatement.startLocation .. u.blockStatement.endLocation];
k.findSkip("{");
outFile.write(k);
// if the last line contains characters, we want to add an extra line for increased visual beauty
if (k[$ - 1] != '\n')
outFile.writeln;
}
}
void parseFile(File inFile, File outFile, VisitorConfig visitorConfig)
{
import dparse.lexer;
import dparse.parser : parseModule;
import dparse.rollback_allocator : RollbackAllocator;
import std.array : uninitializedArray;
if (inFile.size == 0)
warningf("%s is empty", inFile.name);
ubyte[] sourceCode = uninitializedArray!(ubyte[])(to!size_t(inFile.size));
inFile.rawRead(sourceCode);
LexerConfig config;
auto cache = StringCache(StringCache.defaultBucketCount);
auto tokens = getTokensForParser(sourceCode, config, &cache);
RollbackAllocator rba;
auto m = parseModule(tokens.array, inFile.name, &rba);
auto visitor = new TestVisitor(outFile, sourceCode, visitorConfig);
visitor.visit(m);
}
void parseFileDir(string inputDir, string fileName, string outputDir, VisitorConfig visitorConfig)
{
import std.path : buildPath, dirSeparator, buildNormalizedPath;
// file name without its parent directory, e.g. std/uni.d
string fileNameNormalized = (inputDir == "." ? fileName : fileName.replace(inputDir, ""));
// remove leading dots or slashes
while (!fileNameNormalized.empty && fileNameNormalized[0] == '.')
fileNameNormalized = fileNameNormalized[1 .. $];
if (fileNameNormalized.length >= dirSeparator.length &&
fileNameNormalized[0 .. dirSeparator.length] == dirSeparator)
fileNameNormalized = fileNameNormalized[dirSeparator.length .. $];
// convert the file path to a nice output file, e.g. std/uni.d -> std_uni.d
string outName = fileNameNormalized.replace(dirSeparator, "_");
parseFile(File(fileName), File(buildPath(outputDir, outName), "w"), visitorConfig);
}
struct VisitorConfig
{
string[] attributes; /// List of attributes to extract;
string[] ignoredVersions; /// List of disabled version conditions
bool betterCOutput; /// Add custom extern(C) main method for running D's unittests
}
void main(string[] args)
{
import std.getopt;
import std.variant : Algebraic, visit;
string inputDir;
string outputDir = "./out";
string ignoredFilesStr;
string modulePrefix;
string attributesStr;
string ignVersionsStr;
VisitorConfig visitorConfig;
auto helpInfo = getopt(args, config.required,
"inputdir|i", "Folder to start the recursive search for unittest blocks (can be a single file)", &inputDir,
"outputdir|o", "Folder to which the extracted test files should be saved (stdout for a single file)", &outputDir,
"ignore", "Comma-separated list of files to exclude (partial matching is supported)", &ignoredFilesStr,
"attributes|a", "Comma-separated list of UDAs that the unittest should have", &attributesStr,
"undefinedVersions", "Comma-separated list of undefined versions", &ignVersionsStr,
"betterC", "Add custom extern(C) main method for running D's unittests", &visitorConfig.betterCOutput,
);
if (helpInfo.helpWanted)
{
return defaultGetoptPrinter(`phobos_tests_extractor
Searches the input directory recursively for public unittest blocks, i.e.
unittest blocks that are annotated with three slashes (///).
The tests will be extracted as one file for each source file
to in the output directory.
`, helpInfo.options);
}
inputDir = inputDir.asNormalizedPath.array;
Algebraic!(string, File) outputLocation = cast(string) outputDir.asNormalizedPath.array;
visitorConfig.attributes = attributesStr.split(",");
visitorConfig.ignoredVersions = ignVersionsStr.split(",");
if (!exists(outputDir))
mkdir(outputDir);
// if the module prefix is std -> add a dot for the next modules to follow
if (!modulePrefix.empty)
modulePrefix ~= '.';
DirEntry[] files;
if (inputDir.isFile)
{
files = [DirEntry(inputDir)];
inputDir = ".";
// for single files use stdout by default
if (outputDir == "./out")
{
outputLocation = stdout;
}
}
else
{
files = dirEntries(inputDir, SpanMode.depth).filter!(
a => a.name.endsWith(".d") && !a.name.canFind(".git")).array;
}
auto ignoringFiles = ignoredFilesStr.split(",");
foreach (file; files)
{
if (!ignoringFiles.any!(x => file.name.canFind(x)))
{
stderr.writeln("parsing ", file);
outputLocation.visit!(
(string outputFolder) => parseFileDir(inputDir, file, outputFolder, visitorConfig),
(File outputFile) => parseFile(File(file.name, "r"), outputFile, visitorConfig),
);
}
else
{
stderr.writeln("ignoring ", file);
}
}
}
/**
The location of unittest token is known, but there might be attributes preceding it.
*/
private size_t getAttributesStartLocation(const Attribute[] attrs)
{
import dparse.lexer : tok;
if (attrs.length == 0)
return size_t.max;
if (attrs[0].atAttribute !is null)
return attrs[0].atAttribute.startLocation;
if (attrs[0].attribute != tok!"")
return attrs[0].attribute.index;
return size_t.max;
}
private size_t skipPreviousWord(const(ubyte)[] sourceCode, size_t index)
{
return index - sourceCode[0 .. index]
.retro
.enumerate
.find!(c => !whitespace.canFind(c.value))
.find!(c => whitespace.canFind(c.value))
.front.index;
}