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
186 lines (174 loc) · 7.91 KB
/
main.ts
File metadata and controls
186 lines (174 loc) · 7.91 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
'use strict';
import * as vscode from 'vscode';
import * as constants from '../../common/constants';
import { createDeferred, isNotInstalledError } from '../../common/helpers';
import { CANCELLATION_REASON } from '../common/constants';
import { displayTestErrorMessage } from '../common/testUtils';
import { Tests } from '../common/types';
export class TestResultDisplay {
private statusBar: vscode.StatusBarItem;
private discoverCounter = 0;
private ticker = ['|', '/', '-', '|', '/', '-', '\\'];
private progressTimeout;
private progressPrefix: string;
// tslint:disable-next-line:no-any
constructor(private outputChannel: vscode.OutputChannel, private onDidChange: vscode.EventEmitter<any> = null) {
this.statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
}
public dispose() {
this.statusBar.dispose();
}
public set enabled(enable: boolean) {
if (enable) {
this.statusBar.show();
} else {
this.statusBar.hide();
}
}
public displayProgressStatus(testRunResult: Promise<Tests>, debug: boolean = false) {
this.displayProgress('Running Tests', 'Running Tests (Click to Stop)', constants.Commands.Tests_Ask_To_Stop_Test);
testRunResult
.then(tests => this.updateTestRunWithSuccess(tests, debug))
.catch(this.updateTestRunWithFailure.bind(this))
// We don't care about any other exceptions returned by updateTestRunWithFailure
// tslint:disable-next-line:no-empty
.catch(() => { });
}
public displayDiscoverStatus(testDiscovery: Promise<Tests>) {
this.displayProgress('Discovering Tests', 'Discovering Tests (Click to Stop)', constants.Commands.Tests_Ask_To_Stop_Discovery);
return testDiscovery.then(tests => {
this.updateWithDiscoverSuccess(tests);
return tests;
}).catch(reason => {
this.updateWithDiscoverFailure(reason);
return Promise.reject(reason);
});
}
private updateTestRunWithSuccess(tests: Tests, debug: boolean = false): Tests {
this.clearProgressTicker();
// Treat errors as a special case, as we generally wouldn't have any errors
const statusText = [];
const toolTip = [];
let foreColor = '';
if (tests.summary.passed > 0) {
statusText.push(`${constants.Octicons.Test_Pass} ${tests.summary.passed}`);
toolTip.push(`${tests.summary.passed} Passed`);
foreColor = '#66ff66';
}
if (tests.summary.skipped > 0) {
statusText.push(`${constants.Octicons.Test_Skip} ${tests.summary.skipped}`);
toolTip.push(`${tests.summary.skipped} Skipped`);
foreColor = '#66ff66';
}
if (tests.summary.failures > 0) {
statusText.push(`${constants.Octicons.Test_Fail} ${tests.summary.failures}`);
toolTip.push(`${tests.summary.failures} Failed`);
foreColor = 'yellow';
}
if (tests.summary.errors > 0) {
statusText.push(`${constants.Octicons.Test_Error} ${tests.summary.errors}`);
toolTip.push(`${tests.summary.errors} Error${tests.summary.errors > 1 ? 's' : ''}`);
foreColor = 'yellow';
}
this.statusBar.tooltip = toolTip.length === 0 ? 'No Tests Ran' : `${toolTip.join(', ')} (Tests)`;
this.statusBar.text = statusText.length === 0 ? 'No Tests Ran' : statusText.join(' ');
this.statusBar.color = foreColor;
this.statusBar.command = constants.Commands.Tests_View_UI;
if (this.onDidChange) {
this.onDidChange.fire();
}
if (statusText.length === 0 && !debug) {
vscode.window.showWarningMessage('No tests ran, please check the configuration settings for the tests.');
}
return tests;
}
// tslint:disable-next-line:no-any
private updateTestRunWithFailure(reason: any): Promise<any> {
this.clearProgressTicker();
this.statusBar.command = constants.Commands.Tests_View_UI;
if (reason === CANCELLATION_REASON) {
this.statusBar.text = '$(zap) Run Tests';
this.statusBar.tooltip = 'Run Tests';
} else {
this.statusBar.text = '$(alert) Tests Failed';
this.statusBar.tooltip = 'Running Tests Failed';
displayTestErrorMessage('There was an error in running the tests.');
}
return Promise.reject(reason);
}
private displayProgress(message: string, tooltip: string, command: string) {
this.progressPrefix = this.statusBar.text = `$(stop) ${message}`;
this.statusBar.command = command;
this.statusBar.tooltip = tooltip;
this.statusBar.show();
this.clearProgressTicker();
this.progressTimeout = setInterval(() => this.updateProgressTicker(), 150);
}
private updateProgressTicker() {
const text = `${this.progressPrefix} ${this.ticker[this.discoverCounter % 7]}`;
this.discoverCounter += 1;
this.statusBar.text = text;
}
private clearProgressTicker() {
if (this.progressTimeout) {
clearInterval(this.progressTimeout);
}
this.progressTimeout = null;
this.discoverCounter = 0;
}
// tslint:disable-next-line:no-any
private disableTests(): Promise<any> {
// tslint:disable-next-line:no-any
const def = createDeferred<any>();
const pythonConfig = vscode.workspace.getConfiguration('python');
const settingsToDisable = ['unitTest.promptToConfigure', 'unitTest.pyTestEnabled',
'unitTest.unittestEnabled', 'unitTest.nosetestsEnabled'];
function disableTest() {
if (settingsToDisable.length === 0) {
return def.resolve();
}
pythonConfig.update(settingsToDisable.shift(), false)
.then(disableTest.bind(this), disableTest.bind(this));
}
disableTest();
return def.promise;
}
private updateWithDiscoverSuccess(tests: Tests) {
this.clearProgressTicker();
const haveTests = tests && (tests.testFunctions.length > 0);
this.statusBar.text = '$(zap) Run Tests';
this.statusBar.tooltip = 'Run Tests';
this.statusBar.command = constants.Commands.Tests_View_UI;
this.statusBar.show();
if (this.onDidChange) {
this.onDidChange.fire();
}
if (!haveTests) {
vscode.window.showInformationMessage('No tests discovered, please check the configuration settings for the tests.', 'Disable Tests').then(item => {
if (item === 'Disable Tests') {
// tslint:disable-next-line:no-floating-promises
this.disableTests();
}
});
}
}
// tslint:disable-next-line:no-any
private updateWithDiscoverFailure(reason: any) {
this.clearProgressTicker();
this.statusBar.text = '$(zap) Discover Tests';
this.statusBar.tooltip = 'Discover Tests';
this.statusBar.command = constants.Commands.Tests_Discover;
this.statusBar.show();
this.statusBar.color = 'yellow';
if (reason !== CANCELLATION_REASON) {
this.statusBar.text = '$(alert) Test discovery failed';
this.statusBar.tooltip = 'Discovering Tests failed (view \'Python Test Log\' output panel for details)';
// TODO: ignore this quitemode, always display the error message (inform the user).
if (!isNotInstalledError(reason)) {
// TODO: show an option that will invoke a command 'python.test.configureTest' or similar.
// This will be hanlded by main.ts that will capture input from user and configure the tests.
vscode.window.showErrorMessage('There was an error in discovering tests, please check the configuration settings for the tests.');
}
}
}
}