Skip to content

Commit 18d2943

Browse files
authored
Fix sys_platform patch in test suite leaking patching (#3589)
1 parent 8088ecb commit 18d2943

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

tests/config/loader/test_str_convert.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from __future__ import annotations
22

3+
import importlib
34
import sys
45
from pathlib import Path
56
from textwrap import dedent
7+
from types import ModuleType
68
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Set, TypeVar, Union
79

810
import pytest
@@ -164,7 +166,24 @@ def test_invalid_shell_expression(value: str, expected: list[str]) -> None:
164166

165167
@pytest.fixture(params=["win32", "linux2"])
166168
def sys_platform(request: SubRequest, monkeypatch: MonkeyPatch) -> str:
167-
monkeypatch.setattr(sys, "platform", request.param)
169+
class _SelectiveSys(ModuleType):
170+
"""A sys-like proxy that only overrides `platform`."""
171+
172+
def __init__(self, patched_platform: str) -> None:
173+
super().__init__("sys")
174+
self.__dict__["_real"] = sys
175+
self.__dict__["_patched_platform"] = patched_platform
176+
177+
def __getattr__(self, name: str) -> Any:
178+
if name == "platform":
179+
return self.__dict__["_patched_platform"]
180+
return getattr(self.__dict__["_real"], name)
181+
182+
# Patches sys.platform only for the tox.config.loader.str_convert module.
183+
# Everywhere else, sys.platform remains the real value.
184+
mod = importlib.import_module("tox.config.loader.str_convert")
185+
proxy = _SelectiveSys(str(request.param))
186+
monkeypatch.setattr(mod, "sys", proxy, raising=True)
168187
return str(request.param)
169188

170189

0 commit comments

Comments
 (0)