Skip to content

Commit

Permalink
Make Environment= match without value check if given key is in env
Browse files Browse the repository at this point in the history
  • Loading branch information
DaanDeMeyer authored and behrmann committed May 16, 2024
1 parent 2f0739e commit c783b3b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion mkosi/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ def config_match_string(match: str, value: str) -> bool:
def config_match_key_value(match: str, value: dict[str, str]) -> bool:
k, sep, v = match.partition("=")
if not sep:
die(f"{match} is not a key=value pair")
return k in value

return value.get(k, None) == v

Expand Down
3 changes: 2 additions & 1 deletion mkosi/resources/mkosi.md
Original file line number Diff line number Diff line change
Expand Up @@ -1858,7 +1858,8 @@ boolean argument: either `1`, `yes`, or `true` to enable, or `0`, `no`,
`Environment=`

: Matches against a specific key/value pair configured with
`Environment=`.
`Environment=`. If no value is provided, check if the given key is in
the environment regardless of which value it has.

| Matcher | Globs | Rich Comparisons | Default |
|--------------------------|-------|------------------|---------------------------------------|
Expand Down
18 changes: 18 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,24 @@ def test_match_environment(tmp_path: Path) -> None:
_, [conf] = parse_config(["--environment", "MYEN=abd"])
assert conf.image_id != "matched"

(d / "mkosi.conf").write_text(
"""\
[Match]
Environment=MYENV
[Content]
ImageId=matched
"""
)

with chdir(d):
_, [conf] = parse_config(["--environment", "MYENV=abc"])
assert conf.image_id == "matched"
_, [conf] = parse_config(["--environment", "MYENV=abd"])
assert conf.image_id == "matched"
_, [conf] = parse_config(["--environment", "MYEN=abc"])
assert conf.image_id != "matched"


@pytest.mark.parametrize(
"skel,pkgmngr", itertools.product(
Expand Down

0 comments on commit c783b3b

Please sign in to comment.