Skip to content

Commit 53d3787

Browse files
committed
fix: use sys.platform instead of platform.system
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
1 parent 7f411a6 commit 53d3787

File tree

4 files changed

+31
-32
lines changed

4 files changed

+31
-32
lines changed

nox/virtualenv.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@
1919
import functools
2020
import json
2121
import os
22-
import platform
2322
import re
2423
import shutil
2524
import subprocess
2625
import sys
27-
import sysconfig
2826
from pathlib import Path
2927
from socket import gethostbyname
3028
from typing import TYPE_CHECKING, Any, ClassVar
@@ -65,6 +63,10 @@ def __dir__() -> list[str]:
6563
return __all__
6664

6765

66+
# Use for test mocking and to make mypy happy
67+
_PLATFORM = sys.platform
68+
69+
6870
# Problematic environment variables that are stripped from all commands inside
6971
# of a virtualenv. See https://github.com/theacodes/nox/issues/44
7072
_BLACKLISTED_ENV_VARS = frozenset(
@@ -75,8 +77,6 @@ def __dir__() -> list[str]:
7577
"UV_SYSTEM_PYTHON",
7678
]
7779
)
78-
_SYSTEM = platform.system()
79-
_IS_MINGW = sysconfig.get_platform().startswith("mingw")
8080

8181

8282
def find_uv() -> tuple[bool, str]:
@@ -361,7 +361,7 @@ def _clean_location(self) -> bool:
361361
def bin_paths(self) -> list[str]:
362362
"""Returns the location of the conda env's bin folder."""
363363
# see https://github.com/conda/conda/blob/f60f0f1643af04ed9a51da3dd4fa242de81e32f4/conda/activate.py#L563-L572
364-
if _SYSTEM == "Windows":
364+
if _PLATFORM.startswith("win"):
365365
return [
366366
self.location,
367367
os.path.join(self.location, "Library", "mingw-w64", "bin"),
@@ -603,7 +603,7 @@ def _resolved_interpreter(self) -> str:
603603

604604
# The rest of this is only applicable to Windows, so if we don't have
605605
# an interpreter by now, raise.
606-
if _SYSTEM != "Windows":
606+
if not _PLATFORM.startswith("win"):
607607
self._resolved = InterpreterNotFound(self.interpreter)
608608
raise self._resolved
609609

@@ -632,7 +632,7 @@ def _resolved_interpreter(self) -> str:
632632
@property
633633
def bin_paths(self) -> list[str]:
634634
"""Returns the location of the virtualenv's bin folder."""
635-
if _SYSTEM == "Windows" and not _IS_MINGW:
635+
if _PLATFORM.startswith("win"):
636636
return [os.path.join(self.location, "Scripts")]
637637
return [os.path.join(self.location, "bin")]
638638

noxfile.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@
1818
import contextlib
1919
import functools
2020
import os
21-
import platform
2221
import shutil
2322
import sqlite3
2423
import sys
2524

2625
import nox
2726

28-
ON_WINDOWS_CI = "CI" in os.environ and platform.system() == "Windows"
27+
ON_WINDOWS_CI = "CI" in os.environ and sys.platform.startswith("win32")
2928

3029
nox.needs_version = ">=2024.4.15"
3130
nox.options.default_venv_backend = "uv|virtualenv"

tests/test_command.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@
3636
PYTHON = sys.executable
3737

3838
skip_on_windows_primary_console_session = pytest.mark.skipif(
39-
platform.system() == "Windows" and "SECONDARY_CONSOLE_SESSION" not in os.environ,
39+
sys.platform.startswith("win") and "SECONDARY_CONSOLE_SESSION" not in os.environ,
4040
reason="On Windows, this test must run in a separate console session.",
4141
)
4242

4343
only_on_windows = pytest.mark.skipif(
44-
platform.system() != "Windows", reason="Only run this test on Windows."
44+
not sys.platform.startswith("win"), reason="Only run this test on Windows."
4545
)
4646

4747

@@ -127,7 +127,7 @@ def test_run_verbosity_failed_command(
127127

128128

129129
@pytest.mark.skipif(
130-
platform.system() == "Windows",
130+
sys.platform.startswith("win"),
131131
reason="See https://github.com/python/cpython/issues/85815",
132132
)
133133
def test_run_non_str() -> None:
@@ -285,7 +285,7 @@ def enable_ctrl_c(*, enabled: bool) -> None:
285285

286286
def interrupt_process(proc: subprocess.Popen[Any]) -> None:
287287
"""Send SIGINT or CTRL_C_EVENT to the process."""
288-
if platform.system() == "Windows":
288+
if sys.platform.startswith("win"):
289289
# Disable Ctrl-C so we don't terminate ourselves.
290290
enable_ctrl_c(enabled=False)
291291

@@ -301,7 +301,7 @@ def command_with_keyboard_interrupt(
301301
monkeypatch: pytest.MonkeyPatch, marker: Any
302302
) -> None:
303303
"""Monkeypatch Popen.communicate to raise KeyboardInterrupt."""
304-
if platform.system() == "Windows":
304+
if sys.platform.startswith("win"):
305305
# Enable Ctrl-C because the child inherits the setting from us.
306306
enable_ctrl_c(enabled=True)
307307

tests/test_virtualenv.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
from nox.virtualenv import CondaEnv, ProcessEnv, VirtualEnv
4040

41-
IS_WINDOWS = nox.virtualenv._SYSTEM == "Windows"
41+
IS_WINDOWS = sys.platform.startswith("win")
4242
HAS_CONDA = shutil.which("conda") is not None
4343
HAS_UV = shutil.which("uv") is not None
4444
RAISE_ERROR = "RAISE_ERROR"
@@ -249,7 +249,7 @@ def test_conda_env_create_verbose(
249249
assert kwargs["log"]
250250

251251

252-
@mock.patch("nox.virtualenv._SYSTEM", new="Windows")
252+
@mock.patch("nox.virtualenv._PLATFORM", new="win32")
253253
def test_condaenv_bin_windows(make_conda: Callable[..., tuple[CondaEnv, Path]]) -> None:
254254
venv, dir_ = make_conda()
255255
assert [
@@ -406,7 +406,7 @@ def test_bin_paths(
406406
assert str(dir_.joinpath("Scripts" if IS_WINDOWS else "bin")) == venv.bin
407407

408408

409-
@mock.patch("nox.virtualenv._SYSTEM", new="Windows")
409+
@mock.patch("nox.virtualenv._PLATFORM", new="win32")
410410
def test_bin_windows(
411411
make_one: Callable[..., tuple[VirtualEnv | ProcessEnv, Path]],
412412
) -> None:
@@ -899,7 +899,7 @@ def test__resolved_interpreter_none(
899899
("2.7.15", "python2.7"),
900900
],
901901
)
902-
@mock.patch("nox.virtualenv._SYSTEM", new="Linux")
902+
@mock.patch("nox.virtualenv._PLATFORM", new="linux")
903903
@mock.patch.object(shutil, "which", return_value=True)
904904
def test__resolved_interpreter_numerical_non_windows(
905905
which: mock.Mock,
@@ -914,7 +914,7 @@ def test__resolved_interpreter_numerical_non_windows(
914914

915915

916916
@pytest.mark.parametrize("input_", ["2.", "2.7."])
917-
@mock.patch("nox.virtualenv._SYSTEM", new="Linux")
917+
@mock.patch("nox.virtualenv._PLATFORM", new="linux")
918918
@mock.patch.object(shutil, "which", return_value=False)
919919
def test__resolved_interpreter_invalid_numerical_id(
920920
which: mock.Mock,
@@ -929,7 +929,7 @@ def test__resolved_interpreter_invalid_numerical_id(
929929
which.assert_called_once_with(input_)
930930

931931

932-
@mock.patch("nox.virtualenv._SYSTEM", new="Linux")
932+
@mock.patch("nox.virtualenv._PLATFORM", new="linux")
933933
@mock.patch.object(shutil, "which", return_value=False)
934934
def test__resolved_interpreter_32_bit_non_windows(
935935
which: mock.Mock, make_one: Callable[..., tuple[VirtualEnv, Path]]
@@ -941,7 +941,7 @@ def test__resolved_interpreter_32_bit_non_windows(
941941
which.assert_called_once_with("3.6-32")
942942

943943

944-
@mock.patch("nox.virtualenv._SYSTEM", new="Linux")
944+
@mock.patch("nox.virtualenv._PLATFORM", new="linux")
945945
@mock.patch.object(shutil, "which", return_value=True)
946946
def test__resolved_interpreter_non_windows(
947947
which: mock.Mock, make_one: Callable[..., tuple[VirtualEnv, Path]]
@@ -954,7 +954,7 @@ def test__resolved_interpreter_non_windows(
954954
which.assert_called_once_with("python3.6")
955955

956956

957-
@mock.patch("nox.virtualenv._SYSTEM", new="Windows")
957+
@mock.patch("nox.virtualenv._PLATFORM", new="win32")
958958
@mock.patch.object(shutil, "which")
959959
def test__resolved_interpreter_windows_full_path(
960960
which: mock.Mock, make_one: Callable[..., tuple[VirtualEnv, Path]]
@@ -976,7 +976,7 @@ def test__resolved_interpreter_windows_full_path(
976976
("2.7-32", r"c:\python27\python.exe"),
977977
],
978978
)
979-
@mock.patch("nox.virtualenv._SYSTEM", new="Windows")
979+
@mock.patch("nox.virtualenv._PLATFORM", new="win32")
980980
@mock.patch.object(subprocess, "run")
981981
@mock.patch.object(shutil, "which")
982982
def test__resolved_interpreter_windows_pyexe(
@@ -1011,7 +1011,7 @@ def special_run(cmd: str, *args: str, **kwargs: object) -> TextProcessResult:
10111011
which.assert_has_calls([mock.call(input_), mock.call("py")])
10121012

10131013

1014-
@mock.patch("nox.virtualenv._SYSTEM", new="Windows")
1014+
@mock.patch("nox.virtualenv._PLATFORM", new="win32")
10151015
@mock.patch.object(subprocess, "run")
10161016
@mock.patch.object(shutil, "which")
10171017
def test__resolved_interpreter_windows_pyexe_fails(
@@ -1020,8 +1020,8 @@ def test__resolved_interpreter_windows_pyexe_fails(
10201020
# Establish that if the py launcher fails, we give the right error.
10211021
venv, _ = make_one(interpreter="python3.6")
10221022

1023-
# Trick the nox.virtualenv._SYSTEM into thinking that it cannot find python3.6
1024-
# (it likely will on Unix). Also, when the nox.virtualenv._SYSTEM looks for the
1023+
# Trick the nox.virtualenv into thinking that it cannot find python3.6
1024+
# (it likely will on Unix). Also, when the nox.virtualenv looks for the
10251025
# py launcher, give it a dummy that fails.
10261026
def special_run(cmd: str, *args: str, **kwargs: object) -> TextProcessResult: # noqa: ARG001
10271027
return TextProcessResult("", 1)
@@ -1036,7 +1036,7 @@ def special_run(cmd: str, *args: str, **kwargs: object) -> TextProcessResult: #
10361036
which.assert_has_calls([mock.call("python3.6"), mock.call("py")])
10371037

10381038

1039-
@mock.patch("nox.virtualenv._SYSTEM", new="Windows")
1039+
@mock.patch("nox.virtualenv._PLATFORM", new="win32")
10401040
@mock.patch("nox.virtualenv.UV_PYTHON_SUPPORT", new=False)
10411041
def test__resolved_interpreter_windows_path_and_version(
10421042
make_one: Callable[..., tuple[VirtualEnv, Path]],
@@ -1065,7 +1065,7 @@ def test__resolved_interpreter_windows_path_and_version(
10651065
@pytest.mark.parametrize("input_", ["2.7", "python3.7", "goofy"])
10661066
@pytest.mark.parametrize("sysfind_result", [r"c:\python37-x64\python.exe", None])
10671067
@pytest.mark.parametrize("sysexec_result", ["3.7.3\\n", RAISE_ERROR])
1068-
@mock.patch("nox.virtualenv._SYSTEM", new="Windows")
1068+
@mock.patch("nox.virtualenv._PLATFORM", new="win32")
10691069
@mock.patch("nox.virtualenv.UV_PYTHON_SUPPORT", new=False)
10701070
def test__resolved_interpreter_windows_path_and_version_fails(
10711071
input_: str,
@@ -1089,7 +1089,7 @@ def test__resolved_interpreter_windows_path_and_version_fails(
10891089
print(venv._resolved_interpreter)
10901090

10911091

1092-
@mock.patch("nox.virtualenv._SYSTEM", new="Windows")
1092+
@mock.patch("nox.virtualenv._PLATFORM", new="win32")
10931093
@mock.patch.object(shutil, "which")
10941094
def test__resolved_interpreter_not_found(
10951095
which: mock.Mock, make_one: Callable[..., tuple[VirtualEnv, Path]]
@@ -1106,7 +1106,7 @@ def test__resolved_interpreter_not_found(
11061106
print(venv._resolved_interpreter)
11071107

11081108

1109-
@mock.patch("nox.virtualenv._SYSTEM", new="Windows")
1109+
@mock.patch("nox.virtualenv._PLATFORM", new="win32")
11101110
@mock.patch("nox.virtualenv.locate_via_py", new=lambda _: None) # type: ignore[misc] # noqa: PT008
11111111
def test__resolved_interpreter_nonstandard(
11121112
make_one: Callable[..., tuple[VirtualEnv, Path]],
@@ -1119,7 +1119,7 @@ def test__resolved_interpreter_nonstandard(
11191119
print(venv._resolved_interpreter)
11201120

11211121

1122-
@mock.patch("nox.virtualenv._SYSTEM", new="Linux")
1122+
@mock.patch("nox.virtualenv._PLATFORM", new="linux")
11231123
@mock.patch.object(shutil, "which", return_value=True)
11241124
def test__resolved_interpreter_cache_result(
11251125
which: mock.Mock, make_one: Callable[..., tuple[VirtualEnv, Path]]
@@ -1135,7 +1135,7 @@ def test__resolved_interpreter_cache_result(
11351135
assert which.call_count == 1
11361136

11371137

1138-
@mock.patch("nox.virtualenv._SYSTEM", new="Linux")
1138+
@mock.patch("nox.virtualenv._PLATFORM", new="linux")
11391139
@mock.patch.object(shutil, "which", return_value=None)
11401140
def test__resolved_interpreter_cache_failure(
11411141
which: mock.Mock, make_one: Callable[..., tuple[VirtualEnv, Path]]

0 commit comments

Comments
 (0)