|
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 |
|
@@ -59,7 +62,7 @@ def test_options_1(tmp_path, monkeypatch): |
59 | 62 |
|
60 | 63 | default_build_options = options.build_options(identifier=None) |
61 | 64 |
|
62 | | - assert default_build_options.environment == parse_environment("FOO=BAR") |
| 65 | + assert default_build_options.environment == parse_environment('FOO="BAR"') |
63 | 66 |
|
64 | 67 | all_pinned_container_images = _get_pinned_container_images() |
65 | 68 | pinned_x86_64_container_image = all_pinned_container_images["x86_64"] |
@@ -119,30 +122,75 @@ def test_passthrough_evil(tmp_path, monkeypatch, env_var_value): |
119 | 122 | assert parsed_environment.as_dictionary(prev_environment={}) == {"ENV_VAR": env_var_value} |
120 | 123 |
|
121 | 124 |
|
| 125 | +xfail_env_parse = pytest.mark.xfail( |
| 126 | + raises=SystemExit, reason="until we can figure out the right way to quote these values" |
| 127 | +) |
| 128 | + |
| 129 | + |
122 | 130 | @pytest.mark.parametrize( |
123 | 131 | "env_var_value", |
124 | 132 | [ |
125 | 133 | "normal value", |
126 | | - '"value wrapped in quotes"', |
127 | | - 'an unclosed double-quote: "', |
| 134 | + pytest.param('"value wrapped in quotes"', marks=[xfail_env_parse]), |
| 135 | + pytest.param('an unclosed double-quote: "', marks=[xfail_env_parse]), |
128 | 136 | "string\nwith\ncarriage\nreturns\n", |
129 | | - "a trailing backslash \\", |
| 137 | + pytest.param("a trailing backslash \\", marks=[xfail_env_parse]), |
130 | 138 | ], |
131 | 139 | ) |
132 | 140 | def test_toml_environment_evil(tmp_path, monkeypatch, env_var_value): |
133 | 141 | args = get_default_command_line_arguments() |
134 | 142 | args.package_dir = tmp_path |
135 | 143 |
|
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 | | - ) |
| 144 | + tmp_path.joinpath("pyproject.toml").write_text( |
| 145 | + textwrap.dedent( |
| 146 | + f"""\ |
| 147 | + [tool.cibuildwheel.environment] |
| 148 | + EXAMPLE='''{env_var_value}''' |
| 149 | + """ |
144 | 150 | ) |
| 151 | + ) |
145 | 152 |
|
146 | 153 | options = Options(platform="linux", command_line_arguments=args) |
147 | 154 | parsed_environment = options.build_options(identifier=None).environment |
148 | 155 | assert parsed_environment.as_dictionary(prev_environment={}) == {"EXAMPLE": env_var_value} |
| 156 | + |
| 157 | + |
| 158 | +@pytest.mark.parametrize( |
| 159 | + "toml_assignment,result_value", |
| 160 | + [ |
| 161 | + ('TEST_VAR="simple_value"', "simple_value"), |
| 162 | + # spaces |
| 163 | + ('TEST_VAR="simple value"', "simple value"), |
| 164 | + # env var |
| 165 | + ('TEST_VAR="$PARAM"', "spam"), |
| 166 | + ('TEST_VAR="$PARAM $PARAM"', "spam spam"), |
| 167 | + # env var extension |
| 168 | + ('TEST_VAR="before:$PARAM:after"', "before:spam:after"), |
| 169 | + # env var extension with spaces |
| 170 | + ('TEST_VAR="before $PARAM after"', "before spam after"), |
| 171 | + # literal $ - this test is just for reference, I'm not sure if this |
| 172 | + # syntax will work if we change the TOML quoting behaviour |
| 173 | + (r'TEST_VAR="before\\$after"', "before$after"), |
| 174 | + ], |
| 175 | +) |
| 176 | +def test_toml_environment_quoting(tmp_path: Path, toml_assignment, result_value): |
| 177 | + args = get_default_command_line_arguments() |
| 178 | + args.package_dir = tmp_path |
| 179 | + |
| 180 | + tmp_path.joinpath("pyproject.toml").write_text( |
| 181 | + textwrap.dedent( |
| 182 | + f"""\ |
| 183 | + [tool.cibuildwheel.environment] |
| 184 | + {toml_assignment} |
| 185 | + """ |
| 186 | + ) |
| 187 | + ) |
| 188 | + |
| 189 | + options = Options(platform="linux", command_line_arguments=args) |
| 190 | + parsed_environment = options.build_options(identifier=None).environment |
| 191 | + environment_values = parsed_environment.as_dictionary( |
| 192 | + prev_environment={**os.environ, "PARAM": "spam"}, |
| 193 | + executor=local_environment_executor, |
| 194 | + ) |
| 195 | + |
| 196 | + assert environment_values["TEST_VAR"] == result_value |
0 commit comments