Files
cibuildwheel/test/test_environment.py
T

91 lines
3.3 KiB
Python
Raw Normal View History

2020-05-02 22:42:35 +01:00
import os
import subprocess
import textwrap
2020-05-02 16:54:19 +01:00
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_environment_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"""
2020-05-02 16:54:19 +01:00
import os
# explode if environment isn't correct, as set in CIBW_ENVIRONMENT
CIBW_TEST_VAR = os.environ.get("CIBW_TEST_VAR")
CIBW_TEST_VAR_2 = os.environ.get("CIBW_TEST_VAR_2")
CIBW_TEST_VAR_3 = os.environ.get("CIBW_TEST_VAR_3")
2020-05-02 16:54:19 +01:00
PATH = os.environ.get("PATH")
if CIBW_TEST_VAR != "a b c":
raise Exception('CIBW_TEST_VAR should equal "a b c". It was "%s"' % CIBW_TEST_VAR)
if CIBW_TEST_VAR_2 != "1":
raise Exception('CIBW_TEST_VAR_2 should equal "1". It was "%s"' % CIBW_TEST_VAR_2)
if CIBW_TEST_VAR_3 != "test string 3":
raise Exception('CIBW_TEST_VAR_3 should equal "test string 3". It was "%s"' % CIBW_TEST_VAR_3)
2020-05-02 16:54:19 +01:00
if "/opt/cibw_test_path" not in PATH:
raise Exception('PATH should contain "/opt/cibw_test_path". It was "%s"' % PATH)
if "$PATH" in PATH:
raise Exception('$PATH should be expanded in PATH. It was "%s"' % PATH)
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_environment_asserts.generate(project_dir)
# write some information into the CIBW_ENVIRONMENT, for expansion and
# insertion into the environment by cibuildwheel. This is checked
2020-05-02 22:42:35 +01:00
# in setup.py
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_ENVIRONMENT": """CIBW_TEST_VAR="a b c" CIBW_TEST_VAR_2=1 CIBW_TEST_VAR_3="$(echo 'test string 3')" PATH=$PATH:/opt/cibw_test_path""",
"CIBW_ENVIRONMENT_WINDOWS": '''CIBW_TEST_VAR="a b c" CIBW_TEST_VAR_2=1 CIBW_TEST_VAR_3="$(echo 'test string 3')" PATH="$PATH;/opt/cibw_test_path"''',
2021-04-30 17:56:34 -04:00
},
)
2020-05-02 16:54:19 +01:00
# also check that we got the right wheels built
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)
2020-05-02 22:42:35 +01:00
def test_overridden_path(tmp_path, capfd):
2021-05-03 11:45:43 -04:00
project_dir = tmp_path / "project"
output_dir = tmp_path / "output"
2020-05-15 12:35:14 +01:00
project = test_projects.new_c_project()
2020-05-15 12:35:14 +01:00
project.generate(project_dir)
2020-05-03 14:09:10 +01:00
output_dir.mkdir()
2020-05-02 22:42:35 +01:00
# mess up PATH, somehow
with pytest.raises(subprocess.CalledProcessError):
2021-05-03 11:45:43 -04:00
if utils.platform == "linux":
2021-04-30 17:56:34 -04:00
utils.cibuildwheel_run(
project_dir,
output_dir=output_dir,
add_env={
2021-05-03 11:45:43 -04:00
"CIBW_BEFORE_ALL": "mkdir new_path && touch new_path/python && chmod +x new_path/python",
"CIBW_ENVIRONMENT": '''PATH="$(pwd)/new_path:$PATH"''',
2021-04-30 17:56:34 -04:00
},
)
2020-07-05 17:27:29 +01:00
else:
2021-05-03 11:45:43 -04:00
new_path = tmp_path / "another_bin"
2020-07-05 17:27:29 +01:00
new_path.mkdir()
2021-05-03 11:45:43 -04:00
(new_path / "python").touch(mode=0o777)
2020-07-05 17:27:29 +01:00
2021-04-30 17:56:34 -04:00
utils.cibuildwheel_run(
project_dir,
output_dir=output_dir,
add_env={
2021-05-03 11:45:43 -04:00
"NEW_PATH": str(new_path),
"CIBW_ENVIRONMENT": f'''PATH="$NEW_PATH{os.pathsep}$PATH"''',
2021-04-30 17:56:34 -04:00
},
)
2020-05-02 22:42:35 +01:00
assert len(os.listdir(output_dir)) == 0
captured = capfd.readouterr()
assert "python available on PATH doesn't match our installed instance" in captured.err