From 55e534bd1c6ac0b2ca6eec23538be7649736fd20 Mon Sep 17 00:00:00 2001 From: Connor Peet Date: Sat, 7 Jan 2023 09:14:34 -0800 Subject: [PATCH] add continuous run to sample --- test-provider-sample/package.json | 3 ++- test-provider-sample/src/extension.ts | 36 +++++++++++++++++++++------ 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/test-provider-sample/package.json b/test-provider-sample/package.json index d81fbc9e..5be9da2d 100644 --- a/test-provider-sample/package.json +++ b/test-provider-sample/package.json @@ -8,7 +8,8 @@ "license": "MIT", "repository": "https://github.com/Microsoft/vscode-extension-samples", "enabledApiProposals": [ - "testCoverage" + "testCoverage", + "testContinuousRun" ], "engines": { "vscode": "^1.68.0" diff --git a/test-provider-sample/src/extension.ts b/test-provider-sample/src/extension.ts index 815e3142..d652e20b 100644 --- a/test-provider-sample/src/extension.ts +++ b/test-provider-sample/src/extension.ts @@ -5,7 +5,25 @@ export async function activate(context: vscode.ExtensionContext) { const ctrl = vscode.tests.createTestController('mathTestController', 'Markdown Math'); context.subscriptions.push(ctrl); - const runHandler = (request: vscode.TestRunRequest, cancellation: vscode.CancellationToken) => { + const fileChangedEmitter = new vscode.EventEmitter(); + const runHandler = (request: vscode.TestRunRequest2, cancellation: vscode.CancellationToken) => { + if (!request.continuous) { + return startTestRun(request, cancellation); + } + + const l = fileChangedEmitter.event(uri => startTestRun( + new vscode.TestRunRequest2( + [getOrCreateFile(ctrl, uri).file], + undefined, + request.profile, + true + ), + cancellation + )); + cancellation.onCancellationRequested(() => l.dispose()); + }; + + const startTestRun = (request: vscode.TestRunRequest, cancellation: vscode.CancellationToken) => { const queue: { test: vscode.TestItem; data: TestCase }[] = []; const run = ctrl.createTestRun(request); // map of file uris to statements on each line: @@ -90,11 +108,11 @@ export async function activate(context: vscode.ExtensionContext) { await Promise.all(getWorkspaceTestPatterns().map(({ pattern }) => findInitialFiles(ctrl, pattern))); }; - ctrl.createRunProfile('Run Tests', vscode.TestRunProfileKind.Run, runHandler, true); + ctrl.createRunProfile('Run Tests', vscode.TestRunProfileKind.Run, runHandler, true, undefined, true); ctrl.resolveHandler = async item => { if (!item) { - context.subscriptions.push(...startWatchingWorkspace(ctrl)); + context.subscriptions.push(...startWatchingWorkspace(ctrl, fileChangedEmitter)); return; } @@ -166,16 +184,20 @@ async function findInitialFiles(controller: vscode.TestController, pattern: vsco } } -function startWatchingWorkspace(controller: vscode.TestController) { +function startWatchingWorkspace(controller: vscode.TestController, fileChangedEmitter: vscode.EventEmitter ) { return getWorkspaceTestPatterns().map(({ workspaceFolder, pattern }) => { const watcher = vscode.workspace.createFileSystemWatcher(pattern); - watcher.onDidCreate(uri => getOrCreateFile(controller, uri)); - watcher.onDidChange(uri => { + watcher.onDidCreate(uri => { + getOrCreateFile(controller, uri); + fileChangedEmitter.fire(uri); + }); + watcher.onDidChange(async uri => { const { file, data } = getOrCreateFile(controller, uri); if (data.didResolve) { - data.updateFromDisk(controller, file); + await data.updateFromDisk(controller, file); } + fileChangedEmitter.fire(uri); }); watcher.onDidDelete(uri => controller.items.delete(uri.toString()));