test: share one helper for isolated-subprocess test snippets - #2648
test: share one helper for isolated-subprocess test snippets#2648aryanputta wants to merge 3 commits into
Conversation
Five tests ran a self-contained snippet in a fresh interpreter with subprocess.run([sys.executable, "-c", code], ...), and they disagreed on which of the surrounding hazards they handled: a neutral working directory so the parent's cwd cannot shadow the installed package via sys.path, text capture, closed stdin, and a failure message that quotes both streams. Add cuda_python_test_helpers.subprocess_runner.run_python_snippet and route all five through it. cwd is a required argument because forgetting it is the trap the existing comments describe. Pure refactor: unset_env is opt-in per call site, so every caller keeps its current environment handling, and _run_probe keeps check=False because its callers assert on the exit code themselves. Signed-off-by: Aryan Putta <aryansputta@gmail.com>
Cut comments and docstring paragraphs that narrate this refactor or describe code in other files, matching the review guidance on NVIDIA#2196 and NVIDIA#2239: PR rationale belongs in the PR description, and prose that explains behavior elsewhere drifts. Also assert that cwd is non-empty rather than letting an empty string through to subprocess.run, and cover the guard. Signed-off-by: Aryan Putta <aryansputta@gmail.com>
|
Pushed
Diff is now 27 added against 50 removed on those files. No behavior change from the first commit. |
| @@ -0,0 +1,107 @@ | |||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | |||
There was a problem hiding this comment.
As you pointed out, this more correctly belongs in cuda_python_test_helpers, but we don't currently have tests there.
It feels like over-testing to me to have these tests -- we are already exercising the required behavior in the other tests, so I say just delete this file.
| def run_python_snippet( | ||
| code: str, | ||
| *, | ||
| cwd: str | os.PathLike[str], |
There was a problem hiding this comment.
I think we could make this optional. When not provided, use an auto-cleaned-up tempdir for cwd. That should simplify most (but not all) of the call sites.
Signed-off-by: Aryan <aryansputta@gmail.com>
|
@mdboom The requested changes are addressed in |
mdboom
left a comment
There was a problem hiding this comment.
LGTM. Just needs the merge conflicts resolved and another round of tests and I think this is good-to-go.
Description
closes #2412
Five tests run a self-contained snippet in a fresh interpreter with
subprocess.run([sys.executable, "-c", code], ...), each to keep a process-global side effect from reaching the rest of the suite: the CPython pending-call queue, the import-time rlcompleter patch, and a deliberate glibc abort. They had drifted apart on which of the surrounding hazards they handled.The sharpest one is the working directory.
python -cputs the parent's cwd at the head of the child'ssys.path, so a run started fromcuda_core/makes the child import thecuda/core/source tree instead of the installed wheel. Two sites work around it and carry a comment explaining it; the trap is rediscovered per site.cuda_python_test_helpers.subprocess_runner.run_python_snippetis now the single place these are handled:cwdis required rather than defaulted, so the shadowing hazard cannot be forgotten. Output is always captured as text so a failure message can quote both streams.stdinis always closed, becausePYTHONINSPECTkeeps the interpreter alive after-cand an inherited stdin leaves the implicit REPL waiting.All five call sites move to it:
cuda_core/tests/test_rlcompleter_patch.py(_run_probeand the opt-out probe)cuda_core/tests/graph/test_graph_definition_lifetime.py(two sites)cuda_bindings/tests/test_cuda.pyThis is a pure refactor.
unset_envis opt-in per call site, so the two graph tests and the bindings test keep inheritingPYTHONPATHexactly as they do today, and onlytest_rlcompleter_patch.pydrops it as it already did._run_probekeepscheck=Falsebecause both of its callers assert on the exit code themselves.unset_envis applied beforeextra_env, which preserves_run_probe's clear-then-set handling ofCUDA_CORE_DONT_FIX_TAB_COMPLETION; a test pins that ordering.Net across the five sites is 21 added against 59 removed.
The issue counted four sites. The fifth,
test_graph_definition_lifetime.py:790, arrived with #2406 on the same day the issue was filed.The description sketched a
run_in_subprocessfixture that re-runs the current test node withpytest <nodeid>. None of the five sites have that shape, and re-running the node would inherit pytest's rootdir, which is the exact cwd hazard two of them work around. Rationale is in #2412.Checklist