forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.ts
More file actions
188 lines (176 loc) · 7.94 KB
/
runner.ts
File metadata and controls
188 lines (176 loc) · 7.94 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
/// <reference path="../../../../typings/globals/xml2js/index.d.ts" />
'use strict';
import * as path from 'path';
import { createDeferred, createTemporaryFile } from '../../common/helpers';
import { TestFile, TestsToRun, TestSuite, TestFunction, FlattenedTestFunction, Tests, TestStatus, FlattenedTestSuite } from '../common/contracts';
import { extractBetweenDelimiters, flattenTestFiles, updateResults, convertFileToPackage } from '../common/testUtils';
import { BaseTestManager } from '../common/baseTestManager';
import { CancellationToken, OutputChannel } from 'vscode';
import { run } from '../common/runner';
import { Server } from './socketServer';
import { PythonSettings } from '../../common/configSettings';
const settings = PythonSettings.getInstance();
interface TestStatusMap {
status: TestStatus;
summaryProperty: string;
}
const outcomeMapping = new Map<string, TestStatusMap>();
outcomeMapping.set('passed', { status: TestStatus.Pass, summaryProperty: 'passed' });
outcomeMapping.set('failed', { status: TestStatus.Fail, summaryProperty: 'failures' });
outcomeMapping.set('error', { status: TestStatus.Error, summaryProperty: 'errors' });
outcomeMapping.set('skipped', { status: TestStatus.Skipped, summaryProperty: 'skipped' });
interface ITestData {
test: string;
message: string;
outcome: string;
traceback: string;
}
export function runTest(testManager: BaseTestManager, rootDirectory: string, tests: Tests, args: string[], testsToRun?: TestsToRun, token?: CancellationToken, outChannel?: OutputChannel): Promise<Tests> {
tests.summary.errors = 0;
tests.summary.failures = 0;
tests.summary.passed = 0;
tests.summary.skipped = 0;
let failFast = false;
const testLauncherFile = path.join(__dirname, '..', '..', '..', '..', 'pythonFiles', 'PythonTools', 'visualstudio_py_testlauncher.py');
const server = new Server();
server.on('error', (message: string, ...data: string[]) => {
console.log(`${message} ${data.join(' ')}`);
});
server.on('log', (message: string, ...data: string[]) => {
});
server.on('connect', (data) => {
});
server.on('start', (data: { test: string }) => {
});
server.on('result', (data: ITestData) => {
const test = tests.testFunctions.find(t => t.testFunction.nameToRun === data.test);
if (test) {
const statusDetails = outcomeMapping.get(data.outcome);
test.testFunction.status = statusDetails.status;
test.testFunction.message = data.message;
test.testFunction.traceback = data.traceback;
tests.summary[statusDetails.summaryProperty] += 1;
if (failFast && (statusDetails.summaryProperty === 'failures' || statusDetails.summaryProperty === 'errors')) {
testManager.stop();
}
}
});
server.on('socket.disconnected', (data) => {
});
return server.start().then(port => {
let testPaths: string[] = getIdsOfTestsToRun(tests, testsToRun);
for (let counter = 0; counter < testPaths.length; counter++) {
testPaths[counter] = '-t' + testPaths[counter].trim();
}
const startTestDiscoveryDirectory = getStartDirectory(args);
function runTest(testFile: string = '', testId: string = '') {
let testArgs = buildTestArgs(args);
failFast = testArgs.indexOf('--uf') >= 0;
testArgs = testArgs.filter(arg=>arg !== '--uf');
testArgs.push(`--result-port=${port}`);
testArgs.push(`--us=${startTestDiscoveryDirectory}`);
if (testId.length > 0) {
testArgs.push(`-t${testId}`);
}
if (testFile.length > 0) {
testArgs.push(`--testFile=${testFile}`);
}
return run(settings.pythonPath, [testLauncherFile].concat(testArgs), rootDirectory, token, outChannel);
}
// Test everything
if (testPaths.length === 0) {
return runTest();
}
// Ok, the ptvs test runner can only work with one test at a time
let promise = Promise.resolve<string>('');
if (Array.isArray(testsToRun.testFile)) {
testsToRun.testFile.forEach(testFile => {
promise = promise.then(() => runTest(testFile.fullPath, testFile.nameToRun));
});
}
if (Array.isArray(testsToRun.testSuite)) {
testsToRun.testSuite.forEach(testSuite => {
const testFileName = tests.testSuits.find(t => t.testSuite === testSuite).parentTestFile.fullPath;
promise = promise.then(() => runTest(testFileName, testSuite.nameToRun));
});
}
if (Array.isArray(testsToRun.testFunction)) {
testsToRun.testFunction.forEach(testFn => {
const testFileName = tests.testFunctions.find(t => t.testFunction === testFn).parentTestFile.fullPath;
promise = promise.then(() => runTest(testFileName, testFn.nameToRun));
});
}
return promise;
}).then(() => {
updateResults(tests);
return tests;
});
}
function getStartDirectory(args: string[]): string {
let startDirectory = '.';
const indexOfStartDir = args.findIndex(arg => arg.indexOf('-s') === 0 || arg.indexOf('--start-directory') === 0);
if (indexOfStartDir >= 0) {
const startDir = args[indexOfStartDir].trim();
if ((startDir.trim() === '-s' || startDir.trim() === '--start-directory') && args.length >= indexOfStartDir) {
// Assume the next items is the directory
startDirectory = args[indexOfStartDir + 1];
}
else {
const lenToStartFrom = startDir.startsWith('-s') ? '-s'.length : '--start-directory'.length;
startDirectory = startDir.substring(lenToStartFrom).trim();
if (startDirectory.startsWith('=')) {
startDirectory = startDirectory.substring(1);
}
}
}
return startDirectory;
}
function buildTestArgs(args: string[]): string[] {
const startTestDiscoveryDirectory = getStartDirectory(args);
let pattern = 'test*.py';
const indexOfPattern = args.findIndex(arg => arg.indexOf('-p') === 0 || arg.indexOf('--pattern') === 0);
if (indexOfPattern >= 0) {
const patternValue = args[indexOfPattern].trim();
if ((patternValue.trim() === '-p' || patternValue.trim() === '--pattern') && args.length >= indexOfPattern) {
// Assume the next items is the directory
pattern = args[indexOfPattern + 1];
}
else {
const lenToStartFrom = patternValue.startsWith('-p') ? '-p'.length : '--pattern'.length;
pattern = patternValue.substring(lenToStartFrom).trim();
if (pattern.startsWith('=')) {
pattern = pattern.substring(1);
}
}
}
const failFast = args.some(arg => arg.trim() === '-f' || arg.trim() === '--failfast');
const verbosity = args.some(arg => arg.trim().indexOf('-v') === 0) ? 2 : 1;
const testArgs = [`--us=${startTestDiscoveryDirectory}`, `--up=${pattern}`, `--uvInt=${verbosity}`];
if (failFast) {
testArgs.push('--uf');
}
return testArgs;
}
function getIdsOfTestsToRun(tests: Tests, testsToRun: TestsToRun): string[] {
const testIds = [];
if (testsToRun && testsToRun.testFolder) {
// Get test ids of files in these folders
testsToRun.testFolder.map(folder => {
tests.testFiles.forEach(f => {
if (f.fullPath.startsWith(folder.name)) {
testIds.push(f.nameToRun);
}
});
});
}
if (testsToRun && testsToRun.testFile) {
testIds.push(...testsToRun.testFile.map(f => f.nameToRun));
}
if (testsToRun && testsToRun.testSuite) {
testIds.push(...testsToRun.testSuite.map(f => f.nameToRun));
}
if (testsToRun && testsToRun.testFunction) {
testIds.push(...testsToRun.testFunction.map(f => f.nameToRun));
}
return testIds;
}