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

Display test results in CodeLens #24

Merged
merged 13 commits into from
Nov 26, 2017
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@
"type": "boolean",
"default": true,
"description": "If true, dotnet restore will run when refreshing the test explorer."
},
"dotnet-test-explorer.showCodeLens": {
"type": "boolean",
"default": true,
"description": "Determines whether to show the CodeLens test status or not."
}
}
}
Expand All @@ -131,6 +136,7 @@
"vscode": "^1.0.0"
},
"dependencies": {
"applicationinsights": "^0.21.0"
"applicationinsights": "^0.21.0",
"xmldom": "^0.1.27"
}
}
27 changes: 22 additions & 5 deletions src/dotnetTestExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TreeDataProvider, TreeItem, TreeItemCollapsibleState } from "vscode";
import { AppInsightsClient } from "./appInsightsClient";
import { Executor } from "./executor";
import { TestNode } from "./testNode";
import { TestResultsFile } from "./testResultsFile";
import { Utility } from "./utility";

export class DotnetTestExplorer implements TreeDataProvider<TestNode> {
Expand All @@ -16,7 +17,7 @@ export class DotnetTestExplorer implements TreeDataProvider<TestNode> {
*/
private testDirectoryPath: string;

constructor(private context: vscode.ExtensionContext) {}
constructor(private context: vscode.ExtensionContext, private resultsFile: TestResultsFile) { }

/**
* @description
Expand All @@ -41,7 +42,7 @@ export class DotnetTestExplorer implements TreeDataProvider<TestNode> {
*/
public runAllTests(): void {
this.evaluateTestDirectory();
Executor.runInTerminal(`dotnet test${this.checkBuildOption()}${this.checkRestoreOption()}`, this.testDirectoryPath);
Executor.runInTerminal(`dotnet test${this.getDotNetTestOptions()}${this.outputTestResults()}`, this.testDirectoryPath);
AppInsightsClient.sendEvent("runAllTests");
}

Expand All @@ -53,7 +54,7 @@ export class DotnetTestExplorer implements TreeDataProvider<TestNode> {
* to do a restore, so it can be very slow.
*/
public runTest(test: TestNode): void {
Executor.runInTerminal(`dotnet test${this.checkBuildOption()}${this.checkRestoreOption()} --filter FullyQualifiedName~${test.fullName}`, this.testDirectoryPath);
Executor.runInTerminal(`dotnet test${this.getDotNetTestOptions()}${this.outputTestResults()} --filter FullyQualifiedName~${test.fullName}`, this.testDirectoryPath);
AppInsightsClient.sendEvent("runTest");
}

Expand Down Expand Up @@ -175,6 +176,22 @@ export class DotnetTestExplorer implements TreeDataProvider<TestNode> {
return option ? "" : " --no-restore";
}

/**
* @description
* Gets the options for build/restore before running tests.
*/
private getDotNetTestOptions(): string {
return this.checkBuildOption() + this.checkRestoreOption();
}

/**
* @description
* Gets the dotnet test argument to speicfy the output for the test results.
*/
private outputTestResults(): string {
return " --logger \"trx;LogFileName=" + this.resultsFile.fileName + "\"";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return empty string when 'showCodeLens' is false

}

/**
* @description
* Checks to see if the options specify a directory to run the
Expand All @@ -198,7 +215,7 @@ export class DotnetTestExplorer implements TreeDataProvider<TestNode> {
return new Promise((c, e) => {
try {
const results = Executor
.execSync(`dotnet test -t -v=q${this.checkBuildOption()}${this.checkRestoreOption()}`, this.testDirectoryPath)
.execSync(`dotnet test -t -v=q${this.getDotNetTestOptions()}`, this.testDirectoryPath)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You did not add ${this.outputTestResults()}, so the test result will not be shown when VS Code starts. Is it by design?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, please ignore. It only lists the tests.

.split(/[\r\n]+/g)
/*
* The dotnet-cli prefixes all discovered unit tests
Expand All @@ -207,7 +224,7 @@ export class DotnetTestExplorer implements TreeDataProvider<TestNode> {
* structures.
**/
.filter((item) => item && item.startsWith(" "))
.sort((a, b) => a > b ? 1 : b > a ? - 1 : 0 )
.sort((a, b) => a > b ? 1 : b > a ? - 1 : 0)
.map((item) => item.trim());

c(results);
Expand Down
14 changes: 13 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@ import { AppInsightsClient } from "./appInsightsClient";
import { DotnetTestExplorer } from "./dotnetTestExplorer";
import { Executor } from "./executor";
import { TestNode } from "./testNode";
import { TestResultsFile } from "./testResultsFile";
import { TestStatusCodeLensProvider } from "./testStatusCodeLensProvider";
import { Utility } from "./utility";

export function activate(context: vscode.ExtensionContext) {
const dotnetTestExplorer = new DotnetTestExplorer(context);
const testResults = new TestResultsFile();
context.subscriptions.push(testResults);

const dotnetTestExplorer = new DotnetTestExplorer(context, testResults);
vscode.window.registerTreeDataProvider("dotnetTestExplorer", dotnetTestExplorer);
AppInsightsClient.sendEvent("loadExtension");

const codeLensProvider = new TestStatusCodeLensProvider(testResults);
context.subscriptions.push(codeLensProvider);
context.subscriptions.push(vscode.languages.registerCodeLensProvider(
{ language: "csharp", scheme: "file" },
codeLensProvider));

context.subscriptions.push(vscode.commands.registerCommand("dotnet-test-explorer.refreshTestExplorer", () => {
dotnetTestExplorer.refreshTestExplorer();
}));
Expand Down
37 changes: 37 additions & 0 deletions src/testResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export class TestResult {
private className: string;
private method: string;

public constructor(private _testId: string, private _outcome: string) {
}

public get fullName(): string {
return this.className + "." + this.method;
}

public get id(): string {
return this._testId;
}

public get outcome(): string {
return this._outcome;
}

public matches(className: string, method: string): boolean {
// The passed in class name won't have the namespace, hence the
// check with endsWith
return (this.method === method) && this.className.endsWith(className);
}

public updateName(className: string, method: string): void {
this.className = className;

// xUnit includes the class name in the name of the unit test
// i.e. classname.method
if (method.startsWith(className)) {
this.method = method.substring(className.length + 1); // +1 to include the .
} else {
this.method = method;
}
}
}
124 changes: 124 additions & 0 deletions src/testResultsFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
"use strict";
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import { setTimeout } from "timers";
import { Disposable, Event, EventEmitter } from "vscode";
import { DOMParser, Element, Node } from "xmldom";
import { TestResult } from "./testResult";

function findChildElement(node: Node, name: string): Node {
let child = node.firstChild;
while (child) {
if (child.nodeName === name) {
return child;
}

child = child.nextSibling;
}

return null;
}

function getAttributeValue(node: Node, name: string): string {
const attribute = node.attributes.getNamedItem(name);
return (attribute === null) ? null : attribute.nodeValue;
}

function parseUnitTestResults(xml: Element): TestResult[] {
const results: TestResult[] = [];
const nodes = xml.getElementsByTagName("UnitTestResult");

// TSLint wants to use for-of here, but nodes doesn't support it
for (let i = 0; i < nodes.length; i++) { // tslint:disable-line
results.push(new TestResult(
getAttributeValue(nodes[i], "testId"),
getAttributeValue(nodes[i], "outcome"),
));
}

return results;
}

function updateUnitTestDefinitions(xml: Element, results: TestResult[]): void {
const nodes = xml.getElementsByTagName("UnitTest");
const names = new Map<string, any>();

for (let i = 0; i < nodes.length; i++) { // tslint:disable-line
const id = getAttributeValue(nodes[i], "id");
const testMethod = findChildElement(nodes[i], "TestMethod");
if (testMethod) {
names.set(id, {
className: getAttributeValue(testMethod, "className"),
method: getAttributeValue(testMethod, "name"),
});
}
}

for (const result of results) {
const name = names.get(result.id);
if (name) {
result.updateName(name.className, name.method);
}
}
}

export class TestResultsFile implements Disposable {
private static readonly ResultsFileName = "Results.trx";
private onNewResultsEmitter = new EventEmitter<TestResult[]>();
private resultsFile: string;
private watcher: fs.FSWatcher;

public constructor() {
const tempFolder = fs.mkdtempSync(path.join(os.tmpdir(), "test-explorer-"));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Only do this when 'showCodeLens' is true.
Just want to make sure no unnecessary code is running

this.resultsFile = path.join(tempFolder, TestResultsFile.ResultsFileName);

// The change event gets called multiple times, so use a one-second
// delay before we read anything to avoid doing too much work
const me = this;
let changeDelay: NodeJS.Timer;
this.watcher = fs.watch(tempFolder, (eventType, fileName) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only do this when 'showCodeLens' is true

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, done

if ((fileName === TestResultsFile.ResultsFileName) && (eventType === "change")) {
clearTimeout(changeDelay);
changeDelay = setTimeout(() => {
me.parseResults();
}, 1000);
}
});
}

public dispose(): void {
try {
this.watcher.close();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also handle when when 'showCodeLens' is false


// When we ask for a random directory it creates one for us,
// however, we can't delete it if there's a file inside of it
if (fs.existsSync(this.resultsFile)) {
fs.unlinkSync(this.resultsFile);
}

fs.rmdir(path.dirname(this.resultsFile));
} catch (error) {
}
}

public get fileName(): string {
return this.resultsFile;
}

public get onNewResults(): Event<TestResult[]> {
return this.onNewResultsEmitter.event;
}

private parseResults(): void {
const emitter = this.onNewResultsEmitter;
fs.readFile(this.resultsFile, (err, data) => {
if (!err) {
const xdoc = new DOMParser().parseFromString(data.toString(), "application/xml");
const results = parseUnitTestResults(xdoc.documentElement);
updateUnitTestDefinitions(xdoc.documentElement, results);
emitter.fire(results);
}
});
}
}
25 changes: 25 additions & 0 deletions src/testStatusCodeLens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use strict";
import { CodeLens, Range } from "vscode";

export class TestStatusCodeLens extends CodeLens {
public static parseOutcome(outcome: string): string {
if (outcome === "Passed") {
return "✔️";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test on macOS, it is showing a black check mark (https://emojipedia.org/heavy-check-mark/) which is hard to see.
Two improvements:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added settings now so the user can override what we display.

I've kept the heavy check mark for platforms apart from OS X, in which case it uses the white heavy check mark (I'm not a big fan of it, but I can't find any alternatives).

Since the user can specify the text, they could even switch to "passing", "FAILING" etc if they want so hopefully this is OK?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me!

} else if (outcome === "Failed") {
return "❌";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, using unicode: https://emojipedia.org/cross-mark/

} else if (outcome === "NotExecuted") {
return "️️⚠️";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, using unicode

} else {
return null;
}
}

public constructor(range: Range, status: string) {
super(range);

this.command = {
title: status,
command: null,
};
}
}
78 changes: 78 additions & 0 deletions src/testStatusCodeLensProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"use strict";
import { CancellationToken, CodeLens, CodeLensProvider, commands, Disposable, Event, EventEmitter, Range, SymbolInformation, SymbolKind, TextDocument, workspace } from "vscode";
import { TestResult } from "./testResult";
import { TestResultsFile } from "./testResultsFile";
import { TestStatusCodeLens } from "./testStatusCodeLens";
import { Utility } from "./utility";

export class TestStatusCodeLensProvider implements CodeLensProvider {
private disposables: Disposable[] = [];
private enabled: boolean;
private onDidChangeCodeLensesEmitter = new EventEmitter<void>();

// Store everything in a map so we can remember old tests results for the
// scenario where a single test is ran. If the test no longer exists in
// code it will never be mapped to the symbol, so no harm (though there is
// a memory impact)
private testResults = new Map<string, TestResult>();

public constructor(testResultFile: TestResultsFile) {
this.checkEnabledOption();

this.disposables.push(
testResultFile.onNewResults(this.addTestResults, this));

this.disposables.push(
workspace.onDidChangeConfiguration(this.checkEnabledOption, this));
}

public dispose() {
while (this.disposables.length) {
this.disposables.pop().dispose();
}
}

public get onDidChangeCodeLenses(): Event<void> {
return this.onDidChangeCodeLensesEmitter.event;
}

public provideCodeLenses(document: TextDocument, token: CancellationToken): CodeLens[] | Thenable<CodeLens[]> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try with xUnit, NUnit and MSTest. Seems only xUnit is showing the test result icon. It would be great if you could also support NUnit and MSTest. 😃

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should now be fixed in the latest version, I've tried xUnit, MSTest and NUnit (the later two have the same format)

if (!this.enabled) {
return [];
}

const results = this.testResults;
return commands.executeCommand<SymbolInformation[]>("vscode.executeDocumentSymbolProvider", document.uri)
.then((symbols) => {
const mapped: CodeLens[] = [];
for (const symbol of symbols.filter((x) => x.kind === SymbolKind.Method)) {
for (const result of results.values()) {
if (result.matches(symbol.containerName, symbol.name)) {
const state = TestStatusCodeLens.parseOutcome(result.outcome);
if (state) {
mapped.push(new TestStatusCodeLens(symbol.location.range, state));
}
}
}
}

return mapped;
});
}

public resolveCodeLens(codeLens: CodeLens, token: CancellationToken): CodeLens {
return codeLens;
}

private addTestResults(results: TestResult[]) {
for (const result of results) {
this.testResults.set(result.fullName, result);
}

this.onDidChangeCodeLensesEmitter.fire();
}

private checkEnabledOption(): void {
this.enabled = Utility.getConfiguration().get<boolean>("showCodeLens", true);
}
}