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
209 lines (197 loc) · 9.5 KB
/
main.ts
File metadata and controls
209 lines (197 loc) · 9.5 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
'use strict';
import { inject, injectable } from 'inversify';
import { Event, EventEmitter, StatusBarAlignment, StatusBarItem } from 'vscode';
import { IApplicationShell, ICommandManager } from '../../common/application/types';
import * as constants from '../../common/constants';
import { isNotInstalledError } from '../../common/helpers';
import { IConfigurationService } from '../../common/types';
import { Testing } from '../../common/utils/localize';
import { noop } from '../../common/utils/misc';
import { IServiceContainer } from '../../ioc/types';
import { captureTelemetry } from '../../telemetry';
import { EventName } from '../../telemetry/constants';
import { CANCELLATION_REASON } from '../common/constants';
import { ITestsHelper, Tests } from '../common/types';
import { ITestResultDisplay } from '../types';
@injectable()
export class TestResultDisplay implements ITestResultDisplay {
private statusBar: StatusBarItem;
private discoverCounter = 0;
private ticker = ['|', '/', '-', '|', '/', '-', '\\'];
private progressTimeout: NodeJS.Timer | number | null = null;
private _enabled: boolean = false;
private progressPrefix!: string;
private readonly didChange = new EventEmitter<void>();
private readonly appShell: IApplicationShell;
private readonly testsHelper: ITestsHelper;
private readonly cmdManager: ICommandManager;
public get onDidChange(): Event<void> {
return this.didChange.event;
}
// tslint:disable-next-line:no-any
constructor(@inject(IServiceContainer) private serviceContainer: IServiceContainer) {
this.appShell = serviceContainer.get<IApplicationShell>(IApplicationShell);
this.statusBar = this.appShell.createStatusBarItem(StatusBarAlignment.Left);
this.testsHelper = serviceContainer.get<ITestsHelper>(ITestsHelper);
this.cmdManager = serviceContainer.get<ICommandManager>(ICommandManager);
}
public dispose() {
this.clearProgressTicker();
this.statusBar.dispose();
}
public get enabled() {
return this._enabled;
}
public set enabled(enable: boolean) {
this._enabled = enable;
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
.catch(noop);
}
public displayDiscoverStatus(testDiscovery: Promise<Tests>, quietMode: boolean = false) {
this.displayProgress('Discovering Tests', 'Discovering tests (click to stop)', constants.Commands.Tests_Ask_To_Stop_Discovery);
return testDiscovery
.then(tests => {
this.updateWithDiscoverSuccess(tests, quietMode);
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: string[] = [];
const toolTip: string[] = [];
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;
this.didChange.fire();
if (statusText.length === 0 && !debug) {
this.appShell.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';
this.testsHelper.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) {
// tslint:disable-next-line: no-any
clearInterval(this.progressTimeout as any);
}
this.progressTimeout = null;
this.discoverCounter = 0;
}
@captureTelemetry(EventName.UNITTEST_DISABLE)
// tslint:disable-next-line:no-any
private async disableTests(): Promise<any> {
const configurationService = this.serviceContainer.get<IConfigurationService>(IConfigurationService);
const settingsToDisable = ['testing.promptToConfigure', 'testing.pytestEnabled', 'testing.unittestEnabled', 'testing.nosetestsEnabled'];
for (const setting of settingsToDisable) {
await configurationService.updateSetting(setting, false).catch(noop);
}
this.cmdManager.executeCommand('setContext', 'testsDiscovered', false);
}
private updateWithDiscoverSuccess(tests: Tests, quietMode: boolean = false) {
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.didChange) {
this.didChange.fire();
}
if (!haveTests && !quietMode) {
this.appShell
.showInformationMessage('No tests discovered, please check the configuration settings for the tests.', Testing.disableTests(), Testing.configureTests())
.then(item => {
if (item === Testing.disableTests()) {
this.disableTests().catch(ex => console.error('Python Extension: disableTests', ex));
} else if (item === Testing.configureTests()) {
this.cmdManager.executeCommand(constants.Commands.Tests_Configure, undefined, undefined, undefined).then(noop);
}
});
}
}
// 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)';
// tslint:disable-next-line:no-suspicious-comment
// TODO: ignore this quitemode, always display the error message (inform the user).
if (!isNotInstalledError(reason)) {
// tslint:disable-next-line:no-suspicious-comment
// 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.
this.appShell.showErrorMessage('Test discovery error, please check the configuration settings for the tests.');
}
}
}
}