Skip to content

Commit

Permalink
Prefer envvar substitutions with curly brackets
Browse files Browse the repository at this point in the history
  • Loading branch information
maresb committed Nov 12, 2024
1 parent eb63c90 commit 85ac898
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
8 changes: 5 additions & 3 deletions conda_lock/models/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def get_or_raise(val: Optional[str]) -> str:
if user_env_var:
res_replaced = res_replaced._replace(
netloc=make_netloc(
username=f"${user_env_var}",
username=f"${{{user_env_var}}}",
password=res_replaced.password,
host=get_or_raise(res_replaced.hostname),
port=res_replaced.port,
Expand All @@ -180,7 +180,7 @@ def get_or_raise(val: Optional[str]) -> str:
res_replaced = res_replaced._replace(
netloc=make_netloc(
username=res_replaced.username,
password=f"${password_env_var}",
password=f"${{{password_env_var}}}",
host=get_or_raise(res_replaced.hostname),
port=res_replaced.port,
)
Expand All @@ -196,7 +196,9 @@ def get_or_raise(val: Optional[str]) -> str:
# Maybe we should raise here if we have mismatched env vars
logger.warning("Token URL detected without env var")
else:
new_path = token_pattern.sub(rf"\1/t/${token_env_var}\3", res_replaced.path)
new_path = token_pattern.sub(
rf"\1/t/${{{token_env_var}}}\3", res_replaced.path
)
res_replaced = res_replaced._replace(path=new_path)

return CondaUrl(
Expand Down
28 changes: 20 additions & 8 deletions tests/test_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,26 @@ def test_url_auth_info(monkeypatch: "MonkeyPatch") -> None:
monkeypatch.setenv("USER", user)
monkeypatch.setenv("PASSWORD", passwd)

# These two urls are equivalent since we can pull the env vars out.
x = _env_var_normalize("http://$USER:$PASSWORD@host/prefix/t/$TOKEN/suffix")
y = _env_var_normalize(f"http://{user}:{passwd}@host/prefix/t/{token}/suffix")

assert x.env_var_url == y.env_var_url

replaced = y.conda_token_replaced_url()
assert replaced == f"http://{user}:{passwd}@host/t/<TOKEN>/prefix/suffix"
# These three urls are equivalent since we can pull the env vars out.
x = _env_var_normalize("http://${USER}:${PASSWORD}@host/prefix/t/${TOKEN}/suffix")
y = _env_var_normalize("http://$USER:$PASSWORD@host/prefix/t/$TOKEN/suffix")
z = _env_var_normalize(f"http://{user}:{passwd}@host/prefix/t/{token}/suffix")

env_var_url = "http://${USER}:${PASSWORD}@host/prefix/t/${TOKEN}/suffix"
assert user not in env_var_url
assert passwd not in env_var_url
assert token not in env_var_url
assert x.env_var_url == env_var_url
assert y.env_var_url == env_var_url
assert z.env_var_url == env_var_url

replaced = f"http://{user}:{passwd}@host/t/<TOKEN>/prefix/suffix"
assert user in replaced
assert passwd in replaced
assert "<TOKEN>" in replaced
assert x.conda_token_replaced_url() == replaced
assert y.conda_token_replaced_url() == replaced
assert z.conda_token_replaced_url() == replaced


@pytest.mark.parametrize(
Expand Down

0 comments on commit 85ac898

Please sign in to comment.