Skip to content

Commit

Permalink
fix: sho test explorer output and lines
Browse files Browse the repository at this point in the history
  • Loading branch information
elonmallin committed Dec 4, 2023
1 parent cefc19a commit e257b27
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 37 deletions.
33 changes: 28 additions & 5 deletions src/TestProvider/TestCases/TestMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { PhpunitArgBuilder } from "../../PhpunitCommand/PhpunitArgBuilder";
import { SpawnSyncReturns } from "child_process";
import { getTestFailedDiff } from "../../PhpParser/TestDiffParser";
import { ITestCase } from "./ITestCase";
import { IRunConfig } from "../../RunConfig";
import * as os from "os";

export class TestMethod implements ITestCase {
constructor(
Expand Down Expand Up @@ -36,27 +38,48 @@ export class TestMethod implements ITestCase {
argBuilder.addDirectoryOrFile(this.fileName);
argBuilder.withFilter(this.method);

const { status, stdout, stderr, error } = (await PHPUnitTestRunner.runArgs(
const { spawnResult, runConfig } = (await PHPUnitTestRunner.runArgs(
argBuilder,
true,
)) as SpawnSyncReturns<string>;
)) as { spawnResult: SpawnSyncReturns<string>; runConfig: IRunConfig };

const duration = Date.now() - start;

if (status === 0) {
options.appendOutput(
`${[runConfig.exec, ...runConfig.args].join(" ")}${os.EOL}${os.EOL}`,
undefined,
item,
);
if (spawnResult.stdout) {
options.appendOutput(`${spawnResult.stdout}${os.EOL}`, undefined, item);
}
if (spawnResult.stderr) {
console.error(spawnResult.stderr);
}

if (spawnResult.status === 0) {
options.passed(item, duration);

return true;
}

try {
const testDiff = getTestFailedDiff(stdout);
const testDiff = getTestFailedDiff(spawnResult.stdout);
const message = TestMessage.diff(
testDiff.message,
testDiff.expected,
testDiff.actual,
);
message.location = new Location(item.uri!, item.range!);
const lineNumber = /.*\.php:(\d+)/im.exec(spawnResult.stdout);
const range = lineNumber?.[1]
? new Range(
parseInt(lineNumber[1]) - 1,
0,
parseInt(lineNumber[1]) - 1,
0,
)
: item.range!;
message.location = new Location(item.uri!, range);

options.failed(item, message, duration);

Expand Down
3 changes: 0 additions & 3 deletions src/TestProvider/TestExplorerFeature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,12 @@ class TestExplorerFeature {

const runTestQueue = async () => {
for (const { test, data } of queue) {
run.appendOutput(`Running ${test.id}\r\n`);
if (run.token.isCancellationRequested) {
run.skipped(test);
} else {
run.started(test);
await data.run(test, run);
}

run.appendOutput(`Completed ${test.id}\r\n`);
}

run.end();
Expand Down
48 changes: 19 additions & 29 deletions src/phpunittest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,8 @@ export class TestRunner {
return undefined;
}

public async runArgs(argBuilder: PhpunitArgBuilder, childProcess = false) {
populateArgBuilder(argBuilder: PhpunitArgBuilder) {
const config = vscode.workspace.getConfiguration("phpunit");
const order = config.get<string[]>("driverPriority");

const driver = await this.getDriver(order);
if (!driver) {
console.error(`Wasn't able to start phpunit.`);
return;
}

const configArgs = config.get<string[]>("args", []);
argBuilder.addArgs(configArgs);
Expand All @@ -299,6 +292,18 @@ export class TestRunner {
vscode.workspace.workspaceFolders![0].uri.fsPath,
);
}
}

public async runArgs(argBuilder: PhpunitArgBuilder, childProcess = false) {
const config = vscode.workspace.getConfiguration("phpunit");
const order = config.get<string[]>("driverPriority");
const driver = await this.getDriver(order);
if (!driver) {
console.error(`Wasn't able to start phpunit.`);
return;
}

this.populateArgBuilder(argBuilder);

this.lastArgBuilder = argBuilder;
const runConfig = await driver.run(argBuilder.buildArgs());
Expand All @@ -323,13 +328,15 @@ export class TestRunner {
.replace(/'$/g, "");
}
}
return await new Promise((r) =>
const spawnResult = await new Promise((r) =>
r(
spawnSync(runConfig.exec, runConfig.args, {
encoding: "utf8",
}),
),
);

return { spawnResult, runConfig };
} else {
await vscode.commands.executeCommand("workbench.action.terminal.clear");
await vscode.commands.executeCommand(
Expand All @@ -340,37 +347,20 @@ export class TestRunner {
}

public async run(type: RunType) {
let argBuilder = new PhpunitArgBuilder();

const config = vscode.workspace.getConfiguration("phpunit");
const order = config.get<string[]>("driverPriority");

const driver = await this.getDriver(order);
if (!driver) {
console.error(`Wasn't able to start phpunit.`);
return;
}

let argBuilder = new PhpunitArgBuilder();

if (type === "rerun-last-test" && this.lastArgBuilder) {
argBuilder = this.lastArgBuilder;
} else {
const configArgs = config.get<string[]>("args", []);
argBuilder.addArgs(configArgs);

const colors = config.get<string>("colors");
if (colors && configArgs.indexOf(colors) === -1) {
argBuilder.withColors(
colors.replace(/--colors=?/i, "") as "never" | "auto" | "always",
);
}

const pathMappings = config.get<{ [key: string]: string }>("paths");
if (pathMappings) {
argBuilder.withPathMappings(
pathMappings,
vscode.workspace.workspaceFolders![0].uri.fsPath,
);
}
this.populateArgBuilder(argBuilder);

const preferRunClassTestOverQuickPickWindow = config.get<boolean>(
"preferRunClassTestOverQuickPickWindow",
Expand Down

0 comments on commit e257b27

Please sign in to comment.