-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ab22289
Add support for monitoring when there are new test results
samcragg e9ca54b
Parse TRX file
samcragg 019450d
Add CodeLens support
samcragg 1b65c4e
Fix TSLint errors
samcragg 83afd27
Code review fixes
samcragg f38bfdc
Add support for MSTest/NUnit
samcragg d741e97
Fix passing parameters to dotnet test
samcragg a75d816
Add setting for CodeLens status
samcragg c816619
Support multiple instances
samcragg 10bef7b
Reduce amount of work if CodeLens is not eneabled
samcragg 59e8e66
Add ability to customise CodeLens display text
samcragg 5c4eda9
TSLint alterations
samcragg 89baa35
Check the correct CodeLens settings
samcragg 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
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,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; | ||
} | ||
} | ||
} |
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,134 @@ | ||
"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 dispose(): void { | ||
try { | ||
if (this.watcher) { | ||
this.watcher.close(); | ||
} | ||
|
||
if (this.resultsFile) { | ||
// 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 { | ||
this.ensureTemproaryPathExists(); | ||
return this.resultsFile; | ||
} | ||
|
||
public get onNewResults(): Event<TestResult[]> { | ||
return this.onNewResultsEmitter.event; | ||
} | ||
|
||
private ensureTemproaryPathExists(): void { | ||
if (!this.resultsFile) { | ||
const tempFolder = fs.mkdtempSync(path.join(os.tmpdir(), "test-explorer-")); | ||
this.resultsFile = path.join(tempFolder, TestResultsFile.ResultsFileName); | ||
this.watchFolder(tempFolder); | ||
} | ||
} | ||
|
||
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); | ||
} | ||
}); | ||
} | ||
|
||
private watchFolder(folder: string): void { | ||
// 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(folder, (eventType, fileName) => { | ||
if (fileName === TestResultsFile.ResultsFileName) { | ||
clearTimeout(changeDelay); | ||
changeDelay = setTimeout(() => { | ||
me.parseResults(); | ||
}, 1000); | ||
} | ||
}); | ||
} | ||
} |
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,26 @@ | ||
"use strict"; | ||
import { CodeLens, Range } from "vscode"; | ||
import { Utility } from "./utility"; | ||
|
||
export class TestStatusCodeLens extends CodeLens { | ||
public static parseOutcome(outcome: string): string { | ||
if (outcome === "Passed") { | ||
return Utility.codeLensPassed; | ||
} else if (outcome === "Failed") { | ||
return Utility.codeLensFailed; | ||
} else if (outcome === "NotExecuted") { | ||
return Utility.codeLensSkipped; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
public constructor(range: Range, status: string) { | ||
super(range); | ||
|
||
this.command = { | ||
title: status, | ||
command: null, | ||
}; | ||
} | ||
} |
Oops, something went wrong.
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.
You did not add
${this.outputTestResults()}
, so the test result will not be shown when VS Code starts. Is it by design?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.
Oh, please ignore. It only lists the tests.