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

Support testing specific projects #372

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,17 @@
"description": "Whether to enable anonymous usage collection."
},
"dotnet-test-explorer.testProjectPath": {
"type": "string",
"anyOf": [
{
"type": "string"
},
{
"type": "array",
"items": {
"type": "string"
}
}
],
"default": "",
"description": "Glob pattern that points to path of .NET Core test project(s).",
"examples": [
Expand Down
32 changes: 23 additions & 9 deletions src/testCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,25 @@ export class TestCommands implements Disposable {
this.isRunning = false;
}

private runBuildCommandForSpecificDirectory(testDirectoryPath: string): Promise<any> {
private runBuildCommandForSpecificProject(testProjectPath: string): Promise<any> {
return new Promise<void>((resolve, reject) => {

if (Utility.skipBuild) {
Logger.Log(`User has passed --no-build, skipping build`);
resolve();
} else {
Logger.Log(`Executing dotnet build in ${testDirectoryPath}`);
Logger.Log(`Executing dotnet build in ${testProjectPath}`);

Executor.exec("dotnet build", (err: any, stdout: string) => {
const isDirectory = fs.lstatSync(testProjectPath).isDirectory();
const command = isDirectory
? `dotnet build`
: `dotnet build ${testProjectPath}`;

const testDirectoryPath = isDirectory
? testProjectPath
: path.dirname(testProjectPath);

Executor.exec(command, (err: any, stdout: string) => {
if (err) {
reject(new Error("Build command failed"));
}
Expand All @@ -247,13 +256,18 @@ export class TestCommands implements Disposable {
});
}

private runTestCommandForSpecificDirectory(testDirectoryPath: string, testName: string, isSingleTest: boolean, index: number, debug?: boolean): Promise<any[]> {

const trxTestName = index + ".trx";
private runTestCommandForSpecificDirectory(testProjectPath: string, testName: string, isSingleTest: boolean, index: number, debug?: boolean): Promise<any[]> {

return new Promise((resolve, reject) => {
const testResultFile = path.join(this.testResultsFolder, trxTestName);
let command = `dotnet test${Utility.additionalArgumentsOption} --no-build --logger \"trx;LogFileName=${testResultFile}\"`;
let command = `dotnet test${Utility.additionalArgumentsOption} --no-build --logger trx -r ${this.testResultsFolder}`;

const isDirectory = fs.lstatSync(testProjectPath).isDirectory();
if (!isDirectory)
command += ` ${testProjectPath}`;

const testDirectoryPath = isDirectory
? testProjectPath
: path.dirname(testProjectPath);

if (testName && testName.length) {
if (isSingleTest) {
Expand All @@ -263,7 +277,7 @@ export class TestCommands implements Disposable {
}
}

this.runBuildCommandForSpecificDirectory(testDirectoryPath)
this.runBuildCommandForSpecificProject(testProjectPath)
.then(() => {
Logger.Log(`Executing ${command} in ${testDirectoryPath}`);

Expand Down
36 changes: 17 additions & 19 deletions src/testDirectories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,24 @@ export class TestDirectories {
return;
}

const testDirectoryGlob = Utility.getConfiguration().get<string>("testProjectPath");
const testProjectsConfiguration = Utility.getConfiguration().get("testProjectPath");
const testProjectsArray = [].concat(testProjectsConfiguration);

const matchingDirs = [];

vscode.workspace.workspaceFolders.forEach((folder) => {

const globPattern = folder.uri.fsPath.replace("\\", "/") + "/" + testDirectoryGlob;
testProjectsArray.forEach(testProjectGlob => {
const globPattern = folder.uri.fsPath.replace("\\", "/") + "/" + testProjectGlob;

Logger.Log(`Finding projects for pattern ${globPattern}`);
Logger.Log(`Finding projects for pattern ${globPattern}`);

const matchingDirsForWorkspaceFolder = glob.sync(globPattern);
const matchingDirsForWorkspaceFolder = glob.sync(globPattern);

matchingDirs.push(...matchingDirsForWorkspaceFolder);
matchingDirs.push(...matchingDirsForWorkspaceFolder);

Logger.Log(`Found ${matchingDirsForWorkspaceFolder.length} matches for pattern in folder ${folder.uri.fsPath}`);
Logger.Log(`Found ${matchingDirsForWorkspaceFolder.length} matches for pattern in folder ${folder.uri.fsPath}`);
})
});

this.directories = evaluateTestDirectories(matchingDirs);
Expand Down Expand Up @@ -66,28 +69,23 @@ export class TestDirectories {

}
function evaluateTestDirectories(testDirectories: string[]): string[] {
const directories = [];
const directoriesSet = new Set<string>();
const projectPaths = [];
const projectPathsSet = new Set<string>();

for (let testProjectFullPath of testDirectories) {
Logger.Log(`Evaluating match ${testProjectFullPath}`);

if (!fs.existsSync(testProjectFullPath)) {
Logger.LogWarning(`Path ${testProjectFullPath} is not valid`);
} else {

if (fs.lstatSync(testProjectFullPath).isFile()) {
testProjectFullPath = path.dirname(testProjectFullPath);
}

if (glob.sync(`${testProjectFullPath}/+(*.csproj|*.sln|*.fsproj)`).length < 1) {
Logger.LogWarning(`Skipping path ${testProjectFullPath} since it does not contain something we can build (.sln, .csproj, .fsproj)`);
} else if (!directoriesSet.has(testProjectFullPath)) {
if (fs.lstatSync(testProjectFullPath).isDirectory() && glob.sync(`${testProjectFullPath}/+(*.csproj|*.sln|*.slnf|*.fsproj)`).length < 1) {
Logger.LogWarning(`Skipping path ${testProjectFullPath} since it does not contain something we can build (.sln, .slnf, .csproj, .fsproj)`);
} else if (!projectPathsSet.has(testProjectFullPath)) {
Logger.Log(`Adding directory ${testProjectFullPath}`);
directories.push(testProjectFullPath);
directoriesSet.add(testProjectFullPath);
projectPaths.push(testProjectFullPath);
projectPathsSet.add(testProjectFullPath);
}
}
}
return directories;
return projectPaths;
}
18 changes: 15 additions & 3 deletions src/testDiscovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,16 @@ export function discoverTests(testDirectoryPath: string, dotnetTestOptions: stri
});
}

function executeDotnetTest(testDirectoryPath: string, dotnetTestOptions: string): Promise<string> {
function executeDotnetTest(testProjectPath: string, dotnetTestOptions: string): Promise<string> {
return new Promise((resolve, reject) => {
const command = `dotnet test -t -v=q${dotnetTestOptions}`;
const isDirectory = fs.lstatSync(testProjectPath).isDirectory();
const command = isDirectory
? `dotnet test -t -v=q${dotnetTestOptions}`
: `dotnet test -t -v=q${dotnetTestOptions} ${testProjectPath}`;

const testDirectoryPath = isDirectory
? testProjectPath
: path.dirname(testProjectPath);

Logger.Log(`Executing ${command} in ${testDirectoryPath}`);

Expand Down Expand Up @@ -169,11 +176,16 @@ function cleanTestOutput(testOutputFilePath: string) {
fs.rmdirSync(path.dirname(testOutputFilePath));
}

function executeDotnetVstest(assemblyPaths: string[], listTestsTargetPath: string, testDirectoryPath: string): Promise<string> {
function executeDotnetVstest(assemblyPaths: string[], listTestsTargetPath: string, testProjectPath: string): Promise<string> {
return new Promise((resolve, reject) => {
const testAssembliesParam = assemblyPaths.map((f) => `"${f}"`).join(" ");
const command = `dotnet vstest ${testAssembliesParam} /ListFullyQualifiedTests /ListTestsTargetPath:"${listTestsTargetPath}"`;

const isDirectory = fs.lstatSync(testProjectPath).isDirectory();
const testDirectoryPath = isDirectory
? testProjectPath
: path.dirname(testProjectPath);

Logger.Log(`Executing ${command} in ${testDirectoryPath}`);

Executor.exec(
Expand Down