forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
239 lines (221 loc) · 9.02 KB
/
main.ts
File metadata and controls
239 lines (221 loc) · 9.02 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
'use strict';
import * as vscode from 'vscode';
import {Tests, TestsToRun, TestFolder, TestFile, TestStatus, TestSuite, TestFunction, FlattenedTestFunction, CANCELLATION_REASON} from './common/contracts';
import * as nosetests from './nosetest/main';
import * as pytest from './pytest/main';
import * as unittest from './unittest/main';
import {resolveValueAsTestToRun} from './common/testUtils';
import {BaseTestManager} from './common/baseTestManager';
import {PythonSettings, IUnitTestSettings} from '../common/configSettings';
import {TestResultDisplay} from './display/main';
import {TestDisplay} from './display/picker';
import * as fs from 'fs';
import * as path from 'path';
import * as constants from '../common/constants';
import {activateCodeLenses} from './codeLenses/main';
const settings = PythonSettings.getInstance();
let testManager: BaseTestManager;
let pyTestManager: pytest.TestManager;
let unittestManager: unittest.TestManager;
let nosetestManager: nosetests.TestManager;
let testResultDisplay: TestResultDisplay;
let testDisplay: TestDisplay;
let outChannel: vscode.OutputChannel;
let lastRanTests: TestsToRun = null;
export function activate(context: vscode.ExtensionContext, outputChannel: vscode.OutputChannel) {
context.subscriptions.push({ dispose: dispose });
outChannel = outputChannel;
let disposables = registerCommands();
context.subscriptions.push(...disposables);
// Ignore the exceptions returned
// This function is invoked via a command which will be invoked else where in the extension
discoverTests(true, true).catch(() => {
// Ignore the errors
let x = '';
});
settings.addListener('change', onConfigChanged);
context.subscriptions.push(activateCodeLenses());
}
function dispose() {
if (pyTestManager) {
pyTestManager.dispose();
}
if (nosetestManager) {
nosetestManager.dispose();
}
if (unittestManager) {
unittestManager.dispose();
}
}
function registerCommands(): vscode.Disposable[] {
const disposables = [];
disposables.push(vscode.commands.registerCommand(constants.Commands.Tests_Discover, (quiteMode: boolean) => {
// Ignore the exceptions returned
// This command will be invoked else where in the extension
discoverTests(true, quiteMode).catch(() => { return null; });
}));
disposables.push(vscode.commands.registerCommand(constants.Commands.Tests_Run_Failed, () => runTestsImpl(true)));
disposables.push(vscode.commands.registerCommand(constants.Commands.Tests_Run, (testId) => runTestsImpl(testId)));
disposables.push(vscode.commands.registerCommand(constants.Commands.Tests_View_UI, () => displayUI()));
disposables.push(vscode.commands.registerCommand(constants.Commands.Tests_Picker_UI, (file, testFunctions) => displayPickerUI(file, testFunctions)));
disposables.push(vscode.commands.registerCommand(constants.Commands.Tests_Stop, () => stopTests()));
disposables.push(vscode.commands.registerCommand(constants.Commands.Tests_ViewOutput, () => outChannel.show()));
disposables.push(vscode.commands.registerCommand(constants.Commands.Tests_Ask_To_Stop_Discovery, () => displayStopUI('Stop discovering tests')));
disposables.push(vscode.commands.registerCommand(constants.Commands.Tests_Ask_To_Stop_Test, () => displayStopUI('Stop running tests')));
return disposables;
}
function displayUI() {
let testManager = getTestRunner();
if (!testManager) {
return displayTestFrameworkError();
}
testDisplay = testDisplay ? testDisplay : new TestDisplay();
testDisplay.displayTestUI(vscode.workspace.rootPath);
}
function displayPickerUI(file: string, testFunctions: TestFunction[]) {
let testManager = getTestRunner();
if (!testManager) {
return displayTestFrameworkError();
}
testDisplay = testDisplay ? testDisplay : new TestDisplay();
testDisplay.displayFunctionTestPickerUI(vscode.workspace.rootPath, file, testFunctions);
}
function displayStopUI(message: string) {
let testManager = getTestRunner();
if (!testManager) {
return displayTestFrameworkError();
}
testDisplay = testDisplay ? testDisplay : new TestDisplay();
testDisplay.displayStopTestUI(message);
}
let uniTestSettingsString = JSON.stringify(settings.unitTest);
function onConfigChanged() {
// Possible that a test framework has been enabled or some settings have changed
// Meaning we need to re-load the discovered tests (as something could have changed)
const newSettings = JSON.stringify(settings.unitTest);
if (uniTestSettingsString === newSettings) {
return;
}
uniTestSettingsString = newSettings;
if (!settings.unitTest.nosetestsEnabled && !settings.unitTest.pyTestEnabled && !settings.unitTest.unittestEnabled) {
if (testResultDisplay) {
testResultDisplay.enabled = false;
}
if (testManager) {
testManager.stop();
testManager = null;
}
if (pyTestManager) {
pyTestManager.dispose();
pyTestManager = null;
}
if (nosetestManager) {
nosetestManager.dispose();
nosetestManager = null;
}
if (unittestManager) {
unittestManager.dispose();
unittestManager = null;
}
return;
}
if (testResultDisplay) {
testResultDisplay.enabled = true;
}
// No need to display errors
discoverTests(true, true);
}
function displayTestFrameworkError() {
if (settings.unitTest.pyTestEnabled && settings.unitTest.nosetestsEnabled && settings.unitTest.unittestEnabled) {
vscode.window.showErrorMessage("Enable only one of the test frameworks (nosetest or pytest), not both.")
}
else {
vscode.window.showInformationMessage('Please enable one of the test frameworks (pytest or nosetest)');
}
return null;
}
function getTestRunner() {
const rootDirectory = vscode.workspace.rootPath;
if (settings.unitTest.nosetestsEnabled) {
return nosetestManager = nosetestManager ? nosetestManager : new nosetests.TestManager(rootDirectory, outChannel);
}
else if (settings.unitTest.pyTestEnabled) {
return pyTestManager = pyTestManager ? pyTestManager : new pytest.TestManager(rootDirectory, outChannel);
}
else if (settings.unitTest.unittestEnabled) {
return unittestManager = unittestManager ? unittestManager : new unittest.TestManager(rootDirectory, outChannel);
}
return null;
}
function stopTests() {
let testManager = getTestRunner();
if (testManager) {
testManager.stop();
}
}
function discoverTests(ignoreCache?: boolean, quietMode: boolean = false) {
let testManager = getTestRunner();
if (!testManager) {
if (!quietMode) {
displayTestFrameworkError();
}
return Promise.resolve(null);
}
if (testManager && (testManager.status !== TestStatus.Discovering && testManager.status !== TestStatus.Running)) {
testResultDisplay = testResultDisplay ? testResultDisplay : new TestResultDisplay(outChannel);
return testResultDisplay.DisplayDiscoverStatus(testManager.discoverTests(ignoreCache, quietMode), quietMode);
}
else {
return Promise.resolve(null);
}
}
function isTestsToRun(arg: any): arg is TestsToRun {
if (arg && arg.testFunction && Array.isArray(arg.testFunction)){
return true;
}
if (arg && arg.testSuite && Array.isArray(arg.testSuite)){
return true;
}
if (arg && arg.testFile && Array.isArray(arg.testFile)){
return true;
}
return false;
}
function isUri(arg: any): arg is vscode.Uri {
return arg && arg.fsPath && typeof arg.fsPath === 'string';
}
function isFlattenedTestFunction(arg: any): arg is FlattenedTestFunction {
return arg && arg.testFunction && typeof arg.xmlClassName === 'string' &&
arg.parentTestFile && typeof arg.testFunction.name === 'string';
}
function identifyTestType(rootDirectory: string, arg?: vscode.Uri | TestsToRun | boolean | FlattenedTestFunction): TestsToRun | Boolean {
if (typeof arg === 'boolean') {
return arg === true;
}
if (isTestsToRun(arg)) {
return arg;
}
if (isFlattenedTestFunction(arg)) {
return <TestsToRun>{ testFunction: [arg.testFunction] };
}
if (isUri(arg)) {
return resolveValueAsTestToRun(arg.fsPath, rootDirectory);
}
return null;
}
function runTestsImpl(arg?: vscode.Uri | TestsToRun | boolean | FlattenedTestFunction) {
let testManager = getTestRunner();
if (!testManager) {
return displayTestFrameworkError();
}
// lastRanTests = testsToRun;
let runInfo = identifyTestType(vscode.workspace.rootPath, arg);
testResultDisplay = testResultDisplay ? testResultDisplay : new TestResultDisplay(outChannel);
let runPromise = testManager.runTest(runInfo).catch(reason => {
if (reason !== CANCELLATION_REASON) {
outChannel.appendLine('Error: ' + reason);
}
return Promise.reject(reason);
});
testResultDisplay.DisplayProgressStatus(runPromise);
}