Skip to contentSkip to conversation

patchtest: cleanups and fixes, part 1

PATCH Superseded Trevor Gamblin 2 participants 8 messages May 26 – May 28

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover patchtest: cleanups and fixes, part 1

This is the first split of patches coming from a previous series[1] I submitted. Compared to that version, the changes here are mostly refactors to do the following:

- Make sure that patchtest errors early if the user tries to run it without the meta-selftest layer enabled
- Improve the signal-handling logic when the --directory flag is used in a test run
- Encapsulate some bits (like package name fetching) into functions which might be re-used elsewhere
- Improve function and variable naming
- remove unnecessary code where possible
- Fix vague comments and typos/grammatical issues

Note that this was driven partially by Claude Code (Sonnet 4.6 and 4.7), but I've gone back and reworked some of its changes before submission.

Selftests look OK after these changes:

|============================================================================
|Testsuite summary for patchtest
|============================================================================
|# TOTAL: 38
|# XPASS: 18
|# XFAIL: 18
|# XSKIP: 2
|# PASS: 0
|# FAIL: 0
|# SKIP: 0
|# ERROR: 0
|============================================================================

[1]: https://lists.openembedded.org/g/openembedded-core/message/237047

Trevor Gamblin (6):
patchtest: refactor and simplify script
patchtest: check for meta-selftest
patchtest: tests: cleanup test suites
patchtest: correctly abort --directory test
patchtest: tests: base: add helper functions, cleanup
patchtest: tests: test_metadata: cleanup

 meta/lib/patchtest/patchtest_parser.py        |   2 +-
 meta/lib/patchtest/repo.py                    |   4 +
 meta/lib/patchtest/tests/base.py              | 116 ++++-------
 meta/lib/patchtest/tests/test_mbox.py         |  10 +-
 meta/lib/patchtest/tests/test_metadata.py     |  88 ++++----
 meta/lib/patchtest/tests/test_patch.py        |  33 ++-
 .../lib/patchtest/tests/test_python_pylint.py |   8 +-
 scripts/patchtest                             | 197 +++++++++---------
 8 files changed, 206 insertions(+), 252 deletions(-)
Signature · 1 line
2.54.0
#1 lore raw

PATCH 1/6 patchtest: refactor and simplify script

Remove a variety of unused code blocks and inconsistent formatting, then simplify the results-generation logic. This includes removing the 'finally:' block which was never actually reached.

AI-Generated: Uses Claude Code

---
 scripts/patchtest | 159 ++++++++++++++++++----------------------------
 1 file changed, 62 insertions(+), 97 deletions(-)
scripts/patchtest +62 -97
index 143cf08572..6dbb5adb15 100755
@@ -12,6 +12,7 @@
 import json
 import logging
 import os
+import subprocess
 import sys
 import traceback
 import unittest
@@ -30,37 +31,50 @@ loggerhandler = logging.StreamHandler()
 loggerhandler.setFormatter(logging.Formatter("%(message)s"))
 logger.addHandler(loggerhandler)
 logger.setLevel(logging.INFO)
-info = logger.info
-error = logger.error
 
-def getResult(patch, mergepatch, logfile=None):
+
+def _format_test_description(test):
+    return (test.id().split('.')[-1]
+            .replace('_', ' ')
+            .replace("cve", "CVE")
+            .replace("signed off by", "Signed-off-by")
+            .replace("upstream status", "Upstream-Status")
+            .replace("non auh", "non-AUH")
+            .replace("presence format", "presence"))
+
+
+def _emit(line, logfile=None):
+    print(line)
+    if logfile:
+        with open(logfile, "a") as f:
+            f.write(line + "\n")
+
+
+def make_result_class(patch, mergepatch, logfile=None):
 
     class PatchTestResult(unittest.TextTestResult):
         """ Patchtest TextTestResult """
         shouldStop  = True
         longMessage = False
 
-        success     = 'PASS'
-        fail        = 'FAIL'
-        skip        = 'SKIP'
+        success = 'PASS'
+        fail    = 'FAIL'
+        skip    = 'SKIP'
 
         def startTestRun(self):
-            # let's create the repo already, it can be used later on
-            repoargs = {
-                "repodir": PatchtestParser.repodir,
-                "commit": PatchtestParser.basecommit,
-                "branch": PatchtestParser.basebranch,
-                "patch": patch,
-            }
-
-            self.repo_error    = False
-            self.test_error    = False
-            self.test_failure  = False
+            self.repo_error   = False
+            self.test_error   = False
+            self.test_failure = False
 
             try:
-                self.repo = PatchtestParser.repo = PatchTestRepo(**repoargs)
-            except:
-                logger.error(traceback.print_exc())
+                self.repo = PatchtestParser.repo = PatchTestRepo(
+                    patch=patch,
+                    repodir=PatchtestParser.repodir,
+                    commit=PatchtestParser.basecommit,
+                    branch=PatchtestParser.basebranch,
+                )
+            except Exception:
+                traceback.print_exc()
                 self.repo_error = True
                 self.stop()
                 return
@@ -70,100 +84,59 @@ def getResult(patch, mergepatch, logfile=None):
 
         def addError(self, test, err):
             self.test_error = True
-            (ty, va, trace) = err
-            logger.error(traceback.print_exc())
+            traceback.print_exc()
 
         def addFailure(self, test, err):
-            test_description = test.id().split('.')[-1].replace('_', ' ').replace("cve", "CVE").replace("signed off by",
-            "Signed-off-by").replace("upstream status",
-            "Upstream-Status").replace("non auh",
-            "non-AUH").replace("presence format", "presence")
             self.test_failure = True
-            fail_str = '{}: {}: {} ({})'.format(self.fail,
-            test_description, json.loads(str(err[1]))["issue"],
-            test.id())
-            print(fail_str)
-            if logfile:
-                with open(logfile, "a") as f:
-                    f.write(fail_str + "\n")
+            desc = _format_test_description(test)
+            issue = json.loads(str(err[1]))["issue"]
+            _emit('{}: {}: {} ({})'.format(self.fail, desc, issue, test.id()), logfile)
 
         def addSuccess(self, test):
-            test_description = test.id().split('.')[-1].replace('_', ' ').replace("cve", "CVE").replace("signed off by",
-            "Signed-off-by").replace("upstream status",
-            "Upstream-Status").replace("non auh",
-            "non-AUH").replace("presence format", "presence")
-            success_str = '{}: {} ({})'.format(self.success,
-            test_description, test.id())
-            print(success_str)
-            if logfile:
-                with open(logfile, "a") as f:
-                    f.write(success_str + "\n")
+            desc = _format_test_description(test)
+            _emit('{}: {} ({})'.format(self.success, desc, test.id()), logfile)
 
         def addSkip(self, test, reason):
-            test_description = test.id().split('.')[-1].replace('_', ' ').replace("cve", "CVE").replace("signed off by",
-            "Signed-off-by").replace("upstream status",
-            "Upstream-Status").replace("non auh",
-            "non-AUH").replace("presence format", "presence")
-            skip_str = '{}: {}: {} ({})'.format(self.skip,
-            test_description, json.loads(str(reason))["issue"],
-            test.id())
-            print(skip_str)
-            if logfile:
-                with open(logfile, "a") as f:
-                    f.write(skip_str + "\n")
+            desc = _format_test_description(test)
+            issue = json.loads(str(reason))["issue"]
+            _emit('{}: {}: {} ({})'.format(self.skip, desc, issue, test.id()), logfile)
 
         def stopTestRun(self):
-
-            # in case there was an error on repo object creation, just return
-            if self.repo_error:
-                return
-
-            self.repo.clean()
+            if not self.repo_error:
+                self.repo.clean()
 
     return PatchTestResult
 
 def _runner(resultklass, prefix=None):
-    # load test with the corresponding prefix
     loader = unittest.TestLoader()
     if prefix:
         loader.testMethodPrefix = prefix
 
-    # create the suite with discovered tests and the corresponding runner
     suite = loader.discover(
         start_dir=PatchtestParser.testdir,
         pattern=PatchtestParser.pattern,
         top_level_dir=PatchtestParser.topdir,
     )
-    ntc = suite.countTestCases()
 
-    # if there are no test cases, just quit
-    if not ntc:
+    if not suite.countTestCases():
         return 2
-    runner = unittest.TextTestRunner(resultclass=resultklass, verbosity=0)
 
+    runner = unittest.TextTestRunner(resultclass=resultklass, verbosity=0)
     try:
         result = runner.run(suite)
-    except:
-        logger.error(traceback.print_exc())
+    except Exception:
+        traceback.print_exc()
         logger.error('patchtest: something went wrong')
         return 1
-    if result.test_failure or result.test_error:
-        return 1 
 
-    return 0
+    return 1 if (result.test_failure or result.test_error) else 0
 
 def run(patch, logfile=None):
     """ Load, setup and run pre and post-merge tests """
-    # Get the result class and install the control-c handler
     unittest.installHandler()
 
-    # run pre-merge tests, meaning those methods with 'pretest' as prefix
-    premerge_resultklass = getResult(patch, False, logfile)
-    premerge_result = _runner(premerge_resultklass, 'pretest')
-
-    # run post-merge tests, meaning those methods with 'test' as prefix
-    postmerge_resultklass = getResult(patch, True, logfile)
-    postmerge_result = _runner(postmerge_resultklass, 'test')
+    premerge_result = _runner(make_result_class(patch, False, logfile), 'pretest')
+    postmerge_result = _runner(make_result_class(patch, True, logfile), 'test')
 
     print_result_message(premerge_result, postmerge_result)
     return premerge_result or postmerge_result
@@ -183,24 +156,23 @@ def print_result_message(preresult, postresult):
     print("----------------------------------------------------------------------\n")
 
 def main():
-    ret = 0
-    tmp_patch = False
     patch_path = PatchtestParser.patch_path
-    log_results = PatchtestParser.log_results
-    log_path = None
-    patch_list = None
 
-    git_status = os.popen("(cd %s && git status)" % PatchtestParser.repodir).read()
+    git_status = subprocess.run(
+        ['git', '-C', PatchtestParser.repodir, 'status'],
+        capture_output=True, text=True,
+    ).stdout
     status_matches = ["Changes not staged for commit", "Changes to be committed"]
-    if any([match in git_status for match in status_matches]):
+    if any(match in git_status for match in status_matches):
         logger.error("patchtest: there are uncommitted changes in the target repo that would be overwritten. Please commit or restore them before running patchtest")
         return 1
 
     if os.path.isdir(patch_path):
-        patch_list = [os.path.join(patch_path, filename) for filename in sorted(os.listdir(patch_path))]
+        patch_list = [os.path.join(patch_path, f) for f in sorted(os.listdir(patch_path))]
     else:
         patch_list = [patch_path]
 
+    ret = 0
     for patch in patch_list:
         if os.path.getsize(patch) == 0:
             logger.error('patchtest: patch is empty')
@@ -208,19 +180,13 @@ def main():
 
         logger.info('Testing patch %s' % patch)
 
-        if log_results:
+        log_path = None
+        if PatchtestParser.log_results:
             log_path = patch + ".testresult"
             with open(log_path, "a") as f:
                 f.write("Patchtest results for patch '%s':\n\n" % patch)
 
-        try:
-            if log_path:
-                ret = run(patch, log_path)
-            else:
-                ret = run(patch)
-        finally:
-            if tmp_patch:
-                os.remove(patch)
+        ret = run(patch, log_path)
 
     return ret
 
@@ -241,7 +207,6 @@ if __name__ == '__main__':
     try:
         ret = main()
     except Exception:
-        import traceback
         traceback.print_exc(5)
 
     sys.exit(ret)
Signature · 1 line
2.54.0
#2 lore raw

Re: [OE-core][PATCH 1/6] patchtest: refactor and simplify script

Quoted 3 lines · Trevor Gamblin via lists.openembedded.org

Remove a variety of unused code blocks and inconsistent formatting, then simplify the results-generation logic. This includes removing the 'finally:' block which was never actually reached.

Sorry Trevor but there is way too much going on in this patch. It needs splitting into more logical units/changes rather than being lumped all together.

Cheers,

Richard

#3 lore raw

PATCH 2/6 patchtest: check for meta-selftest

In reply to Trevor Gamblin

The patchtest suite (in particular, the selftests) makes use of the meta-selftest layer for full operation. Make sure that the layer is added to bblayers.conf before kicking off any test runs.

---
 scripts/patchtest | 11 +++++++++++
 1 file changed, 11 insertions(+)
scripts/patchtest +11 -0
index 6dbb5adb15..e8ace03905 100755
@@ -167,6 +167,17 @@ def main():
         logger.error("patchtest: there are uncommitted changes in the target repo that would be overwritten. Please commit or restore them before running patchtest")
         return 1
 
+    builddir = os.environ.get('BUILDDIR')
+    if builddir:
+        bblayers_conf = os.path.join(builddir, 'conf', 'bblayers.conf')
+        if os.path.exists(bblayers_conf):
+            with open(bblayers_conf) as f:
+                if 'meta-selftest' not in f.read():
+                    logger.error(
+                        "patchtest: meta-selftest layer not found in %s - add it to BBLAYERS before running patchtest" % bblayers_conf
+                    )
+                    return 1
+
     if os.path.isdir(patch_path):
         patch_list = [os.path.join(patch_path, f) for f in sorted(os.listdir(patch_path))]
     else:
Signature · 1 line
2.54.0
#4 lore raw

PATCH 3/6 patchtest: tests: cleanup test suites

In reply to Trevor Gamblin

Do a sweep of the test suites to remove redundant code, make variable names more explicit, and fix some formatting. Some logic (such as the bits for checking valid Upstream-Status tags) are also moved directly to the relevant functions, instead of being left in the setup stage. This should make maintenance easier going forward.

AI-Generated: Uses Claude Code

---
 meta/lib/patchtest/tests/base.py              |  4 ---
 meta/lib/patchtest/tests/test_mbox.py         | 10 +++---
 meta/lib/patchtest/tests/test_metadata.py     | 32 ++++++------------
 meta/lib/patchtest/tests/test_patch.py        | 33 ++++++++-----------
 .../lib/patchtest/tests/test_python_pylint.py |  8 ++---
 5 files changed, 33 insertions(+), 54 deletions(-)
meta/lib/patchtest/tests/base.py +0 -4
index 919ca136bb..8263932dda 100644
@@ -22,10 +22,6 @@ info = logger.info
 warn = logger.warn
 error = logger.error
 
-Commit = collections.namedtuple(
-    "Commit", ["author", "subject", "commit_message", "shortlog", "payload"]
-)
-
 Commit = collections.namedtuple('Commit', ['author', 'subject', 'commit_message', 'shortlog', 'payload'])
 
 class PatchtestOEError(Exception):
meta/lib/patchtest/tests/test_mbox.py +5 -5
index 5cf02af7bd..feb87fa2ae 100644
@@ -74,11 +74,11 @@ class TestMbox(base.Base):
             shortlog = re.sub(r'^(\[.*?\])+ ', '', commit.shortlog)
             if shortlog.startswith('Revert "'):
                 continue
-            l = len(shortlog)
-            if l > patchtest_patterns.mbox_shortlog_maxlength:
+            length = len(shortlog)
+            if length > patchtest_patterns.mbox_shortlog_maxlength:
                 self.fail(
                     "Edit shortlog so that it is %d characters or less (currently %d characters)"
-                    % (patchtest_patterns.mbox_shortlog_maxlength, l),
+                    % (patchtest_patterns.mbox_shortlog_maxlength, length),
                     commit=commit,
                 )
 
@@ -120,7 +120,7 @@ class TestMbox(base.Base):
             folders = patch.path.split('/')
             base_path = folders[0]
             for project in [self.bitbake, self.doc, self.oe, self.poky]:
-                if base_path in  project.paths:
+                if base_path in project.paths:
                     self.fail('Series sent to the wrong mailing list or some patches from the series correspond to different mailing lists',
                               data=[('Suggested ML', '%s [%s]' % (project.listemail, project.gitrepo)),
                                     ('Patch\'s path:', patch.path)])
@@ -173,7 +173,7 @@ class TestMbox(base.Base):
         for commit in self.commits:
             if patchtest_patterns.auh_email in commit.commit_message:
                 self.fail(
-                    "Invalid author %s. Resend the series with a valid patch author"
+                    "Commit message contains AUH email address %s. Resend the series with a valid patch author"
                     % patchtest_patterns.auh_email,
                     commit=commit,
                 )
meta/lib/patchtest/tests/test_metadata.py +10 -22
index 30da8dbe60..442e80973d 100644
@@ -8,7 +8,6 @@ import base
 import collections
 import os
 import patchtest_patterns
-import pyparsing
 from patchtest_parser import PatchtestParser
 
 # Data store commonly used to share values between pre and post-merge tests
@@ -108,39 +107,28 @@ class TestMetadata(base.Metadata):
                             ],
                         )
 
-    def pretest_src_uri_left_files(self):
-        # these tests just make sense on patches that can be merged
-        if not PatchtestParser.repo.canbemerged():
-            self.skip("Patch cannot be merged")
-        if not self.modified:
-            self.skip('No modified recipes, skipping pretest')
-
-        # get the proper metadata values
+    def _collect_src_uri(self, key_prefix):
         for pn in self.modified:
-            # we are not interested in images
             if 'core-image' in pn:
                 continue
             rd = self.tinfoil.parse_recipe(pn)
             PatchTestDataStore[
-                "%s-%s-%s" % (self.shortid(), patchtest_patterns.metadata_src_uri, pn)
+                "%s-%s-%s" % (key_prefix, patchtest_patterns.metadata_src_uri, pn)
             ] = rd.getVar(patchtest_patterns.metadata_src_uri)
 
-    def test_src_uri_left_files(self):
-        # these tests just make sense on patches that can be merged
+    def pretest_src_uri_left_files(self):
         if not PatchtestParser.repo.canbemerged():
             self.skip("Patch cannot be merged")
         if not self.modified:
             self.skip('No modified recipes, skipping pretest')
+        self._collect_src_uri(self.shortid())
 
-        # get the proper metadata values
-        for pn in self.modified:
-            # we are not interested in images
-            if 'core-image' in pn:
-                continue
-            rd = self.tinfoil.parse_recipe(pn)
-            PatchTestDataStore[
-                "%s-%s-%s" % (self.shortid(), patchtest_patterns.metadata_src_uri, pn)
-            ] = rd.getVar(patchtest_patterns.metadata_src_uri)
+    def test_src_uri_left_files(self):
+        if not PatchtestParser.repo.canbemerged():
+            self.skip("Patch cannot be merged")
+        if not self.modified:
+            self.skip('No modified recipes, skipping test')
+        self._collect_src_uri(self.shortid())
 
         for pn in self.modified:
             pretest_src_uri = PatchTestDataStore[
meta/lib/patchtest/tests/test_patch.py +14 -19
index 3b33b5a199..9f1d7d2c8f 100644
@@ -28,21 +28,16 @@ class TestPatch(base.Base):
     def setUp(self):
         if self.unidiff_parse_error:
             self.skip('Parse error %s' % self.unidiff_parse_error)
-
-        self.valid_status = ", ".join(patchtest_patterns.upstream_status_nonliteral_valid_status)
-        self.standard_format = "Upstream-Status: <Valid status>"
-
-        # we are just interested in series that introduce CVE patches, thus discard other
-        # possibilities: modification to current CVEs, patch directly introduced into the
-        # recipe, upgrades already including the CVE, etc.
-        new_cves = [p for p in self.patchset if p.path.endswith('.patch') and p.is_added_file]
-        if not new_cves:
+        if not TestPatch.newpatches:
             self.skip('No new CVE patches introduced')
 
     def test_upstream_status_presence_format(self):
         if not TestPatch.newpatches:
             self.skip("There are no new software patches, no reason to test Upstream-Status presence/format")
 
+        valid_status = ", ".join(patchtest_patterns.upstream_status_nonliteral_valid_status)
+        standard_format = "Upstream-Status: <Valid status>"
+
         for newpatch in TestPatch.newpatches:
             payload = newpatch.__str__()
             lines = payload.splitlines()
@@ -71,16 +66,16 @@ class TestPatch(base.Base):
                         'Upstream-Status is present only after the patch scissors. '
                         "It must be placed in the patch header before the scissors line.",
                         data=[
-                            ("Standard format", self.standard_format),
-                            ("Valid status", self.valid_status),
+                            ("Standard format", standard_format),
+                            ("Valid status", valid_status),
                         ],
                     )
                 else:
                     self.fail(
                         "Added patch file is missing Upstream-Status: <Valid status> in the commit message",
                         data=[
-                            ("Standard format", self.standard_format),
-                            ("Valid status", self.valid_status),
+                            ("Standard format", standard_format),
+                            ("Valid status", valid_status),
                         ],
                     )
 
@@ -97,8 +92,8 @@ class TestPatch(base.Base):
                         "but was found afterwards.",
                         data=[
                             ("Current", line.lstrip("+")),
-                            ("Standard format", self.standard_format),
-                            ("Valid status", self.valid_status),
+                            ("Standard format", standard_format),
+                            ("Valid status", valid_status),
                         ],
                     )
 
@@ -142,14 +137,14 @@ class TestPatch(base.Base):
                             "Upstream-Status is in incorrect format",
                             data=[
                                 ("Current", pe.pstr),
-                                ("Standard format", self.standard_format),
-                                ("Valid status", self.valid_status),
+                                ("Standard format", standard_format),
+                                ("Valid status", valid_status),
                             ],
                         )
 
     def test_signed_off_by_presence(self):
         if not TestPatch.newpatches:
-            self.skip("There are no new software patches, no reason to test %s presence" % PatchSignedOffBy.mark)
+            self.skip("There are no new software patches, no reason to test %s presence" % TestPatch.mark)
 
         for newpatch in TestPatch.newpatches:
             payload = newpatch.__str__()
@@ -162,7 +157,7 @@ class TestPatch(base.Base):
                 self.fail('A patch file has been added without a Signed-off-by tag: \'%s\'' % os.path.basename(newpatch.path))
 
     def test_cve_tag_format(self):
-        for commit in TestPatch.commits:
+        for commit in self.commits:
             if patchtest_patterns.cve.search_string(
                 commit.shortlog
             ) or patchtest_patterns.cve.search_string(commit.commit_message):
meta/lib/patchtest/tests/test_python_pylint.py +4 -4
index ec9129bc79..37d1de9436 100644
@@ -47,7 +47,7 @@ class PyLint(base.Base):
         for pythonpatch in self.pythonpatches:
             # a condition checking whether a file is renamed or not
             # unidiff doesn't support this yet
-            if pythonpatch.target_file is not pythonpatch.path:
+            if pythonpatch.target_file != pythonpatch.path:
                 path = pythonpatch.target_file[2:]
             else:
                 path = pythonpatch.path
@@ -55,9 +55,9 @@ class PyLint(base.Base):
             reporter = TextReporter(pylint_output)
             lint.Run([self.pylint_options, pythonpatch.path], reporter=reporter, exit=False)
             for line in pylint_output.readlines():
-                    if not '*' in line:
-                        if line.strip():
-                            self.pylint_test[line.strip().split(' ',1)[0]] = line.strip().split(' ',1)[1]
+                if not '*' in line:
+                    if line.strip():
+                        self.pylint_test[line.strip().split(' ',1)[0]] = line.strip().split(' ',1)[1]
 
         for issue in self.pylint_test:
              if self.pylint_test[issue] not in self.pylint_pretest.values():
Signature · 1 line
2.54.0
#5 lore raw

PATCH 4/6 patchtest: correctly abort --directory test

In reply to Trevor Gamblin

Currently, patchtest run with --directory responds to a CTRL+C press by aborting only the current patch being tested, and moves onto the next. Instead, this key press should stop the test entirely. Remove usage of the unittest.installHandler() function (which intercepts the signal) and handle it ourselves.

Also make sure that the branch is properly reset with git am --abort afterwards, and that the return code is properly set in the sigint handler function. Finally, update patchtest_parser.py so that the helper info reflects this change.

AI-Generated: Uses Claude Code

---
 meta/lib/patchtest/patchtest_parser.py |  2 +-
 meta/lib/patchtest/repo.py             |  4 ++++
 scripts/patchtest                      | 31 +++++++++++++++++++++++---
 3 files changed, 33 insertions(+), 4 deletions(-)
meta/lib/patchtest/patchtest_parser.py +1 -1
index 2a11cb76c2..69bcb4b8e5 100644
@@ -37,7 +37,7 @@ class PatchtestParser(object):
                             help='The patch to be tested')
 
         target_patch_group.add_argument('--directory', metavar='DIRECTORY', dest='patch_path',
-                            help='The directory containing patches to be tested')
+                            help='The directory containing patches to be tested. CTRL+C aborts the entire directory test.')
 
         parser.add_argument('--repodir', metavar='REPO',
                             default=default_repodir,
meta/lib/patchtest/repo.py +4 -0
index 6a7d7d2d3b..b4cf9e8ded 100644
@@ -85,6 +85,10 @@ class PatchTestRepo(object):
             self._patchmerged = True
 
     def clean(self):
+        try:
+            self.repo.git.execute(['git', 'am', '--abort'])
+        except git.exc.GitCommandError:
+            pass
         self.repo.git.execute(['git', 'checkout', self.current_branch if self.current_branch else self.current_commit])
         self.repo.git.execute(['git', 'branch', '-D', self._workingbranch])
         self._patchmerged = False
scripts/patchtest +28 -3
index e8ace03905..435610b54f 100755
@@ -12,6 +12,7 @@
 import json
 import logging
 import os
+import signal
 import subprocess
 import sys
 import traceback
@@ -133,8 +134,6 @@ def _runner(resultklass, prefix=None):
 
 def run(patch, logfile=None):
     """ Load, setup and run pre and post-merge tests """
-    unittest.installHandler()
-
     premerge_result = _runner(make_result_class(patch, False, logfile), 'pretest')
     postmerge_result = _runner(make_result_class(patch, True, logfile), 'test')
 
@@ -183,10 +182,26 @@ def main():
     else:
         patch_list = [patch_path]
 
+    interrupted = False
+    previous_sigint = signal.getsignal(signal.SIGINT)
+
+    def _sigint_handler(signum, frame):
+        nonlocal interrupted
+        interrupted = True
+        # Restore previous handler so a second CTRL+C exits immediately
+        signal.signal(signal.SIGINT, previous_sigint)
+        signal.default_int_handler(signum, frame)
+
+    signal.signal(signal.SIGINT, _sigint_handler)
+
     ret = 0
     for patch in patch_list:
+        if interrupted:
+            break
+
         if os.path.getsize(patch) == 0:
             logger.error('patchtest: patch is empty')
+            signal.signal(signal.SIGINT, previous_sigint)
             return 1
 
         logger.info('Testing patch %s' % patch)
@@ -197,8 +212,18 @@ def main():
             with open(log_path, "a") as f:
                 f.write("Patchtest results for patch '%s':\n\n" % patch)
 
-        ret = run(patch, log_path)
+        try:
+            result = run(patch, log_path)
+            ret = ret or result
+        except KeyboardInterrupt:
+            interrupted = True
+
+        if interrupted:
+            logger.error('\npatchtest: interrupted')
+            signal.signal(signal.SIGINT, previous_sigint)
+            return 1
 
+    signal.signal(signal.SIGINT, previous_sigint)
     return ret
 
 if __name__ == '__main__':
Signature · 1 line
2.54.0
#6 lore raw

PATCH 5/6 patchtest: tests: base: add helper functions, cleanup

In reply to Trevor Gamblin

- Encapsulate the package name acquisition logic in _find_pn()
- Simplify the _getvar() function used across the test suite
- Drastically shorten the get_metadata_stats() function
- Reword and clean up some vague comments and typos

AI-Generated: Uses Claude Code

---
 meta/lib/patchtest/tests/base.py | 112 ++++++++++++-------------------
 1 file changed, 44 insertions(+), 68 deletions(-)
meta/lib/patchtest/tests/base.py +44 -68
index 8263932dda..30c207f016 100644
@@ -133,18 +133,17 @@ class Metadata(Base):
     def setUpClassLocal(cls):
         cls.tinfoil = cls.setup_tinfoil()
 
-        # get info about added/modified/remove recipes
+        # get info about added/modified/removed recipes
         cls.added, cls.modified, cls.removed = cls.get_metadata_stats(cls.patchset)
 
     @classmethod
     def tearDownClassLocal(cls):
-        cls.tinfoil.shutdown()
+        if cls.tinfoil:
+            cls.tinfoil.shutdown()
 
     @classmethod
     def setup_tinfoil(cls, config_only=False):
-        """Initialize tinfoil api from bitbake"""
-
-        # import relevant libraries
+        """Initialize tinfoil api from bitbake."""
         try:
             scripts_path = os.path.join(PatchtestParser.repodir, "scripts", "lib")
             if scripts_path not in sys.path:
@@ -156,21 +155,19 @@ class Metadata(Base):
             raise PatchtestOEError('Could not import tinfoil module')
 
         orig_cwd = os.path.abspath(os.curdir)
-
-        # Load tinfoil
         tinfoil = None
         try:
             builddir = os.environ.get('BUILDDIR')
             if not builddir:
                 logger.warn('Bitbake environment not loaded?')
-                return tinfoil
+                return None
             os.chdir(builddir)
             tinfoil = bb.tinfoil.Tinfoil()
             tinfoil.prepare(config_only=config_only)
-        except bb.tinfoil.TinfoilUIException as te:
+        except bb.tinfoil.TinfoilUIException:
             if tinfoil:
                 tinfoil.shutdown()
-            raise PatchtestOEError('Could not prepare properly tinfoil (TinfoilUIException)')
+            raise PatchtestOEError('Could not properly configure tinfoil (TinfoilUIException)')
         except Exception as e:
             if tinfoil:
                 tinfoil.shutdown()
@@ -180,69 +177,48 @@ class Metadata(Base):
 
         return tinfoil
 
+    @staticmethod
+    def _find_pn(data, path):
+        """Find the PN from tinfoil recipe cache data for a given file path."""
+        pn = None
+        pn_native = None
+        for _path, _pn in data:
+            if path in _path:
+                if 'native' in _pn:
+                    pn_native = _pn
+                else:
+                    pn = _pn
+                    break
+        else:
+            if pn_native:
+                return pn_native
+            path_basename = path.split('_')[0]
+            for _path, _pn in data:
+                _path_basename = _path.split('_')[0]
+                if path_basename == _path_basename:
+                    pn = _pn
+        return pn
+
     @classmethod
-    def get_metadata_stats(cls, patchset):
-        """Get lists of added, modified and removed metadata files"""
+    def _getvar(cls, path, varname):
+        """Return a recipe variable value via tinfoil if available."""
+        if cls.tinfoil:
+            data = list(cls.tinfoil.cooker.recipecaches[''].pkg_fn.items())
+            pn = cls._find_pn(data, path)
+            rd = cls.tinfoil.parse_recipe(pn)
+            return rd.getVar(varname)
 
-        def find_pn(data, path):
-            """Find the PN from data"""
-            pn = None
-            pn_native = None
-            for _path, _pn in data:
-                if path in _path:
-                    if 'native' in _pn:
-                        # store the native PN but look for the non-native one first
-                        pn_native = _pn
-                    else:
-                        pn = _pn
-                        break
-            else:
-                # sent the native PN if found previously
-                if pn_native:
-                    return pn_native
-
-                # on renames (usually upgrades), we need to check (FILE) base names
-                # because the unidiff library does not provided the new filename, just the modified one
-                # and tinfoil datastore, once the patch is merged, will contain the new filename
-                path_basename = path.split('_')[0]
-                for _path, _pn in data:
-                    _path_basename = _path.split('_')[0]
-                    if path_basename == _path_basename:
-                        pn = _pn
-            return pn
-
-        if not cls.tinfoil:
-            cls.tinfoil = cls.setup_tinfoil()
-
-        added_paths, modified_paths, removed_paths = [], [], []
+    @classmethod
+    def get_metadata_stats(cls, patchset):
+        """Return lists of absolute paths for added, modified and removed recipe files."""
         added, modified, removed = [], [], []
-
-        # get metadata filename additions, modification and removals
         for patch in patchset:
             if patch.path.endswith('.bb') or patch.path.endswith('.bbappend') or patch.path.endswith('.inc'):
+                abspath = os.path.join(os.path.abspath(PatchtestParser.repodir), patch.path)
                 if patch.is_added_file:
-                    added_paths.append(
-                        os.path.join(
-                            os.path.abspath(PatchtestParser.repodir), patch.path
-                        )
-                    )
+                    added.append(abspath)
                 elif patch.is_modified_file:
-                    modified_paths.append(
-                        os.path.join(
-                            os.path.abspath(PatchtestParser.repodir), patch.path
-                        )
-                    )
+                    modified.append(abspath)
                 elif patch.is_removed_file:
-                    removed_paths.append(
-                        os.path.join(
-                            os.path.abspath(PatchtestParser.repodir), patch.path
-                        )
-                    )
-
-        data = cls.tinfoil.cooker.recipecaches[''].pkg_fn.items()
-
-        added = [find_pn(data,path) for path in added_paths]
-        modified = [find_pn(data,path) for path in modified_paths]
-        removed = [find_pn(data,path) for path in removed_paths]
-
-        return [a for a in added if a], [m for m in modified if m], [r for r in removed if r]
+                    removed.append(abspath)
+        return added, modified, removed
Signature · 1 line
2.54.0
#7 lore raw

PATCH 6/6 patchtest: tests: test_metadata: cleanup

In reply to Trevor Gamblin

- Use the new _getvar() version
- Replace usage of "pn" as a variable with the more explicit "path"
- Fix some vague comments and typos

AI-Generated: Uses Claude Code

---
 meta/lib/patchtest/tests/test_metadata.py | 62 ++++++++++-------------
 1 file changed, 28 insertions(+), 34 deletions(-)
meta/lib/patchtest/tests/test_metadata.py +28 -34
index 442e80973d..f180309991 100644
@@ -19,32 +19,28 @@ class TestMetadata(base.Metadata):
         if not self.added:
             self.skip('No added recipes, skipping test')
 
-        # TODO: this is a workaround so we can parse the recipe not
-        # containing the LICENSE var: add some default license instead
-        # of INVALID into auto.conf, then remove this line at the end
+        # workaround: set a default license so tinfoil can parse recipes
+        # that don't have LICENSE set yet
         auto_conf = os.path.join(os.environ.get('BUILDDIR'), 'conf', 'auto.conf')
         open_flag = 'w'
         if os.path.exists(auto_conf):
             open_flag = 'a'
         with open(auto_conf, open_flag) as fd:
-            for pn in self.added:
+            for path in self.added:
                 fd.write('LICENSE ??= "%s"\n' % patchtest_patterns.invalid_license)
 
         no_license = False
-        for pn in self.added:
-            rd = self.tinfoil.parse_recipe(pn)
-            license = rd.getVar(patchtest_patterns.metadata_lic)
-            if license == patchtest_patterns.invalid_license:
+        for path in self.added:
+            license = self._getvar(path, patchtest_patterns.metadata_lic)
+            if not license or license == patchtest_patterns.invalid_license:
                 no_license = True
                 break
 
-        # remove auto.conf line or the file itself
         if open_flag == 'w':
             os.remove(auto_conf)
         else:
-            fd = open(auto_conf, 'r')
-            lines = fd.readlines()
-            fd.close()
+            with open(auto_conf, 'r') as fd:
+                lines = fd.readlines()
             with open(auto_conf, 'w') as fd:
                 fd.write(''.join(lines[:-1]))
 
@@ -55,14 +51,13 @@ class TestMetadata(base.Metadata):
         if not self.added:
             self.skip('No added recipes, skipping test')
 
-        for pn in self.added:
-            rd = self.tinfoil.parse_recipe(pn)
-            pathname = rd.getVar('FILE')
+        for path in self.added:
+            pathname = self._getvar(path, 'FILE') or path
             # we are not interested in images
             if '/images/' in pathname:
                 continue
-            lic_files_chksum = rd.getVar(patchtest_patterns.metadata_chksum)
-            if rd.getVar(patchtest_patterns.license_var) == patchtest_patterns.closed:
+            lic_files_chksum = self._getvar(path, patchtest_patterns.metadata_chksum)
+            if self._getvar(path, patchtest_patterns.license_var) == patchtest_patterns.closed:
                 continue
             if not lic_files_chksum:
                 self.fail(
@@ -108,13 +103,13 @@ class TestMetadata(base.Metadata):
                         )
 
     def _collect_src_uri(self, key_prefix):
-        for pn in self.modified:
-            if 'core-image' in pn:
+        for path in self.modified:
+            if 'core-image' in os.path.basename(path):
                 continue
-            rd = self.tinfoil.parse_recipe(pn)
+            src_uri = self._getvar(path, patchtest_patterns.metadata_src_uri)
             PatchTestDataStore[
-                "%s-%s-%s" % (key_prefix, patchtest_patterns.metadata_src_uri, pn)
-            ] = rd.getVar(patchtest_patterns.metadata_src_uri)
+                "%s-%s-%s" % (key_prefix, patchtest_patterns.metadata_src_uri, path)
+            ] = src_uri or ''
 
     def pretest_src_uri_left_files(self):
         if not PatchtestParser.repo.canbemerged():
@@ -130,12 +125,12 @@ class TestMetadata(base.Metadata):
             self.skip('No modified recipes, skipping test')
         self._collect_src_uri(self.shortid())
 
-        for pn in self.modified:
+        for path in self.modified:
             pretest_src_uri = PatchTestDataStore[
-                "pre%s-%s-%s" % (self.shortid(), patchtest_patterns.metadata_src_uri, pn)
+                "pre%s-%s-%s" % (self.shortid(), patchtest_patterns.metadata_src_uri, path)
             ].split()
             test_src_uri = PatchTestDataStore[
-                "%s-%s-%s" % (self.shortid(), patchtest_patterns.metadata_src_uri, pn)
+                "%s-%s-%s" % (self.shortid(), patchtest_patterns.metadata_src_uri, path)
             ].split()
 
             pretest_files = set([os.path.basename(patch.split(';')[0]) for patch in pretest_src_uri if patch.startswith('file://')])
@@ -154,7 +149,7 @@ class TestMetadata(base.Metadata):
                 filesremoved_from_usr_uri = pretest_files - test_files
 
                 # finally, get those patches removed at SRC_URI and not removed from the patchset
-                # TODO: we are not taking into account  renames, so test may raise false positives
+                # TODO: we are not taking into account renames, so test may raise false positives
                 not_removed = filesremoved_from_usr_uri - filesremoved_from_patchset
                 if not_removed:
                     self.fail('Patches not removed from tree. Remove them and amend the submitted mbox',
@@ -164,15 +159,15 @@ class TestMetadata(base.Metadata):
         if not self.added:
             self.skip('No added recipes, skipping test')
 
-        for pn in self.added:
+        for path in self.added:
+            pn = os.path.basename(path).split('_')[0]
             # we are not interested in images
             if 'core-image' in pn:
                 continue
-            rd = self.tinfoil.parse_recipe(pn)
-            summary = rd.getVar(patchtest_patterns.metadata_summary)
+            summary = self._getvar(path, patchtest_patterns.metadata_summary)
 
             # "${PN} version ${PN}-${PR}" is the default, so fail if default
-            if summary.startswith("%s version" % pn):
+            if not summary or summary.startswith("%s version" % pn):
                 self.fail(
                     "%s is missing in newly added recipe" % patchtest_patterns.metadata_summary
                 )
@@ -186,12 +181,11 @@ class TestMetadata(base.Metadata):
             or PatchtestParser.repo.patch.branch == "dunfell"
         ):
             self.skip("No modified recipes or older target branch, skipping test")
-        for pn in self.modified:
+        for path in self.modified:
             # we are not interested in images
-            if 'core-image' in pn:
+            if 'core-image' in os.path.basename(path):
                 continue
-            rd = self.tinfoil.parse_recipe(pn)
-            cve_check_ignore = rd.getVar(patchtest_patterns.cve_check_ignore_var)
+            cve_check_ignore = self._getvar(path, patchtest_patterns.cve_check_ignore_var)
 
             if cve_check_ignore is not None:
                 self.fail(
Signature · 1 line
2.54.0