forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Refactor pytest and unittest test discovery #25599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eleanorjboyd
wants to merge
9
commits into
microsoft:main
Choose a base branch
from
eleanorjboyd:familiar-moth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+524
−336
Open
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e2f4691
Refactor pytest test discovery
eleanorjboyd 48a62d2
add unittest too
eleanorjboyd 3d7d0c6
more refactoring
eleanorjboyd db97ada
fix test
eleanorjboyd 0977cf3
Update src/client/testing/testController/unittest/testDiscoveryAdapte…
eleanorjboyd 07bce78
Update src/client/testing/testController/pytest/pytestHelpers.ts
eleanorjboyd b313fc7
fixes based on comments
eleanorjboyd 4d4f57d
track all disposables
eleanorjboyd a5fe81c
Merge branch 'main' into familiar-moth
eleanorjboyd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
src/client/testing/testController/common/discoveryHelpers.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
| import { CancellationTokenSource, Uri } from 'vscode'; | ||
| import { Deferred } from '../../../common/utils/async'; | ||
| import { traceError, traceInfo, traceVerbose } from '../../../logging'; | ||
| import { createDiscoveryErrorPayload, fixLogLinesNoTrailing } from './utils'; | ||
| import { ITestResultResolver } from './types'; | ||
|
|
||
| /** | ||
| * Test provider type for logging purposes. | ||
| */ | ||
| export type TestProvider = 'pytest' | 'unittest'; | ||
|
|
||
| /** | ||
| * Creates standard process event handlers for test discovery subprocess. | ||
| * Handles stdout/stderr logging and error reporting on process exit. | ||
| * | ||
| * @param testProvider - The test framework being used ('pytest' or 'unittest') | ||
| * @param uri - The workspace URI | ||
| * @param cwd - The current working directory | ||
| * @param resultResolver - Resolver for test discovery results | ||
| * @param deferredTillExecClose - Deferred to resolve when process closes | ||
| * @param allowedSuccessCodes - Additional exit codes to treat as success (e.g., pytest exit code 5 for no tests found) | ||
| */ | ||
| export function createProcessHandlers( | ||
| testProvider: TestProvider, | ||
| uri: Uri, | ||
| cwd: string, | ||
| resultResolver: ITestResultResolver | undefined, | ||
| deferredTillExecClose: Deferred<void>, | ||
| allowedSuccessCodes: number[] = [], | ||
| ): { | ||
| onStdout: (data: any) => void; | ||
| onStderr: (data: any) => void; | ||
| onExit: (code: number | null, signal: NodeJS.Signals | null) => void; | ||
| onClose: (code: number | null, signal: NodeJS.Signals | null) => void; | ||
| } { | ||
| const isSuccessCode = (code: number | null): boolean => { | ||
| return code === 0 || (code !== null && allowedSuccessCodes.includes(code)); | ||
| }; | ||
|
|
||
| return { | ||
| onStdout: (data: any) => { | ||
| const out = fixLogLinesNoTrailing(data.toString()); | ||
| traceInfo(out); | ||
| }, | ||
| onStderr: (data: any) => { | ||
| const out = fixLogLinesNoTrailing(data.toString()); | ||
| traceError(out); | ||
| }, | ||
| onExit: (code: number | null, signal: NodeJS.Signals | null) => { | ||
| // The 'exit' event fires when the process terminates, but streams may still be open. | ||
| if (!isSuccessCode(code)) { | ||
| const exitCodeNote = | ||
| allowedSuccessCodes.length > 0 | ||
| ? ` Note: Exit codes ${allowedSuccessCodes.join(', ')} are also treated as success.` | ||
| : ''; | ||
| traceError( | ||
| `${testProvider} discovery subprocess exited with code ${code} and signal ${signal} for workspace ${uri.fsPath}.${exitCodeNote}`, | ||
| ); | ||
| } else if (code === 0) { | ||
| traceVerbose(`${testProvider} discovery subprocess exited successfully for workspace ${uri.fsPath}`); | ||
| } | ||
| }, | ||
| onClose: (code: number | null, signal: NodeJS.Signals | null) => { | ||
| // We resolve the deferred here to ensure all output has been captured. | ||
| if (!isSuccessCode(code)) { | ||
| traceError( | ||
| `${testProvider} discovery failed with exit code ${code} and signal ${signal} for workspace ${uri.fsPath}. Creating error payload.`, | ||
| ); | ||
| resultResolver?.resolveDiscovery(createDiscoveryErrorPayload(code, signal, cwd)); | ||
| } else { | ||
| traceVerbose(`${testProvider} discovery subprocess streams closed for workspace ${uri.fsPath}`); | ||
| } | ||
| deferredTillExecClose?.resolve(); | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Handles cleanup when test discovery is cancelled. | ||
| * Kills the subprocess (if running), resolves the completion deferred, and cancels the discovery pipe. | ||
| * | ||
| * @param testProvider - The test framework being used ('pytest' or 'unittest') | ||
| * @param proc - The process to kill | ||
| * @param processCompletion - Deferred to resolve | ||
| * @param pipeCancellation - Cancellation token source to cancel | ||
| * @param uri - The workspace URI | ||
| */ | ||
| export function cleanupOnCancellation( | ||
| testProvider: TestProvider, | ||
| proc: { kill: () => void } | undefined, | ||
| processCompletion: Deferred<void>, | ||
| pipeCancellation: CancellationTokenSource, | ||
| uri: Uri, | ||
| ): void { | ||
| traceInfo(`Test discovery cancelled, killing ${testProvider} subprocess for workspace ${uri.fsPath}`); | ||
| if (proc) { | ||
| traceVerbose(`Killing ${testProvider} subprocess for workspace ${uri.fsPath}`); | ||
| proc.kill(); | ||
| } else { | ||
| traceVerbose(`No ${testProvider} subprocess to kill for workspace ${uri.fsPath} (proc is undefined)`); | ||
| } | ||
| traceVerbose(`Resolving process completion deferred for ${testProvider} discovery in workspace ${uri.fsPath}`); | ||
| processCompletion.resolve(); | ||
| traceVerbose(`Cancelling discovery pipe for ${testProvider} discovery in workspace ${uri.fsPath}`); | ||
| pipeCancellation.cancel(); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.