Skip to content

Commit

Permalink
add continuous run to sample
Browse files Browse the repository at this point in the history
  • Loading branch information
connor4312 committed Jan 7, 2023
1 parent 5192db2 commit 978f653
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
3 changes: 2 additions & 1 deletion test-provider-sample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"license": "MIT",
"repository": "https://github.com/Microsoft/vscode-extension-samples",
"enabledApiProposals": [
"testCoverage"
"testCoverage",
"testContinuousRun"
],
"engines": {
"vscode": "^1.68.0"
Expand Down
36 changes: 29 additions & 7 deletions test-provider-sample/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<vscode.Uri>();
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:
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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<vscode.Uri> ) {
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()));

Expand Down

0 comments on commit 978f653

Please sign in to comment.