diff mbox series

[17/17] tests/layers: Add fragments tests

Message ID 20261130152847.3472849-17-richard.purdie@linuxfoundation.org
State New
Headers show
Series [01/17] uihelper: Fix exception when TaskStarted event handler breaks | expand

Commit Message

Richard Purdie Nov. 30, 2026, 3:28 p.m. UTC
Add some tests to ensure the addfragment behaviour we want is tested.

AI-Generated: Uses Claude (Opus 5.5)
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
---
 lib/bb/tests/layers.py | 81 ++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 78 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/lib/bb/tests/layers.py b/lib/bb/tests/layers.py
index 70767b50b6d..c55066fe109 100644
--- a/lib/bb/tests/layers.py
+++ b/lib/bb/tests/layers.py
@@ -19,9 +19,9 @@  import bb.utils
 
 
 class _LayerBuilder(bb.cookerdata.CookerDataBuilder):
-    def __init__(self, topdir):
+    def __init__(self, topdir, skip_fragments=True):
         cfg = types.SimpleNamespace(prefile=[], postfile=[], tracking=False,
-                                    skip_fragments=True, env={})
+                                    skip_fragments=skip_fragments, env={})
         super().__init__(cfg)
         self.topdir = topdir
 
@@ -30,6 +30,8 @@  class _LayerBuilder(bb.cookerdata.CookerDataBuilder):
 
 
 class LayerTestBase(unittest.TestCase):
+    skip_fragments = True
+
     CORE_CONF = """\
 LAYERNAME = "core"
 LAYERSERIES_CORENAMES = "series1 series2"
@@ -75,7 +77,7 @@  LAYER_PASSTHROUGH_VARS_PREFIXES = "PASSVAR"
     def parse(self):
         self.write("conf/bblayers.conf",
                    'BBLAYERS = "%s"\n%s' % (" ".join(self.layers), self.bblayers_extra))
-        builder = _LayerBuilder(self.topdir)
+        builder = _LayerBuilder(self.topdir, self.skip_fragments)
         return builder.parseConfigurationFiles([], [])
 
     def parse_fails(self, *expected):
@@ -354,6 +356,79 @@  class LayerVarsConfTest(LayerTestBase):
         self.assertIsNone(dc[""].getVar("FROM_LAYERVARS"))
 
 
+class FragmentsTest(LayerTestBase):
+    skip_fragments = False
+
+    # Mirrors the way OE-Core uses addfragments: it is called once before the
+    # distro configuration (which a builtin fragment can select) and again
+    # afterwards so the distro can enable fragments too
+    BITBAKE_CONF = """\
+CACHE = "${TOPDIR}/cache"
+FRAGS_METADATA_VARS = "FRAG_SUMMARY"
+FRAGS_BUILTIN = "distro:DISTRO"
+addfragments conf/fragments FRAGMENTS FRAGS_METADATA_VARS FRAGS_BUILTIN
+include conf/distro/${DISTRO}.conf
+addfragments conf/fragments FRAGMENTS FRAGS_METADATA_VARS FRAGS_BUILTIN
+"""
+
+    def setUp(self):
+        super().setUp()
+        self.write("core/conf/bitbake.conf", self.BITBAKE_CONF)
+        self.write("core/conf/fragments/a.conf",
+                   'FRAG_SUMMARY = "Fragment a"\nFRAG_A = "set"\nCOUNT += "a"\n')
+        self.write("core/conf/fragments/b.conf",
+                   'FRAG_SUMMARY = "Fragment b"\nFRAG_B = "set"\nCOUNT += "b"\n')
+        self.write("core/conf/distro/mydistro.conf",
+                   'DISTRO_SET = "1"\nFRAGMENTS += "core/a"\n')
+
+    def test_distro_fragment_applied(self):
+        self.bblayers_extra = 'DISTRO = "mydistro"\n'
+        d, _ = self.parse()
+        self.assertEqual(d.getVar("DISTRO_SET"), "1")
+        self.assertEqual(d.getVar("FRAG_A"), "set")
+        self.assertEqual(d.getVarFlag("FRAG_SUMMARY", "core/a"), "Fragment a")
+        self.assertIsNone(d.getVar("FRAG_SUMMARY"))
+
+    def test_fragments_applied_once(self):
+        # distro/mydistro and core/b are seen by both addfragments calls, and
+        # core/a is enabled by both the user and the distro
+        self.bblayers_extra = 'FRAGMENTS = "distro/mydistro core/b core/a"\n'
+        d, _ = self.parse()
+        self.assertEqual(d.getVar("DISTRO"), "mydistro")
+        self.assertEqual(d.getVar("DISTRO_SET"), "1")
+        self.assertEqual(d.getVar("COUNT").split(), ["b", "a"])
+
+    def test_missing_distro_fragment_fatal(self):
+        self.write("core/conf/distro/mydistro.conf", 'FRAGMENTS += "core/missing"\n')
+        self.bblayers_extra = 'DISTRO = "mydistro"\n'
+        with self.assertLogs("BitBake", level="ERROR") as logs:
+            with self.assertRaises(bb.BBHandledException):
+                self.parse()
+        self.assertIn("core/missing", "\n".join(logs.output))
+
+    def test_recommendation_satisfied_by_distro_fragment(self):
+        self.simple_layer("other", 'LAYER_FRAGMENT_RECOMMENDS_ANY = "core/a"\n')
+        self.bblayers_extra = 'DISTRO = "mydistro"\n'
+        with self.assertNoLogs("BitBake", level="WARNING"):
+            self.parse()
+
+    def test_recommendation_warned_once(self):
+        self.simple_layer("other", 'LAYER_FRAGMENT_RECOMMENDS_ANY = "core/b"\n')
+        self.bblayers_extra = 'DISTRO = "mydistro"\n'
+        with self.assertLogs("BitBake", level="WARNING") as logs:
+            self.parse()
+        warnings = [l for l in logs.output if "recommends one of the fragments" in l]
+        self.assertEqual(len(warnings), 1, logs.output)
+
+    def test_recommendation_not_checked_when_skipping_fragments(self):
+        self.skip_fragments = True
+        self.simple_layer("other", 'LAYER_FRAGMENT_RECOMMENDS_ANY = "core/b"\n')
+        self.bblayers_extra = 'DISTRO = "mydistro"\n'
+        with self.assertNoLogs("BitBake", level="WARNING"):
+            d, _ = self.parse()
+        self.assertIsNone(d.getVar("FRAG_A"))
+
+
 class LayerVarsDefaultPreferenceTest(LayerTestBase):
     """
     End-to-end check that DEFAULT_PREFERENCE set in a layer's