Skip to content

Commit

Permalink
fix: conditionally activate extension
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed Mar 21, 2022
1 parent c659196 commit 0b18649
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 32 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/discover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
TestDescribe,
TestCase,
testItemIdMap,
} from "./test_data";
} from "./TestData";
import parse from "./pure/parsers";
import { NamedBlock } from "./pure/parsers/parser_nodes";

Expand Down
42 changes: 13 additions & 29 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@ import { debounce } from "mighty-promise";
import * as vscode from "vscode";
import { extensionId } from "./config";
import { discoverAllFilesInWorkspace, discoverTestFromDoc } from "./discover";
import { getVitePath as getVitestPath, TestRunner } from "./pure/runner";
import { runTest } from "./run_test";
import { WEAKMAP_TEST_DATA, TestFile } from "./test_data";
import { isVitestEnv } from "./pure/isVitestEnv";
import { runHandler } from "./runHandler";
import { WEAKMAP_TEST_DATA, TestFile } from "./TestData";

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))
) {
return;
}

export function activate(context: vscode.ExtensionContext) {
const ctrl = vscode.tests.createTestController(
`${extensionId}`,
"Vitest Test Provider"
);

ctrl.refreshHandler = async () => {
await discoverAllFilesInWorkspace(ctrl);
};
Expand Down Expand Up @@ -52,28 +61,3 @@ export function activate(context: vscode.ExtensionContext) {
}

export function deactivate() {}

async function runHandler(
ctrl: vscode.TestController,
request: vscode.TestRunRequest,
cancellation: vscode.CancellationToken
) {
if (vscode.workspace.workspaceFolders === undefined) {
return;
}

const runner = new TestRunner(
vscode.workspace.workspaceFolders[0].uri.path,
getVitestPath(vscode.workspace.workspaceFolders[0].uri.path)
);

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

function gatherTestItems(collection: vscode.TestItemCollection) {
const items: vscode.TestItem[] = [];
collection.forEach((item) => items.push(item));
return items;
}
27 changes: 27 additions & 0 deletions src/pure/isVitestEnv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { execSync } from "child_process";
import { existsSync } from "fs";
import { readFile } from "fs-extra";
import path = require("path");
import { getVitestPath } from "./runner";

export async function isVitestEnv(projectRoot: string) {
if (getVitestPath(projectRoot)) {
return true;
}

const pkgPath = path.join(projectRoot, "package.json") as string;
const pkg = JSON.parse(await readFile(pkgPath, "utf-8")) as any;
if (existsSync(pkg)) {
if (pkg.devDependencies && pkg.devDependencies["vitest"]) {
return true;
}
if (pkg.devDependencies && pkg.devDependencies["jest"]) {
return false;
}
}

return (
execSync(path.join(projectRoot, "vite.config.js")) ||
execSync(path.join(projectRoot, "vite.config.ts"))
);
}
2 changes: 1 addition & 1 deletion src/pure/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { tmpdir } from "os";
import { Lock } from "mighty-promise";
import execa = require("execa");

export function getVitePath(projectRoot: string): string | undefined {
export function getVitestPath(projectRoot: string): string | undefined {
const node_modules = path.resolve(projectRoot, "node_modules");
if (!existsSync(node_modules)) {
return;
Expand Down
27 changes: 27 additions & 0 deletions src/runHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as vscode from "vscode";
import { getVitestPath as getVitestPath, TestRunner } from "./pure/runner";
import { runTest } from "./runTest";

export async function runHandler(
ctrl: vscode.TestController,
request: vscode.TestRunRequest,
cancellation: vscode.CancellationToken
) {
if (vscode.workspace.workspaceFolders === undefined) {
return;
}

const runner = new TestRunner(
vscode.workspace.workspaceFolders[0].uri.path,
getVitestPath(vscode.workspace.workspaceFolders[0].uri.path)
);

const tests = request.include ?? gatherTestItems(ctrl.items);
const run = ctrl.createTestRun(request);
await Promise.all(tests.map((test) => runTest(runner, run, test)));
}
function gatherTestItems(collection: vscode.TestItemCollection) {
const items: vscode.TestItem[] = [];
collection.forEach((item) => items.push(item));
return items;
}
2 changes: 1 addition & 1 deletion src/run_test.ts → src/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
WEAKMAP_TEST_DATA,
getTestCaseId,
TestFile,
} from "./test_data";
} from "./TestData";

export async function runTest(
runner: TestRunner,
Expand Down

0 comments on commit 0b18649

Please sign in to comment.