1
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT License.
3
3
4
- import { inject , injectable } from 'inversify' ;
4
+ import { inject , injectable , named } from 'inversify' ;
5
5
import { CancellationTokenSource } from 'vscode' ;
6
6
import { IServiceContainer } from '../../../ioc/types' ;
7
7
import { 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' ;
9
9
import { IArgumentsService , TestFilter } from '../../types' ;
10
10
11
11
@injectable ( )
12
12
export class TestDiscoveryService implements ITestDiscoveryService {
13
13
private argsService : IArgumentsService ;
14
14
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 ) {
16
18
this . argsService = this . serviceContainer . get < IArgumentsService > ( IArgumentsService , PYTEST_PROVIDER ) ;
17
19
this . helper = this . serviceContainer . get < ITestsHelper > ( ITestsHelper ) ;
20
+ this . runner = this . serviceContainer . get < ITestRunner > ( ITestRunner ) ;
18
21
}
19
22
public async discoverTests ( options : TestDiscoveryOptions ) : Promise < Tests > {
20
23
const args = this . buildTestCollectionArgs ( options ) ;
@@ -39,7 +42,7 @@ export class TestDiscoveryService implements ITestDiscoveryService {
39
42
40
43
return this . helper . mergeTests ( results ) ;
41
44
}
42
- protected buildTestCollectionArgs ( options : TestDiscoveryOptions ) {
45
+ private buildTestCollectionArgs ( options : TestDiscoveryOptions ) {
43
46
// Remove unwnted arguments (which happen to be test directories & test specific args).
44
47
const args = this . argsService . filterArguments ( options . args , TestFilter . discovery ) ;
45
48
if ( options . ignoreCache && args . indexOf ( '--cache-clear' ) === - 1 ) {
@@ -48,19 +51,24 @@ export class TestDiscoveryService implements ITestDiscoveryService {
48
51
if ( args . indexOf ( '-s' ) === - 1 ) {
49
52
args . splice ( 0 , 0 , '-s' ) ;
50
53
}
54
+ args . splice ( 0 , 0 , '--collect-only' ) ;
51
55
return args ;
52
56
}
53
- protected async discoverTestsInTestDirectory ( options : TestDiscoveryOptions ) : Promise < Tests > {
57
+ private async discoverTestsInTestDirectory ( options : TestDiscoveryOptions ) : Promise < Tests > {
54
58
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
+ } ;
58
66
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 ) {
61
69
return Promise . reject < Tests > ( 'cancelled' ) ;
62
70
}
63
71
64
- return discoveryService . discoverTests ( discoveryOptions ) ;
72
+ return this . testParser . parse ( data , options ) ;
65
73
}
66
74
}
0 commit comments