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
|
#!/usr/bin/env dub
/++dub.sdl:
name "tests_extractor"
dependency "libdparse" version="~>0.24.0"
dflags "-fall-instantiations" platform="gdc"
+/
// Written in the D programming language.
import dparse.ast;
import std.algorithm;
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;
this(File outFile, ubyte[] sourceCode)
{
this.outFile = outFile;
this.sourceCode = sourceCode;
}
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);
}
override void visit(const Declaration decl)
{
if (decl.unittest_ !is null && decl.unittest_.comment !is null)
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)
{
if (vcd.token.text == "StdDdoc")
skipTrue = true;
}
// Search if/version block
if (!skipTrue)
{
foreach (d; decl.trueDeclarations)
visit(d);
}
// Search else block
foreach (d; decl.falseDeclarations)
visit(d);
}
private:
void print(const Unittest u, const Attribute[] attributes)
{
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
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;
}
}
bool parseFile(File inFile, File outFile)
{
import dparse.lexer;
import dparse.parser : parseModule;
import dparse.rollback_allocator : RollbackAllocator;
import std.array : uninitializedArray;
if (inFile.size == 0)
return false;
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);
visitor.visit(m);
return visitor.outFile.size != 0;
}
void parseFileDir(string inputDir, string fileName, string outputDir)
{
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, "_");
auto outFile = buildPath(outputDir, outName);
// Removes the output file if nothing was written
if (!parseFile(File(fileName), File(outFile, "w")))
remove(outFile);
}
void main(string[] args)
{
import std.getopt;
string inputDir;
string outputDir = "./out";
string modulePrefix;
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,
);
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 the output directory.
`, helpInfo.options);
}
inputDir = inputDir.asNormalizedPath.array;
outputDir= outputDir.asNormalizedPath.array;
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)
{
stderr.writeln("ignoring ", inputDir);
return;
}
else
{
files = dirEntries(inputDir, SpanMode.depth).filter!(
a => a.name.endsWith(".d") && !a.name.canFind(".git")).array;
}
foreach (file; files)
{
stderr.writeln("parsing ", file);
parseFileDir(inputDir, file, outputDir);
}
}
|