|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import os |
3 | 4 | import platform as platform_module |
4 | 5 | import textwrap |
| 6 | +from pathlib import Path |
5 | 7 |
|
6 | 8 | import pytest |
7 | 9 |
|
8 | 10 | from cibuildwheel.__main__ import get_build_identifiers |
| 11 | +from cibuildwheel.bashlex_eval import local_environment_executor |
9 | 12 | from cibuildwheel.environment import parse_environment |
10 | 13 | from cibuildwheel.options import Options, _get_pinned_container_images |
11 | 14 |
|
@@ -133,16 +136,53 @@ def test_toml_environment_evil(tmp_path, monkeypatch, env_var_value): |
133 | 136 | args = get_default_command_line_arguments() |
134 | 137 | args.package_dir = tmp_path |
135 | 138 |
|
136 | | - with tmp_path.joinpath("pyproject.toml").open("w") as f: |
137 | | - f.write( |
138 | | - textwrap.dedent( |
139 | | - f"""\ |
140 | | - [tool.cibuildwheel.environment] |
141 | | - EXAMPLE='''{env_var_value}''' |
142 | | - """ |
143 | | - ) |
| 139 | + tmp_path.joinpath("pyproject.toml").write_text( |
| 140 | + textwrap.dedent( |
| 141 | + f"""\ |
| 142 | + [tool.cibuildwheel.environment] |
| 143 | + EXAMPLE='''{env_var_value}''' |
| 144 | + """ |
144 | 145 | ) |
| 146 | + ) |
145 | 147 |
|
146 | 148 | options = Options(platform="linux", command_line_arguments=args) |
147 | 149 | parsed_environment = options.build_options(identifier=None).environment |
148 | 150 | assert parsed_environment.as_dictionary(prev_environment={}) == {"EXAMPLE": env_var_value} |
| 151 | + |
| 152 | + |
| 153 | +@pytest.mark.parametrize( |
| 154 | + "toml_assignment,result_value", |
| 155 | + [ |
| 156 | + ('TEST_VAR="simple_value"', "simple_value"), |
| 157 | + # spaces |
| 158 | + ('TEST_VAR="simple value"', "simple value"), |
| 159 | + # env var |
| 160 | + ('TEST_VAR="$PARAM"', "spam"), |
| 161 | + ('TEST_VAR="$PARAM $PARAM"', "spam spam"), |
| 162 | + # env var extension |
| 163 | + ('TEST_VAR="before:$PARAM:after"', "before:spam:after"), |
| 164 | + # env var extension with spaces |
| 165 | + ('TEST_VAR="before $PARAM after"', "before spam after"), |
| 166 | + ], |
| 167 | +) |
| 168 | +def test_toml_environment_quoting(tmp_path: Path, toml_assignment, result_value): |
| 169 | + args = get_default_command_line_arguments() |
| 170 | + args.package_dir = tmp_path |
| 171 | + |
| 172 | + tmp_path.joinpath("pyproject.toml").write_text( |
| 173 | + textwrap.dedent( |
| 174 | + f"""\ |
| 175 | + [tool.cibuildwheel.environment] |
| 176 | + {toml_assignment} |
| 177 | + """ |
| 178 | + ) |
| 179 | + ) |
| 180 | + |
| 181 | + options = Options(platform="linux", command_line_arguments=args) |
| 182 | + parsed_environment = options.build_options(identifier=None).environment |
| 183 | + environment_values = parsed_environment.as_dictionary( |
| 184 | + prev_environment={**os.environ, "PARAM": "spam"}, |
| 185 | + executor=local_environment_executor, |
| 186 | + ) |
| 187 | + |
| 188 | + assert environment_values["TEST_VAR"] == result_value |
0 commit comments