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
63 lines (59 loc) · 3.17 KB
/
runner.ts
File metadata and controls
63 lines (59 loc) · 3.17 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
'use strict';
import * as path from 'path';
import { CancellationToken, OutputChannel, Uri } from 'vscode';
import { PythonSettings } from '../../common/configSettings';
import { createTemporaryFile } from '../../common/helpers';
import { run } from '../common/runner';
import { ITestDebugLauncher, ITestResultsService, Tests, TestsToRun } from '../common/types';
import { PassCalculationFormulae, updateResultsFromXmlLogFile } from '../common/xUnitParser';
export function runTest(testResultsService: ITestResultsService, debugLauncher: ITestDebugLauncher, rootDirectory: string, tests: Tests, args: string[], testsToRun?: TestsToRun, token?: CancellationToken, outChannel?: OutputChannel, debug?: boolean): Promise<Tests> {
let testPaths = [];
if (testsToRun && testsToRun.testFolder) {
testPaths = testPaths.concat(testsToRun.testFolder.map(f => f.nameToRun));
}
if (testsToRun && testsToRun.testFile) {
testPaths = testPaths.concat(testsToRun.testFile.map(f => f.nameToRun));
}
if (testsToRun && testsToRun.testSuite) {
testPaths = testPaths.concat(testsToRun.testSuite.map(f => f.nameToRun));
}
if (testsToRun && testsToRun.testFunction) {
testPaths = testPaths.concat(testsToRun.testFunction.map(f => f.nameToRun));
}
let xmlLogFile = '';
let xmlLogFileCleanup: Function = null;
return createTemporaryFile('.xml').then(xmlLogResult => {
xmlLogFile = xmlLogResult.filePath;
xmlLogFileCleanup = xmlLogResult.cleanupCallback;
if (testPaths.length > 0) {
// Ignore the test directories, as we're running a specific test
args = args.filter(arg => arg.trim().startsWith('-'));
}
const testArgs = testPaths.concat(args, [`--junitxml=${xmlLogFile}`]);
const pythonSettings = PythonSettings.getInstance(Uri.file(rootDirectory));
if (debug) {
const testLauncherFile = path.join(__dirname, '..', '..', '..', '..', 'pythonFiles', 'PythonTools', 'testlauncher.py');
const pytestlauncherargs = [rootDirectory, 'my_secret', pythonSettings.unitTest.debugPort.toString(), 'pytest'];
const debuggerArgs = [testLauncherFile].concat(pytestlauncherargs).concat(testArgs);
// tslint:disable-next-line:prefer-type-cast no-any
return debugLauncher.launchDebugger(rootDirectory, debuggerArgs, token, outChannel) as Promise<any>;
} else {
// tslint:disable-next-line:prefer-type-cast no-any
return run(pythonSettings.unitTest.pyTestPath, testArgs, rootDirectory, token, outChannel) as Promise<any>;
}
}).then(() => {
return updateResultsFromLogFiles(tests, xmlLogFile, testResultsService);
}).then(result => {
xmlLogFileCleanup();
return result;
}).catch(reason => {
xmlLogFileCleanup();
return Promise.reject(reason);
});
}
export function updateResultsFromLogFiles(tests: Tests, outputXmlFile: string, testResultsService: ITestResultsService): Promise<Tests> {
return updateResultsFromXmlLogFile(tests, outputXmlFile, PassCalculationFormulae.pytest).then(() => {
testResultsService.updateResults(tests);
return tests;
});
}