|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
| 3 | +import importlib |
3 | 4 | import sys
|
4 | 5 | from pathlib import Path
|
5 | 6 | from textwrap import dedent
|
| 7 | +from types import ModuleType |
6 | 8 | from typing import TYPE_CHECKING, Any, Dict, List, Optional, Set, TypeVar, Union
|
7 | 9 |
|
8 | 10 | import pytest
|
@@ -164,7 +166,24 @@ def test_invalid_shell_expression(value: str, expected: list[str]) -> None:
|
164 | 166 |
|
165 | 167 | @pytest.fixture(params=["win32", "linux2"])
|
166 | 168 | 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) |
168 | 187 | return str(request.param)
|
169 | 188 |
|
170 | 189 |
|
|
0 commit comments