diff mbox series

[13/17] tests/runqueue: Add a test for RecipeCheckEvent/BB_CACHE_EXTRAVARS

Message ID 20261130152847.3472849-13-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 a test for the new BB_CACHE_EXTRAVARS code and the associated
event. Check the event is triggered and data is passed through to
the event as expected.

Initial code was from Claude, heavily editted.

AI-Generated: Uses Claude (Opus 5.5)
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
---
 .../classes/recipecheckevent.bbclass          | 16 +++++
 lib/bb/tests/runqueue.py                      | 63 +++++++++++++++++--
 2 files changed, 74 insertions(+), 5 deletions(-)
 create mode 100644 lib/bb/tests/runqueue-tests/classes/recipecheckevent.bbclass
diff mbox series

Patch

diff --git a/lib/bb/tests/runqueue-tests/classes/recipecheckevent.bbclass b/lib/bb/tests/runqueue-tests/classes/recipecheckevent.bbclass
new file mode 100644
index 00000000000..ec12724e712
--- /dev/null
+++ b/lib/bb/tests/runqueue-tests/classes/recipecheckevent.bbclass
@@ -0,0 +1,16 @@ 
+#
+# Test helper for BB_CACHE_EXTRAVARS: records the per-recipe values passed to
+# event handlers through bb.runqueue.RecipeCheckEvent as JSON in
+# ${TOPDIR}/extravars.json, keyed by recipe file basename.
+#
+
+addhandler extravars_test_handler
+extravars_test_handler[eventmask] = "bb.runqueue.RecipeCheckEvent"
+python extravars_test_handler() {
+    import json
+    result = {}
+    for fn, values in e.recipedata.items():
+        result[os.path.basename(fn)] = values
+    with open(e.data.expand("${TOPDIR}/extravars.json"), "w") as f:
+        json.dump(result, f, sort_keys=True)
+}
diff --git a/lib/bb/tests/runqueue.py b/lib/bb/tests/runqueue.py
index d8c49f8c0c0..9b4287aaf96 100644
--- a/lib/bb/tests/runqueue.py
+++ b/lib/bb/tests/runqueue.py
@@ -12,13 +12,14 @@  import tempfile
 import subprocess
 import sys
 import time
+import json
 
 #
 # TODO:
 # Add tests on task ordering (X happens before Y after Z)
 #
 
-class RunQueueTests(unittest.TestCase):
+class RunQueueBase(unittest.TestCase):
 
     alltasks = ['package', 'fetch', 'unpack', 'patch', 'prepare_recipe_sysroot', 'configure',
                 'compile', 'install', 'packagedata', 'package_qa', 'package_write_rpm', 'package_write_ipk',
@@ -53,6 +54,13 @@  class RunQueueTests(unittest.TestCase):
                 os.remove(tasklog)
         return tasks
 
+    def shutdown(self, tempdir):
+        # Wait for the hashserve socket to disappear else we'll see races with the tempdir cleanup
+        while (os.path.exists(tempdir + "/hashserve.sock") or os.path.exists(tempdir + "cache/hashserv.db-wal") or os.path.exists(tempdir + "/bitbake.lock")):
+            time.sleep(0.5)
+
+class RunQueueTests(RunQueueBase):
+
     def test_no_setscenevalid(self):
         with tempfile.TemporaryDirectory(prefix="runqueuetest") as tempdir:
             cmd = ["bitbake", "a1"]
@@ -427,8 +435,53 @@  class RunQueueTests(unittest.TestCase):
 
             self.shutdown(tempdir)
 
-    def shutdown(self, tempdir):
-        # Wait for the hashserve socket to disappear else we'll see races with the tempdir cleanup
-        while (os.path.exists(tempdir + "/hashserve.sock") or os.path.exists(tempdir + "cache/hashserv.db-wal") or os.path.exists(tempdir + "/bitbake.lock")):
-            time.sleep(0.5)
+# Test BB_CACHE_EXTRAVARS and RecipeCheckEvent
+class cacheExtraVarsTests(RunQueueBase):
+
+    def trigger_recipecheckevent(self, tempdir, conf, targets):
+        # Helper to write the config, run bitbake and trigger RecipeCheckEvent
+        # returning json data of the event
+        postconf = os.path.join(tempdir, "extravars.conf")
+        with open(postconf, "w") as f:
+            f.write('INHERIT += "recipecheckevent"\n' + conf)
+        result = os.path.join(tempdir, "extravars.json")
+        if os.path.exists(result):
+            os.remove(result)
+        try:
+            output = self.run_bitbakecmd(["bitbake", "-R", postconf] + targets, tempdir, cleanup=True)
+            self.assertTrue(os.path.exists(result), "RecipeCheckEvent handler did not run")
+            with open(result) as f:
+                return json.load(f)
+        finally:
+            if os.path.exists(result):
+                os.remove(result)
+
+    def test_cache_extravars(self):
+        # Check variables listed in BB_CACHE_EXTRAVARS are stored in the recipe
+        # cache and passed to RecipeCheckEvent.
+
+        conf = 'OVERRIDES = "pn-${PN}"\n' \
+               'BB_CACHE_EXTRAVARS = "EXTRAVAR_A EXTRAVAR_B EXTRAVAR_UNSET"\n' \
+               'EXTRAVAR_A = "default"\n' \
+               'EXTRAVAR_A:pn-a1 = "a1-value"\n' \
+               'EXTRAVAR_B = "${PN}-b"\n' \
+               'EXTRAVAR_UNLISTED = "should-not-appear"\n'
+        expected = {
+            "a1.bb": {"EXTRAVAR_A": "a1-value", "EXTRAVAR_B": "a1-b", "EXTRAVAR_UNSET": ""},
+            "b1.bb": {"EXTRAVAR_A": "default", "EXTRAVAR_B": "b1-b", "EXTRAVAR_UNSET": ""},
+        }
+        with tempfile.TemporaryDirectory(prefix="runqueuetest") as tempdir:
+            # b1 depends on a1, so both expected
+            data = self.trigger_recipecheckevent(tempdir, conf, ["b1"])
+            self.assertEqual(data, expected)
+
+            self.shutdown(tempdir)
+
+    def test_cache_extravars_unset(self):
+        # Without BB_CACHE_EXTRAVARS the event still fires, with no values
+        with tempfile.TemporaryDirectory(prefix="runqueuetest") as tempdir:
+            data = self.trigger_recipecheckevent(tempdir, "", ["a1"])
+            self.assertEqual(data, {"a1.bb": {}})
+
+            self.shutdown(tempdir)