forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.ts
More file actions
370 lines (328 loc) · 14.4 KB
/
utils.ts
File metadata and controls
370 lines (328 loc) · 14.4 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as net from 'net';
import * as path from 'path';
import { CancellationToken, Position, TestController, TestItem, Uri, Range } from 'vscode';
import { traceError, traceLog, traceVerbose } from '../../../logging';
import { EnableTestAdapterRewrite } from '../../../common/experiments/groups';
import { IExperimentService } from '../../../common/types';
import { IServiceContainer } from '../../../ioc/types';
import { DebugTestTag, ErrorTestItemOptions, RunTestTag } from './testItemUtilities';
import {
DiscoveredTestItem,
DiscoveredTestNode,
DiscoveredTestPayload,
EOTTestPayload,
ExecutionTestPayload,
ITestResultResolver,
} from './types';
import { Deferred, createDeferred } from '../../../common/utils/async';
export function fixLogLines(content: string): string {
const lines = content.split(/\r?\n/g);
return `${lines.join('\r\n')}\r\n`;
}
export function fixLogLinesNoTrailing(content: string): string {
const lines = content.split(/\r?\n/g);
return `${lines.join('\r\n')}`;
}
export interface IJSONRPCData {
extractedJSON: string;
remainingRawData: string;
}
export interface ParsedRPCHeadersAndData {
headers: Map<string, string>;
remainingRawData: string;
}
export interface ExtractOutput {
uuid: string | undefined;
cleanedJsonData: string | undefined;
remainingRawData: string;
}
export const JSONRPC_UUID_HEADER = 'Request-uuid';
export const JSONRPC_CONTENT_LENGTH_HEADER = 'Content-Length';
export const JSONRPC_CONTENT_TYPE_HEADER = 'Content-Type';
export const MESSAGE_ON_TESTING_OUTPUT_MOVE =
'Starting now, all test run output will be sent to the Test Result panel,' +
' while test discovery output will be sent to the "Python" output channel instead of the "Python Test Log" channel.' +
' The "Python Test Log" channel will be deprecated within the next month.' +
' See https://github.com/microsoft/vscode-python/wiki/New-Method-for-Output-Handling-in-Python-Testing for details.';
export function createTestingDeferred(): Deferred<void> {
return createDeferred<void>();
}
export function extractJsonPayload(rawData: string, uuids: Array<string>): ExtractOutput {
/**
* Extracts JSON-RPC payload from the provided raw data.
* @param {string} rawData - The raw string data from which the JSON payload will be extracted.
* @param {Array<string>} uuids - The list of UUIDs that are active.
* @returns {string} The remaining raw data after the JSON payload is extracted.
*/
const rpcHeaders: ParsedRPCHeadersAndData = parseJsonRPCHeadersAndData(rawData);
// verify the RPC has a UUID and that it is recognized
let uuid = rpcHeaders.headers.get(JSONRPC_UUID_HEADER);
uuid = checkUuid(uuid, uuids);
const payloadLength = rpcHeaders.headers.get('Content-Length');
// separate out the data within context length of the given payload from the remaining data in the buffer
const rpcContent: IJSONRPCData = ExtractJsonRPCData(payloadLength, rpcHeaders.remainingRawData);
const cleanedJsonData = rpcContent.extractedJSON;
const { remainingRawData } = rpcContent;
// if the given payload has the complete json, process it otherwise wait for the rest in the buffer
if (cleanedJsonData.length === Number(payloadLength)) {
// call to process this data
// remove this data from the buffer
return { uuid, cleanedJsonData, remainingRawData };
}
// wait for the remaining
return { uuid: undefined, cleanedJsonData: undefined, remainingRawData: rawData };
}
export function checkUuid(uuid: string | undefined, uuids: Array<string>): string | undefined {
if (!uuid) {
// no UUID found, this could occurred if the payload is full yet so send back without erroring
return undefined;
}
if (!uuids.includes(uuid)) {
// no UUID found, this could occurred if the payload is full yet so send back without erroring
throw new Error('On data received: Error occurred because the payload UUID is not recognized');
}
return uuid;
}
export function parseJsonRPCHeadersAndData(rawData: string): ParsedRPCHeadersAndData {
/**
* Parses the provided raw data to extract JSON-RPC specific headers and remaining data.
*
* This function aims to extract specific JSON-RPC headers (like UUID, content length,
* and content type) from the provided raw string data. Headers are expected to be
* delimited by newlines and the format should be "key:value". The function stops parsing
* once it encounters an empty line, and the rest of the data after this line is treated
* as the remaining raw data.
*
* @param {string} rawData - The raw string containing headers and possibly other data.
* @returns {ParsedRPCHeadersAndData} An object containing the parsed headers as a map and the
* remaining raw data after the headers.
*/
const lines = rawData.split('\n');
let remainingRawData = '';
const headerMap = new Map<string, string>();
for (let i = 0; i < lines.length; i += 1) {
const line = lines[i];
if (line === '') {
remainingRawData = lines.slice(i + 1).join('\n');
break;
}
const [key, value] = line.split(':');
if (value && value.trim()) {
if ([JSONRPC_UUID_HEADER, JSONRPC_CONTENT_LENGTH_HEADER, JSONRPC_CONTENT_TYPE_HEADER].includes(key)) {
headerMap.set(key.trim(), value.trim());
}
}
}
return {
headers: headerMap,
remainingRawData,
};
}
export function ExtractJsonRPCData(payloadLength: string | undefined, rawData: string): IJSONRPCData {
/**
* Extracts JSON-RPC content based on provided headers and raw data.
*
* This function uses the `Content-Length` header from the provided headers map
* to determine how much of the rawData string represents the actual JSON content.
* After extracting the expected content, it also returns any remaining data
* that comes after the extracted content as remaining raw data.
*
* @param {string | undefined} payloadLength - The value of the `Content-Length` header.
* @param {string} rawData - The raw string data from which the JSON content will be extracted.
*
* @returns {IJSONRPCContent} An object containing the extracted JSON content and any remaining raw data.
*/
const length = parseInt(payloadLength ?? '0', 10);
const data = rawData.slice(0, length);
const remainingRawData = rawData.slice(length);
return {
extractedJSON: data,
remainingRawData,
};
}
export function pythonTestAdapterRewriteEnabled(serviceContainer: IServiceContainer): boolean {
const experiment = serviceContainer.get<IExperimentService>(IExperimentService);
return experiment.inExperimentSync(EnableTestAdapterRewrite.experiment);
}
export async function startTestIdServer(testIds: string[]): Promise<number> {
const startServer = (): Promise<number> =>
new Promise((resolve, reject) => {
const server = net.createServer((socket: net.Socket) => {
// Convert the test_ids array to JSON
const testData = JSON.stringify(testIds);
// Create the headers
const headers = [`Content-Length: ${Buffer.byteLength(testData)}`, 'Content-Type: application/json'];
// Create the payload by concatenating the headers and the test data
const payload = `${headers.join('\r\n')}\r\n\r\n${testData}`;
// Send the payload to the socket
socket.write(payload);
// Handle socket events
socket.on('data', (data) => {
traceLog('Received data:', data.toString());
});
socket.on('end', () => {
traceLog('Client disconnected');
});
});
server.listen(0, () => {
const { port } = server.address() as net.AddressInfo;
traceLog(`Server listening on port ${port}`);
resolve(port);
});
server.on('error', (error: Error) => {
reject(error);
});
});
// Start the server and wait until it is listening
let returnPort = 0;
try {
await startServer()
.then((assignedPort) => {
traceVerbose(`Server started for pytest test ids server and listening on port ${assignedPort}`);
returnPort = assignedPort;
})
.catch((error) => {
traceError('Error starting server for pytest test ids server:', error);
return 0;
})
.finally(() => returnPort);
return returnPort;
} catch {
traceError('Error starting server for pytest test ids server, cannot get port.');
return returnPort;
}
}
export function buildErrorNodeOptions(uri: Uri, message: string, testType: string): ErrorTestItemOptions {
const labelText = testType === 'pytest' ? 'pytest Discovery Error' : 'Unittest Discovery Error';
return {
id: `DiscoveryError:${uri.fsPath}`,
label: `${labelText} [${path.basename(uri.fsPath)}]`,
error: message,
};
}
export function populateTestTree(
testController: TestController,
testTreeData: DiscoveredTestNode,
testRoot: TestItem | undefined,
resultResolver: ITestResultResolver,
token?: CancellationToken,
): void {
// If testRoot is undefined, use the info of the root item of testTreeData to create a test item, and append it to the test controller.
if (!testRoot) {
testRoot = testController.createTestItem(testTreeData.path, testTreeData.name, Uri.file(testTreeData.path));
testRoot.canResolveChildren = true;
testRoot.tags = [RunTestTag, DebugTestTag];
testController.items.add(testRoot);
}
// Recursively populate the tree with test data.
testTreeData.children.forEach((child) => {
if (!token?.isCancellationRequested) {
if (isTestItem(child)) {
const testItem = testController.createTestItem(child.id_, child.name, Uri.file(child.path));
testItem.tags = [RunTestTag, DebugTestTag];
const range = new Range(
new Position(Number(child.lineno) - 1, 0),
new Position(Number(child.lineno), 0),
);
testItem.canResolveChildren = false;
testItem.range = range;
testItem.tags = [RunTestTag, DebugTestTag];
testRoot!.children.add(testItem);
// add to our map
resultResolver.runIdToTestItem.set(child.runID, testItem);
resultResolver.runIdToVSid.set(child.runID, child.id_);
resultResolver.vsIdToRunId.set(child.id_, child.runID);
} else {
// testRoot.children.get(child.path);
let node = testRoot!.children.get(child.path);
if (node !== undefined) {
console.log('WE MADE IT');
}
if (child.path.includes('.py')) {
console.log('IS A FILE DELETE CHIDLREN');
// delete the given file node from the children of its parent
testRoot!.children.delete(child.path);
// mark the node as undefined so a new node must be created below
node = undefined;
}
if (!node) {
node = testController.createTestItem(child.id_, child.name, Uri.file(child.path));
node.canResolveChildren = true;
node.tags = [RunTestTag, DebugTestTag];
testRoot!.children.add(node);
}
populateTestTree(testController, child, node, resultResolver, token);
}
}
});
}
function isTestItem(test: DiscoveredTestNode | DiscoveredTestItem): test is DiscoveredTestItem {
return test.type_ === 'test';
}
export function createExecutionErrorPayload(
code: number | null,
signal: NodeJS.Signals | null,
testIds: string[],
cwd: string,
): ExecutionTestPayload {
const etp: ExecutionTestPayload = {
cwd,
status: 'error',
error: `Test run failed, the python test process was terminated before it could exit on its own for workspace ${cwd}`,
result: {},
};
// add error result for each attempted test.
for (let i = 0; i < testIds.length; i = i + 1) {
const test = testIds[i];
etp.result![test] = {
test,
outcome: 'error',
message: ` \n The python test process was terminated before it could exit on its own, the process errored with: Code: ${code}, Signal: ${signal}`,
};
}
return etp;
}
export function createDiscoveryErrorPayload(
code: number | null,
signal: NodeJS.Signals | null,
cwd: string,
): DiscoveredTestPayload {
return {
cwd,
status: 'error',
error: [
` \n The python test process was terminated before it could exit on its own, the process errored with: Code: ${code}, Signal: ${signal} for workspace ${cwd}`,
],
};
}
export function createEmptyFileDiscoveryPayload(cwd: string, fileUri: Uri): DiscoveredTestPayload {
return {
cwd,
status: 'error-empty',
error: [fileUri.fsPath],
};
}
export function createEOTPayload(executionBool: boolean): EOTTestPayload {
return {
commandType: executionBool ? 'execution' : 'discovery',
eot: true,
} as EOTTestPayload;
}
/**
* Splits a test name into its parent test name and subtest unique section.
*
* @param testName The full test name string.
* @returns A tuple where the first item is the parent test name and the second item is the subtest section or `testName` if no subtest section exists.
*/
export function splitTestNameWithRegex(testName: string): [string, string] {
// If a match is found, return the parent test name and the subtest (whichever was captured between parenthesis or square brackets).
// Otherwise, return the entire testName for the parent and entire testName for the subtest.
const regex = /^(.*?) ([\[(].*[\])])$/;
const match = testName.match(regex);
if (match) {
return [match[1].trim(), match[2] || match[3] || testName];
}
return [testName, testName];
}