Skip to content

Commit

Permalink
feat: getting test result from debug
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed Mar 25, 2022
1 parent 4517821 commit 78aaa4b
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 87 deletions.
2 changes: 1 addition & 1 deletion samples/basic/test/add.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, describe, it } from "vitest";
import { expect, describe, it, beforeAll } from "vitest";

describe("addition", () => {
it("run 1", () => {
Expand Down
4 changes: 4 additions & 0 deletions src/TestData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export function getTestCaseId(
if (data instanceof TestDescribe || data instanceof TestCase) {
return `${data.fileItem.uri}/${name}`;
} else {
if (childItem == null) {
return undefined;
}

return `${childItem.uri}/${name}`;
}
}
Expand Down
1 change: 0 additions & 1 deletion src/discover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ export class TestFileDiscoverer extends vscode.Disposable {

let workspacePrefix = this.workspaceCommonPrefix.get(workspacePath)!;
if (!uri.path.startsWith(workspacePrefix)) {
console.log("NOT starts with!!!!!!!");
const p = uri.path;
for (let i = 0; i < workspacePrefix.length; i++) {
if (p[i] !== workspacePrefix[i]) {
Expand Down
4 changes: 2 additions & 2 deletions src/pure/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ interface TestResult {
};
}

interface AggregatedResult {
export interface AggregatedResult {
numFailedTests: number;
numFailedTestSuites: number;
numPassedTests: number;
Expand Down Expand Up @@ -145,7 +145,7 @@ export class TestRunner {
}
}

async function getNodeVersion() {
export async function getNodeVersion() {
const process = spawn("node", ["-v"], {
stdio: ["ignore", "pipe", "pipe"],
});
Expand Down
212 changes: 129 additions & 83 deletions src/runHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import * as vscode from "vscode";
import { getVitestPath as getVitestPath, TestRunner } from "./pure/runner";
import {
AggregatedResult,
getNodeVersion,
getTempPath,
getVitestPath as getVitestPath,
TestRunner,
} from "./pure/runner";
import groupBy = require("lodash.groupby");
import {
getAllTestCases,
Expand All @@ -8,10 +14,13 @@ import {
TestFile,
} from "./TestData";
import { getConfig } from "./config";
import { readFile } from "fs-extra";
import { existsSync } from "fs";

export async function debugHandler(
export async function runHandler(
ctrl: vscode.TestController,
request: vscode.TestRunRequest
request: vscode.TestRunRequest,
cancellation: vscode.CancellationToken
) {
if (
vscode.workspace.workspaceFolders === undefined ||
Expand All @@ -20,63 +29,20 @@ export async function debugHandler(
return;
}

const tests = request.include ?? [];
if (tests.length === 1) {
await debugTest(vscode.workspace.workspaceFolders[0], tests[0]);
} else {
await debugTest(vscode.workspace.workspaceFolders[0]);
}
}

async function debugTest(
workspaceFolder: vscode.WorkspaceFolder,
testItem?: vscode.TestItem
) {
let config = {
type: "pwa-node",
request: "launch",
name: "Debug Current Test File",
autoAttachChildProcesses: true,
skipFiles: ["<node_internals>/**", "**/node_modules/**"],
program: getVitestPath(workspaceFolder.uri.path),
args: [] as string[],
smartStep: true,
console: "integratedTerminal",
};

if (testItem) {
const data = WEAKMAP_TEST_DATA.get(testItem);
if (!data) {
console.error("Item not found");
return;
}

config.args = [
"run",
data.getFilePath(),
"--testNamePattern",
data.getFullPattern(),
];
} else {
config.args = ["run"];
}

if (config.program == null) {
vscode.window.showErrorMessage("Cannot find vitest");
return;
}
const runner = new TestRunner(
vscode.workspace.workspaceFolders[0].uri.path,
getVitestPath(vscode.workspace.workspaceFolders[0].uri.path)
);

try {
vscode.debug.startDebugging(workspaceFolder, config);
} catch (e) {
console.error(`startDebugging error ${(e as any).toString()}`);
}
const tests = request.include ?? gatherTestItems(ctrl.items);
const run = ctrl.createTestRun(request);
await runTest(ctrl, runner, run, tests);
run.end();
}

export async function runHandler(
export async function debugHandler(
ctrl: vscode.TestController,
request: vscode.TestRunRequest,
cancellation: vscode.CancellationToken
request: vscode.TestRunRequest
) {
if (
vscode.workspace.workspaceFolders === undefined ||
Expand All @@ -85,14 +51,9 @@ export async function runHandler(
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 runTest(ctrl, runner, run, tests);
await runTest(ctrl, undefined, run, tests, true);
run.end();
}

Expand All @@ -104,10 +65,15 @@ function gatherTestItems(collection: vscode.TestItemCollection) {

async function runTest(
ctrl: vscode.TestController,
runner: TestRunner,
runner: TestRunner | undefined,
run: vscode.TestRun,
items: readonly vscode.TestItem[]
items: readonly vscode.TestItem[],
isDebug = false
) {
if (!isDebug && runner === undefined) {
throw new Error("should provide runner if not debug");
}

const config = getConfig();
const testCaseSet: Set<vscode.TestItem> = new Set();
const testItemIdMap = new Map<string, vscode.TestItem>();
Expand Down Expand Up @@ -156,27 +122,38 @@ async function runTest(
pathToFile.set(file.uri!.path, file);
}

const out = await runner
.scheduleRun(
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),
config.env || undefined,
config.commandLine ? config.commandLine.trim().split(" ") : undefined
)
.catch((e) => {
run.appendOutput("Run test failed \r\n" + (e as Error) + "\r\n");
run.appendOutput("" + (e as Error)?.stack + "\r\n");
testCaseSet.forEach((testCase) => {
run.errored(testCase, new vscode.TestMessage((e as Error)?.toString()));
});
let out;

try {
if (!isDebug) {
out = await runner!.scheduleRun(
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),
config.env || undefined,
config.commandLine ? config.commandLine.trim().split(" ") : undefined
);
} else {
out = await debugTest(vscode.workspace.workspaceFolders![0], run, items);
}
} catch (e) {
console.error(e);
run.appendOutput("Run test failed \r\n" + (e as Error) + "\r\n");
run.appendOutput("" + (e as Error)?.stack + "\r\n");
testCaseSet.forEach((testCase) => {
run.errored(testCase, new vscode.TestMessage((e as Error)?.toString()));
});
testCaseSet.clear();
}

if (out === undefined) {
testCaseSet.forEach((testCase) => {
run.errored(testCase, new vscode.TestMessage("Internal Error"));
});
return;
}

Expand All @@ -186,7 +163,7 @@ async function runTest(
results.forEach((result, index) => {
const id =
getTestCaseId(
pathToFile.get(result.testFilePath!)!,
pathToFile.get(result?.testFilePath || "")!,
result.displayName!
) || "";
const child = testItemIdMap.get(id)!;
Expand Down Expand Up @@ -235,3 +212,72 @@ async function runTest(
});
}
}

async function debugTest(
workspaceFolder: vscode.WorkspaceFolder,
run: vscode.TestRun,
testItems: readonly vscode.TestItem[]
) {
let config = {
type: "pwa-node",
request: "launch",
name: "Debug Current Test File",
autoAttachChildProcesses: true,
skipFiles: ["<node_internals>/**", "**/node_modules/**"],
program: getVitestPath(workspaceFolder.uri.path),
args: [] as string[],
smartStep: true,
console: "integratedTerminal",
};

const outputFilePath = getTempPath();
const testData = testItems.map((item) => WEAKMAP_TEST_DATA.get(item)!);
config.args = [
"run",
...new Set(testData.map((x) => x.getFilePath())),
testData.length === 1 ? "--testNamePattern" : "",
testData.length === 1 ? testData[0].getFullPattern() : "",
"--reporter=default",
"--reporter=json",
"--outputFile",
outputFilePath,
];

if (config.program == null) {
vscode.window.showErrorMessage("Cannot find vitest");
return;
}

return new Promise<AggregatedResult>((resolve, reject) => {
vscode.debug.startDebugging(workspaceFolder, config).then(
() => {
vscode.debug.onDidChangeActiveDebugSession((e) => {
if (!e) {
console.log("DISCONNECTED");
setTimeout(async () => {
if (!existsSync(outputFilePath)) {
const prefix =
`When running:\n` +
` ${config.program + " " + config.args.join(" ")}\n` +
`cwd: ${workspaceFolder.uri.fsPath}\n` +
`node: ${await getNodeVersion()}` +
`env.PATH: ${process.env.PATH}`;
reject(new Error(prefix));
return;
}

const file = await readFile(outputFilePath, "utf-8");
const out = JSON.parse(file) as AggregatedResult;
resolve(out);
});
}
});
},
(err) => {
console.error(err);
console.log("START DEBUGGING FAILED");
reject();
}
);
});
}

0 comments on commit 78aaa4b

Please sign in to comment.