From 0003f889e6b8dd0a8da21cb382f175dab17985ac Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Fri, 5 Jun 2026 07:52:26 -0700 Subject: [PATCH] Studio: stop ROCm worker test leaking a fake utils into sys.modules (#6027) test_direct_wheel_url_returns_none_without_cuda_major set sys.modules "utils"/"utils.hardware" (and structlog/loggers) to MagicMocks without cleanup. Once run.py started importing utils.cpu_threads (#5760), the leaked non-package utils made later tests in the same job fail with 'No module named utils.cpu_threads; utils is not a package', e.g. all of test_selection_logic.py's TestStudioLocalhostIpv6Warning. Use monkeypatch.setitem so the stubs are undone after the test. --- tests/studio/install/test_rocm_support.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/tests/studio/install/test_rocm_support.py b/tests/studio/install/test_rocm_support.py index 1d6e4f4c51..1c1a4df377 100644 --- a/tests/studio/install/test_rocm_support.py +++ b/tests/studio/install/test_rocm_support.py @@ -1123,7 +1123,7 @@ class TestWorkerRocmMambaSsm: source = _WHEEL_UTILS_PATH.read_text(encoding = "utf-8") assert "getattr(torch.version, 'hip', None)" in source - def test_direct_wheel_url_returns_none_without_cuda_major(self): + def test_direct_wheel_url_returns_none_without_cuda_major(self, monkeypatch): """_direct_wheel_url should return None when cuda_major is empty (ROCm).""" # Load module for function access _worker_spec = importlib.util.spec_from_file_location( @@ -1132,12 +1132,15 @@ class TestWorkerRocmMambaSsm: assert _worker_spec is not None and _worker_spec.loader is not None worker_mod = importlib.util.module_from_spec(_worker_spec) - # Mock all the imports worker.py needs - sys.modules["structlog"] = MagicMock() - sys.modules["loggers"] = MagicMock() - sys.modules["loggers"].get_logger = MagicMock(return_value = MagicMock()) - sys.modules["utils"] = MagicMock() - sys.modules["utils.hardware"] = MagicMock() + # Stub worker.py's imports via monkeypatch so the fakes (notably a + # non-package "utils") are undone and don't break later tests that + # import the real utils.* package. + loggers_mock = MagicMock() + loggers_mock.get_logger = MagicMock(return_value = MagicMock()) + monkeypatch.setitem(sys.modules, "structlog", MagicMock()) + monkeypatch.setitem(sys.modules, "loggers", loggers_mock) + monkeypatch.setitem(sys.modules, "utils", MagicMock()) + monkeypatch.setitem(sys.modules, "utils.hardware", MagicMock()) try: _worker_spec.loader.exec_module(worker_mod)