Files
cibuildwheel/test/test_before_all.py
T

99 lines
3.3 KiB
Python
Raw Normal View History

2026-05-28 00:20:08 +02:00
from __future__ import annotations
import subprocess
2020-05-18 10:42:13 +02:00
import textwrap
2020-05-11 17:09:54 +02:00
2021-01-06 13:50:58 -05:00
import pytest
from . import test_projects, utils
2020-05-18 10:42:13 +02:00
2026-05-28 00:20:08 +02:00
TYPE_CHECKING = False
if TYPE_CHECKING:
from pathlib import Path
2020-05-18 10:42:13 +02:00
project_with_before_build_asserts = test_projects.new_c_project(
2021-04-30 17:56:34 -04:00
setup_py_add=textwrap.dedent(
2021-05-03 11:45:43 -04:00
r"""
2024-05-26 12:41:20 +02:00
import os
2020-05-18 10:42:13 +02:00
with open("text_info.txt") as f:
stored_text = f.read()
print("## stored text: " + stored_text)
assert stored_text == "sample text 123"
2024-05-26 12:41:20 +02:00
# assert that the Python version as written to python_prefix.txt in the CIBW_BEFORE_ALL step
# is not the same one as is currently running.
with open('python_prefix.txt') as f:
stored_prefix = f.read()
print('stored_prefix', stored_prefix)
print('sys.prefix', sys.prefix)
# Works around path-comparison bugs caused by short-paths on Windows e.g.
# vssadm~1 instead of vssadministrator
assert not os.path.samefile(stored_prefix, sys.prefix)
2021-05-03 11:45:43 -04:00
"""
2021-04-30 17:56:34 -04:00
)
2020-05-18 10:42:13 +02:00
)
2020-05-11 17:09:54 +02:00
2026-04-01 10:23:02 -04:00
def test(tmp_path: Path) -> None:
2021-05-03 11:45:43 -04:00
project_dir = tmp_path / "project"
2020-05-18 10:42:13 +02:00
project_with_before_build_asserts.generate(project_dir)
2020-05-11 17:09:54 +02:00
2021-05-03 11:45:43 -04:00
with (project_dir / "text_info.txt").open(mode="w") as ff:
2020-05-11 17:09:54 +02:00
print("dummy text", file=ff)
# write python version information to a temporary file, this is checked in
# setup.py
#
# note, before_all runs in whatever the host environment is, `python`
# might be any version of python (even Python 2 on Travis ci!), so this is
# written to be broadly compatible
2024-05-26 12:41:20 +02:00
before_all_command = (
"""python -c "import os, sys; f = open('{project}/text_info.txt', 'w'); f.write('sample text '+os.environ.get('TEST_VAL', '')); f.close()" && """
'''python -c "import sys; f = open('{project}/python_prefix.txt', 'w'); f.write(sys.prefix); f.close()"'''
2024-05-26 12:41:20 +02:00
)
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_BEFORE_ALL": before_all_command,
2024-05-26 12:41:20 +02:00
"CIBW_BEFORE_ALL_LINUX": f'{before_all_command} && python -c "import sys; assert sys.version_info >= (3, 8)"',
2021-05-03 11:45:43 -04:00
"CIBW_ENVIRONMENT": "TEST_VAL='123'",
2021-04-30 17:56:34 -04:00
},
2024-05-26 12:41:20 +02:00
single_python=True,
2021-04-30 17:56:34 -04:00
)
2020-05-11 17:09:54 +02:00
# also check that we got the right wheels
2024-05-26 12:41:20 +02:00
expected_wheels = utils.expected_wheels("spam", "0.1.0", single_python=True)
assert set(actual_wheels) == set(expected_wheels)
2026-04-01 10:23:02 -04:00
def test_failing_command(tmp_path: Path) -> None:
2021-05-03 11:45:43 -04:00
project_dir = tmp_path / "project"
test_projects.new_c_project().generate(project_dir)
with pytest.raises(subprocess.CalledProcessError):
2021-04-30 17:56:34 -04:00
utils.cibuildwheel_run(
project_dir,
add_env={
2021-05-03 11:45:43 -04:00
"CIBW_BEFORE_ALL": "false",
"CIBW_BEFORE_ALL_WINDOWS": "exit /b 1",
2021-04-30 17:56:34 -04:00
},
)
2026-04-01 10:23:02 -04:00
def test_cwd(tmp_path: Path) -> None:
2021-05-03 11:45:43 -04:00
project_dir = tmp_path / "project"
test_projects.new_c_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_BEFORE_ALL": f'''python -c "import os; assert os.getcwd() == {str(project_dir)!r}"''',
"CIBW_BEFORE_ALL_LINUX": '''python -c "import os; assert os.getcwd() == '/project'"''',
2021-04-30 17:56:34 -04:00
},
2024-05-26 12:41:20 +02:00
single_python=True,
2021-04-30 17:56:34 -04:00
)
2024-05-26 12:41:20 +02:00
expected_wheels = utils.expected_wheels("spam", "0.1.0", single_python=True)
2020-05-11 17:09:54 +02:00
assert set(actual_wheels) == set(expected_wheels)