|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | +import { CancellationTokenSource, Uri } from 'vscode'; |
| 4 | +import { Deferred } from '../../../common/utils/async'; |
| 5 | +import { traceError, traceInfo, traceVerbose } from '../../../logging'; |
| 6 | +import { createDiscoveryErrorPayload, fixLogLinesNoTrailing } from './utils'; |
| 7 | +import { ITestResultResolver } from './types'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Test provider type for logging purposes. |
| 11 | + */ |
| 12 | +export type TestProvider = 'pytest' | 'unittest'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Creates standard process event handlers for test discovery subprocess. |
| 16 | + * Handles stdout/stderr logging and error reporting on process exit. |
| 17 | + * |
| 18 | + * @param testProvider - The test framework being used ('pytest' or 'unittest') |
| 19 | + * @param uri - The workspace URI |
| 20 | + * @param cwd - The current working directory |
| 21 | + * @param resultResolver - Resolver for test discovery results |
| 22 | + * @param deferredTillExecClose - Deferred to resolve when process closes |
| 23 | + * @param allowedSuccessCodes - Additional exit codes to treat as success (e.g., pytest exit code 5 for no tests found) |
| 24 | + */ |
| 25 | +export function createProcessHandlers( |
| 26 | + testProvider: TestProvider, |
| 27 | + uri: Uri, |
| 28 | + cwd: string, |
| 29 | + resultResolver: ITestResultResolver | undefined, |
| 30 | + deferredTillExecClose: Deferred<void>, |
| 31 | + allowedSuccessCodes: number[] = [], |
| 32 | +): { |
| 33 | + onStdout: (data: any) => void; |
| 34 | + onStderr: (data: any) => void; |
| 35 | + onExit: (code: number | null, signal: NodeJS.Signals | null) => void; |
| 36 | + onClose: (code: number | null, signal: NodeJS.Signals | null) => void; |
| 37 | +} { |
| 38 | + const isSuccessCode = (code: number | null): boolean => { |
| 39 | + return code === 0 || (code !== null && allowedSuccessCodes.includes(code)); |
| 40 | + }; |
| 41 | + |
| 42 | + return { |
| 43 | + onStdout: (data: any) => { |
| 44 | + const out = fixLogLinesNoTrailing(data.toString()); |
| 45 | + traceInfo(out); |
| 46 | + }, |
| 47 | + onStderr: (data: any) => { |
| 48 | + const out = fixLogLinesNoTrailing(data.toString()); |
| 49 | + traceError(out); |
| 50 | + }, |
| 51 | + onExit: (code: number | null, signal: NodeJS.Signals | null) => { |
| 52 | + // The 'exit' event fires when the process terminates, but streams may still be open. |
| 53 | + if (!isSuccessCode(code)) { |
| 54 | + const exitCodeNote = |
| 55 | + allowedSuccessCodes.length > 0 |
| 56 | + ? ` Note: Exit codes ${allowedSuccessCodes.join(', ')} are also treated as success.` |
| 57 | + : ''; |
| 58 | + traceError( |
| 59 | + `${testProvider} discovery subprocess exited with code ${code} and signal ${signal} for workspace ${uri.fsPath}.${exitCodeNote}`, |
| 60 | + ); |
| 61 | + } else if (code === 0) { |
| 62 | + traceVerbose(`${testProvider} discovery subprocess exited successfully for workspace ${uri.fsPath}`); |
| 63 | + } |
| 64 | + }, |
| 65 | + onClose: (code: number | null, signal: NodeJS.Signals | null) => { |
| 66 | + // We resolve the deferred here to ensure all output has been captured. |
| 67 | + if (!isSuccessCode(code)) { |
| 68 | + traceError( |
| 69 | + `${testProvider} discovery failed with exit code ${code} and signal ${signal} for workspace ${uri.fsPath}. Creating error payload.`, |
| 70 | + ); |
| 71 | + resultResolver?.resolveDiscovery(createDiscoveryErrorPayload(code, signal, cwd)); |
| 72 | + } else { |
| 73 | + traceVerbose(`${testProvider} discovery subprocess streams closed for workspace ${uri.fsPath}`); |
| 74 | + } |
| 75 | + deferredTillExecClose?.resolve(); |
| 76 | + }, |
| 77 | + }; |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Handles cleanup when test discovery is cancelled. |
| 82 | + * Kills the subprocess (if running), resolves the completion deferred, and cancels the discovery pipe. |
| 83 | + * |
| 84 | + * @param testProvider - The test framework being used ('pytest' or 'unittest') |
| 85 | + * @param proc - The process to kill |
| 86 | + * @param processCompletion - Deferred to resolve |
| 87 | + * @param pipeCancellation - Cancellation token source to cancel |
| 88 | + * @param uri - The workspace URI |
| 89 | + */ |
| 90 | +export function cleanupOnCancellation( |
| 91 | + testProvider: TestProvider, |
| 92 | + proc: { kill: () => void } | undefined, |
| 93 | + processCompletion: Deferred<void>, |
| 94 | + pipeCancellation: CancellationTokenSource, |
| 95 | + uri: Uri, |
| 96 | +): void { |
| 97 | + traceInfo(`Test discovery cancelled, killing ${testProvider} subprocess for workspace ${uri.fsPath}`); |
| 98 | + if (proc) { |
| 99 | + proc.kill(); |
| 100 | + } |
| 101 | + processCompletion.resolve(); |
| 102 | + pipeCancellation.cancel(); |
| 103 | +} |
0 commit comments