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
150 lines (143 loc) · 5.98 KB
/
configuration.ts
File metadata and controls
150 lines (143 loc) · 5.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
'use strict';
import * as vscode from 'vscode';
import { PythonSettings } from '../common/configSettings';
import { Product } from '../common/installer';
import { TestConfigurationManager } from './common/testConfigurationManager';
import * as nose from './nosetest/testConfigurationManager';
import * as pytest from './pytest/testConfigurationManager';
import * as unittest from './unittest/testConfigurationManager';
import { getSubDirectories } from '../common/utils';
import * as path from 'path';
const settings = PythonSettings.getInstance();
function promptToEnableAndConfigureTestFramework(outputChannel: vscode.OutputChannel, messageToDisplay: string = 'Select a test framework/tool to enable', enableOnly: boolean = false): Thenable<any> {
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',
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: messageToDisplay
};
return vscode.window.showQuickPick(items, options).then(item => {
if (!item) {
return Promise.reject(null);
}
let configMgr: TestConfigurationManager;
switch (item.product) {
case Product.unittest: {
configMgr = new unittest.ConfigurationManager(outputChannel);
break;
}
case Product.pytest: {
configMgr = new pytest.ConfigurationManager(outputChannel);
break;
}
case Product.nosetest: {
configMgr = new nose.ConfigurationManager(outputChannel);
break;
}
default: {
throw new Error('Invalid test configuration');
}
}
if (enableOnly) {
// Ensure others are disabled
if (item.product !== Product.unittest) {
(new unittest.ConfigurationManager(outputChannel)).disable();
}
if (item.product !== Product.pytest) {
(new pytest.ConfigurationManager(outputChannel)).disable();
}
if (item.product !== Product.nosetest) {
(new nose.ConfigurationManager(outputChannel)).disable();
}
return configMgr.enable();
}
// 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(): Thenable<any> {
const pythonConfig = vscode.workspace.getConfiguration('python');
if (settings.unitTest.promptToConfigure) {
return configMgr.enable();
}
return pythonConfig.update('unitTest.promptToConfigure', undefined).then(() => {
return configMgr.enable();
}, reason => {
return configMgr.enable().then(() => Promise.reject(reason));
});
}
return configMgr.configure(vscode.workspace.rootPath).then(() => {
return enableTest();
}).catch(reason => {
return enableTest().then(() => Promise.reject(reason));
});
});
}
export function displayTestFrameworkError(outputChannel: vscode.OutputChannel): Thenable<any> {
let enabledCount = settings.unitTest.pyTestEnabled ? 1 : 0;
enabledCount += settings.unitTest.nosetestsEnabled ? 1 : 0;
enabledCount += settings.unitTest.unittestEnabled ? 1 : 0;
if (enabledCount > 1) {
return promptToEnableAndConfigureTestFramework(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(outputChannel);
}
return Promise.reject(null);
});
}
}
export function displayPromptToEnableTests(rootDir: string, outputChannel: vscode.OutputChannel): Thenable<any> {
if (settings.unitTest.pyTestEnabled ||
settings.unitTest.nosetestsEnabled ||
settings.unitTest.unittestEnabled) {
return Promise.reject(null);
}
if (!settings.unitTest.promptToConfigure) {
return Promise.reject(null);
}
const yes = 'Yes';
const no = `Later`;
const noNotAgain = `No, don't ask again`;
return checkIfHasTestDirs(rootDir).then(hasTests => {
if (!hasTests) {
return Promise.reject(null);
}
return vscode.window.showInformationMessage('You seem to have tests, would you like to enable a test framework?', yes, no, noNotAgain).then(item => {
if (!item || item === no) {
return Promise.reject(null);
}
if (item === yes) {
return promptToEnableAndConfigureTestFramework(outputChannel);
}
else {
const pythonConfig = vscode.workspace.getConfiguration('python');
return pythonConfig.update('unitTest.promptToConfigure', false);
}
});
});
}
function checkIfHasTestDirs(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;
});
}