Files
cibuildwheel/test/test_before_build.py
T

88 lines
3.0 KiB
Python
Raw Normal View History

import subprocess
2020-05-02 22:50:52 +01:00
import textwrap
2021-01-06 13:50:58 -05:00
import pytest
from . import test_projects, utils
2020-05-02 16:54:19 +01: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"""
2021-02-18 08:15:37 +01:00
import os
2020-05-02 16:54:19 +01:00
# assert that the Python version as written to pythonversion.txt in the CIBW_BEFORE_BUILD step
# is the same one as is currently running.
2020-05-02 22:42:35 +01:00
version_file = 'c:\\pythonversion.txt' if sys.platform == 'win32' else '/tmp/pythonversion.txt'
2020-05-02 16:54:19 +01:00
with open(version_file) as f:
stored_version = f.read()
2020-05-02 22:42:35 +01:00
print('stored_version', stored_version)
print('sys.version', sys.version)
2020-05-02 16:54:19 +01:00
assert stored_version == sys.version
# check that the executable also was written
2020-05-02 22:42:35 +01:00
executable_file = 'c:\\pythonexecutable.txt' if sys.platform == 'win32' else '/tmp/pythonexecutable.txt'
2020-05-02 16:54:19 +01:00
with open(executable_file) as f:
stored_executable = f.read()
2020-05-02 22:42:35 +01:00
print('stored_executable', stored_executable)
print('sys.executable', sys.executable)
2020-05-02 16:54:19 +01:00
# windows/mac are case insensitive
2020-05-02 22:42:35 +01:00
assert os.path.realpath(stored_executable).lower() == os.path.realpath(sys.executable).lower()
2021-05-03 11:45:43 -04:00
"""
2021-04-30 17:56:34 -04:00
)
2020-05-02 16:54:19 +01:00
)
2020-05-03 14:08:57 +01:00
def test(tmp_path):
2021-05-03 11:45:43 -04:00
project_dir = tmp_path / "project"
2020-05-02 16:54:19 +01:00
project_with_before_build_asserts.generate(project_dir)
2021-04-30 17:56:34 -04:00
before_build = (
2021-05-03 11:45:43 -04:00
"""python -c "import sys; open('{output_dir}pythonversion.txt', 'w').write(sys.version)" && """
2021-04-30 17:56:34 -04:00
'''python -c "import sys; open('{output_dir}pythonexecutable.txt', 'w').write(sys.executable)"'''
)
2020-05-02 16:54:19 +01:00
# build the wheels
2021-04-30 17:56:34 -04:00
actual_wheels = utils.cibuildwheel_run(
project_dir,
add_env={
# write python version information to a temporary file, this is
# checked in setup.py
2021-05-03 11:45:43 -04:00
"CIBW_BEFORE_BUILD": before_build.format(output_dir="/tmp/"),
"CIBW_BEFORE_BUILD_WINDOWS": before_build.format(output_dir=r"c:\\"),
2021-04-30 17:56:34 -04:00
},
)
2020-05-02 16:54:19 +01:00
# also check that we got the right wheels
2021-05-03 11:45:43 -04:00
expected_wheels = utils.expected_wheels("spam", "0.1.0")
2020-05-02 16:54:19 +01:00
assert set(actual_wheels) == set(expected_wheels)
def test_failing_command(tmp_path):
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_BUILD": "false",
"CIBW_BEFORE_BUILD_WINDOWS": "exit /b 1",
2021-04-30 17:56:34 -04:00
},
)
def test_cwd(tmp_path):
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_BUILD": f'''python -c "import os; assert os.getcwd() == {str(project_dir)!r}"''',
"CIBW_BEFORE_BUILD_LINUX": '''python -c "import os; assert os.getcwd() == '/project'"''',
2021-04-30 17:56:34 -04:00
},
)
2021-05-03 11:45:43 -04:00
expected_wheels = utils.expected_wheels("spam", "0.1.0")
assert set(actual_wheels) == set(expected_wheels)