forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpicker.ts
More file actions
187 lines (173 loc) · 6.71 KB
/
picker.ts
File metadata and controls
187 lines (173 loc) · 6.71 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
import {QuickPickItem, window} from 'vscode';
import * as vscode from 'vscode';
import {Tests, TestsToRun, TestFolder, TestFile, TestFunction, TestSuite, FlattenedTestFunction, TestStatus} from '../common/contracts';
import {getDiscoveredTests} from '../common/testUtils';
import * as constants from '../../common/constants';
import * as path from 'path';
export class TestDisplay {
constructor() {
}
public displayStopTestUI(message: string) {
window.showQuickPick([message]).then(item => {
if (item === message) {
vscode.commands.executeCommand(constants.Commands.Tests_Stop);
}
})
}
public displayTestUI(rootDirectory: string) {
const tests = getDiscoveredTests();
window.showQuickPick(buildItems(rootDirectory, tests), { matchOnDescription: true, matchOnDetail: true }).then(onItemSelected);
}
public displayFunctionTestPickerUI(rootDirectory: string, fileName: string, testFunctions: TestFunction[]) {
const tests = getDiscoveredTests();
if (!tests) {
return;
}
const testFile = tests.testFiles.find(file => file.name === fileName || file.fullPath === fileName);
if (!testFile) {
return;
}
const flattenedFunctions = tests.testFunctions.filter(fn => {
return fn.parentTestFile.name === testFile.name &&
testFunctions.some(testFunc => testFunc.nameToRun === fn.testFunction.nameToRun);
});
window.showQuickPick(buildItemsForFunctions(rootDirectory, flattenedFunctions), { matchOnDescription: true, matchOnDetail: true }).then(onItemSelected);
}
}
enum Type {
RunAll = 0,
ReDiscover = 1,
RunFailed = 2,
RunFolder = 3,
RunFile = 4,
RunClass = 5,
RunMethod = 6,
ViewTestOutput = 7,
Null = 8
}
const statusIconMapping = new Map<TestStatus, string>();
statusIconMapping.set(TestStatus.Pass, constants.Octicons.Test_Pass);
statusIconMapping.set(TestStatus.Fail, constants.Octicons.Test_Fail);
statusIconMapping.set(TestStatus.Error, constants.Octicons.Test_Error);
statusIconMapping.set(TestStatus.Skipped, constants.Octicons.Test_Skip);
interface TestItem extends QuickPickItem {
type: Type;
fn?: FlattenedTestFunction;
}
function getSummary(tests?: Tests) {
if (!tests || !tests.summary) {
return '';
}
const statusText = [];
if (tests.summary.passed > 0) {
statusText.push(`${constants.Octicons.Test_Pass} ${tests.summary.passed} Passed`);
}
if (tests.summary.failures > 0) {
statusText.push(`${constants.Octicons.Test_Fail} ${tests.summary.failures} Failed`);
}
if (tests.summary.errors > 0) {
const plural = tests.summary.errors === 1 ? '' : 's';
statusText.push(`${constants.Octicons.Test_Error} ${tests.summary.errors} Error` + plural);
}
if (tests.summary.skipped > 0) {
statusText.push(`${constants.Octicons.Test_Skip} ${tests.summary.skipped} Skipped`);
}
return statusText.join(', ').trim();
}
function buildItems(rootDirectory: string, tests?: Tests): TestItem[] {
const items: TestItem[] = [];
items.push({ description: '', label: 'Run All Tests', type: Type.RunAll });
items.push({ description: '', label: 'Rediscover Tests', type: Type.ReDiscover });
let summary = getSummary(tests);
let separatorAdded = false;;
// Add an empty space because we'd like a separtor between actions and tests
if (summary.length === 0 && tests && tests.summary.failures === 0) {
separatorAdded = true;
summary = ' ';
}
items.push({ description: '', label: 'View Test Output', type: Type.ViewTestOutput, detail: summary });
if (!tests) {
return items;
}
if (tests.summary.failures > 0) {
items.push({ description: '', label: 'Run Failed Tests', type: Type.RunFailed, detail: `${constants.Octicons.Test_Fail} ${tests.summary.failures} Failed` });
}
if (tests.testFunctions.length > 0 && !separatorAdded) {
items.push({ description: '', label: '', type: Type.Null, detail: `` });
}
let functionItems = buildItemsForFunctions(rootDirectory, tests.testFunctions, true, true);
items.push(...functionItems);
return items;
}
const statusSortPrefix = {};
statusSortPrefix[TestStatus.Error] = '1';
statusSortPrefix[TestStatus.Fail] = '2';
statusSortPrefix[TestStatus.Skipped] = '3';
statusSortPrefix[TestStatus.Pass] = '4';
function buildItemsForFunctions(rootDirectory: string, tests: FlattenedTestFunction[], sortBasedOnResults: boolean = false, displayStatusIcons: boolean = false): TestItem[] {
let functionItems: TestItem[] = [];
tests.forEach(fn => {
const classPrefix = fn.parentTestSuite ? fn.parentTestSuite.name + '.' : '';
let icon = '';
if (displayStatusIcons && statusIconMapping.has(fn.testFunction.status)) {
icon = `${statusIconMapping.get(fn.testFunction.status)} `;
}
functionItems.push({
description: '',
detail: path.relative(rootDirectory, fn.parentTestFile.fullPath),
label: icon + fn.testFunction.name,
type: Type.RunMethod,
fn: fn
});
});
functionItems.sort((a, b) => {
let sortAPrefix = '5-';
let sortBPrefix = '5-';
if (sortBasedOnResults) {
sortAPrefix = statusSortPrefix[a.fn.testFunction.status] ? statusSortPrefix[a.fn.testFunction.status] : sortAPrefix;
sortBPrefix = statusSortPrefix[b.fn.testFunction.status] ? statusSortPrefix[b.fn.testFunction.status] : sortBPrefix;
}
if (sortAPrefix + a.detail + a.label < sortBPrefix + b.detail + b.label) {
return -1;
}
if (sortAPrefix + a.detail + a.label > sortBPrefix + b.detail + b.label) {
return 1;
}
return 0;
});
return functionItems;
}
function onItemSelected(selection: TestItem) {
if (!selection || typeof selection.type !== 'number') {
return;
}
let cmd = '';
let args = [];
switch (selection.type) {
case Type.Null: {
return;
}
case Type.RunAll: {
cmd = constants.Commands.Tests_Run;
break;
}
case Type.ReDiscover: {
cmd = constants.Commands.Tests_Discover;
break;
}
case Type.ViewTestOutput: {
cmd = constants.Commands.Tests_ViewOutput;
break;
}
case Type.RunFailed: {
cmd = constants.Commands.Tests_Run_Failed;
break;
}
case Type.RunMethod: {
cmd = constants.Commands.Tests_Run;
args.push(selection.fn);
break;
}
}
vscode.commands.executeCommand(cmd, ...args);
}