Skip to content
This repository was archived by the owner on Nov 25, 2025. It is now read-only.

Commit 845de8b

Browse files
authored
add config option for arguments used in test runner
1 parent 179bc33 commit 845de8b

2 files changed

Lines changed: 56 additions & 4 deletions

File tree

package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,19 @@
136136
],
137137
"default": "zls"
138138
},
139+
"zig.testArgs": {
140+
"type": "array",
141+
"items": {
142+
"type": "string"
143+
},
144+
"default": [
145+
"test",
146+
"--test-filter",
147+
"${filter}",
148+
"${path}"
149+
],
150+
"description": "Arguments to pass to 'zig' for running tests. Supported variables: ${filter}, ${path}."
151+
},
139152
"zig.zls.debugLog": {
140153
"scope": "resource",
141154
"type": "boolean",

src/zigTestRunnerProvider.ts

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import util from "util";
66

77
import { DebouncedFunc, throttle } from "lodash-es";
88

9-
import { getWorkspaceFolder, isWorkspaceFile } from "./zigUtil";
9+
import { getWorkspaceFolder, isWorkspaceFile, workspaceConfigUpdateNoThrow } from "./zigUtil";
1010
import { zigProvider } from "./zigSetup";
1111

1212
const execFile = util.promisify(childProcess.execFile);
@@ -118,21 +118,60 @@ export default class ZigTestRunnerProvider {
118118
}
119119

120120
private async runTest(test: vscode.TestItem): Promise<{ output: string; success: boolean }> {
121+
const config = vscode.workspace.getConfiguration("zig");
121122
const zigPath = zigProvider.getZigPath();
122123
if (!zigPath) {
123124
return { output: "Unable to run test without Zig", success: false };
124125
}
125126
if (test.uri === undefined) {
126127
return { output: "Unable to determine file location", success: false };
127128
}
129+
130+
const testUri = test.uri;
131+
const wsFolder = getWorkspaceFolder(testUri.fsPath)?.uri.fsPath ?? path.dirname(testUri.fsPath);
132+
128133
const parts = test.id.split(".");
129134
const lastPart = parts[parts.length - 1];
130-
const args = ["test", "--test-filter", lastPart, test.uri.fsPath];
135+
136+
const testArgsConf = config.get<string[]>("testArgs") ?? [];
137+
const args: string[] =
138+
testArgsConf.length > 0
139+
? testArgsConf.map((v) => v.replace("${filter}", lastPart).replace("${path}", testUri.fsPath))
140+
: [];
141+
131142
try {
132-
const { stderr: output } = await execFile(zigPath, args);
143+
const { stderr: output } = await execFile(zigPath, args, { cwd: wsFolder });
144+
133145
return { output: output.replaceAll("\n", "\r\n"), success: true };
134146
} catch (e) {
135-
return { output: (e as Error).message.replaceAll("\n", "\r\n"), success: false };
147+
if (e instanceof Error) {
148+
if (
149+
config.get<string[]>("testArgs")?.toString() ===
150+
config.inspect<string[]>("testArgs")?.defaultValue?.toString() &&
151+
(e.message.includes("no module named") || e.message.includes("import of file outside module path"))
152+
) {
153+
void vscode.window
154+
.showInformationMessage("Use build script to run tests?", "Yes", "No")
155+
.then(async (response) => {
156+
if (response === "Yes") {
157+
await workspaceConfigUpdateNoThrow(
158+
config,
159+
"testArgs",
160+
["build", "test", "-Dtest-filter=${filter}"],
161+
false,
162+
);
163+
void vscode.commands.executeCommand(
164+
"workbench.action.openWorkspaceSettings",
165+
"@id:zig.testArgs",
166+
);
167+
}
168+
});
169+
}
170+
171+
return { output: e.message.replaceAll("\n", "\r\n"), success: false };
172+
} else {
173+
return { output: "Failed to run test\r\n", success: false };
174+
}
136175
}
137176
}
138177

0 commit comments

Comments
 (0)