diff --git a/test/test_environment.py b/test/test_environment.py index 4382df44..2a10ed5a 100644 --- a/test/test_environment.py +++ b/test/test_environment.py @@ -2,6 +2,7 @@ from __future__ import annotations import os import subprocess +import sys import textwrap import pytest @@ -35,6 +36,7 @@ project_with_environment_asserts = test_projects.new_c_project( def test(tmp_path): + python_echo = f"'{sys.executable}' -c \"import sys; print(*sys.argv[1:])\"" project_dir = tmp_path / "project" project_with_environment_asserts.generate(project_dir) @@ -45,7 +47,7 @@ def test(tmp_path): project_dir, add_env={ "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"''', + "CIBW_ENVIRONMENT_WINDOWS": f'''CIBW_TEST_VAR="a b c" CIBW_TEST_VAR_2=1 CIBW_TEST_VAR_3="$({python_echo} 'test string 3')" PATH="$PATH;/opt/cibw_test_path"''', }, ) diff --git a/unit_test/environment_test.py b/unit_test/environment_test.py index 38678771..f4229f80 100644 --- a/unit_test/environment_test.py +++ b/unit_test/environment_test.py @@ -1,9 +1,13 @@ from __future__ import annotations import os +import sys from cibuildwheel.environment import parse_environment +# this command is equivalent to Unix 'echo', but works on Windows too +PYTHON_ECHO = f"'{sys.executable}' -c \"import sys; print(*sys.argv[1:])\"" + def test_basic_parsing(): environment_recipe = parse_environment("VAR=1 VBR=2") @@ -30,7 +34,7 @@ def test_inheritance(): def test_shell_eval(): - environment_recipe = parse_environment('VAR="$(echo "a test" string)"') + environment_recipe = parse_environment(f'VAR="$({PYTHON_ECHO} "a test" string)"') env_copy = os.environ.copy() env_copy.pop("VAR", None) @@ -41,11 +45,12 @@ def test_shell_eval(): def test_shell_eval_and_env(): - environment_recipe = parse_environment('VAR="$(echo "$PREV_VAR" string)"') + environment_recipe = parse_environment(f'VAR="$({PYTHON_ECHO} "$PREV_VAR" string)"') - environment_dict = environment_recipe.as_dictionary(prev_environment={"PREV_VAR": "1 2 3"}) + prev_environment = {**os.environ, "PREV_VAR": "1 2 3"} + environment_dict = environment_recipe.as_dictionary(prev_environment=prev_environment) - assert environment_dict == {"PREV_VAR": "1 2 3", "VAR": "1 2 3 string"} + assert environment_dict == {**prev_environment, "VAR": "1 2 3 string"} def test_empty_var(): @@ -75,10 +80,12 @@ def test_no_vars_pass_through(): def test_operators_inside_eval(): - environment_recipe = parse_environment('SOMETHING="$(echo a; echo b; echo c)"') + environment_recipe = parse_environment( + f'SOMETHING="$({PYTHON_ECHO} a; {PYTHON_ECHO} b; {PYTHON_ECHO} c)"' + ) - # pass the existing process env so PATH is available - environment_dict = environment_recipe.as_dictionary(os.environ.copy()) + # pass the existing process env so subcommands can be run in the evaluation + environment_dict = environment_recipe.as_dictionary(prev_environment=os.environ.copy()) assert environment_dict.get("SOMETHING") == "a\nb\nc" @@ -86,7 +93,6 @@ def test_operators_inside_eval(): def test_substitution_with_backslash(): environment_recipe = parse_environment('PATH2="somewhere_else;$PATH1"') - # pass the existing process env so PATH is available environment_dict = environment_recipe.as_dictionary(prev_environment={"PATH1": "c:\\folder\\"}) assert environment_dict.get("PATH2") == "somewhere_else;c:\\folder\\" @@ -94,11 +100,11 @@ def test_substitution_with_backslash(): def test_awkwardly_quoted_variable(): environment_recipe = parse_environment( - 'VAR2=something"like this""$VAR1"$VAR1$(echo "there is more")"$(echo "and more!")"' + f'VAR2=something"like this""$VAR1"$VAR1$({PYTHON_ECHO} "there is more")"$({PYTHON_ECHO} "and more!")"' ) - # pass the existing process env so PATH is available - environment_dict = environment_recipe.as_dictionary({"VAR1": "but wait"}) + prev_environment = {**os.environ, "VAR1": "but wait"} + environment_dict = environment_recipe.as_dictionary(prev_environment=prev_environment) assert ( environment_dict.get("VAR2") == "somethinglike thisbut waitbut waitthere is moreand more!"