From cccc2acae9c746839df9c0006a34f9df4f79af99 Mon Sep 17 00:00:00 2001 From: jack1142 <6032823+jack1142@users.noreply.github.com> Date: Fri, 29 Jul 2022 18:04:27 +0200 Subject: [PATCH] Use an equivalent of Unix echo written in Python --- test/test_environment.py | 3 ++- unit_test/environment_test.py | 33 ++++++++++----------------------- 2 files changed, 12 insertions(+), 24 deletions(-) diff --git a/test/test_environment.py b/test/test_environment.py index d2b75f0a..079081ce 100644 --- a/test/test_environment.py +++ b/test/test_environment.py @@ -33,6 +33,7 @@ project_with_environment_asserts = test_projects.new_c_project( def test(tmp_path): + python_echo = 'python -c "import sys; print(*sys.argv[1:])"' project_dir = tmp_path / "project" project_with_environment_asserts.generate(project_dir) @@ -43,7 +44,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="$(cmd /C 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 8c67b259..696dff63 100644 --- a/unit_test/environment_test.py +++ b/unit_test/environment_test.py @@ -1,8 +1,9 @@ import os -import sys from cibuildwheel.environment import parse_environment +PYTHON_ECHO = 'python -c "import sys; print(*sys.argv[1:])"' + def test_basic_parsing(): environment_recipe = parse_environment("VAR=1 VBR=2") @@ -29,11 +30,7 @@ def test_inheritance(): def test_shell_eval(): - if sys.platform == "win32": - env_string = 'VAR="$(cmd /C "echo a test string")"' - else: - env_string = 'VAR="$(echo "a test" string)"' - environment_recipe = parse_environment(env_string) + environment_recipe = parse_environment(f'VAR="$({PYTHON_ECHO} "a test" string)"') env_copy = os.environ.copy() env_copy.pop("VAR", None) @@ -44,11 +41,7 @@ def test_shell_eval(): def test_shell_eval_and_env(): - if sys.platform == "win32": - env_string = 'VAR="$(cmd /C "echo $PREV_VAR string")"' - else: - env_string = 'VAR="$(echo "$PREV_VAR" string)"' - environment_recipe = parse_environment(env_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"}) @@ -82,11 +75,9 @@ def test_no_vars_pass_through(): def test_operators_inside_eval(): - if sys.platform == "win32": - env_string = 'SOMETHING="$(cmd /C echo a; cmd /C echo b; cmd /C echo c)"' - else: - env_string = 'SOMETHING="$(echo a; echo b; echo c)"' - environment_recipe = parse_environment(env_string) + 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()) @@ -104,13 +95,9 @@ def test_substitution_with_backslash(): def test_awkwardly_quoted_variable(): - if sys.platform == "win32": - env_string = 'VAR2=something"like this""$VAR1"$VAR1$(cmd /C echo there is more)"$(cmd /C echo and more!)"' - else: - env_string = ( - 'VAR2=something"like this""$VAR1"$VAR1$(echo "there is more")"$(echo "and more!")"' - ) - environment_recipe = parse_environment(env_string) + environment_recipe = parse_environment( + 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"})