forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpytest.testMessageService.test.ts
More file actions
246 lines (239 loc) · 11.7 KB
/
pytest.testMessageService.test.ts
File metadata and controls
246 lines (239 loc) · 11.7 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { assert } from 'chai';
import * as fs from 'fs';
import * as path from 'path';
import { instance, mock } from 'ts-mockito';
import * as typeMoq from 'typemoq';
import * as vscode from 'vscode';
import { IWorkspaceService } from '../../../client/common/application/types';
import { EXTENSION_ROOT_DIR } from '../../../client/common/constants';
import { ProductNames } from '../../../client/common/installer/productNames';
import { FileSystem } from '../../../client/common/platform/fileSystem';
import { Product } from '../../../client/common/types';
import { ICondaService, IInterpreterService } from '../../../client/interpreter/contracts';
import { InterpreterService } from '../../../client/interpreter/interpreterService';
import { CondaService } from '../../../client/pythonEnvironments/discovery/locators/services/condaService';
import { TestDiscoveredTestParser } from '../../../client/testing/common/services/discoveredTestParser';
import { TestResultsService } from '../../../client/testing/common/services/testResultsService';
import { DiscoveredTests } from '../../../client/testing/common/services/types';
import { ITestVisitor, TestDiscoveryOptions, Tests, TestStatus } from '../../../client/testing/common/types';
import { XUnitParser } from '../../../client/testing/common/xUnitParser';
import { TestMessageService } from '../../../client/testing/pytest/services/testMessageService';
import {
ILocationStackFrameDetails,
IPythonTestFailMessage,
IPythonTestMessage,
PythonTestMessageSeverity,
} from '../../../client/testing/types';
import { rootWorkspaceUri, updateSetting } from '../../common';
import { initialize, initializeTest, IS_MULTI_ROOT_TEST } from '../../initialize';
import { UnitTestIocContainer } from '../serviceRegistry';
import { ITestDetails, testScenarios } from './pytest_run_tests_data';
const UNITTEST_TEST_FILES_PATH = path.join(EXTENSION_ROOT_DIR, 'src', 'test', 'pythonFiles', 'testFiles', 'standard');
const PYTEST_RESULTS_PATH = path.join(
EXTENSION_ROOT_DIR,
'src',
'test',
'pythonFiles',
'testFiles',
'pytestFiles',
'results',
);
const filterdTestScenarios = testScenarios.filter((ts) => {
return !ts.shouldRunFailed;
});
async function testMessageProperties(
message: IPythonTestMessage,
expectedMessage: IPythonTestMessage,
imported: boolean = false,
status: TestStatus,
) {
const failure = message as IPythonTestFailMessage;
const expectedFailure = expectedMessage as IPythonTestFailMessage;
assert.equal(message.code, expectedMessage.code, 'IPythonTestMessage code');
assert.equal(failure.message, expectedFailure.message, 'IPythonTestMessage message');
assert.equal(message.severity, expectedMessage.severity, 'IPythonTestMessage severity');
assert.equal(message.provider, expectedMessage.provider, 'IPythonTestMessage provider');
assert.isNumber(message.testTime, 'IPythonTestMessage testTime');
assert.equal(message.status, expectedMessage.status, 'IPythonTestMessage status');
assert.equal(message.testFilePath, expectedMessage.testFilePath, 'IPythonTestMessage testFilePath');
if (status !== TestStatus.Pass) {
assert.equal(
failure.locationStack[0].lineText,
expectedFailure.locationStack[0].lineText,
'IPythonTestMessage line text',
);
assert.equal(
failure.locationStack[0].location.uri.fsPath,
expectedFailure.locationStack[0].location.uri.fsPath,
'IPythonTestMessage locationStack fsPath',
);
if (status !== TestStatus.Skipped) {
assert.equal(
failure.locationStack[1].lineText,
expectedFailure.locationStack[1].lineText,
'IPythonTestMessage line text',
);
assert.equal(
failure.locationStack[1].location.uri.fsPath,
expectedFailure.locationStack[1].location.uri.fsPath,
'IPythonTestMessage locationStack fsPath',
);
}
if (imported) {
assert.equal(
failure.locationStack[2].lineText,
expectedFailure.locationStack[2].lineText,
'IPythonTestMessage imported line text',
);
assert.equal(
failure.locationStack[2].location.uri.fsPath,
expectedFailure.locationStack[2].location.uri.fsPath,
'IPythonTestMessage imported location fsPath',
);
}
}
}
/**
* Generate a Diagnostic object (including DiagnosticRelatedInformation) using the provided test details that reflects
* what the Diagnostic for the associated test should be in order for it to be compared to by the actual Diagnostic
* for the test.
*
* @param testDetails Test details for a specific test.
*/
async function getExpectedLocationStackFromTestDetails(
testDetails: ITestDetails,
): Promise<ILocationStackFrameDetails[]> {
const locationStack: ILocationStackFrameDetails[] = [];
const testFilePath = path.join(UNITTEST_TEST_FILES_PATH, testDetails.fileName);
const testFileUri = vscode.Uri.file(testFilePath);
let expectedSourceTestFilePath = testFilePath;
if (testDetails.imported) {
expectedSourceTestFilePath = path.join(UNITTEST_TEST_FILES_PATH, testDetails.sourceFileName!);
}
const expectedSourceTestFileUri = vscode.Uri.file(expectedSourceTestFilePath);
if (testDetails.imported) {
// Stack should include the class furthest down the chain from the file that was executed.
locationStack.push({
location: new vscode.Location(testFileUri, testDetails.classDefRange!),
lineText: testDetails.simpleClassName!,
});
}
locationStack.push({
location: new vscode.Location(expectedSourceTestFileUri, testDetails.testDefRange!),
lineText: testDetails.sourceTestName,
});
if (testDetails.status !== TestStatus.Skipped) {
locationStack.push({
location: new vscode.Location(expectedSourceTestFileUri, testDetails.issueRange!),
lineText: testDetails.issueLineText!,
});
}
return locationStack;
}
suite('Unit Tests - PyTest - TestMessageService', () => {
let ioc: UnitTestIocContainer;
const filesystem = new FileSystem();
const configTarget = IS_MULTI_ROOT_TEST
? vscode.ConfigurationTarget.WorkspaceFolder
: vscode.ConfigurationTarget.Workspace;
suiteSetup(async () => {
await initialize();
await updateSetting('testing.pytestArgs', [], rootWorkspaceUri, configTarget);
});
async function initializeDI() {
ioc = new UnitTestIocContainer();
ioc.registerCommonTypes();
ioc.registerUnitTestTypes();
ioc.registerVariableTypes();
// Mocks.
ioc.registerMockProcessTypes();
ioc.serviceManager.addSingletonInstance<ICondaService>(ICondaService, instance(mock(CondaService)));
ioc.serviceManager.addSingletonInstance<IInterpreterService>(
IInterpreterService,
instance(mock(InterpreterService)),
);
}
// Build tests for the test data that is relevant for this platform.
filterdTestScenarios.forEach((scenario) => {
suite(scenario.scenarioName, async () => {
let testMessages: IPythonTestMessage[];
suiteSetup(async () => {
await initializeTest();
await initializeDI();
// Setup the service container for use by the parser.
const testVisitor = typeMoq.Mock.ofType<ITestVisitor>();
const outChannel = typeMoq.Mock.ofType<vscode.OutputChannel>();
const cancelToken = typeMoq.Mock.ofType<vscode.CancellationToken>();
cancelToken.setup((c) => c.isCancellationRequested).returns(() => false);
const options: TestDiscoveryOptions = {
args: [],
cwd: UNITTEST_TEST_FILES_PATH,
ignoreCache: true,
outChannel: outChannel.object,
token: cancelToken.object,
workspaceFolder: vscode.Uri.file(__dirname),
};
// Setup the parser.
const workspaceService = ioc.serviceContainer.get<IWorkspaceService>(IWorkspaceService);
const parser = new TestDiscoveredTestParser(workspaceService);
const discoveryOutput = fs
.readFileSync(path.join(PYTEST_RESULTS_PATH, scenario.discoveryOutput), 'utf8')
.replace(
/\/Users\/donjayamanne\/.vscode-insiders\/extensions\/pythonVSCode\/src\/test\/pythonFiles\/testFiles/g,
path.dirname(UNITTEST_TEST_FILES_PATH),
)
.replace(/\\/g, '/');
const discoveredTest: DiscoveredTests[] = JSON.parse(discoveryOutput);
options.workspaceFolder = vscode.Uri.file(discoveredTest[0].root);
const parsedTests: Tests = parser.parse(options.workspaceFolder, discoveredTest);
const xUnitParser = new XUnitParser(filesystem);
await xUnitParser.updateResultsFromXmlLogFile(
parsedTests,
path.join(PYTEST_RESULTS_PATH, scenario.runOutput),
);
const testResultsService = new TestResultsService(testVisitor.object);
testResultsService.updateResults(parsedTests);
const testMessageService = new TestMessageService(ioc.serviceContainer);
testMessages = await testMessageService.getFilteredTestMessages(UNITTEST_TEST_FILES_PATH, parsedTests);
});
suiteTeardown(async () => {
await ioc.dispose();
await updateSetting('testing.pytestArgs', [], rootWorkspaceUri, configTarget);
});
scenario.testDetails!.forEach((td) => {
suite(td.nameToRun, () => {
let testMessage: IPythonTestMessage;
let expectedMessage: IPythonTestMessage;
suiteSetup(async () => {
let expectedSeverity: PythonTestMessageSeverity;
if (td.status === TestStatus.Error || td.status === TestStatus.Fail) {
expectedSeverity = PythonTestMessageSeverity.Error;
} else if (td.status === TestStatus.Skipped) {
expectedSeverity = PythonTestMessageSeverity.Skip;
} else {
expectedSeverity = PythonTestMessageSeverity.Pass;
}
const expectedLocationStack = await getExpectedLocationStackFromTestDetails(td);
expectedMessage = {
code: td.nameToRun,
severity: expectedSeverity,
provider: ProductNames.get(Product.pytest)!,
testTime: 0,
status: td.status,
message: td.message,
locationStack: expectedLocationStack,
testFilePath: path.join(UNITTEST_TEST_FILES_PATH, td.fileName),
} as IPythonTestFailMessage;
testMessage = testMessages.find((tm) => tm.code === td.nameToRun)!;
});
test('Message', async () => {
await testMessageProperties(testMessage, expectedMessage, td.imported, td.status);
});
});
});
});
});
});