]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Always set distribution in tests
authorPaul Meyer <49727155+katexochen@users.noreply.github.com>
Mon, 11 Sep 2023 15:59:29 +0000 (17:59 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 18 Sep 2023 12:19:53 +0000 (14:19 +0200)
This enables executing the tests on distributions that aren't
targeted by mkosi. Before, the tests would fail as the detected
distribution was None.

Co-authored-by: Malte Poll <mp@edgeless.systems>
mkosi/config.py
tests/test_config.py

index f5c61e08bc991505abec328efada913536c62cec..13c30fca7c6e87f328ecf62de488319dd2cd8884 100644 (file)
@@ -292,6 +292,15 @@ def config_default_compression(namespace: argparse.Namespace) -> Compression:
         return Compression.none
 
 
+def config_default_distribution(namespace: argparse.Namespace) -> Distribution:
+    detected = detect_distribution()[0]
+
+    if not detected:
+        die("Distribution of your host can't be detected or isn't a supported target. Please set Distribution= in your config.")
+
+    return detected
+
+
 def config_default_release(namespace: argparse.Namespace) -> str:
     # If the configured distribution matches the host distribution, use the same release as the host.
     hd, hr = detect_distribution()
@@ -899,7 +908,7 @@ SETTINGS = (
         section="Distribution",
         parse=config_make_enum_parser(Distribution),
         match=config_make_enum_matcher(Distribution),
-        default=detect_distribution()[0],
+        default_factory=config_default_distribution,
         choices=Distribution.values(),
         help="Distribution to install",
     ),
index 0250da1f637954d71ae20a5f1acb1653a60c791d..082759c7e361ea62b7aaf54fa4067240fab3cab2 100644 (file)
@@ -171,6 +171,7 @@ def test_parse_config(tmp_path: Path) -> None:
 
 
 def test_parse_load_verb(tmp_path: Path) -> None:
+    (tmp_path / "mkosi.conf").write_text("[Distribution]\nDistribution=fedora")
     with chdir(tmp_path):
         assert parse_config(["build"])[0].verb == Verb.build
         assert parse_config(["clean"])[0].verb == Verb.clean
@@ -209,8 +210,24 @@ def test_parse_config_files_filter(tmp_path: Path) -> None:
         confd = Path("mkosi.conf.d")
         confd.mkdir()
 
-        (confd / "10-file.conf").write_text("[Content]\nPackages=yes")
-        (confd / "20-file.noconf").write_text("[Content]\nPackages=nope")
+        (confd / "10-file.conf").write_text(
+            """\
+            [Distribution]
+            Distribution=fedora
+
+            [Content]
+            Packages=yes
+            """
+        )
+        (confd / "20-file.noconf").write_text(
+            """\
+            [Distribution]
+            Distribution=fedora
+
+            [Content]
+            Packages=yes
+            """
+        )
 
         _, [config] = parse_config()
         assert config.packages == ["yes"]
@@ -218,7 +235,7 @@ def test_parse_config_files_filter(tmp_path: Path) -> None:
 
 def test_compression(tmp_path: Path) -> None:
     with chdir(tmp_path):
-        _, [config] = parse_config(["--format", "disk", "--compress-output", "False"])
+        _, [config] = parse_config(["--format", "disk", "--compress-output", "False", "--distribution", "fedora"])
         assert config.compress_output == Compression.none
 
 
@@ -424,6 +441,7 @@ def test_match_imageversion(tmp_path: Path, op: str, version: str) -> None:
         parent.write_text(
             """\
             [Distribution]
+            Distribution=fedora
             ImageId=testimage
             ImageVersion=123
             """
@@ -479,11 +497,24 @@ def test_package_manager_tree(tmp_path: Path, skel: Optional[Path], pkgmngr: Opt
     with chdir(tmp_path):
         config = Path("mkosi.conf")
         with config.open("w") as f:
-            f.write("[Content]\n")
+            f.write(
+                """
+                [Distribution]
+                Distribution=fedora
+
+                [Content]
+                """
+            )
             if skel is not None:
-                f.write(f"SkeletonTrees={skel}\n")
+                f.write(f"""
+                SkeletonTrees={skel}
+                """
+            )
             if pkgmngr is not None:
-                f.write(f"PackageManagerTrees={pkgmngr}\n")
+                f.write(f"""
+                PackageManagerTrees={pkgmngr}
+                """
+            )
 
         _, [conf] = parse_config()