forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestFiles.ts
More file actions
167 lines (146 loc) · 6.31 KB
/
testFiles.ts
File metadata and controls
167 lines (146 loc) · 6.31 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
'use strict';
import * as vscode from 'vscode';
import {CodeLensProvider, TextDocument, CancellationToken, CodeLens, SymbolInformation} from 'vscode';
import * as telemetryContracts from '../../common/telemetryContracts';
import {Tests, TestFile, TestsToRun, TestSuite, TestFunction} from '../common/contracts';
import * as constants from '../../common/constants';
import {getDiscoveredTests} from '../common/testUtils';
interface CodeLensData {
symbolKind: vscode.SymbolKind;
symbolName: string;
fileName: string;
}
interface FunctionsAndSuites {
functions: TestFunction[];
suites: TestSuite[];
}
export class TestFileCodeLensProvider implements CodeLensProvider {
public provideCodeLenses(document: TextDocument, token: CancellationToken): Thenable<CodeLens[]> {
let testItems = getDiscoveredTests();
if (!testItems || testItems.testFiles.length === 0 || testItems.testFunctions.length === 0) {
return Promise.resolve([]);
}
let cancelTokenSrc = new vscode.CancellationTokenSource();
token.onCancellationRequested(() => { cancelTokenSrc.cancel(); });
// Strop trying to build the code lenses if unable to get a list of
// symbols in this file afrer x time
setTimeout(() => {
if (!cancelTokenSrc.token.isCancellationRequested) {
cancelTokenSrc.cancel();
}
}, constants.Delays.MaxUnitTestCodeLensDelay);
return getCodeLenses(document.uri, token);
}
resolveCodeLens(codeLens: CodeLens, token: CancellationToken): CodeLens | Thenable<CodeLens> {
codeLens.command = { command: 'python.runtests', title: 'Test' };
return Promise.resolve(codeLens);
}
}
function getCodeLenses(documentUri: vscode.Uri, token: vscode.CancellationToken): Thenable<CodeLens[]> {
const tests = getDiscoveredTests();
if (!tests) {
return null;
}
const file = tests.testFiles.find(file => file.fullPath === documentUri.fsPath);
if (!file) {
return Promise.resolve([]);
}
const allFuncsAndSuites = getAllTestSuitesAndFunctionsPerFile(file);
return vscode.commands.executeCommand('vscode.executeDocumentSymbolProvider', documentUri, token)
.then((symbols: vscode.SymbolInformation[]) => {
return symbols.filter(symbol => {
return symbol.kind === vscode.SymbolKind.Function ||
symbol.kind === vscode.SymbolKind.Method ||
symbol.kind === vscode.SymbolKind.Class;
}).map(symbol => {
// This is bloody crucial, if the start and end columns are the same
// then vscode goes bonkers when ever you edit a line (start scrolling magically)
const range = new vscode.Range(symbol.location.range.start,
new vscode.Position(symbol.location.range.end.line,
symbol.location.range.end.character + 1));
return getCodeLens(documentUri.fsPath, allFuncsAndSuites,
range, symbol.name, symbol.kind);
}).filter(codeLens => codeLens !== null);
}, reason => {
if (token.isCancellationRequested) {
return [];
}
return Promise.reject(reason);
});
}
// Move all of this rubbis into a separate file // too long
const testParametirizedFunction = /.*\[.*\]/g;
function getCodeLens(fileName: string, allFuncsAndSuites: FunctionsAndSuites,
range: vscode.Range, symbolName: string, symbolKind: vscode.SymbolKind): vscode.CodeLens {
switch (symbolKind) {
case vscode.SymbolKind.Function:
case vscode.SymbolKind.Method: {
return getFunctionCodeLens(fileName, allFuncsAndSuites, symbolName, range);
}
case vscode.SymbolKind.Class: {
const cls = allFuncsAndSuites.suites.find(cls => cls.name === symbolName);
if (!cls) {
return null;
}
return new CodeLens(range, {
title: constants.Text.CodeLensUnitTest,
command: constants.Commands.Tests_Run,
arguments: [<TestsToRun>{ testSuite: [cls] }]
});
}
}
return null;
}
function getFunctionCodeLens(filePath: string, functionsAndSuites: FunctionsAndSuites,
symbolName: string, range: vscode.Range): vscode.CodeLens {
const fn = functionsAndSuites.functions.find(fn => fn.name === symbolName);
if (fn) {
return new CodeLens(range, {
title: constants.Text.CodeLensUnitTest,
command: constants.Commands.Tests_Run,
arguments: [<TestsToRun>{ testFunction: [fn] }]
});
}
// Ok, possible we're dealing with parameterized unit tests
// If we have [ in the name, then this is a parameterized function
let functions = functionsAndSuites.functions.filter(fn => fn.name.startsWith(symbolName + '[') && fn.name.endsWith(']'));
if (functions.length === 0) {
return null;
}
if (functions.length === 0) {
return new CodeLens(range, {
title: constants.Text.CodeLensUnitTest,
command: constants.Commands.Tests_Run,
arguments: [<TestsToRun>{ testFunction: functions }]
});
}
// Find all flattened functions
return new CodeLens(range, {
title: constants.Text.CodeLensUnitTest + ' (Multiple)',
command: constants.Commands.Tests_Picker_UI,
arguments: [filePath, functions]
});
}
function getAllTestSuitesAndFunctionsPerFile(testFile: TestFile): FunctionsAndSuites {
const all = { functions: testFile.functions, suites: [] };
testFile.suites.forEach(suite => {
all.suites.push(suite);
const allChildItems = getAllTestSuitesAndFunctions(suite);
all.functions.push(...allChildItems.functions);
all.suites.push(...allChildItems.suites);
});
return all;
}
function getAllTestSuitesAndFunctions(testSuite: TestSuite): FunctionsAndSuites {
const all = { functions: [], suites: [] };
testSuite.functions.forEach(fn => {
all.functions.push(fn);
});
testSuite.suites.forEach(suite => {
all.suites.push(suite);
const allChildItems = getAllTestSuitesAndFunctions(suite);
all.functions.push(...allChildItems.functions);
all.suites.push(...allChildItems.suites);
});
return all;
}