From: Daan De Meyer Date: Thu, 16 May 2024 14:53:36 +0000 (+0200) Subject: Make Environment= match without value check if given key is in env X-Git-Tag: v23.1~49 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c783b3b0718293387aa5c2b438fa17eebb84142b;p=thirdparty%2Fmkosi.git Make Environment= match without value check if given key is in env --- diff --git a/mkosi/config.py b/mkosi/config.py index f58b2ca8f..78321248f 100644 --- a/mkosi/config.py +++ b/mkosi/config.py @@ -587,7 +587,7 @@ def config_make_string_matcher(allow_globs: bool = False) -> ConfigMatchCallback 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 diff --git a/mkosi/resources/mkosi.md b/mkosi/resources/mkosi.md index b76f8622d..a67cbacc6 100644 --- a/mkosi/resources/mkosi.md +++ b/mkosi/resources/mkosi.md @@ -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 | |--------------------------|-------|------------------|---------------------------------------| diff --git a/tests/test_config.py b/tests/test_config.py index 8d5888871..e7cea3879 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -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(