Files
cibuildwheel/unit_test/main_tests/conftest.py
T

79 lines
2.1 KiB
Python
Raw Normal View History

2019-12-15 20:28:38 +01:00
import subprocess
import sys
2020-06-16 14:54:50 +02:00
from pathlib import Path
2019-12-15 20:28:38 +01:00
import pytest
2021-01-06 13:50:58 -05:00
from cibuildwheel import linux, macos, util, windows
2019-12-15 20:28:38 +01:00
2020-02-03 20:42:55 +01:00
class ArgsInterceptor:
2019-12-15 20:28:38 +01:00
def __call__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
2020-06-16 14:54:50 +02:00
MOCK_PACKAGE_DIR = Path('some_package_dir')
2019-12-15 20:28:38 +01:00
2019-12-15 20:28:38 +01:00
@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
'''
2019-12-15 20:28:38 +01:00
def fail_on_call(*args, **kwargs):
raise RuntimeError("This should never be called")
2020-06-17 12:06:39 +02:00
def ignore_call(*args, **kwargs):
pass
2019-12-15 20:28:38 +01:00
monkeypatch.setattr(subprocess, 'Popen', fail_on_call)
2020-01-31 17:11:56 +00:00
monkeypatch.setattr(util, 'download', fail_on_call)
2019-12-15 20:28:38 +01:00
monkeypatch.setattr(windows, 'build', fail_on_call)
monkeypatch.setattr(linux, 'build', fail_on_call)
monkeypatch.setattr(macos, 'build', fail_on_call)
2020-06-17 12:06:39 +02:00
monkeypatch.setattr(Path, 'mkdir', ignore_call)
@pytest.fixture(autouse=True)
2020-03-28 16:25:57 +01:00
def fake_package_dir(monkeypatch):
'''
Monkey-patch enough for the main() function to run
'''
2020-06-16 14:54:50 +02:00
real_path_exists = Path.exists
2020-06-16 14:54:50 +02:00
def mock_path_exists(path):
if path == MOCK_PACKAGE_DIR / 'setup.py':
return True
else:
2020-06-16 14:54:50 +02:00
return real_path_exists(path)
2020-06-16 14:54:50 +02:00
monkeypatch.setattr(Path, 'exists', mock_path_exists)
monkeypatch.setattr(sys, 'argv', ['cibuildwheel', str(MOCK_PACKAGE_DIR)])
2019-12-15 20:28:38 +01:00
@pytest.fixture(params=['linux', 'macos', 'windows'])
def platform(request, monkeypatch):
platform_value = request.param
monkeypatch.setenv('CIBW_PLATFORM', platform_value)
return platform_value
@pytest.fixture
def intercepted_build_args(platform, monkeypatch):
intercepted = ArgsInterceptor()
2020-12-21 11:00:33 +00:00
if platform == 'linux':
monkeypatch.setattr(linux, 'build', intercepted)
elif platform == 'macos':
monkeypatch.setattr(macos, 'build', intercepted)
elif platform == 'windows':
monkeypatch.setattr(windows, 'build', intercepted)
else:
raise ValueError(f'unknown platform value: {platform}')
2019-12-15 20:28:38 +01:00
return intercepted