11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT License.
33
4- import { inject , injectable } from 'inversify' ;
4+ import { inject , injectable , named } from 'inversify' ;
55import { CancellationTokenSource } from 'vscode' ;
66import { IServiceContainer } from '../../../ioc/types' ;
77import { PYTEST_PROVIDER } from '../../common/constants' ;
8- import { ITestDiscoveryService , ITestsHelper , TestDiscoveryOptions , Tests } from '../../common/types' ;
8+ import { ITestDiscoveryService , ITestRunner , ITestsHelper , ITestsParser , Options , TestDiscoveryOptions , Tests } from '../../common/types' ;
99import { IArgumentsService , TestFilter } from '../../types' ;
1010
1111@injectable ( )
1212export class TestDiscoveryService implements ITestDiscoveryService {
1313 private argsService : IArgumentsService ;
1414 private helper : ITestsHelper ;
15- constructor ( @inject ( IServiceContainer ) private serviceContainer : IServiceContainer ) {
15+ private runner : ITestRunner ;
16+ constructor ( @inject ( IServiceContainer ) private serviceContainer : IServiceContainer ,
17+ @inject ( ITestsParser ) @named ( PYTEST_PROVIDER ) private testParser : ITestsParser ) {
1618 this . argsService = this . serviceContainer . get < IArgumentsService > ( IArgumentsService , PYTEST_PROVIDER ) ;
1719 this . helper = this . serviceContainer . get < ITestsHelper > ( ITestsHelper ) ;
20+ this . runner = this . serviceContainer . get < ITestRunner > ( ITestRunner ) ;
1821 }
1922 public async discoverTests ( options : TestDiscoveryOptions ) : Promise < Tests > {
2023 const args = this . buildTestCollectionArgs ( options ) ;
@@ -39,7 +42,7 @@ export class TestDiscoveryService implements ITestDiscoveryService {
3942
4043 return this . helper . mergeTests ( results ) ;
4144 }
42- protected buildTestCollectionArgs ( options : TestDiscoveryOptions ) {
45+ private buildTestCollectionArgs ( options : TestDiscoveryOptions ) {
4346 // Remove unwnted arguments (which happen to be test directories & test specific args).
4447 const args = this . argsService . filterArguments ( options . args , TestFilter . discovery ) ;
4548 if ( options . ignoreCache && args . indexOf ( '--cache-clear' ) === - 1 ) {
@@ -48,19 +51,24 @@ export class TestDiscoveryService implements ITestDiscoveryService {
4851 if ( args . indexOf ( '-s' ) === - 1 ) {
4952 args . splice ( 0 , 0 , '-s' ) ;
5053 }
54+ args . splice ( 0 , 0 , '--collect-only' ) ;
5155 return args ;
5256 }
53- protected async discoverTestsInTestDirectory ( options : TestDiscoveryOptions ) : Promise < Tests > {
57+ private async discoverTestsInTestDirectory ( options : TestDiscoveryOptions ) : Promise < Tests > {
5458 const token = options . token ? options . token : new CancellationTokenSource ( ) . token ;
55- const discoveryOptions = { ...options } ;
56- discoveryOptions . args = [ 'discover' , 'pytest' , '--' , ...options . args ] ;
57- discoveryOptions . token = token ;
59+ const runOptions : Options = {
60+ args : options . args ,
61+ cwd : options . cwd ,
62+ workspaceFolder : options . workspaceFolder ,
63+ token,
64+ outChannel : options . outChannel
65+ } ;
5866
59- const discoveryService = this . serviceContainer . get < ITestDiscoveryService > ( ITestDiscoveryService , 'common' ) ;
60- if ( discoveryOptions . token && discoveryOptions . token . isCancellationRequested ) {
67+ const data = await this . runner . run ( PYTEST_PROVIDER , runOptions ) ;
68+ if ( options . token && options . token . isCancellationRequested ) {
6169 return Promise . reject < Tests > ( 'cancelled' ) ;
6270 }
6371
64- return discoveryService . discoverTests ( discoveryOptions ) ;
72+ return this . testParser . parse ( data , options ) ;
6573 }
6674}
0 commit comments