Files

136 lines
4.6 KiB
Python
Raw Permalink Normal View History

import platform
import subprocess
from typing import Tuple
import pytest
from . import test_projects, utils
basic_project = test_projects.new_c_project()
ALL_MACOS_WHEELS = {
2021-05-03 11:45:43 -04:00
*utils.expected_wheels("spam", "0.1.0", machine_arch="x86_64"),
*utils.expected_wheels("spam", "0.1.0", machine_arch="arm64"),
}
2021-01-06 21:33:41 +00:00
def get_xcode_version() -> Tuple[int, int]:
2021-02-14 12:56:33 -05:00
output = subprocess.run(
2021-05-03 11:45:43 -04:00
["xcodebuild", "-version"],
universal_newlines=True,
2021-02-14 12:56:33 -05:00
check=True,
stdout=subprocess.PIPE,
).stdout
lines = output.splitlines()
_, version_str = lines[0].split()
2021-05-03 11:45:43 -04:00
version_parts = version_str.split(".")
return (int(version_parts[0]), int(version_parts[1]))
def test_cross_compiled_build(tmp_path):
2021-05-03 11:45:43 -04:00
if utils.platform != "macos":
pytest.skip("this test is only relevant to macos")
2021-01-30 10:52:00 +00:00
if get_xcode_version() < (12, 2):
2021-05-03 11:45:43 -04:00
pytest.skip("this test only works with Xcode 12.2 or greater")
2021-05-03 11:45:43 -04:00
project_dir = tmp_path / "project"
basic_project.generate(project_dir)
2021-04-30 17:56:34 -04:00
actual_wheels = utils.cibuildwheel_run(
project_dir,
add_env={
2021-05-03 11:45:43 -04:00
"CIBW_BUILD": "cp39-*",
"CIBW_ARCHS": "x86_64, universal2, arm64",
2021-04-30 17:56:34 -04:00
},
)
2021-05-03 11:45:43 -04:00
expected_wheels = [w for w in ALL_MACOS_WHEELS if "cp39" in w]
assert set(actual_wheels) == set(expected_wheels)
2021-05-03 11:45:43 -04:00
@pytest.mark.parametrize("build_universal2", [False, True])
def test_cross_compiled_test(tmp_path, capfd, build_universal2):
2021-05-03 11:45:43 -04:00
if utils.platform != "macos":
pytest.skip("this test is only relevant to macos")
2021-01-30 10:52:00 +00:00
if get_xcode_version() < (12, 2):
2021-05-03 11:45:43 -04:00
pytest.skip("this test only works with Xcode 12.2 or greater")
2021-05-03 11:45:43 -04:00
project_dir = tmp_path / "project"
basic_project.generate(project_dir)
2021-04-30 17:56:34 -04:00
actual_wheels = utils.cibuildwheel_run(
project_dir,
add_env={
2021-05-03 11:45:43 -04:00
"CIBW_BUILD": "cp39-*",
"CIBW_TEST_COMMAND": '''python -c "import platform; print('running tests on ' + platform.machine())"''',
"CIBW_ARCHS": "universal2" if build_universal2 else "x86_64 arm64",
2021-04-30 17:56:34 -04:00
},
)
captured = capfd.readouterr()
2021-05-03 11:45:43 -04:00
if platform.machine() == "x86_64":
# ensure that tests were run on only x86_64
2021-05-03 11:45:43 -04:00
assert "running tests on x86_64" in captured.out
assert "running tests on arm64" not in captured.out
if build_universal2:
2021-04-30 17:56:34 -04:00
assert (
2021-05-03 11:45:43 -04:00
"While universal2 wheels can be built on x86_64, the arm64 part of them cannot currently be tested"
2021-04-30 17:56:34 -04:00
in captured.err
)
else:
2021-04-30 17:56:34 -04:00
assert (
2021-05-03 11:45:43 -04:00
"While arm64 wheels can be built on x86_64, they cannot be tested" in captured.err
2021-04-30 17:56:34 -04:00
)
2021-05-03 11:45:43 -04:00
elif platform.machine() == "arm64":
# ensure that tests were run on both x86_64 and arm64
2021-05-03 11:45:43 -04:00
assert "running tests on x86_64" in captured.out
assert "running tests on arm64" in captured.out
2021-01-06 21:33:41 +00:00
if build_universal2:
2021-05-03 11:45:43 -04:00
expected_wheels = [w for w in ALL_MACOS_WHEELS if "cp39" in w and "universal2" in w]
2021-01-06 21:33:41 +00:00
else:
2021-05-03 11:45:43 -04:00
expected_wheels = [w for w in ALL_MACOS_WHEELS if "cp39" in w and "universal2" not in w]
2021-01-06 21:33:41 +00:00
assert set(actual_wheels) == set(expected_wheels)
2021-05-03 11:45:43 -04:00
@pytest.mark.parametrize("skip_arm64_test", [False, True])
def test_universal2_testing(tmp_path, capfd, skip_arm64_test):
2021-05-03 11:45:43 -04:00
if utils.platform != "macos":
pytest.skip("this test is only relevant to macos")
2021-01-30 10:52:00 +00:00
if get_xcode_version() < (12, 2):
2021-05-03 11:45:43 -04:00
pytest.skip("this test only works with Xcode 12.2 or greater")
if platform.machine() != "x86_64":
pytest.skip("this test only works on x86_64")
2021-05-03 11:45:43 -04:00
project_dir = tmp_path / "project"
basic_project.generate(project_dir)
2021-04-30 17:56:34 -04:00
actual_wheels = utils.cibuildwheel_run(
project_dir,
add_env={
2021-05-03 11:45:43 -04:00
"CIBW_BUILD": "cp39-*",
"CIBW_TEST_COMMAND": '''python -c "import platform; print('running tests on ' + platform.machine())"''',
"CIBW_ARCHS": "universal2",
"CIBW_TEST_SKIP": "*_universal2:arm64" if skip_arm64_test else "",
2021-04-30 17:56:34 -04:00
},
)
captured = capfd.readouterr()
2021-05-03 11:45:43 -04:00
if platform.machine() == "x86_64":
assert "running tests on x86_64" in captured.out
assert "running tests on arm64" not in captured.out
2021-05-03 11:45:43 -04:00
warning_message = "While universal2 wheels can be built on x86_64, the arm64 part of them cannot currently be tested"
if skip_arm64_test:
assert warning_message not in captured.err
else:
assert warning_message in captured.err
2021-05-03 11:45:43 -04:00
expected_wheels = [w for w in ALL_MACOS_WHEELS if "cp39" in w and "universal2" in w]
assert set(actual_wheels) == set(expected_wheels)