22// Licensed under the MIT License.
33
44import { Uri } from 'vscode' ;
5- import path from 'path' ;
5+ import * as path from 'path' ;
66import { IConfigurationService , ITestOutputChannel } from '../../../common/types' ;
77import { createDeferred , Deferred } from '../../../common/utils/async' ;
88import { traceVerbose } from '../../../logging' ;
@@ -13,7 +13,10 @@ import {
1313 SpawnOptions ,
1414} from '../../../common/process/types' ;
1515import { EXTENSION_ROOT_DIR } from '../../../constants' ;
16+ import { removePositionalFoldersAndFiles } from './arguments' ;
1617
18+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
19+ ( global as any ) . EXTENSION_ROOT_DIR = EXTENSION_ROOT_DIR ;
1720/**
1821 * Wrapper Class for pytest test execution. This is where we call `runTestCommand`?
1922 */
@@ -93,12 +96,29 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
9396 // need to check what will happen in the exec service is NOT defined and is null
9497 const execService = await executionFactory ?. createActivatedEnvironment ( creationOptions ) ;
9598
96- const testIdsString = testIds . join ( ' ' ) ;
97- console . debug ( 'what to do with debug bool?' , debugBool ) ;
9899 try {
99- execService ?. exec ( [ '-m' , 'pytest' , '-p' , 'vscode_pytest' , testIdsString ] . concat ( pytestArgs ) , spawnOptions ) ;
100+ // Remove positional test folders and files, we will add as needed per node
101+ const testArgs = removePositionalFoldersAndFiles ( pytestArgs ) ;
102+
103+ // if user has provided `--rootdir` then use that, otherwise add `cwd`
104+ if ( testArgs . filter ( ( a ) => a . startsWith ( '--rootdir' ) ) . length === 0 ) {
105+ // Make sure root dir is set so pytest can find the relative paths
106+ testArgs . splice ( 0 , 0 , '--rootdir' , uri . fsPath ) ;
107+ }
108+
109+ if ( debugBool && ! testArgs . some ( ( a ) => a . startsWith ( '--capture' ) || a === '-s' ) ) {
110+ testArgs . push ( '--capture' , 'no' ) ;
111+ }
112+
113+ console . debug ( `Running test with arguments: ${ testArgs . join ( ' ' ) } \r\n` ) ;
114+ console . debug ( `Current working directory: ${ uri . fsPath } \r\n` ) ;
115+
116+ const argArray = [ '-m' , 'pytest' , '-p' , 'vscode_pytest' ] . concat ( testArgs ) . concat ( testIds ) ;
117+ console . debug ( 'argArray' , argArray ) ;
118+ execService ?. exec ( argArray , spawnOptions ) ;
100119 } catch ( ex ) {
101- console . error ( ex ) ;
120+ console . debug ( `Error while running tests: ${ testIds } \r\n${ ex } \r\n\r\n` ) ;
121+ return Promise . reject ( ex ) ;
102122 }
103123
104124 return deferred . promise ;
0 commit comments