@@ -21,7 +21,60 @@ import { buildPytestEnv as configureSubprocessEnv, handleSymlinkAndRootDir } fro
2121import { cleanupOnCancellation , createProcessHandlers } from '../common/discoveryHelpers' ;
2222
2323/**
24- * Wrapper class for unittest test discovery. This is where we call `runTestCommand`. #this seems incorrectly copied
24+ * Sets up the discovery named pipe and wires up cancellation.
25+ * @param resultResolver The resolver to handle discovered test data
26+ * @param token Optional cancellation token from the caller
27+ * @param uri Workspace URI for logging
28+ * @returns Object containing the pipe name and cancellation source
29+ */
30+ async function setupDiscoveryPipe (
31+ resultResolver : ITestResultResolver | undefined ,
32+ token : CancellationToken | undefined ,
33+ uri : Uri ,
34+ ) : Promise < { pipeName : string ; cancellation : CancellationTokenSource } > {
35+ const discoveryPipeCancellation = new CancellationTokenSource ( ) ;
36+
37+ // Wire up cancellation from external token
38+ token ?. onCancellationRequested ( ( ) => {
39+ traceInfo ( `Test discovery cancelled.` ) ;
40+ discoveryPipeCancellation . cancel ( ) ;
41+ } ) ;
42+
43+ // Start the named pipe with the discovery listener
44+ const discoveryPipeName = await startDiscoveryNamedPipe ( ( data : DiscoveredTestPayload ) => {
45+ if ( ! token ?. isCancellationRequested ) {
46+ resultResolver ?. resolveDiscovery ( data ) ;
47+ }
48+ } , discoveryPipeCancellation . token ) ;
49+
50+ traceVerbose ( `Created discovery pipe: ${ discoveryPipeName } for workspace ${ uri . fsPath } ` ) ;
51+
52+ return {
53+ pipeName : discoveryPipeName ,
54+ cancellation : discoveryPipeCancellation ,
55+ } ;
56+ }
57+
58+ /**
59+ * Configures the subprocess environment for pytest discovery.
60+ * @param envVarsService Service to retrieve environment variables
61+ * @param uri Workspace URI
62+ * @param discoveryPipeName Name of the discovery pipe to pass to the subprocess
63+ * @returns Configured environment variables for the subprocess
64+ */
65+ async function configureDiscoveryEnv (
66+ envVarsService : IEnvironmentVariablesProvider | undefined ,
67+ uri : Uri ,
68+ discoveryPipeName : string ,
69+ ) : Promise < NodeJS . ProcessEnv > {
70+ const fullPluginPath = path . join ( EXTENSION_ROOT_DIR , 'python_files' ) ;
71+ const envVars = await envVarsService ?. getEnvironmentVariables ( uri ) ;
72+ const mutableEnv = await configureSubprocessEnv ( envVars , fullPluginPath , discoveryPipeName ) ;
73+ return mutableEnv ;
74+ }
75+
76+ /**
77+ * Wrapper class for pytest test discovery. This is where we call the pytest subprocess.
2578 */
2679export class PytestTestDiscoveryAdapter implements ITestDiscoveryAdapter {
2780 constructor (
@@ -36,33 +89,26 @@ export class PytestTestDiscoveryAdapter implements ITestDiscoveryAdapter {
3689 token ?: CancellationToken ,
3790 interpreter ?: PythonEnvironment ,
3891 ) : Promise < void > {
39- // Setup: cancellation and discovery pipe
40- const discoveryPipeCancellation = new CancellationTokenSource ( ) ;
41- try {
42- token ?. onCancellationRequested ( ( ) => {
43- traceInfo ( `Test discovery cancelled.` ) ;
44- discoveryPipeCancellation . cancel ( ) ;
45- } ) ;
46-
47- const discoveryPipeName = await startDiscoveryNamedPipe ( ( data : DiscoveredTestPayload ) => {
48- if ( ! token ?. isCancellationRequested ) {
49- this . resultResolver ?. resolveDiscovery ( data ) ;
50- }
51- } , discoveryPipeCancellation . token ) ;
52- traceVerbose ( `Created discovery pipe: ${ discoveryPipeName } for workspace ${ uri . fsPath } ` ) ;
92+ // Setup discovery pipe and cancellation
93+ const { pipeName : discoveryPipeName , cancellation : discoveryPipeCancellation } = await setupDiscoveryPipe (
94+ this . resultResolver ,
95+ token ,
96+ uri ,
97+ ) ;
5398
99+ try {
54100 // Build pytest command and arguments
55101 const settings = this . configSettings . getSettings ( uri ) ;
56102 let { pytestArgs } = settings . testing ;
57103 const cwd = settings . testing . cwd && settings . testing . cwd . length > 0 ? settings . testing . cwd : uri . fsPath ;
58104 pytestArgs = await handleSymlinkAndRootDir ( cwd , pytestArgs ) ;
59- const execArgs = [ '-m' , 'pytest' , '-p' , 'vscode_pytest' , '--collect-only' ] . concat ( pytestArgs ) ;
60- traceVerbose ( `Running pytest discovery with command: ${ execArgs . join ( ' ' ) } for workspace ${ uri . fsPath } .` ) ;
105+ const commandArgs = [ '-m' , 'pytest' , '-p' , 'vscode_pytest' , '--collect-only' ] . concat ( pytestArgs ) ;
106+ traceVerbose (
107+ `Running pytest discovery with command: ${ commandArgs . join ( ' ' ) } for workspace ${ uri . fsPath } .` ,
108+ ) ;
61109
62110 // Configure subprocess environment
63- const fullPluginPath = path . join ( EXTENSION_ROOT_DIR , 'python_files' ) ;
64- const envVars = await this . envVarsService ?. getEnvironmentVariables ( uri ) ;
65- const mutableEnv = await configureSubprocessEnv ( envVars , fullPluginPath , discoveryPipeName ) ;
111+ const mutableEnv = await configureDiscoveryEnv ( this . envVarsService , uri , discoveryPipeName ) ;
66112
67113 // Setup process handlers (shared by both execution paths)
68114 const deferredTillExecClose : Deferred < void > = createTestingDeferred ( ) ;
@@ -82,7 +128,7 @@ export class PytestTestDiscoveryAdapter implements ITestDiscoveryAdapter {
82128
83129 const proc = await runInBackground ( pythonEnv , {
84130 cwd,
85- args : execArgs ,
131+ args : commandArgs ,
86132 env : ( mutableEnv as unknown ) as { [ key : string ] : string } ,
87133 } ) ;
88134 traceInfo ( `Started pytest discovery subprocess (environment extension) for workspace ${ uri . fsPath } ` ) ;
@@ -128,7 +174,7 @@ export class PytestTestDiscoveryAdapter implements ITestDiscoveryAdapter {
128174 } ;
129175
130176 let resultProc : ChildProcess | undefined ;
131- const result = execService . execObservable ( execArgs , spawnOptions ) ;
177+ const result = execService . execObservable ( commandArgs , spawnOptions ) ;
132178 resultProc = result ?. proc ;
133179
134180 if ( ! resultProc ) {
0 commit comments