-
-
Notifications
You must be signed in to change notification settings - Fork 144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Phobos public tests extractor script #203
Merged
andralex
merged 3 commits into
dlang:master
from
wilzbach:add-check-for-public-unittests
Dec 15, 2016
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
#!/usr/bin/env dub | ||
/+ dub.sdl: | ||
name "check_phobos" | ||
dependency "libdparse" version="~>0.7.0-beta.2" | ||
+/ | ||
/* | ||
* Parses all public unittests that are visible on dlang.org | ||
* (= annotated with three slashes) | ||
* | ||
* Copyright (C) 2016 by D Language Foundation | ||
* | ||
* 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.conv; | ||
import std.exception; | ||
import std.experimental.logger; | ||
import std.file; | ||
import std.path; | ||
import std.range; | ||
import std.regex; | ||
import std.stdio; | ||
import std.string; | ||
|
||
class TestVisitor : ASTVisitor | ||
{ | ||
File outFile; | ||
ubyte[] sourceCode; | ||
string moduleName; | ||
|
||
this(string outFileName, string moduleName, ubyte[] sourceCode) | ||
{ | ||
this.outFile = File(outFileName, "w"); | ||
this.moduleName = moduleName; | ||
this.sourceCode = sourceCode; | ||
} | ||
|
||
alias visit = ASTVisitor.visit; | ||
|
||
override void visit(const Unittest u) | ||
{ | ||
// scan the previous line for ddoc header | ||
auto prevLine = sourceCode[0 .. u.location].retro; | ||
prevLine.findSkip("\n"); // skip forward to the previous line | ||
auto ddocCommentSlashes = prevLine.until('\n').count('/'); | ||
|
||
// only look for comments annotated with three slashes (///) | ||
if (ddocCommentSlashes != 3) | ||
return; | ||
|
||
// write the origin source code line | ||
outFile.writefln("// Line %d", u.line); | ||
|
||
// 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; | ||
} | ||
} | ||
|
||
void parseTests(string fileName, string moduleName, string outFileName) | ||
{ | ||
import dparse.lexer; | ||
import dparse.parser; | ||
import dparse.rollback_allocator; | ||
import std.array : uninitializedArray; | ||
|
||
assert(exists(fileName)); | ||
|
||
File f = File(fileName); | ||
|
||
if (f.size == 0) | ||
{ | ||
warningf("%s is empty", fileName); | ||
return; | ||
} | ||
|
||
ubyte[] sourceCode = uninitializedArray!(ubyte[])(to!size_t(f.size)); | ||
f.rawRead(sourceCode); | ||
LexerConfig config; | ||
StringCache cache = StringCache(StringCache.defaultBucketCount); | ||
auto tokens = getTokensForParser(sourceCode, config, &cache); | ||
RollbackAllocator rba; | ||
Module m = parseModule(tokens.array, fileName, &rba); | ||
auto visitor = new TestVisitor(outFileName, moduleName, sourceCode); | ||
visitor.visit(m); | ||
} | ||
|
||
void parseFile(string inputDir, string fileName, string outputDir, string modulePrefix = "") | ||
{ | ||
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 its module path, e.g. std/uni.d -> std.uni | ||
string moduleName = modulePrefix ~ fileNameNormalized.replace(".d", "") | ||
.replace(dirSeparator, ".") | ||
.replace(".package", ""); | ||
|
||
// convert the file path to a nice output file, e.g. std/uni.d -> std_uni.d | ||
string outName = fileNameNormalized.replace(dirSeparator, "_"); | ||
|
||
parseTests(fileName, moduleName, buildPath(outputDir, outName)); | ||
} | ||
|
||
void main(string[] args) | ||
{ | ||
import std.getopt; | ||
|
||
string inputDir; | ||
string outputDir = "./out"; | ||
string ignoredFilesStr; | ||
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", &outputDir, | ||
"moduleprefix", "Module prefix to use for all files (e.g. std.algorithm)", &modulePrefix, | ||
"ignore", "Comma-separated list of files to exclude (partial matching is supported)", &ignoredFilesStr); | ||
|
||
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; | ||
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) | ||
{ | ||
files = [DirEntry(inputDir)]; | ||
inputDir = "."; | ||
} | ||
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))) | ||
{ | ||
writeln("parsing ", file); | ||
parseFile(inputDir, file, outputDir, modulePrefix); | ||
} | ||
else | ||
{ | ||
writeln("ignoring ", file); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On thing I'd like to add to our usual style guidelines is imports should be ordered alphabetically (it did happen at least to me to import something twice when the imports list is long). Also, I wonder how our opinionated community would feel about preferring comma-separated imports instead of one per line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea -> dlang-community/D-Scanner#382
I personally prefer line-separated imports because it's easy to add/remove them and the git log is clean.
For such scripts one wants to do sth. like
import std.*
- I have seen quite many people having their convenience scripting package that public imports most of Phobos