Skip to content

Commit

Permalink
fix: test registry bug
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed Mar 21, 2022
1 parent f538b2b commit d041868
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 68 deletions.
19 changes: 13 additions & 6 deletions src/TestData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,27 @@ export class TestCase {
export class TestFile {
resolved = false;
pattern = "";
public async updateFromDisk(
controller: vscode.TestController,
item: vscode.TestItem
) {
constructor(public item: vscode.TestItem) {}
public async updateFromDisk(controller: vscode.TestController) {
const item = this.item;
try {
const content = await getContentFromFilesystem(item.uri!);
item.error = undefined;
this.item.error = undefined;
discoverTestFromFileContent(controller, content, item, this);
this.resolved = true;
} catch (e) {
item.error = (e as Error).stack;
this.item.error = (e as Error).stack;
}
}

load(ctrl: vscode.TestController): Promise<void> | undefined {
if (this.resolved) {
return;
}

return this.updateFromDisk(ctrl);
}

getFullPattern(): string {
return this.pattern;
}
Expand Down
27 changes: 19 additions & 8 deletions src/discover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ export function discoverTestFromDoc(
}

// TODO: use config
if (!e.uri.path.match(/test\.[tj]sx?(.git)?$/)) {
if (
!e.uri.path.match(/test\.[tj]sx?(.git)?$/) ||
e.uri.path.match(/node_modules/)
) {
return;
}

Expand All @@ -43,7 +46,7 @@ function getOrCreateFile(controller: vscode.TestController, uri: vscode.Uri) {
);
controller.items.add(file);

const data = new TestFile();
const data = new TestFile(file);
WEAKMAP_TEST_DATA.set(file, data);

file.canResolveChildren = true;
Expand Down Expand Up @@ -146,24 +149,32 @@ export async function discoverAllFilesInWorkspace(
vscode.workspace.workspaceFolders.map(async (workspaceFolder) => {
const pattern = new vscode.RelativePattern(
workspaceFolder,
"**/*.test.ts"
"**/*.test.{js,ts,tsx,jsx}"
);
const watcher = vscode.workspace.createFileSystemWatcher(pattern);

const filter = (v: vscode.Uri) => !v.path.includes("node_modules");
// When files are created, make sure there's a corresponding "file" node in the tree
watcher.onDidCreate((uri) => getOrCreateFile(controller, uri));
watcher.onDidCreate(
(uri) => filter(uri) && getOrCreateFile(controller, uri)
);
// When files change, re-parse them. Note that you could optimize this so
// that you only re-parse children that have been resolved in the past.
watcher.onDidChange((uri) => {
if (!filter(uri)) {
return;
}

const { data, file } = getOrCreateFile(controller, uri);
data.updateFromDisk(controller, file);
data.updateFromDisk(controller);
});
// And, finally, delete TestItems for removed files. This is simple, since
// we use the URI as the TestItem's ID.
watcher.onDidDelete((uri) => controller.items.delete(uri.toString()));
watcher.onDidDelete(
(uri) => filter(uri) && controller.items.delete(uri.toString())
);

for (const file of await vscode.workspace.findFiles(pattern)) {
getOrCreateFile(controller, file);
filter(file) && getOrCreateFile(controller, file);
}

return watcher;
Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function activate(context: vscode.ExtensionContext) {
} else {
const data = WEAKMAP_TEST_DATA.get(item);
if (data instanceof TestFile) {
await data.updateFromDisk(ctrl, item);
await data.updateFromDisk(ctrl);
}
}
};
Expand Down
13 changes: 6 additions & 7 deletions src/pure/runner.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { existsSync, readFile } from "fs-extra";
import * as path from "path";
import { tmpdir } from "os";
import { Lock } from "mighty-promise";
import { Lock, PriorityTaskQueue, TaskQueue } from "mighty-promise";
import execa = require("execa");

export function getVitestPath(projectRoot: string): string | undefined {
Expand Down Expand Up @@ -57,7 +57,9 @@ interface AggregatedResult {
}

export class TestRunner {
private lock = new Lock();
private queue = new TaskQueue<Promise<AggregatedResult>>({
maxParallelNum: 4,
});
constructor(
private workspacePath: string,
private vitePath: string | undefined
Expand All @@ -66,8 +68,7 @@ export class TestRunner {
testFile: string | undefined,
testNamePattern: string | undefined
): Promise<AggregatedResult> {
const release = await this.lock.acquire(10000).catch(() => () => {});
try {
return this.queue.push(async () => {
const path = getTempPath();
const args = [
"--reporter=json",
Expand Down Expand Up @@ -117,8 +118,6 @@ export class TestRunner {
const file = await readFile(path, "utf-8");
const out = JSON.parse(file) as AggregatedResult;
return out;
} finally {
release();
}
});
}
}
107 changes: 61 additions & 46 deletions src/runHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export async function runHandler(

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

Expand All @@ -34,13 +36,19 @@ function gatherTestItems(collection: vscode.TestItemCollection) {
}

async function runTest(
ctrl: vscode.TestController,
runner: TestRunner,
run: vscode.TestRun,
item: vscode.TestItem
) {
const testingData = WEAKMAP_TEST_DATA.get(item);
if (!testingData) {
throw new Error("file item not found");
console.error("Item not found");
throw new Error("Item not found");
}

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

let file: vscode.TestItem;
Expand All @@ -60,53 +68,60 @@ async function runTest(
run.started(testCase);
});

const data = WEAKMAP_TEST_DATA.get(item)!;
const out = await runner.scheduleRun(item.uri!.fsPath, data.getFullPattern());
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();
}
try {
const data = WEAKMAP_TEST_DATA.get(item)!;
const out = await runner.scheduleRun(
item.uri!.fsPath,
data.getFullPattern()
);
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 || "")
);
if (!child || !testCaseSet.has(child)) {
return;
}
}

if (result.skipped || result.status == null) {
run.skipped(child);
}
});
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;
}

testCaseSet.forEach((testCase) => {
run.skipped(testCase);
run.appendOutput(`Cannot find test ${testCase.id}`);
});
} else {
testCaseSet.forEach((testCase) => {
run.errored(
testCase,
new vscode.TestMessage(
"Testing is not started correctly. Please check your configuration."
)
);
});
if (result.skipped || result.status == null) {
run.skipped(child);
}
});

testCaseSet.forEach((testCase) => {
run.skipped(testCase);
run.appendOutput(`Cannot find test ${testCase.id}`);
});
} else {
testCaseSet.forEach((testCase) => {
run.errored(
testCase,
new vscode.TestMessage(
"Testing is not started correctly. Please check your configuration."
)
);
});
}
} catch (e) {
run.errored(item, new vscode.TestMessage((e as any).toString()));
}
}

0 comments on commit d041868

Please sign in to comment.