@@ -6,7 +6,7 @@ import util from "util";
66
77import { DebouncedFunc , throttle } from "lodash-es" ;
88
9- import { getWorkspaceFolder , isWorkspaceFile } from "./zigUtil" ;
9+ import { getWorkspaceFolder , isWorkspaceFile , workspaceConfigUpdateNoThrow } from "./zigUtil" ;
1010import { zigProvider } from "./zigSetup" ;
1111
1212const 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