Repository: wntrblm/nox (python)

Issue:
test_run_env_systemroot seems to be failing on python 3.12.8
### Current Behavior

nox' test `test_run_env_systemroot` fails on python 3.12.8

### Expected Behavior

`test_run_env_systemroot` should not fail on >=python 3.12.8

### Steps To Reproduce

I am reporting this as a failure of a current rebuilding on NixOS that bumped python 3.12.7 -> 3.12.8.

The build logs from CI can be found here: https://hydra.nixos.org/build/281820678/nixlog/1

I tried to dig into the source code changes of 3.12.8 for `shutil.which` and i think the test is failing because of the following change:
https://github.com/python/cpython/commit/8899e85de100557899da05f0b37867a371a73800

The change intention on cpython is to fix behavior for which on windows systems.
The `test_run_env_systemroot` is running `shutil.which` with mocked `sys.platform = "win32"` so should be affected by the change.

In `Lib/shutil.py` the line 1558 change removes `cmd` from the windows lookup path thus the test is failing in our ci suite.





### Environment

```Markdown
- OS: NixOS 25.05 (nixos-unstable)
- Python: 3.12.8
- Nox: 2024.10.09
```


### Anything else?

In addition `os.pathsep` (line 1553) does return an unexpected  pathseperator when having the mock `sys.platform = "win32"` active. So i kind of have the feeling that the overall test setup is not correct when building in linux systems

I am actually not sure why `sys.platform` is mocked on the test at all, shouldnt it work the same on any platform?

Pull request description:
tests: fix broken mock on CPython 3.12.8+
Should fix #902 and our test suite, where 3.12 and 3.13 are failing.


Patch:
diff --git a/nox/command.py b/nox/command.py
index fe30307..dc5e57d 100644
--- a/nox/command.py
+++ b/nox/command.py
@@ -31,6 +31,8 @@ if TYPE_CHECKING:
 
 __all__ = ["CommandFailed", "ExternalType", "run", "which"]
 
+_PLATFORM = sys.platform
+
 
 def __dir__() -> list[str]:
     return __all__
@@ -72,7 +74,7 @@ def _clean_env(env: Mapping[str, str | None] | None = None) -> dict[str, str] |
     clean_env = {k: v for k, v in env.items() if v is not None}
 
     # Ensure systemroot is passed down, otherwise Windows will explode.
-    if sys.platform == "win32":
+    if _PLATFORM.startswith("win"):
         clean_env.setdefault("SYSTEMROOT", os.environ.get("SYSTEMROOT", ""))
 
     return clean_env


Test patch:
diff --git a/tests/test_command.py b/tests/test_command.py
index bfb8527..dd1af5f 100644
--- a/tests/test_command.py
+++ b/tests/test_command.py
@@ -162,7 +162,7 @@ def test_run_env_remove(monkeypatch: pytest.MonkeyPatch) -> None:
     )
 
 
-@mock.patch("sys.platform", "win32")
+@mock.patch("nox.command._PLATFORM", "win32")
 def test_run_env_systemroot() -> None:
     systemroot = os.environ.setdefault("SYSTEMROOT", "sigil")