forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfiguration.ts
More file actions
157 lines (152 loc) · 6.96 KB
/
configuration.ts
File metadata and controls
157 lines (152 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'use strict';
import { inject, injectable } from 'inversify';
import { Uri } from 'vscode';
import { IApplicationShell, IWorkspaceService } from '../common/application/types';
import { traceError } from '../common/logger';
import { IConfigurationService, Product } from '../common/types';
import { IServiceContainer } from '../ioc/types';
import { sendTelemetryEvent } from '../telemetry';
import { EventName } from '../telemetry/constants';
import { TestConfiguringTelemetry, TestTool } from '../telemetry/types';
import { BufferedTestConfigSettingsService } from './common/services/configSettingService';
import { ITestsHelper, UnitTestProduct } from './common/types';
import {
ITestConfigSettingsService,
ITestConfigurationManager,
ITestConfigurationManagerFactory,
ITestConfigurationService
} from './types';
@injectable()
export class UnitTestConfigurationService implements ITestConfigurationService {
private readonly configurationService: IConfigurationService;
private readonly appShell: IApplicationShell;
private readonly workspaceService: IWorkspaceService;
constructor(@inject(IServiceContainer) private serviceContainer: IServiceContainer) {
this.configurationService = serviceContainer.get<IConfigurationService>(IConfigurationService);
this.appShell = serviceContainer.get<IApplicationShell>(IApplicationShell);
this.workspaceService = serviceContainer.get<IWorkspaceService>(IWorkspaceService);
}
public async displayTestFrameworkError(wkspace: Uri): Promise<void> {
const settings = this.configurationService.getSettings(wkspace);
let enabledCount = settings.testing.pytestEnabled ? 1 : 0;
enabledCount += settings.testing.nosetestsEnabled ? 1 : 0;
enabledCount += settings.testing.unittestEnabled ? 1 : 0;
if (enabledCount > 1) {
return this._promptToEnableAndConfigureTestFramework(
wkspace,
'Enable only one of the test frameworks (unittest, pytest or nosetest).',
true
);
} else {
const option = 'Enable and configure a Test Framework';
const item = await this.appShell.showInformationMessage(
'No test framework configured (unittest, pytest or nosetest)',
option
);
if (item === option) {
return this._promptToEnableAndConfigureTestFramework(wkspace);
}
return Promise.reject(null);
}
}
public async selectTestRunner(placeHolderMessage: string): Promise<UnitTestProduct | undefined> {
const items = [
{
label: 'unittest',
product: Product.unittest,
description: 'Standard Python test framework',
detail: 'https://docs.python.org/3/library/unittest.html'
},
{
label: 'pytest',
product: Product.pytest,
description: 'pytest framework',
// tslint:disable-next-line:no-http-string
detail: 'http://docs.pytest.org/'
},
{
label: 'nose',
product: Product.nosetest,
description: 'nose framework',
detail: 'https://nose.readthedocs.io/'
}
];
const options = {
ignoreFocusOut: true,
matchOnDescription: true,
matchOnDetail: true,
placeHolder: placeHolderMessage
};
const selectedTestRunner = await this.appShell.showQuickPick(items, options);
// tslint:disable-next-line:prefer-type-cast
return selectedTestRunner ? (selectedTestRunner.product as UnitTestProduct) : undefined;
}
public async enableTest(wkspace: Uri, product: UnitTestProduct): Promise<void> {
const factory = this.serviceContainer.get<ITestConfigurationManagerFactory>(ITestConfigurationManagerFactory);
const configMgr = factory.create(wkspace, product);
return this._enableTest(wkspace, configMgr);
}
public async promptToEnableAndConfigureTestFramework(wkspace: Uri) {
await this._promptToEnableAndConfigureTestFramework(wkspace, undefined, false, 'commandpalette');
}
private _enableTest(wkspace: Uri, configMgr: ITestConfigurationManager) {
const pythonConfig = this.workspaceService.getConfiguration('python', wkspace);
if (pythonConfig.get<boolean>('testing.promptToConfigure')) {
return configMgr.enable();
}
return pythonConfig.update('testing.promptToConfigure', undefined).then(
() => {
return configMgr.enable();
},
reason => {
return configMgr.enable().then(() => Promise.reject(reason));
}
);
}
private async _promptToEnableAndConfigureTestFramework(
wkspace: Uri,
messageToDisplay: string = 'Select a test framework/tool to enable',
enableOnly: boolean = false,
trigger: 'ui' | 'commandpalette' = 'ui'
) {
const telemetryProps: TestConfiguringTelemetry = {
trigger: trigger,
failed: false
};
try {
const selectedTestRunner = await this.selectTestRunner(messageToDisplay);
if (typeof selectedTestRunner !== 'number') {
return Promise.reject(null);
}
const helper = this.serviceContainer.get<ITestsHelper>(ITestsHelper);
telemetryProps.tool = helper.parseProviderName(selectedTestRunner) as TestTool;
const delayed = new BufferedTestConfigSettingsService();
const factory = this.serviceContainer.get<ITestConfigurationManagerFactory>(
ITestConfigurationManagerFactory
);
const configMgr = factory.create(wkspace, selectedTestRunner, delayed);
if (enableOnly) {
await configMgr.enable();
} else {
// Configure everything before enabling.
// Cuz we don't want the test engine (in main.ts file - tests get discovered when config changes are detected)
// to start discovering tests when tests haven't been configured properly.
await configMgr
.configure(wkspace)
.then(() => this._enableTest(wkspace, configMgr))
.catch(reason => {
return this._enableTest(wkspace, configMgr).then(() => Promise.reject(reason));
});
}
const cfg = this.serviceContainer.get<ITestConfigSettingsService>(ITestConfigSettingsService);
try {
await delayed.apply(cfg);
} catch (exc) {
traceError('Python Extension: applying unit test config updates', exc);
telemetryProps.failed = true;
}
} finally {
sendTelemetryEvent(EventName.UNITTEST_CONFIGURING, undefined, telemetryProps);
}
}
}