Skip to content

Commit

Permalink
feat: run all tests in one go
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed Mar 22, 2022
1 parent f8cb348 commit ac86db0
Show file tree
Hide file tree
Showing 7 changed files with 347 additions and 302 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"devDependencies": {
"@types/fs-extra": "^9.0.13",
"@types/glob": "^7.2.0",
"@types/lodash.groupby": "^4.6.6",
"@types/mocha": "^9.1.0",
"@types/node": "14.x",
"@types/vscode": "^1.59.0",
Expand All @@ -65,6 +66,7 @@
"@babel/types": "^7.17.0",
"@rauschma/stringio": "^1.4.0",
"fs-extra": "^10.0.1",
"lodash.groupby": "^4.6.0",
"mighty-promise": "^0.0.8"
}
}
2 changes: 1 addition & 1 deletion samples/basic/test/add.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect, describe, it } from "vitest";
describe("addition", () => {
it("run", () => {
console.log("run");
expect(1 + 1).toBe(2);
expect(1 + 1).toBe(3);
});

it("failed", () => {
Expand Down
2 changes: 1 addition & 1 deletion samples/basic/test/mul.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, describe, it } from "vitest";

describe("mul", () => {
it("run", () => {});
it.skip("run", () => {});
});
2 changes: 1 addition & 1 deletion src/TestData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function getAllTestCases(
item.children.forEach((child) => {
getAllTestCases(child, agg);
});
} else {
} else if (WEAKMAP_TEST_DATA.get(item) instanceof TestCase) {
agg.push(item);
}
return agg;
Expand Down
1 change: 0 additions & 1 deletion src/pure/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ export class TestRunner {

for await (const line of chunksToLinesAsync(child.stdout)) {
log(line + "\r\n");
console.log("LINE", line);
}
} catch (e) {
console.error(e);
Expand Down
143 changes: 85 additions & 58 deletions src/runHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { resolve } from "path";
import * as vscode from "vscode";
import { getVitestPath as getVitestPath, TestRunner } from "./pure/runner";
import groupBy = require("lodash.groupby");
import {
getAllTestCases,
WEAKMAP_TEST_DATA,
Expand Down Expand Up @@ -91,9 +91,7 @@ export async function runHandler(

const tests = request.include ?? gatherTestItems(ctrl.items);
const run = ctrl.createTestRun(request);
await Promise.allSettled(
tests.map((test) => runTest(ctrl, runner, run, test))
);
await runTest(ctrl, runner, run, tests);
run.end();
}

Expand All @@ -107,75 +105,103 @@ async function runTest(
ctrl: vscode.TestController,
runner: TestRunner,
run: vscode.TestRun,
item: vscode.TestItem
items: readonly vscode.TestItem[]
) {
const testingData = WEAKMAP_TEST_DATA.get(item);
if (!testingData) {
console.error("Item not found");
throw new Error("Item not found");
}
const testCaseSet: Set<vscode.TestItem> = new Set();
const fileToTestCasesMap = new Map<string, vscode.TestItem[]>();
const fileItems: vscode.TestItem[] = [];
for (const item of items) {
const testingData = WEAKMAP_TEST_DATA.get(item);
if (!testingData) {
console.error("Item not found");
throw new Error("Item not found");
}

if (testingData instanceof TestFile) {
await testingData.load(ctrl);
}
if (testingData instanceof TestFile) {
await testingData.load(ctrl);
}

let file: vscode.TestItem;
if (testingData instanceof TestFile) {
file = item;
} else {
file = testingData.fileItem;
if (!file) {
throw new Error("file item not found");
let file: vscode.TestItem;
if (testingData instanceof TestFile) {
file = item;
} else {
file = testingData.fileItem;
if (!file) {
throw new Error("file item not found");
}
}

fileItems.push(file);
const fileTestCases = getAllTestCases(file);
fileToTestCasesMap.set(item.uri!.path, fileTestCases);
for (const test of getAllTestCases(item)) {
testCaseSet.add(test);
}
}

const fileTestCases = getAllTestCases(file);
const testCaseSet = new Set(getAllTestCases(item));
testCaseSet.forEach((testCase) => {
run.enqueued(testCase);
run.started(testCase);
});

const pathToFile = new Map<string, vscode.TestItem>();
for (const file of fileItems) {
pathToFile.set(file.uri!.path, file);
}

try {
const data = WEAKMAP_TEST_DATA.get(item)!;
const out = await runner.scheduleRun(
[item.uri!.fsPath],
data.getFullPattern(),
(msg) => run.appendOutput(msg, undefined, item)
fileItems.map((x) => x.uri!.fsPath),
items.length === 1
? WEAKMAP_TEST_DATA.get(items[0])!.getFullPattern()
: "",
items.length === 1
? (msg) => run.appendOutput(msg, undefined, items[0])
: (msg) => run.appendOutput(msg)
);
if (out.testResults.length !== 0) {
out.testResults.forEach((result, index) => {
let child: undefined | vscode.TestItem = fileTestCases[index];
const id = getTestCaseId(item, result.displayName!) || "";
if (!child || !child.id.startsWith(id)) {
console.error("not match");
console.dir(out.testResults);
console.dir(fileTestCases);
throw new Error();
}

if (!child || !testCaseSet.has(child)) {
return;
}

testCaseSet.delete(child);
switch (result.status) {
case "pass":
run.passed(child, result.perfStats?.runtime);
return;
case "fail":
run.failed(
child,
new vscode.TestMessage(result.failureMessage || "")
);
return;
}

if (result.skipped || result.status == null) {
run.skipped(child);
Object.values(groupBy(out.testResults, (x) => x.testFilePath)).forEach(
(results) => {
results.forEach((result, index) => {
const fileTestCases = fileToTestCasesMap.get(result.testFilePath!)!;
/**
* ATTENTION: Current implementation assumes that testResults are ordered by
* original test case position for each test file
*/
let child: undefined | vscode.TestItem = fileTestCases[index];
const id =
getTestCaseId(
pathToFile.get(result.testFilePath!)!,
result.displayName!
) || "";
if (!child || !child.id.startsWith(id)) {
console.error("not match");
throw new Error("not match");
}

if (!child || !testCaseSet.has(child)) {
return;
}

testCaseSet.delete(child);
switch (result.status) {
case "pass":
run.passed(child, result.perfStats?.runtime);
return;
case "fail":
run.failed(
child,
new vscode.TestMessage(result.failureMessage || "")
);
return;
}

if (result.skipped || result.status == null) {
run.skipped(child);
}
});
}
});

);
testCaseSet.forEach((testCase) => {
run.skipped(testCase);
run.appendOutput(`Cannot find test ${testCase.id}`);
Expand All @@ -191,6 +217,7 @@ async function runTest(
});
}
} catch (e) {
run.errored(item, new vscode.TestMessage((e as any).toString()));
console.error(e);
run.appendOutput("Run test failed " + (e as Error).toString());
}
}
Loading

0 comments on commit ac86db0

Please sign in to comment.