Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: run test through filtered 'zig build test' instead of 'zig test' if build.zig is present. #399

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@
],
"default": "zls"
},
"zig.testArgs": {
"type": "array",
"items": {
"type": "string"
},
"default": ["test", "--test-filter", "${filter}", "${path}"],
"description": "Arguments to pass to 'zig' for running tests. Supported variables: ${filter}, ${path}."
},
"zig.zls.debugLog": {
"scope": "resource",
"type": "boolean",
Expand Down
15 changes: 13 additions & 2 deletions src/zigTestRunnerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,29 @@ export default class ZigTestRunnerProvider {
}

private async runTest(test: vscode.TestItem): Promise<{ output: string; success: boolean }> {
const config = vscode.workspace.getConfiguration("zig");
const zigPath = zigProvider.getZigPath();
if (!zigPath) {
return { output: "Unable to run test without Zig", success: false };
}
if (test.uri === undefined) {
return { output: "Unable to determine file location", success: false };
}

const testUri = test.uri;
const wsFolder = getWorkspaceFolder(testUri.fsPath)?.uri.fsPath ?? path.dirname(testUri.fsPath);

const parts = test.id.split(".");
const lastPart = parts[parts.length - 1];
const args = ["test", "--test-filter", lastPart, test.uri.fsPath];

const testArgsConf = config.get<string[]>('testArgs') || [];
const args: string[] = (testArgsConf.length > 0)
? testArgsConf.map((v) => v.replace("${filter}", lastPart).replace("${path}", testUri.fsPath))
: [];

try {
const { stderr: output } = await execFile(zigPath, args);
const { stderr: output } = await execFile(zigPath, args, { cwd: wsFolder });

return { output: output.replaceAll("\n", "\r\n"), success: true };
} catch (e) {
return { output: (e as Error).message.replaceAll("\n", "\r\n"), success: false };
Expand Down
Loading