-
Notifications
You must be signed in to change notification settings - Fork 97
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
Changes from 9 commits
ab22289
e9ca54b
019450d
1b65c4e
83afd27
f38bfdc
d741e97
a75d816
c816619
10bef7b
59e8e66
5c4eda9
89baa35
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> { | ||
|
@@ -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 | ||
|
@@ -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"); | ||
} | ||
|
||
|
@@ -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"); | ||
} | ||
|
||
|
@@ -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 + "\""; | ||
} | ||
|
||
/** | ||
* @description | ||
* Checks to see if the options specify a directory to run the | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You did not add There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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); | ||
|
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; | ||
} | ||
} | ||
} |
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-")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. Only do this when 'showCodeLens' is true. |
||
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) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only do this when 'showCodeLens' is true There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
}); | ||
} | ||
} |
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 "✔️"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks great to me! |
||
} else if (outcome === "Failed") { | ||
return "❌"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 "️️⚠️"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
}; | ||
} | ||
} |
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[]> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. 😃 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} |
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.
return empty string when 'showCodeLens' is false