|
| 1 | +import contextlib |
| 2 | +import sys |
| 3 | +from pathlib import Path |
| 4 | +from typing import Dict |
| 5 | + |
| 6 | +import pytest |
| 7 | +import setuptools._distutils.util |
| 8 | + |
| 9 | +from cibuildwheel.errors import FatalError |
| 10 | +from cibuildwheel.util import CIProvider, detect_ci_provider |
| 11 | +from cibuildwheel.windows import PythonConfiguration, setup_setuptools_cross_compile |
| 12 | + |
| 13 | +# monkeypatching os.name is too flaky. E.g. It works on my machine, but fails in pipeline |
| 14 | +if not sys.platform.startswith("win"): |
| 15 | + pytest.skip("Windows-only tests", allow_module_level=True) |
| 16 | + |
| 17 | + |
| 18 | +@contextlib.contextmanager |
| 19 | +def patched_environment(monkeypatch: pytest.MonkeyPatch, environment: Dict[str, str]): |
| 20 | + with monkeypatch.context() as mp: |
| 21 | + for envvar, val in environment.items(): |
| 22 | + mp.setenv(name=envvar, value=val) |
| 23 | + yield |
| 24 | + |
| 25 | + |
| 26 | +def test_x86(tmp_path: Path, monkeypatch: pytest.MonkeyPatch): |
| 27 | + arch = "32" |
| 28 | + environment: Dict[str, str] = {} |
| 29 | + |
| 30 | + configuration = PythonConfiguration("irrelevant", arch, "irrelevant", None) |
| 31 | + |
| 32 | + setup_setuptools_cross_compile(tmp_path, configuration, tmp_path, environment) |
| 33 | + with patched_environment(monkeypatch, environment): |
| 34 | + target_platform = setuptools._distutils.util.get_platform() |
| 35 | + |
| 36 | + assert environment["VSCMD_ARG_TGT_ARCH"] == "x86" |
| 37 | + assert target_platform == "win32" |
| 38 | + |
| 39 | + |
| 40 | +def test_x64(tmp_path: Path, monkeypatch: pytest.MonkeyPatch): |
| 41 | + arch = "64" |
| 42 | + environment: Dict[str, str] = {} |
| 43 | + |
| 44 | + configuration = PythonConfiguration("irrelevant", arch, "irrelevant", None) |
| 45 | + |
| 46 | + setup_setuptools_cross_compile(tmp_path, configuration, tmp_path, environment) |
| 47 | + with patched_environment(monkeypatch, environment): |
| 48 | + target_platform = setuptools._distutils.util.get_platform() |
| 49 | + |
| 50 | + assert environment["VSCMD_ARG_TGT_ARCH"] == "x64" |
| 51 | + assert target_platform == "win-amd64" |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.skipif( |
| 55 | + detect_ci_provider() == CIProvider.azure_pipelines, reason="arm64 not recognised on azure" |
| 56 | +) |
| 57 | +def test_arm(tmp_path: Path, monkeypatch: pytest.MonkeyPatch): |
| 58 | + arch = "ARM64" |
| 59 | + environment: Dict[str, str] = {} |
| 60 | + |
| 61 | + configuration = PythonConfiguration("irrelevant", arch, "irrelevant", None) |
| 62 | + |
| 63 | + setup_setuptools_cross_compile(tmp_path, configuration, tmp_path, environment) |
| 64 | + with patched_environment(monkeypatch, environment): |
| 65 | + target_platform = setuptools._distutils.util.get_platform() |
| 66 | + |
| 67 | + assert environment["VSCMD_ARG_TGT_ARCH"] == "arm64" |
| 68 | + assert target_platform == "win-arm64" |
| 69 | + |
| 70 | + |
| 71 | +def test_env_set(tmp_path: Path): |
| 72 | + arch = "32" |
| 73 | + environment = {"VSCMD_ARG_TGT_ARCH": "x64"} |
| 74 | + |
| 75 | + configuration = PythonConfiguration("irrelevant", arch, "irrelevant", None) |
| 76 | + |
| 77 | + with pytest.raises(FatalError, match="VSCMD_ARG_TGT_ARCH"): |
| 78 | + setup_setuptools_cross_compile(tmp_path, configuration, tmp_path, environment) |
| 79 | + |
| 80 | + |
| 81 | +def test_env_blank(tmp_path: Path, monkeypatch: pytest.MonkeyPatch): |
| 82 | + arch = "32" |
| 83 | + environment = {"VSCMD_ARG_TGT_ARCH": ""} |
| 84 | + |
| 85 | + configuration = PythonConfiguration("irrelevant", arch, "irrelevant", None) |
| 86 | + |
| 87 | + setup_setuptools_cross_compile(tmp_path, configuration, tmp_path, environment) |
| 88 | + with patched_environment(monkeypatch, environment): |
| 89 | + target_platform = setuptools._distutils.util.get_platform() |
| 90 | + |
| 91 | + assert environment["VSCMD_ARG_TGT_ARCH"] == "x86" |
| 92 | + assert target_platform == "win32" |
0 commit comments