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
158 lines (151 loc) · 6.59 KB
/
Copy pathrunner.ts
File metadata and controls
158 lines (151 loc) · 6.59 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
/// <reference path="../../../../typings/globals/xml2js/index.d.ts" />
'use strict';
import * as vscode from 'vscode';
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(rootDirectory: string, tests: Tests, args: string[], testsToRun?: TestsToRun, token?: CancellationToken, outChannel?: OutputChannel, debug?: boolean): Promise<Tests> {
tests.summary.errors = 0;
tests.summary.failures = 0;
tests.summary.passed = 0;
tests.summary.skipped = 0;
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[]) => {
var x = '';
});
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;
}
});
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();
}
let testArgs = buildTestArgs(args);
const pyTestRunnerArgs = [`--result-port=${port}`];
if (debug === true) {
pyTestRunnerArgs.concat([`--secret=my_secret`, `--port=3000`]);
}
testArgs = [testLauncherFile].concat(testArgs).concat(pyTestRunnerArgs).concat(testPaths);
const promise = run(settings.pythonPath, testArgs, rootDirectory, token, outChannel);
if (debug === true) {
vscode.commands.executeCommand('vscode.startDebug', {
"name": "Debug Unit Test",
"type": "python",
"request": "attach",
"localRoot": rootDirectory,
"remoteRoot": rootDirectory,
"port": 3000,
"secret": "my_secret",
"host": "localhost"
});
}
return promise;
}).then(() => {
updateResults(tests);
return tests;
});
}
function buildTestArgs(args: string[]): string[] {
let startDirectory = '.';
let pattern = 'test*.py';
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);
}
}
}
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=${startDirectory}`, `--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;
}