-
-
Notifications
You must be signed in to change notification settings - Fork 643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
extra_env_vars support fnmatch globs #21781
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
from __future__ import annotations | ||
|
||
import fnmatch | ||
import re | ||
from dataclasses import dataclass | ||
from typing import Dict, Optional, Sequence | ||
|
@@ -53,7 +54,8 @@ def check_and_set(name: str, value: Optional[str]): | |
if name_value_match: | ||
check_and_set(name_value_match[1], name_value_match[2]) | ||
elif shorthand_re.match(env_var): | ||
check_and_set(env_var, self.get(env_var)) | ||
for name, value in self.get_or_match(env_var).items(): | ||
check_and_set(name, value) | ||
else: | ||
raise ValueError( | ||
f"An invalid variable was requested via the --test-extra-env-var " | ||
|
@@ -62,6 +64,12 @@ def check_and_set(name: str, value: Optional[str]): | |
|
||
return FrozenDict(env_var_subset) | ||
|
||
def get_or_match(self, name_or_pattern: str) -> dict[str, str]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Totally minor, but given this is just used as a if value := self.get(name_or_pattern):
yield name_or_pattern, value
for k, v in self.items():
if fnmatch.fnmatch(k, name_or_pattern):
yield k, v |
||
"""Get the value of an envvar if it has an exact match, otherwise all fnmatches.""" | ||
if name_or_pattern in self: | ||
return {name_or_pattern: self.get(name_or_pattern)} | ||
return {k: v for k, v in self.items() if fnmatch.fnmatch(k, name_or_pattern)} | ||
|
||
|
||
@dataclass(frozen=True) | ||
class EnvironmentVarsRequest: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,3 +37,20 @@ def test_invalid_variable() -> None: | |
"An invalid variable was requested via the --test-extra-env-var mechanism: 3INVALID" | ||
in str(exc) | ||
) | ||
|
||
|
||
def test_envvar_fnmatch() -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add a test of the exact-matching behaviour? E.g. include an env variable |
||
"""Test fnmatch patterns correctly pull in all matching envvars.""" | ||
|
||
pants_env = CompleteEnvironmentVars( | ||
{ | ||
"LETTER_C": "prefix_char_match", | ||
"LETTER_PI": "prefix", | ||
} | ||
) | ||
|
||
char_match = pants_env.get_subset(["LETTER_?"]) | ||
assert char_match == {"LETTER_C": "prefix_char_match"} | ||
|
||
multichar_match = pants_env.get_subset(["LETTER_*"]) | ||
assert multichar_match == {"LETTER_C": "prefix_char_match", "LETTER_PI": "prefix"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think many targets have
extra_env_vars
fields too, so maybe: