Files
cibuildwheel/unit_test/environment_test.py
T

112 lines
3.4 KiB
Python
Raw Normal View History

from __future__ import annotations
2019-11-09 12:16:25 +01:00
import os
2022-07-29 18:52:26 +02:00
import sys
2019-11-12 23:51:27 +00:00
from cibuildwheel.environment import parse_environment
# this command is equivalent to Unix 'echo', but works on Windows too
2022-07-29 18:57:47 +02:00
PYTHON_ECHO = f"'{sys.executable}' -c \"import sys; print(*sys.argv[1:])\""
def test_basic_parsing():
2021-05-03 11:45:43 -04:00
environment_recipe = parse_environment("VAR=1 VBR=2")
2021-04-30 17:56:34 -04:00
environment_dict = environment_recipe.as_dictionary(prev_environment={})
2021-05-03 11:45:43 -04:00
assert environment_dict == {"VAR": "1", "VBR": "2"}
2019-11-12 23:51:27 +00:00
def test_quotes():
2021-05-03 11:45:43 -04:00
environment_recipe = parse_environment("A=1 VAR=\"1 NOT_A_VAR=2\" VBR='vbr'")
2021-04-30 17:56:34 -04:00
environment_dict = environment_recipe.as_dictionary(prev_environment={})
2021-05-03 11:45:43 -04:00
assert environment_dict == {"A": "1", "VAR": "1 NOT_A_VAR=2", "VBR": "vbr"}
2019-11-12 23:51:27 +00:00
def test_inheritance():
2021-05-03 11:45:43 -04:00
environment_recipe = parse_environment("PATH=$PATH:/usr/local/bin")
2021-05-03 11:45:43 -04:00
environment_dict = environment_recipe.as_dictionary(prev_environment={"PATH": "/usr/bin"})
2021-05-03 11:45:43 -04:00
assert environment_dict == {"PATH": "/usr/bin:/usr/local/bin"}
2017-09-01 13:24:12 +01:00
2019-11-12 23:51:27 +00:00
def test_shell_eval():
environment_recipe = parse_environment(f'VAR="$({PYTHON_ECHO} "a test" string)"')
2019-11-09 12:16:25 +01:00
env_copy = os.environ.copy()
2021-05-03 11:45:43 -04:00
env_copy.pop("VAR", None)
2019-11-09 12:16:25 +01:00
2021-04-30 17:56:34 -04:00
environment_dict = environment_recipe.as_dictionary(prev_environment=env_copy)
2021-05-03 11:45:43 -04:00
assert environment_dict["VAR"] == "a test string"
2019-11-12 23:51:27 +00:00
def test_shell_eval_and_env():
environment_recipe = parse_environment(f'VAR="$({PYTHON_ECHO} "$PREV_VAR" string)"')
prev_environment = {**os.environ, "PREV_VAR": "1 2 3"}
environment_dict = environment_recipe.as_dictionary(prev_environment=prev_environment)
assert environment_dict == {**prev_environment, "VAR": "1 2 3 string"}
2019-11-12 23:51:27 +00:00
2017-09-01 13:24:12 +01:00
def test_empty_var():
2021-05-03 11:45:43 -04:00
environment_recipe = parse_environment("CFLAGS=")
2017-09-01 13:24:12 +01:00
2021-05-03 11:45:43 -04:00
environment_dict = environment_recipe.as_dictionary(prev_environment={"CFLAGS": "-Wall"})
2017-09-01 13:24:12 +01:00
2021-05-03 11:45:43 -04:00
assert environment_dict == {"CFLAGS": ""}
2017-09-06 18:35:42 +01:00
2019-11-12 23:51:27 +00:00
2017-09-06 18:35:42 +01:00
def test_no_vars():
2021-05-03 11:45:43 -04:00
environment_recipe = parse_environment("")
2017-09-06 18:35:42 +01:00
environment_dict = environment_recipe.as_dictionary(prev_environment={})
assert environment_dict == {}
2019-11-12 23:51:27 +00:00
2017-09-06 18:35:42 +01:00
def test_no_vars_pass_through():
2021-05-03 11:45:43 -04:00
environment_recipe = parse_environment("")
2017-09-06 18:35:42 +01:00
2021-04-30 17:56:34 -04:00
environment_dict = environment_recipe.as_dictionary(
2021-05-03 11:45:43 -04:00
prev_environment={"CIBUILDWHEEL": "awesome"}
2021-04-30 17:56:34 -04:00
)
2017-09-06 18:35:42 +01:00
2021-05-03 11:45:43 -04:00
assert environment_dict == {"CIBUILDWHEEL": "awesome"}
def test_operators_inside_eval():
environment_recipe = parse_environment(
f'SOMETHING="$({PYTHON_ECHO} a; {PYTHON_ECHO} b; {PYTHON_ECHO} c)"'
)
# pass the existing process env so subcommands can be run in the evaluation
environment_dict = environment_recipe.as_dictionary(prev_environment=os.environ.copy())
2021-05-03 11:45:43 -04:00
assert environment_dict.get("SOMETHING") == "a\nb\nc"
2020-07-12 21:45:46 +01:00
def test_substitution_with_backslash():
environment_recipe = parse_environment('PATH2="somewhere_else;$PATH1"')
2021-05-03 11:45:43 -04:00
environment_dict = environment_recipe.as_dictionary(prev_environment={"PATH1": "c:\\folder\\"})
2020-07-12 21:45:46 +01:00
2021-05-03 11:45:43 -04:00
assert environment_dict.get("PATH2") == "somewhere_else;c:\\folder\\"
def test_awkwardly_quoted_variable():
2021-04-30 17:56:34 -04:00
environment_recipe = parse_environment(
f'VAR2=something"like this""$VAR1"$VAR1$({PYTHON_ECHO} "there is more")"$({PYTHON_ECHO} "and more!")"'
2021-04-30 17:56:34 -04:00
)
prev_environment = {**os.environ, "VAR1": "but wait"}
environment_dict = environment_recipe.as_dictionary(prev_environment=prev_environment)
2021-07-14 13:45:04 -04:00
assert (
environment_dict.get("VAR2") == "somethinglike thisbut waitbut waitthere is moreand more!"
)