Files

108 lines
3.4 KiB
Python
Raw Permalink Normal View History

2026-05-28 00:20:08 +02:00
from __future__ import annotations
2021-02-04 14:27:54 -05:00
import contextlib
2021-01-18 09:54:32 -05:00
import platform as platform_module
2019-12-15 20:28:38 +01:00
import subprocess
import sys
2020-06-16 14:54:50 +02:00
from pathlib import Path
2026-04-01 10:23:02 -04:00
from typing import Any
2019-12-15 20:28:38 +01:00
import pytest
2025-07-15 20:45:53 -07:00
from cibuildwheel import architecture
from cibuildwheel.logger import Logger
2025-08-01 12:32:01 -04:00
from cibuildwheel.platforms import android, ios, linux, macos, pyodide, windows
2025-01-27 20:35:56 +01:00
from cibuildwheel.util import file
2019-12-15 20:28:38 +01:00
2026-05-28 00:20:08 +02:00
TYPE_CHECKING = False
if TYPE_CHECKING:
from collections.abc import Generator, Iterator
2019-12-15 20:28:38 +01:00
2020-02-03 20:42:55 +01:00
class ArgsInterceptor:
2024-10-22 10:16:59 -04:00
def __init__(self) -> None:
2022-09-09 08:34:47 -04:00
self.call_count = 0
2026-04-01 10:23:02 -04:00
self.args: tuple[Any, ...] = ()
self.kwargs: dict[str, Any] = {}
2022-09-09 08:34:47 -04:00
2026-04-01 10:23:02 -04:00
def __call__(self, *args: Any, **kwargs: Any) -> None:
2022-09-09 08:34:47 -04:00
self.call_count += 1
2019-12-15 20:28:38 +01:00
self.args = args
self.kwargs = kwargs
@pytest.fixture(autouse=True)
2026-04-01 10:23:02 -04:00
def mock_protection(monkeypatch: pytest.MonkeyPatch) -> None:
2021-05-03 11:45:43 -04:00
"""
Ensure that a unit test will never actually run a cibuildwheel 'build'
function, which shouldn't be run on a developer's machine
2021-05-03 11:45:43 -04:00
"""
2026-04-01 10:23:02 -04:00
def fail_on_call(*args: object, **kwargs: object) -> None:
2022-09-05 13:11:46 -04:00
msg = "This should never be called"
raise RuntimeError(msg)
2019-12-15 20:28:38 +01:00
2026-04-01 10:23:02 -04:00
def ignore_call(*args: object, **kwargs: object) -> None:
2020-06-17 12:06:39 +02:00
pass
2021-05-03 11:45:43 -04:00
monkeypatch.setattr(subprocess, "Popen", fail_on_call)
2025-01-27 20:35:56 +01:00
monkeypatch.setattr(file, "download", fail_on_call)
2021-05-03 11:45:43 -04:00
monkeypatch.setattr(windows, "build", fail_on_call)
monkeypatch.setattr(linux, "build", fail_on_call)
monkeypatch.setattr(macos, "build", fail_on_call)
monkeypatch.setattr(pyodide, "build", fail_on_call)
2021-05-03 11:45:43 -04:00
monkeypatch.setattr(Path, "mkdir", ignore_call)
monkeypatch.setattr(architecture, "_check_aarch32_el0", lambda: True)
2020-06-17 12:06:39 +02:00
@pytest.fixture(autouse=True)
2026-04-01 10:23:02 -04:00
def fake_package_dir_autouse(fake_package_dir: list[str]) -> None:
2021-10-12 02:05:47 +01:00
pass
2021-01-19 20:52:06 -05:00
2021-02-04 14:27:54 -05:00
@pytest.fixture(autouse=True)
2026-04-01 10:23:02 -04:00
def disable_print_wheels(monkeypatch: pytest.MonkeyPatch) -> None:
2021-02-04 14:27:54 -05:00
@contextlib.contextmanager
2026-04-01 10:23:02 -04:00
def empty_cm(*args: object, **kwargs: object) -> Generator[None, None, None]:
2021-02-04 14:27:54 -05:00
yield
2025-07-15 20:45:53 -07:00
monkeypatch.setattr(Logger, "print_summary", empty_cm)
2021-02-04 14:27:54 -05:00
@pytest.fixture
2026-04-01 10:23:02 -04:00
def allow_empty(monkeypatch: pytest.MonkeyPatch, fake_package_dir: list[str]) -> None:
2023-01-30 15:53:44 -05:00
monkeypatch.setattr(sys, "argv", [*fake_package_dir, "--allow-empty"])
2019-12-15 20:28:38 +01:00
2021-05-03 11:45:43 -04:00
@pytest.fixture(params=["linux", "macos", "windows"])
2026-04-01 10:23:02 -04:00
def platform(request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch) -> str:
platform_value: str = request.param
2021-05-03 11:45:43 -04:00
monkeypatch.setenv("CIBW_PLATFORM", platform_value)
2021-01-18 01:03:15 -05:00
2021-05-03 11:45:43 -04:00
if platform_value == "windows":
monkeypatch.setattr(platform_module, "machine", lambda: "AMD64")
2021-01-18 09:54:32 -05:00
else:
2021-05-03 11:45:43 -04:00
monkeypatch.setattr(platform_module, "machine", lambda: "x86_64")
2021-01-18 09:54:32 -05:00
2021-05-03 11:45:43 -04:00
if platform_value == "macos":
monkeypatch.setattr(macos, "get_macos_version", lambda: (11, 1))
2021-01-23 18:15:29 +00:00
2019-12-15 20:28:38 +01:00
return platform_value
@pytest.fixture
2026-04-01 10:23:02 -04:00
def intercepted_build_args(monkeypatch: pytest.MonkeyPatch) -> Iterator[ArgsInterceptor]:
2019-12-15 20:28:38 +01:00
intercepted = ArgsInterceptor()
2020-12-21 11:00:33 +00:00
2025-08-01 12:32:01 -04:00
monkeypatch.setattr(android, "build", intercepted)
monkeypatch.setattr(ios, "build", intercepted)
2022-09-09 08:34:47 -04:00
monkeypatch.setattr(linux, "build", intercepted)
monkeypatch.setattr(macos, "build", intercepted)
monkeypatch.setattr(pyodide, "build", intercepted)
2025-08-01 12:32:01 -04:00
monkeypatch.setattr(windows, "build", intercepted)
2020-12-21 11:00:33 +00:00
2022-09-09 08:34:47 -04:00
yield intercepted
# check that intercepted_build_args only ever had one set of args
assert intercepted.call_count <= 1