From be706083986187fab87ffa78f07c341aa166e2b7 Mon Sep 17 00:00:00 2001 From: Connor Peet Date: Mon, 24 May 2021 10:12:32 -0700 Subject: [PATCH] enhancements! --- test-provider-sample/package.json | 8 ++-- test-provider-sample/src/testController.ts | 49 ++++++++++++++-------- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/test-provider-sample/package.json b/test-provider-sample/package.json index 87b8e75f..d24f83af 100644 --- a/test-provider-sample/package.json +++ b/test-provider-sample/package.json @@ -1,8 +1,7 @@ { - "enableProposedApi": true, "name": "test-provider-sample", "displayName": "test-provider-sample", - "description": "Sample showing how to use Proposed API", + "description": "Sample showing how to use the test APIs", "version": "0.0.1", "publisher": "vscode-samples", "repository": "https://github.com/Microsoft/vscode-extension-samples", @@ -13,15 +12,14 @@ "Other" ], "activationEvents": [ - "*" + "workspaceContains:*.md" ], "main": "./out/extension.js", "contributes": { "commands": [ { "command": "test-provider-sample.runTests", - "title": "Custom Run Tests", - "icon": "$(debug-start)" + "title": "Custom Run Tests" } ], "views": { diff --git a/test-provider-sample/src/testController.ts b/test-provider-sample/src/testController.ts index 8906c021..ffccf10b 100644 --- a/test-provider-sample/src/testController.ts +++ b/test-provider-sample/src/testController.ts @@ -18,6 +18,10 @@ export class MathTestController implements vscode.TestController, cancellation: vscode.CancellationToken) { const run = vscode.test.createTestRun(request); - const runTests = async (tests: Iterable>) => { + const queue: vscode.TestItem[] = []; + const discoverTests = async (tests: Iterable>) => { for (const test of tests) { if (request.exclude?.includes(test)) { continue; } if (test.data instanceof TestCase) { - run.appendOutput(`Running ${test.id}\r\n`); - if (cancellation.isCancellationRequested) { - run.setState(test, vscode.TestResultState.Skipped); - } else { - run.setState(test, vscode.TestResultState.Running); - await test.data.run(run); - } - run.appendOutput(`Completed ${test.id}\r\n`); + run.setState(test, vscode.TestResultState.Queued); + queue.push(test as vscode.TestItem); } else { if (test.data instanceof TestFile && test.children.size === 0) { await test.data.refresh(); } - await runTests(test.children.values()); + await discoverTests(test.children.values()); } } }; - runTests(request.tests).then(() => run.end()); + const runTestQueue = async () => { + for (const test of queue) { + run.appendOutput(`Running ${test.id}\r\n`); + if (cancellation.isCancellationRequested) { + run.setState(test, vscode.TestResultState.Skipped); + } else { + run.setState(test, vscode.TestResultState.Running); + await test.data.run(run); + } + run.appendOutput(`Completed ${test.id}\r\n`); + } + + run.end(); + }; + + discoverTests(request.tests).then(runTestQueue); } } @@ -220,7 +234,6 @@ class TestFile { }); this.prune(thisGeneration); - this.item.error = this.item.children.size === 0 ? new vscode.MarkdownString('No _tests_ were **found** in this file') : undefined; } /** @@ -246,7 +259,7 @@ class TestFile { class TestHeading { public static create(label: string, range: vscode.Range, generation: number, parent: vscode.TestItem) { const item = vscode.test.createTestItem({ - id: `mktests/${parent.uri.toString()}/${label}`, + id: `mktests/${parent.uri!.toString()}/${label}`, label, uri: parent.uri, }, new TestHeading(generation)); @@ -272,7 +285,7 @@ class TestCase { ) { const label = `${a} ${operator} ${b} = ${expected}`; const item = vscode.test.createTestItem({ - id: `mktests/${parent.uri.toString()}/${label}`, + id: `mktests/${parent.uri!.toString()}/${label}`, label, uri: parent.uri, }); @@ -292,16 +305,18 @@ class TestCase { ) { } async run(options: vscode.TestRun): Promise { - await new Promise(resolve => setTimeout(resolve, 1000 + Math.random() * 1000)); const start = Date.now(); + await new Promise(resolve => setTimeout(resolve, 1000 + Math.random() * 1000)); const actual = this.evaluate(); const duration = Date.now() - start; + console.log('run', this.item.label); + if (actual === this.expected) { - options.setState(this.item, vscode.TestResultState.Passed); + options.setState(this.item, vscode.TestResultState.Passed, duration); } else { const message = vscode.TestMessage.diff(`Expected ${this.item.label}`, String(this.expected), String(actual)); - message.location = new vscode.Location(this.item.uri, this.item.range!); + message.location = new vscode.Location(this.item.uri!, this.item.range!); options.appendMessage(this.item, message); options.setState(this.item, vscode.TestResultState.Failed, duration); }