Skip to content
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

test_extractor: Allow compile-only unittests for certain version's #438

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions tests_extractor.d
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class TestVisitor : ASTVisitor
ubyte[] sourceCode;
string moduleName;
VisitorConfig config;
bool compileOnly;

this(File outFile, ubyte[] sourceCode, VisitorConfig config)
{
Expand Down Expand Up @@ -78,13 +79,17 @@ class TestVisitor : ASTVisitor

override void visit(const ConditionalDeclaration decl)
{
const compileOnlySave = this.compileOnly;
scope (exit) this.compileOnly = compileOnlySave;

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);
this.compileOnly = this.compileOnly || config.compileOnlyVersions.canFind(id);
}

// search if/version block
Expand Down Expand Up @@ -179,11 +184,14 @@ private:
}

// write the unittest block
const pre = compileOnly ? " if (false) {" : "";
const post = compileOnly ? "}" : "";

if (config.betterCOutput)
outFile.writef("void unittest_line_%s()\n{\n", line);
outFile.writef("void unittest_line_%s()\n{%s\n", line, pre);
else
outFile.write("unittest\n{\n");
scope(exit) outFile.writeln("}\n");
outFile.writef("unittest\n{%s\n", pre);
scope(exit) outFile.writeln(post, "}\n");

// add an import to the current module
outFile.writefln(" import %s;", moduleName);
Expand Down Expand Up @@ -245,6 +253,7 @@ struct VisitorConfig
{
string[] attributes; /// List of attributes to extract;
string[] ignoredVersions; /// List of disabled version conditions
string[] compileOnlyVersions; /// List of version's whose contained unittests should only be compiled (but not run)
bool betterCOutput; /// Add custom extern(C) main method for running D's unittests
}

Expand All @@ -259,6 +268,7 @@ void main(string[] args)
string modulePrefix;
string attributesStr;
string ignVersionsStr;
string compileOnlyStr;
VisitorConfig visitorConfig;

auto helpInfo = getopt(args, config.required,
Expand All @@ -267,6 +277,7 @@ void main(string[] args)
"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,
"compileOnlyVersions", "Comma-separated list of versions whose tests should only be compiled", &compileOnlyStr,
"betterC", "Add custom extern(C) main method for running D's unittests", &visitorConfig.betterCOutput,
);

Expand All @@ -284,6 +295,7 @@ to in the output directory.
Algebraic!(string, File) outputLocation = cast(string) outputDir.asNormalizedPath.array;
visitorConfig.attributes = attributesStr.split(",");
visitorConfig.ignoredVersions = ignVersionsStr.split(",");
visitorConfig.compileOnlyVersions = compileOnlyStr.split(",");

if (!exists(outputDir))
mkdir(outputDir);
Expand Down