Skip to content

Commit

Permalink
docs: add comments to entry file
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed May 16, 2022
1 parent d05d866 commit 464c82c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 27 deletions.
File renamed without changes.
40 changes: 36 additions & 4 deletions src/discover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ export class TestFileDiscoverer extends vscode.Disposable {
vscode.workspace.workspaceFolders?.map((x) => x.uri.fsPath) || [];
}

async discoverAllFilesInWorkspace(
async watchAllTestFilesInWorkspace(
controller: vscode.TestController,
): Promise<vscode.FileSystemWatcher[]> {
for (const watch of this.lastWatches) {
watch.dispose();
}

if (!vscode.workspace.workspaceFolders) {
return []; // handle the case of no open folders
return []; // handle the case of no opened folders
}

const watchers = [] as vscode.FileSystemWatcher[];
Expand All @@ -70,7 +70,7 @@ export class TestFileDiscoverer extends vscode.Disposable {
return;
}

const { data, file } = this.getOrCreateFile(controller, uri);
const { data } = this.getOrCreateFile(controller, uri);
if (!data.resolved) {
return;
}
Expand All @@ -82,7 +82,10 @@ export class TestFileDiscoverer extends vscode.Disposable {
watcher.onDidDelete((uri) => controller.items.delete(uri.toString()));

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

watchers.push(watcher);
Expand All @@ -95,6 +98,35 @@ export class TestFileDiscoverer extends vscode.Disposable {
return watchers;
}

async discoverAllTestFilesInWorkspace(
controller: vscode.TestController,
): Promise<void> {
if (!vscode.workspace.workspaceFolders) {
return;
}

await Promise.all(
vscode.workspace.workspaceFolders.map(async (workspaceFolder) => {
const exclude = getConfig().exclude;
for (const include of getConfig().include) {
const pattern = new vscode.RelativePattern(
workspaceFolder.uri,
include,
);
const filter = (v: vscode.Uri) =>
exclude.every((x) => !minimatch(v.fsPath, x, { dot: true }));

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

public discoverTestFromDoc(
ctrl: vscode.TestController,
e: vscode.TextDocument,
Expand Down
44 changes: 23 additions & 21 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { debugHandler, runHandler, updateSnapshot } from "./runHandler";
import { TestFile, WEAKMAP_TEST_DATA } from "./TestData";
import semver from "semver";
import { TestWatcher } from "./watch";
import { Command } from "./commands";
import { Command } from "./command";

export async function activate(context: vscode.ExtensionContext) {
if (
Expand All @@ -31,17 +31,17 @@ export async function activate(context: vscode.ExtensionContext) {
);

const fileDiscoverer = new TestFileDiscoverer();
// run on refreshing test list
ctrl.refreshHandler = async () => {
// TODO: should delete redundant tests here
context.subscriptions.push(
...(await fileDiscoverer.discoverAllFilesInWorkspace(ctrl)),
);
await fileDiscoverer.discoverAllTestFilesInWorkspace(ctrl);
};

ctrl.resolveHandler = async (item) => {
if (!item) {
// item == null, when user opened the testing panel
// in this case, we should discover and watch all the testing files
context.subscriptions.push(
...(await fileDiscoverer.discoverAllFilesInWorkspace(ctrl)),
...(await fileDiscoverer.watchAllTestFilesInWorkspace(ctrl)),
);
} else {
const data = WEAKMAP_TEST_DATA.get(item);
Expand All @@ -51,26 +51,28 @@ export async function activate(context: vscode.ExtensionContext) {
}
};

const vitest = getVitestCommand(
const vitestCmd = getVitestCommand(
vscode.workspace.workspaceFolders[0].uri.fsPath,
);
const vitestVersion = await getVitestVersion(vitest);
const vitestVersion = await getVitestVersion(vitestCmd);
console.dir({ vitestVersion });
let testWatcher: undefined | TestWatcher;
if (vitest) {
testWatcher = TestWatcher.create(ctrl, fileDiscoverer, vitest);
context.subscriptions.push(
testWatcher,
vscode.commands.registerCommand(
Command.StartWatching,
() => {
testWatcher!.watch();
},
),
);
}

if (semver.gte(vitestVersion, "0.8.0")) {
// enable run/debug/watch tests only if vitest version >= 0.8.0
let testWatcher: undefined | TestWatcher;
if (vitestCmd) {
testWatcher = TestWatcher.create(ctrl, fileDiscoverer, vitestCmd);
context.subscriptions.push(
testWatcher,
vscode.commands.registerCommand(
Command.StartWatching,
() => {
testWatcher!.watch();
},
),
);
}

registerRunHandler(ctrl, testWatcher);
} else {
// v0.8.0 introduce a breaking change in json format
Expand Down
4 changes: 2 additions & 2 deletions src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { buildWatchClient } from "./pure/watch/client";
import type { File, Task } from "vitest";
import { TestFileDiscoverer } from "./discover";
import { effect, ref } from "@vue/reactivity";
import { ChildProcess, spawn } from "child_process";
import { ChildProcess } from "child_process";
import { getTasks } from "@vitest/ws-client";
import {
TestCase,
Expand Down Expand Up @@ -173,7 +173,7 @@ export class TestWatcher extends Disposable {

private readonly onFileUpdated = (files?: File[]) => {
if (files == undefined) {
this.discover.discoverAllFilesInWorkspace(this.ctrl);
this.discover.watchAllTestFilesInWorkspace(this.ctrl);
} else {
for (const file of files) {
this.discover.discoverTestFromPath(
Expand Down

0 comments on commit 464c82c

Please sign in to comment.