From e4f235080d45093624cdcf6a9838949b41c45ebd Mon Sep 17 00:00:00 2001 From: Remi Schnekenburger Date: Tue, 25 Apr 2023 22:17:34 +0200 Subject: [PATCH] test with supportContinuous run add usage of createRunProfile with supportContinuousRun also trigger some action --- test-provider-sample/package.json | 16 ++-- test-provider-sample/src/extension.ts | 106 ++++---------------------- 2 files changed, 18 insertions(+), 104 deletions(-) diff --git a/test-provider-sample/package.json b/test-provider-sample/package.json index 412c67a35..1ca3db01e 100644 --- a/test-provider-sample/package.json +++ b/test-provider-sample/package.json @@ -1,16 +1,12 @@ { "name": "test-provider-sample", "displayName": "test-provider-sample", - "description": "Sample showing how to use the test APIs", - "version": "0.0.1", - "publisher": "vscode-samples", + "description": "Test Mock APIs", + "version": "0.0.2", + "publisher": "rschnekenbu", "private": true, "license": "MIT", "repository": "https://github.com/Microsoft/vscode-extension-samples", - "enabledApiProposals": [ - "testCoverage", - "testContinuousRun" - ], "engines": { "vscode": "^1.68.0" }, @@ -18,16 +14,14 @@ "Other" ], "activationEvents": [ - "workspaceContains:*.md" + "*" ], "main": "./out/extension.js", "scripts": { "vscode:prepublish": "npm run compile", "compile": "tsc -p ./", "lint": "eslint \"src/**/*.ts\"", - "watch": "tsc -watch -p ./", - "download-api": "vscode-dts main && vscode-dts dev", - "postinstall": "npm run download-api" + "watch": "tsc -watch -p ./" }, "devDependencies": { "@types/node": "^16.11.7", diff --git a/test-provider-sample/src/extension.ts b/test-provider-sample/src/extension.ts index d0c2a292e..e62ed7737 100644 --- a/test-provider-sample/src/extension.ts +++ b/test-provider-sample/src/extension.ts @@ -6,107 +6,24 @@ export async function activate(context: vscode.ExtensionContext) { context.subscriptions.push(ctrl); const fileChangedEmitter = new vscode.EventEmitter(); - const runHandler = (request: vscode.TestRunRequest2, cancellation: vscode.CancellationToken) => { + const runHandler = (request: vscode.TestRunRequest, cancellation: vscode.CancellationToken) => { if (!request.continuous) { - return startTestRun(request); - } + return startSingleTestRun(request); + } - const l = fileChangedEmitter.event(uri => startTestRun( - new vscode.TestRunRequest2( - [getOrCreateFile(ctrl, uri).file], - undefined, - request.profile, - true - ), - )); - cancellation.onCancellationRequested(() => l.dispose()); + return startContinuousTestRun(request); + }; - const startTestRun = (request: vscode.TestRunRequest) => { - const queue: { test: vscode.TestItem; data: TestCase }[] = []; - const run = ctrl.createTestRun(request); - // map of file uris to statements on each line: - const coveredLines = new Map(); - - const discoverTests = async (tests: Iterable) => { - for (const test of tests) { - if (request.exclude?.includes(test)) { - continue; - } - - const data = testData.get(test); - if (data instanceof TestCase) { - run.enqueued(test); - queue.push({ test, data }); - } else { - if (data instanceof TestFile && !data.didResolve) { - await data.updateFromDisk(ctrl, test); - } - - await discoverTests(gatherTestItems(test.children)); - } - - if (test.uri && !coveredLines.has(test.uri.toString())) { - try { - const lines = (await getContentFromFilesystem(test.uri)).split('\n'); - coveredLines.set( - test.uri.toString(), - lines.map((lineText, lineNo) => - lineText.trim().length ? new vscode.StatementCoverage(0, new vscode.Position(lineNo, 0)) : undefined - ) - ); - } catch { - // ignored - } - } - } - }; - - const runTestQueue = async () => { - for (const { test, data } of queue) { - run.appendOutput(`Running ${test.id}\r\n`); - if (run.token.isCancellationRequested) { - run.skipped(test); - } else { - run.started(test); - await data.run(test, run); - } - - const lineNo = test.range!.start.line; - const fileCoverage = coveredLines.get(test.uri!.toString()); - if (fileCoverage) { - fileCoverage[lineNo]!.executionCount++; - } - - run.appendOutput(`Completed ${test.id}\r\n`); - } - - run.end(); - }; - - run.coverageProvider = { - provideFileCoverage() { - const coverage: vscode.FileCoverage[] = []; - for (const [uri, statements] of coveredLines) { - coverage.push( - vscode.FileCoverage.fromDetails( - vscode.Uri.parse(uri), - statements.filter((s): s is vscode.StatementCoverage => !!s) - ) - ); - } - - return coverage; - }, - }; - - discoverTests(request.include ?? gatherTestItems(ctrl.items)).then(runTestQueue); + const startSingleTestRun = (request: vscode.TestRunRequest) => { + vscode.window.showInformationMessage('startSingleTestRun : '+request); }; - ctrl.refreshHandler = async () => { - await Promise.all(getWorkspaceTestPatterns().map(({ pattern }) => findInitialFiles(ctrl, pattern))); + const startContinuousTestRun = (request: vscode.TestRunRequest) => { + vscode.window.showInformationMessage('startContinuousTestRun : '+request); }; + ctrl.createRunProfile('Run Tests', vscode.TestRunProfileKind.Run, runHandler, true, undefined, true); ctrl.resolveHandler = async item => { @@ -132,6 +49,7 @@ export async function activate(context: vscode.ExtensionContext) { const { file, data } = getOrCreateFile(ctrl, e.uri); data.updateFromContents(ctrl, e.getText(), file); + console.log('test-provider-sample: update md'); } for (const document of vscode.workspace.textDocuments) { @@ -142,6 +60,8 @@ export async function activate(context: vscode.ExtensionContext) { vscode.workspace.onDidOpenTextDocument(updateNodeForDocument), vscode.workspace.onDidChangeTextDocument(e => updateNodeForDocument(e.document)), ); + + console.log('test-provider-sample activated'); } function getOrCreateFile(controller: vscode.TestController, uri: vscode.Uri) {