Skip to content

Commit

Permalink
test with supportContinuous run
Browse files Browse the repository at this point in the history
add usage of createRunProfile with supportContinuousRun
also trigger some action
  • Loading branch information
rschnekenbu committed Apr 25, 2023
1 parent 20dc659 commit e4f2350
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 104 deletions.
16 changes: 5 additions & 11 deletions test-provider-sample/package.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
{
"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"
},
"categories": [
"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",
Expand Down
106 changes: 13 additions & 93 deletions test-provider-sample/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,107 +6,24 @@ export async function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(ctrl);

const fileChangedEmitter = new vscode.EventEmitter<vscode.Uri>();
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</* file uri */ string, (vscode.StatementCoverage | undefined)[]>();

const discoverTests = async (tests: Iterable<vscode.TestItem>) => {
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 => {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit e4f2350

Please sign in to comment.