forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfiguration.ts
More file actions
159 lines (153 loc) · 6.98 KB
/
configuration.ts
File metadata and controls
159 lines (153 loc) · 6.98 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
158
159
'use strict';
import * as path from 'path';
import * as vscode from 'vscode';
import { OutputChannel, Uri } from 'vscode';
import { PythonSettings } from '../common/configSettings';
import { Installer, Product } from '../common/installer';
import { getSubDirectories } from '../common/utils';
import { TestConfigSettingsService } from './common/configSettingService';
import { TestConfigurationManager } from './common/testConfigurationManager';
import { selectTestWorkspace } from './common/testUtils';
import { UnitTestProduct } from './common/types';
import { ConfigurationManager } from './nosetest/testConfigurationManager';
import * as nose from './nosetest/testConfigurationManager';
import * as pytest from './pytest/testConfigurationManager';
import * as unittest from './unittest/testConfigurationManager';
// tslint:disable-next-line:no-any
async function promptToEnableAndConfigureTestFramework(wkspace: Uri, outputChannel: vscode.OutputChannel, messageToDisplay: string = 'Select a test framework/tool to enable', enableOnly: boolean = false) {
const selectedTestRunner = await selectTestRunner(messageToDisplay);
if (typeof selectedTestRunner !== 'number') {
return Promise.reject(null);
}
const configMgr: TestConfigurationManager = createTestConfigurationManager(wkspace, selectedTestRunner, outputChannel);
if (enableOnly) {
// Ensure others are disabled
if (selectedTestRunner !== Product.unittest) {
createTestConfigurationManager(wkspace, Product.unittest, outputChannel).disable();
}
if (selectedTestRunner !== Product.pytest) {
createTestConfigurationManager(wkspace, Product.pytest, outputChannel).disable();
}
if (selectedTestRunner !== Product.nosetest) {
createTestConfigurationManager(wkspace, Product.nosetest, outputChannel).disable();
}
return configMgr.enable();
}
return configMgr.configure(wkspace).then(() => {
return enableTest(wkspace, configMgr);
}).catch(reason => {
return enableTest(wkspace, configMgr).then(() => Promise.reject(reason));
});
}
export function displayTestFrameworkError(wkspace: Uri, outputChannel: vscode.OutputChannel) {
const settings = PythonSettings.getInstance();
let enabledCount = settings.unitTest.pyTestEnabled ? 1 : 0;
enabledCount += settings.unitTest.nosetestsEnabled ? 1 : 0;
enabledCount += settings.unitTest.unittestEnabled ? 1 : 0;
if (enabledCount > 1) {
return promptToEnableAndConfigureTestFramework(wkspace, outputChannel, 'Enable only one of the test frameworks (unittest, pytest or nosetest).', true);
} else {
const option = 'Enable and configure a Test Framework';
return vscode.window.showInformationMessage('No test framework configured (unittest, pytest or nosetest)', option).then(item => {
if (item === option) {
return promptToEnableAndConfigureTestFramework(wkspace, outputChannel);
}
return Promise.reject(null);
});
}
}
export async function displayPromptToEnableTests(rootDir: string, outputChannel: vscode.OutputChannel) {
const settings = PythonSettings.getInstance(vscode.Uri.file(rootDir));
if (settings.unitTest.pyTestEnabled ||
settings.unitTest.nosetestsEnabled ||
settings.unitTest.unittestEnabled) {
return;
}
if (!settings.unitTest.promptToConfigure) {
return;
}
const yes = 'Yes';
const no = 'Later';
const noNotAgain = 'No, don\'t ask again';
const hasTests = checkForExistenceOfTests(rootDir);
if (!hasTests) {
return;
}
const item = await vscode.window.showInformationMessage('You seem to have tests, would you like to enable a test framework?', yes, no, noNotAgain);
if (!item || item === no) {
return;
}
if (item === yes) {
await promptToEnableAndConfigureTestFramework(vscode.workspace.getWorkspaceFolder(vscode.Uri.file(rootDir)).uri, outputChannel);
} else {
const pythonConfig = vscode.workspace.getConfiguration('python');
await pythonConfig.update('unitTest.promptToConfigure', false);
}
}
// 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.
function enableTest(wkspace: Uri, configMgr: ConfigurationManager) {
const pythonConfig = vscode.workspace.getConfiguration('python', wkspace);
// tslint:disable-next-line:no-backbone-get-set-outside-model
if (pythonConfig.get<boolean>('unitTest.promptToConfigure')) {
return configMgr.enable();
}
return pythonConfig.update('unitTest.promptToConfigure', undefined).then(() => {
return configMgr.enable();
}, reason => {
return configMgr.enable().then(() => Promise.reject(reason));
});
}
function checkForExistenceOfTests(rootDir: string): Promise<boolean> {
return getSubDirectories(rootDir).then(subDirs => {
return subDirs.map(dir => path.relative(rootDir, dir)).filter(dir => dir.match(/test/i)).length > 0;
});
}
function createTestConfigurationManager(wkspace: Uri, product: Product, outputChannel: OutputChannel) {
const installer = new Installer(outputChannel);
const configSettingService = new TestConfigSettingsService();
switch (product) {
case Product.unittest: {
return new unittest.ConfigurationManager(wkspace, outputChannel, installer, configSettingService);
}
case Product.pytest: {
return new pytest.ConfigurationManager(wkspace, outputChannel, installer, configSettingService);
}
case Product.nosetest: {
return new nose.ConfigurationManager(wkspace, outputChannel, installer, configSettingService);
}
default: {
throw new Error('Invalid test configuration');
}
}
}
async function selectTestRunner(placeHolderMessage: string): Promise<UnitTestProduct | undefined> {
const items = [{
label: 'unittest',
product: Product.unittest,
description: 'Standard Python test framework',
detail: 'https://docs.python.org/2/library/unittest.html'
},
{
label: 'pytest',
product: Product.pytest,
description: 'Can run unittest (including trial) and nose test suites out of the box',
// tslint:disable-next-line:no-http-string
detail: 'http://docs.pytest.org/en/latest/'
},
{
label: 'nose',
product: Product.nosetest,
description: 'nose framework',
detail: 'https://docs.python.org/2/library/unittest.html'
}];
const options = {
matchOnDescription: true,
matchOnDetail: true,
placeHolder: placeHolderMessage
};
const selectedTestRunner = await vscode.window.showQuickPick(items, options);
// tslint:disable-next-line:prefer-type-cast
return selectedTestRunner ? selectedTestRunner.product as UnitTestProduct : undefined;
}