From: Daan De Meyer Date: Sat, 25 Jan 2025 17:17:53 +0000 (+0100) Subject: Check if list matches are empty if empty string is matched against X-Git-Tag: v25.3~16^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4d0808238b4cbb2e2a0bebba1147239e9eaefdc9;p=thirdparty%2Fmkosi.git Check if list matches are empty if empty string is matched against If we do something like ``` [Match] Profiles= ... ``` It should succeed if the list of profiles is empty, so let's implement that. --- diff --git a/mkosi/config.py b/mkosi/config.py index c918cde58..e6eb8818f 100644 --- a/mkosi/config.py +++ b/mkosi/config.py @@ -738,6 +738,9 @@ def config_match_build_sources(match: str, value: list[ConfigTree]) -> bool: def config_make_list_matcher(parse: Callable[[str], T]) -> ConfigMatchCallback[list[T]]: def config_match_list(match: str, value: list[T]) -> bool: + if not match: + return len(value) == 0 + return parse(match) in value return config_match_list @@ -4255,9 +4258,6 @@ class ParseContext: v = self.expand_specifiers(v, path) - if not v: - die("Match value cannot be empty") - if s := SETTINGS_LOOKUP_BY_NAME.get(k): if not s.match: die(f"{k} cannot be used in [{section}]") diff --git a/tests/test_config.py b/tests/test_config.py index 7da3fb5af..c47918826 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -696,6 +696,27 @@ def test_match_multiple(tmp_path: Path) -> None: assert config.image_id != "abcde" +def test_match_empty(tmp_path: Path) -> None: + with chdir(tmp_path): + Path("mkosi.conf").write_text( + """\ + [Match] + Profiles= + + [Build] + Environment=ABC=QED + """ + ) + + _, [config] = parse_config([]) + + assert config.environment.get("ABC") == "QED" + + _, [config] = parse_config(["--profile", "profile"]) + + assert config.environment.get("ABC") is None + + @pytest.mark.parametrize("dist1,dist2", itertools.combinations_with_replacement(Distribution, 2)) def test_match_distribution(tmp_path: Path, dist1: Distribution, dist2: Distribution) -> None: with chdir(tmp_path):