Files
cibuildwheel/unit_test/main_tests/conftest.py
T
Henry SchreinerandJoe Rickerby eb39da0b10 feat: add --only (#1098)
* fix(style): use sub recommended replacement method

See https://docs.python.org/3/library/re.html#re.escape

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* feat: add --build

* refactor: switch to using --only

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* docs: cover --only

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* docs: undo change to --plat for auditwheel

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* (unrelated) speed up running the unit tests

based on a profile run, this speeds up the unit tests by 3x

* Remove `intercepted_build_args` reliance on `platform` fixture

* Add test that checks if 'only' overrides env var options

* Ensure that the --only command line option overrides env vars

* Fix UnboundLocalError

* Test cleanups

* style: typing.cast instead of plain cast

* feat: add only: to the GHA

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: Joe Rickerby <joerick@mac.com>
2022-09-09 08:34:47 -04:00

95 lines
2.5 KiB
Python

from __future__ import annotations
import contextlib
import platform as platform_module
import subprocess
import sys
from pathlib import Path
import pytest
from cibuildwheel import linux, macos, util, windows
class ArgsInterceptor:
def __init__(self):
self.call_count = 0
self.args = None
self.kwargs = None
def __call__(self, *args, **kwargs):
self.call_count += 1
self.args = args
self.kwargs = kwargs
@pytest.fixture(autouse=True)
def mock_protection(monkeypatch):
"""
Ensure that a unit test will never actually run a cibuildwheel 'build'
function, which shouldn't be run on a developer's machine
"""
def fail_on_call(*args, **kwargs):
raise RuntimeError("This should never be called")
def ignore_call(*args, **kwargs):
pass
monkeypatch.setattr(subprocess, "Popen", fail_on_call)
monkeypatch.setattr(util, "download", fail_on_call)
monkeypatch.setattr(windows, "build", fail_on_call)
monkeypatch.setattr(linux, "build", fail_on_call)
monkeypatch.setattr(macos, "build", fail_on_call)
monkeypatch.setattr(Path, "mkdir", ignore_call)
@pytest.fixture(autouse=True)
def fake_package_dir_autouse(fake_package_dir):
pass
@pytest.fixture(autouse=True)
def disable_print_wheels(monkeypatch):
@contextlib.contextmanager
def empty_cm(*args, **kwargs):
yield
monkeypatch.setattr(util, "print_new_wheels", empty_cm)
@pytest.fixture
def allow_empty(request, monkeypatch, fake_package_dir):
monkeypatch.setattr(sys, "argv", fake_package_dir + ["--allow-empty"])
@pytest.fixture(params=["linux", "macos", "windows"])
def platform(request, monkeypatch):
platform_value = request.param
monkeypatch.setenv("CIBW_PLATFORM", platform_value)
if platform_value == "windows":
monkeypatch.setattr(platform_module, "machine", lambda: "AMD64")
else:
monkeypatch.setattr(platform_module, "machine", lambda: "x86_64")
if platform_value == "macos":
monkeypatch.setattr(macos, "get_macos_version", lambda: (11, 1))
return platform_value
@pytest.fixture
def intercepted_build_args(monkeypatch):
intercepted = ArgsInterceptor()
monkeypatch.setattr(linux, "build", intercepted)
monkeypatch.setattr(macos, "build", intercepted)
monkeypatch.setattr(windows, "build", intercepted)
yield intercepted
# check that intercepted_build_args only ever had one set of args
assert intercepted.call_count <= 1