Files
cibuildwheel/unit_test/options_test.py
T

117 lines
3.6 KiB
Python
Raw Normal View History

2021-10-12 02:05:47 +01:00
import platform as platform_module
2021-09-19 00:19:28 -04:00
2021-11-21 15:19:45 -05:00
import pytest
2021-09-19 00:19:28 -04:00
from cibuildwheel.__main__ import get_build_identifiers
from cibuildwheel.environment import parse_environment
2021-10-12 02:05:47 +01:00
from cibuildwheel.options import Options, _get_pinned_docker_images
from .utils import get_default_command_line_arguments
2021-09-19 00:19:28 -04:00
PYPROJECT_1 = """
[tool.cibuildwheel]
build = ["cp38*", "cp37*"]
environment = {FOO="BAR"}
test-command = "pyproject"
manylinux-x86_64-image = "manylinux1"
2021-11-21 15:19:45 -05:00
environment-pass = ["EXAMPLE_ENV"]
2021-09-19 00:19:28 -04:00
[tool.cibuildwheel.macos]
test-requires = "else"
[[tool.cibuildwheel.overrides]]
select = "cp37*"
test-command = "pyproject-override"
manylinux-x86_64-image = "manylinux2014"
"""
2021-10-12 02:05:47 +01:00
def test_options_1(tmp_path, monkeypatch):
2021-09-19 00:19:28 -04:00
with tmp_path.joinpath("pyproject.toml").open("w") as f:
f.write(PYPROJECT_1)
2021-10-12 02:05:47 +01:00
args = get_default_command_line_arguments()
2022-04-26 22:21:27 -04:00
args.package_dir = tmp_path
2021-10-12 02:05:47 +01:00
monkeypatch.setattr(platform_module, "machine", lambda: "x86_64")
options = Options(platform="linux", command_line_arguments=args)
2021-09-19 00:19:28 -04:00
identifiers = get_build_identifiers(
2021-10-12 02:05:47 +01:00
platform="linux",
build_selector=options.globals.build_selector,
architectures=options.globals.architectures,
2021-09-19 00:19:28 -04:00
)
override_display = """\
2021-10-12 02:05:47 +01:00
test_command: 'pyproject'
cp37-manylinux_x86_64: 'pyproject-override'"""
2021-09-19 00:19:28 -04:00
2021-10-12 02:05:47 +01:00
print(options.summary(identifiers))
2021-09-19 00:19:28 -04:00
2021-10-12 02:05:47 +01:00
assert override_display in options.summary(identifiers)
default_build_options = options.build_options(identifier=None)
assert default_build_options.environment == parse_environment('FOO="BAR"')
2021-09-19 00:19:28 -04:00
all_pinned_docker_images = _get_pinned_docker_images()
pinned_x86_64_docker_image = all_pinned_docker_images["x86_64"]
2021-10-12 02:05:47 +01:00
local = options.build_options("cp38-manylinux_x86_64")
2021-09-19 00:19:28 -04:00
assert local.manylinux_images is not None
assert local.test_command == "pyproject"
assert local.manylinux_images["x86_64"] == pinned_x86_64_docker_image["manylinux1"]
2021-10-12 02:05:47 +01:00
local = options.build_options("cp37-manylinux_x86_64")
2021-09-19 00:19:28 -04:00
assert local.manylinux_images is not None
assert local.test_command == "pyproject-override"
assert local.manylinux_images["x86_64"] == pinned_x86_64_docker_image["manylinux2014"]
2021-11-21 15:19:45 -05:00
def test_passthrough(tmp_path, monkeypatch):
with tmp_path.joinpath("pyproject.toml").open("w") as f:
f.write(PYPROJECT_1)
args = get_default_command_line_arguments()
2022-04-26 22:21:27 -04:00
args.package_dir = tmp_path
2021-11-21 15:19:45 -05:00
monkeypatch.setattr(platform_module, "machine", lambda: "x86_64")
monkeypatch.setenv("EXAMPLE_ENV", "ONE")
options = Options(platform="linux", command_line_arguments=args)
default_build_options = options.build_options(identifier=None)
assert default_build_options.environment.as_dictionary(prev_environment={}) == {
"FOO": "BAR",
"EXAMPLE_ENV": "ONE",
}
@pytest.mark.parametrize(
"env_var_value",
[
"normal value",
'"value wrapped in quotes"',
"an unclosed single-quote: '",
'an unclosed double-quote: "',
"string\nwith\ncarriage\nreturns\n",
"a trailing backslash \\",
],
)
def test_passthrough_evil(tmp_path, monkeypatch, env_var_value):
args = get_default_command_line_arguments()
2022-04-26 22:21:27 -04:00
args.package_dir = tmp_path
2021-11-21 15:19:45 -05:00
monkeypatch.setattr(platform_module, "machine", lambda: "x86_64")
monkeypatch.setenv("CIBW_ENVIRONMENT_PASS_LINUX", "ENV_VAR")
options = Options(platform="linux", command_line_arguments=args)
monkeypatch.setenv("ENV_VAR", env_var_value)
parsed_environment = options.build_options(identifier=None).environment
assert parsed_environment.as_dictionary(prev_environment={}) == {"ENV_VAR": env_var_value}