]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Check if list matches are empty if empty string is matched against
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Sat, 25 Jan 2025 17:17:53 +0000 (18:17 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Sat, 25 Jan 2025 17:26:11 +0000 (18:26 +0100)
If we do something like

```
[Match]
Profiles=

...
```

It should succeed if the list of profiles is empty, so let's implement that.

mkosi/config.py
tests/test_config.py

index c918cde584ee2c68c6bd959143714dde58a59e67..e6eb8818fbc51145a47217a5b04d4afaebca7cae 100644 (file)
@@ -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}]")
index 7da3fb5af66a93fd9a65820c9a9ebf06ecb01fa3..c47918826dcc4a9ad8d587149757390d164d07aa 100644 (file)
@@ -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):