forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollector.ts
More file actions
218 lines (199 loc) · 8.72 KB
/
Copy pathcollector.ts
File metadata and controls
218 lines (199 loc) · 8.72 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
210
211
212
213
214
215
216
217
218
'use strict';
import { execPythonFile } from './../../common/utils';
import { TestFile, TestSuite, TestFunction, Tests } from '../common/contracts';
import * as os from 'os';
import { extractBetweenDelimiters, flattenTestFiles, convertFileToPackage } from '../common/testUtils';
import * as vscode from 'vscode';
import * as path from 'path';
import { PythonSettings } from '../../common/configSettings';
import { OutputChannel } from 'vscode';
const pythonSettings = PythonSettings.getInstance();
const argsToExcludeForDiscovery = ['-x', '--exitfirst',
'--fixtures-per-test', '--pdb', '--runxfail',
'--lf', '--last-failed', '--ff', '--failed-first',
'--cache-show', '--cache-clear',
'-v', '--verbose', '-q', '-quiet',
'--disable-pytest-warnings', '-l', '--showlocals'];
const settingsInArgsToExcludeForDiscovery = [];
export function discoverTests(rootDirectory: string, args: string[], token: vscode.CancellationToken, ignoreCache: boolean, outChannel: OutputChannel): Promise<Tests> {
let logOutputLines: string[] = [''];
let testFiles: TestFile[] = [];
let parentNodes: { indent: number, item: TestFile | TestSuite }[] = [];
const errorLine = /==*( *)ERRORS( *)=*/;
const errorFileLine = /__*( *)ERROR collecting (.*)/;
const lastLineWithErrors = /==*.*/;
let haveErrors = false;
// Remove unwanted arguments
args = args.filter(arg => {
if (argsToExcludeForDiscovery.indexOf(arg.trim()) !== -1) {
return false;
}
if (settingsInArgsToExcludeForDiscovery.some(setting => setting.indexOf(arg.trim()) === 0)) {
return false;
}
return true;
});
if (ignoreCache && args.indexOf('--cache-clear') === -1) {
args.push('--cache-clear');
}
function processOutput(output: string) {
output.split(/\r?\n/g).forEach((line, index, lines) => {
if (token && token.isCancellationRequested) {
return;
}
if (line.trim().startsWith('<Module \'') || index === lines.length - 1) {
// process the previous lines
parsePyTestModuleCollectionResult(rootDirectory, logOutputLines, testFiles, parentNodes);
logOutputLines = [''];
}
if (errorLine.test(line)) {
haveErrors = true;
logOutputLines = [''];
return;
}
if (errorFileLine.test(line)) {
haveErrors = true;
if (logOutputLines.length !== 1 && logOutputLines[0].length !== 0) {
parsePyTestModuleCollectionError(rootDirectory, logOutputLines, testFiles, parentNodes);
logOutputLines = [''];
}
}
if (lastLineWithErrors.test(line) && haveErrors) {
parsePyTestModuleCollectionError(rootDirectory, logOutputLines, testFiles, parentNodes);
logOutputLines = [''];
}
if (index === 0) {
if (output.startsWith(os.EOL) || lines.length > 1) {
logOutputLines[logOutputLines.length - 1] += line;
logOutputLines.push('');
return;
}
logOutputLines[logOutputLines.length - 1] += line;
return;
}
if (index === lines.length - 1) {
logOutputLines[logOutputLines.length - 1] += line;
return;
}
logOutputLines[logOutputLines.length - 1] += line;
logOutputLines.push('');
return;
});
}
return execPythonFile(pythonSettings.unitTest.pyTestPath, args.concat(['--collect-only']), rootDirectory, false, null, token)
.then(data => {
outChannel.appendLine(data);
processOutput(data);
if (token && token.isCancellationRequested) {
return Promise.reject<Tests>('cancelled');
}
return flattenTestFiles(testFiles);
});
}
const DELIMITER = '\'';
function parsePyTestModuleCollectionError(rootDirectory: string, lines: string[], testFiles: TestFile[],
parentNodes: { indent: number, item: TestFile | TestSuite }[]) {
lines = lines.filter(line => line.trim().length > 0);
if (lines.length <= 1) {
return;
}
let errorFileLine = lines[0];
let fileName = errorFileLine.substring(errorFileLine.indexOf('ERROR collecting') + 'ERROR collecting'.length).trim();
fileName = fileName.substr(0, fileName.lastIndexOf(' '));
const currentPackage = convertFileToPackage(fileName);
const fullyQualifiedName = path.isAbsolute(fileName) ? fileName : path.resolve(rootDirectory, fileName);
const testFile = {
functions: [], suites: [], name: fileName, fullPath: fullyQualifiedName,
nameToRun: fileName, xmlName: currentPackage, time: 0, errorsWhenDiscovering: lines.join('\n')
};
testFiles.push(testFile);
parentNodes.push({ indent: 0, item: testFile });
return;
}
function parsePyTestModuleCollectionResult(rootDirectory: string, lines: string[], testFiles: TestFile[], parentNodes: { indent: number, item: TestFile | TestSuite }[]) {
let currentPackage: string = '';
lines.forEach(line => {
const trimmedLine = line.trim();
const name = extractBetweenDelimiters(trimmedLine, DELIMITER, DELIMITER);
const indent = line.indexOf('<');
if (trimmedLine.startsWith('<Module \'')) {
currentPackage = convertFileToPackage(name);
const fullyQualifiedName = path.isAbsolute(name) ? name : path.resolve(rootDirectory, name);
const testFile = {
functions: [], suites: [], name: name, fullPath: fullyQualifiedName,
nameToRun: name, xmlName: currentPackage, time: 0
};
testFiles.push(testFile);
parentNodes.push({ indent: indent, item: testFile });
return;
}
const parentNode = findParentOfCurrentItem(indent, parentNodes);
if (trimmedLine.startsWith('<Class \'') || trimmedLine.startsWith('<UnitTestCase \'')) {
const isUnitTest = trimmedLine.startsWith('<UnitTestCase \'');
const rawName = parentNode.item.nameToRun + `::${name}`;
const xmlName = parentNode.item.xmlName + `.${name}`;
const testSuite: TestSuite = { name: name, nameToRun: rawName, functions: [], suites: [], isUnitTest: isUnitTest, isInstance: false, xmlName: xmlName, time: 0 };
parentNode.item.suites.push(testSuite);
parentNodes.push({ indent: indent, item: testSuite });
return;
}
if (trimmedLine.startsWith('<Instance \'')) {
let suite = (parentNode.item as TestSuite);
// suite.rawName = suite.rawName + '::()';
// suite.xmlName = suite.xmlName + '.()';
suite.isInstance = true;
return;
}
if (trimmedLine.startsWith('<TestCaseFunction \'') || trimmedLine.startsWith('<Function \'')) {
const rawName = parentNode.item.nameToRun + '::' + name;
const fn: TestFunction = { name: name, nameToRun: rawName, time: 0 };
parentNode.item.functions.push(fn);
return;
}
});
}
function findParentOfCurrentItem(indentOfCurrentItem: number, parentNodes: { indent: number, item: TestFile | TestSuite }[]): { indent: number, item: TestFile | TestSuite } {
while (parentNodes.length > 0) {
let parentNode = parentNodes[parentNodes.length - 1];
if (parentNode.indent < indentOfCurrentItem) {
return parentNode;
}
parentNodes.pop();
continue;
}
return null;
}
/* Sample output from py.test --collect-only
<Module 'test_another.py'>
<Class 'Test_CheckMyApp'>
<Instance '()'>
<Function 'test_simple_check'>
<Function 'test_complex_check'>
<Module 'test_one.py'>
<UnitTestCase 'Test_test1'>
<TestCaseFunction 'test_A'>
<TestCaseFunction 'test_B'>
<Module 'test_two.py'>
<UnitTestCase 'Test_test1'>
<TestCaseFunction 'test_A2'>
<TestCaseFunction 'test_B2'>
<Module 'testPasswords/test_Pwd.py'>
<UnitTestCase 'Test_Pwd'>
<TestCaseFunction 'test_APwd'>
<TestCaseFunction 'test_BPwd'>
<Module 'testPasswords/test_multi.py'>
<Class 'Test_CheckMyApp'>
<Instance '()'>
<Function 'test_simple_check'>
<Function 'test_complex_check'>
<Class 'Test_NestedClassA'>
<Instance '()'>
<Function 'test_nested_class_methodB'>
<Class 'Test_nested_classB_Of_A'>
<Instance '()'>
<Function 'test_d'>
<Function 'test_username'>
<Function 'test_parametrized_username[one]'>
<Function 'test_parametrized_username[two]'>
<Function 'test_parametrized_username[three]'>
*/