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
134 lines (124 loc) · 5.58 KB
/
main.ts
File metadata and controls
134 lines (124 loc) · 5.58 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
'use strict';
import * as vscode from 'vscode';
import {Tests, TestsToRun, TestFolder, TestFile, TestStatus, TestSuite, TestFunction, CANCELLATION_REASON} from '../common/contracts';
import {PythonSettings} from '../../common/configSettings';
import * as constants from '../../common/constants';
import {displayTestErrorMessage} from '../common/testUtils';
const settings = PythonSettings.getInstance();
export class TestResultDisplay {
private statusBar: vscode.StatusBarItem;
constructor(private outputChannel: vscode.OutputChannel) {
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(tests: Promise<Tests>) {
this.displayProgress('Running Tests', `Running Tests (Click to Stop)`, constants.Commands.Tests_Ask_To_Stop_Test);
tests
.then(this.updateTestRunWithSuccess.bind(this))
.catch(this.updateTestRunWithFailure.bind(this))
// We don't care about any other exceptions returned by updateTestRunWithFailure
.catch(() => { });
}
private updateTestRunWithSuccess(tests: Tests): Tests {
this.clearProgressTicker();
// Treat errors as a special case, as we generally wouldn't have any errors
const statusText = [];
const toolTip = [];
if (tests.summary.passed > 0) {
statusText.push(`${constants.Octicons.Test_Pass} ${tests.summary.passed}`);
toolTip.push(`${tests.summary.passed} Passed`);
}
if (tests.summary.failures > 0) {
statusText.push(`${constants.Octicons.Test_Fail} ${tests.summary.failures}`);
toolTip.push(`${tests.summary.failures} Failed`);
}
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' : ''}`);
}
if (tests.summary.skipped > 0) {
statusText.push(`${constants.Octicons.Test_Skip} ${tests.summary.skipped}`);
toolTip.push(`${tests.summary.skipped} Skipped`);
}
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.command = constants.Commands.Tests_View_UI;
return tests;
}
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 = `$(octicon-alert) Tests Failed`;
this.statusBar.tooltip = 'Running Tests Failed';
displayTestErrorMessage('There was an error in running the tests.');
}
return Promise.reject(reason);
}
private discoverCounter = 0;
private ticker = ['|', '/', '-', '|', '/', '-', '\\'];
private progressTimeout;
private progressPrefix: string;
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() {
let 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;
}
public DisplayDiscoverStatus(tests: Promise<Tests>, quietMode: boolean = false) {
this.displayProgress('Discovering Tests', 'Discovering Tests (Click to Stop)', constants.Commands.Tests_Ask_To_Stop_Discovery);
return tests.then(tests => {
this.updateWithDiscoverSuccess(tests);
return tests;
}).catch(reason => {
this.updateWithDiscoverFailure(reason, quietMode);
return Promise.reject(reason);
});
}
private updateWithDiscoverSuccess(tests: Tests) {
this.clearProgressTicker();
const haveTests = tests && (tests.testFunctions.length > 0);
this.statusBar.text = haveTests ? '$(zap) Run Tests' : 'No Tests';
this.statusBar.tooltip = haveTests ? 'Run Tests' : 'No Tests discovered';
this.statusBar.command = haveTests ? 'python.viewTests' : constants.Commands.Tests_Discover;
this.statusBar.show();
}
private updateWithDiscoverFailure(reason: any, quietMode: boolean = false) {
this.clearProgressTicker();
this.statusBar.text = `$(zap) Discover Tests`;
this.statusBar.tooltip = 'Discover Tests';
this.statusBar.command = constants.Commands.Tests_Discover;
this.statusBar.show();
if (reason !== CANCELLATION_REASON && !quietMode) {
vscode.window.showErrorMessage('There was an error in discovering tests');
}
}
}