Skip to content

Commit

Permalink
fix: debug & run tests on win
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed Mar 29, 2022
1 parent d2ac2e6 commit 27ec1a9
Show file tree
Hide file tree
Showing 10 changed files with 607 additions and 267 deletions.
511 changes: 280 additions & 231 deletions samples/basic/pnpm-lock.yaml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions samples/basic/test/fail_to_run.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test("aaaaaa", () => {});
14 changes: 7 additions & 7 deletions src/TestData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type TestData = TestFile | TestDescribe | TestCase;

export function getTestCaseId(
childItem: vscode.TestItem,
name: string
name: string,
): string | undefined {
const data = WEAKMAP_TEST_DATA.get(childItem);
if (data instanceof TestDescribe || data instanceof TestCase) {
Expand All @@ -28,7 +28,7 @@ export function getTestCaseId(

export function getAllTestCases(
item: vscode.TestItem,
agg: vscode.TestItem[] = []
agg: vscode.TestItem[] = [],
) {
if (item.children.size) {
item.children.forEach((child) => {
Expand All @@ -44,15 +44,15 @@ export class TestDescribe {
constructor(
public pattern: string,
public fileItem: vscode.TestItem,
public parent: TestDescribe | TestFile
public parent: TestDescribe | TestFile,
) {}

getFullPattern(): string {
return getFullPattern(this);
}

getFilePath(): string {
return this.fileItem.uri!.path;
return this.fileItem.uri!.fsPath;
}
}

Expand All @@ -61,15 +61,15 @@ export class TestCase {
public pattern: string,
public fileItem: vscode.TestItem,
public parent: TestDescribe | TestFile,
public index: number
public index: number,
) {}

getFullPattern(): string {
return getFullPattern(this);
}

getFilePath(): string {
return this.fileItem.uri!.path;
return this.fileItem.uri!.fsPath;
}
}

Expand Down Expand Up @@ -102,7 +102,7 @@ export class TestFile {
}

getFilePath(): string {
return this.item.uri!.path;
return this.item.uri!.fsPath;
}
}

Expand Down
21 changes: 11 additions & 10 deletions src/discover.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as vscode from "vscode";
import { sep } from "path";
import parse from "./pure/parsers";
import { NamedBlock } from "./pure/parsers/parser_nodes";
import {
Expand Down Expand Up @@ -32,7 +33,7 @@ export class TestFileDiscoverer extends vscode.Disposable {
this.workspaceCommonPrefix.clear();
});
this.workspacePaths =
vscode.workspace.workspaceFolders?.map((x) => x.uri.path) || [];
vscode.workspace.workspaceFolders?.map((x) => x.uri.fsPath) || [];
}

async discoverAllFilesInWorkspace(
Expand All @@ -57,7 +58,7 @@ export class TestFileDiscoverer extends vscode.Disposable {
);
const watcher = vscode.workspace.createFileSystemWatcher(pattern);
const filter = (v: vscode.Uri) =>
exclude.every((x) => !minimatch(v.path, x, { dot: true }));
exclude.every((x) => !minimatch(v.fsPath, x, { dot: true }));
watcher.onDidCreate(
(uri) => filter(uri) && this.getOrCreateFile(controller, uri),
);
Expand Down Expand Up @@ -119,22 +120,22 @@ export class TestFileDiscoverer extends vscode.Disposable {
}

const workspacePath = this.workspacePaths.find((x) =>
uri.path.startsWith(x)
uri.fsPath.startsWith(x)
);
let name;
if (workspacePath) {
if (!this.workspaceCommonPrefix.has(workspacePath)) {
const path = uri.path.split("/");
const path = uri.fsPath.split(sep);
this.workspaceCommonPrefix.set(
workspacePath,
path.slice(0, -1).join("/") + "/",
path.slice(0, -1).join(sep) + sep,
);
this.workspaceItems.set(workspacePath, new Set());
}

let workspacePrefix = this.workspaceCommonPrefix.get(workspacePath)!;
if (!uri.path.startsWith(workspacePrefix)) {
const p = uri.path;
if (!uri.fsPath.startsWith(workspacePrefix)) {
const p = uri.fsPath;
for (let i = 0; i < workspacePrefix.length; i++) {
if (p[i] !== workspacePrefix[i]) {
workspacePrefix = workspacePrefix.slice(0, i);
Expand All @@ -145,13 +146,13 @@ export class TestFileDiscoverer extends vscode.Disposable {
this.workspaceCommonPrefix.set(workspacePath, workspacePrefix);
const items = this.workspaceItems.get(workspacePath)!;
items.forEach((v) => {
v.label = v.uri!.path.substring(workspacePrefix.length);
v.label = v.uri!.fsPath.substring(workspacePrefix.length);
});
}

name = uri.path.substring(workspacePrefix.length);
name = uri.fsPath.substring(workspacePrefix.length);
} else {
name = uri.path.split("/").pop()!;
name = uri.fsPath.split(sep).pop()!;
}

const file = controller.createTestItem(uri.toString(), name, uri);
Expand Down
4 changes: 2 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export async function activate(context: vscode.ExtensionContext) {
if (
vscode.workspace.workspaceFolders == null ||
vscode.workspace.workspaceFolders.length === 0 ||
!(await isVitestEnv(vscode.workspace.workspaceFolders[0].uri.path))
!(await isVitestEnv(vscode.workspace.workspaceFolders[0].uri.fsPath))
) {
return;
}
Expand Down Expand Up @@ -44,7 +44,7 @@ export async function activate(context: vscode.ExtensionContext) {
};

const vitestPath = getVitestPath(
vscode.workspace.workspaceFolders[0].uri.path,
vscode.workspace.workspaceFolders[0].uri.fsPath,
);
if (vitestPath != null) {
const vitestVersion = await getVitestVersion(vitestPath);
Expand Down
9 changes: 4 additions & 5 deletions src/pure/isVitestEnv.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { execSync } from "child_process";
import { existsSync } from "fs";
import { readFile } from "fs-extra";
import path = require("path");
Expand All @@ -21,9 +20,9 @@ export async function isVitestEnv(projectRoot: string) {
}

return (
execSync(path.join(projectRoot, "vite.config.js")) ||
execSync(path.join(projectRoot, "vite.config.ts")) ||
execSync(path.join(projectRoot, "vitest.config.js")) ||
execSync(path.join(projectRoot, "vitest.config.ts"))
existsSync(path.join(projectRoot, "vite.config.js")) ||
existsSync(path.join(projectRoot, "vite.config.ts")) ||
existsSync(path.join(projectRoot, "vitest.config.js")) ||
existsSync(path.join(projectRoot, "vitest.config.ts"))
);
}

0 comments on commit 27ec1a9

Please sign in to comment.