From patchwork Sun Dec 14 17:54:32 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 903 Return-Path: X-Original-To: u-boot-concept@u-boot.org Delivered-To: u-boot-concept@u-boot.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765734942; bh=Zf49s6aXAEc5+2gtCwVgxRfP+7IL9sFoqWO+/CSlxYc=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=XBK/9z3q+CwOV8r8LRzuezDnFEZ/Zkdrn6JMhheZ7RQ1PZaDbsmmsed8ZBOVFSuZS VGgpiTtvI/SGWvDuxqiSDy8vJItq0hz+nWkXt7ZkKKLDlk8H/v3GEDiMvSbUWqiDd0 jkEZwiLMBESdpJWbYUn0mZGjt0pj5LrQlbWqvnUkkHvUpWqpYNNgcPhvXxffF3VpGC +G4Xoa8Yi/biep0LO6zZGs3V248vhbuKSbc799PzTDD6w6LUXQpu4SPxBFojpip2T2 OQIx6HvJu27ePNJgEJiNlUh0e2BezNYt21wK+yTZArKOnUb4psorgWAHaU8l2EsO/L GFRS1tMKLT+Lg== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id F33CC68AFE for ; Sun, 14 Dec 2025 10:55:42 -0700 (MST) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id jYcOIeSBpM2e for ; Sun, 14 Dec 2025 10:55:42 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765734942; bh=Zf49s6aXAEc5+2gtCwVgxRfP+7IL9sFoqWO+/CSlxYc=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=XBK/9z3q+CwOV8r8LRzuezDnFEZ/Zkdrn6JMhheZ7RQ1PZaDbsmmsed8ZBOVFSuZS VGgpiTtvI/SGWvDuxqiSDy8vJItq0hz+nWkXt7ZkKKLDlk8H/v3GEDiMvSbUWqiDd0 jkEZwiLMBESdpJWbYUn0mZGjt0pj5LrQlbWqvnUkkHvUpWqpYNNgcPhvXxffF3VpGC +G4Xoa8Yi/biep0LO6zZGs3V248vhbuKSbc799PzTDD6w6LUXQpu4SPxBFojpip2T2 OQIx6HvJu27ePNJgEJiNlUh0e2BezNYt21wK+yTZArKOnUb4psorgWAHaU8l2EsO/L GFRS1tMKLT+Lg== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id DE9A168A84 for ; Sun, 14 Dec 2025 10:55:42 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765734941; bh=EmcI4OxTi1IDiJuXJcmjaDiCbhSoww6mHEJ/zZyQbto=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=r5XJLTP6F0tDTePkhhjR+xbr/C3dn1J0mt9iCb6X/RiPZuXRBTBMNaPGg374JNBMh wFG0PDZQ9zCvq+381pdl6Eb8dF5PXqtYfjJiDO2WYktUggessJmccx+dfSgr28eAA+ WXEZUCpMGtC0CemoMVfD5HfOVtDDMY9RsaCvyVQ8fIRC4DZpZftFZFpJE9/Q36lWGd Shc4PbsifIkE0JFJdQtrqRNGCQp4Hk43j6HiHbDxosA5wfIpFEQnbR3MMRPPaaINh/ Rxl+qoj/VUzDjlNP+5ZXkLfqOsVqv8YeFoknL0JHJxALVLul0xeFkuaPyZ/+ZsuRHr hfxkM5Jry1UJA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 2FD0B68A84; Sun, 14 Dec 2025 10:55:41 -0700 (MST) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10026) with ESMTP id K4LlccnYmeC8; Sun, 14 Dec 2025 10:55:41 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1765734937; bh=W641O1WM04SiWVEbRs5XbVl73jEnY0DQen6phSmhR0o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Yykqct3lJi5wjUwA6IAMLDWy/OsYhu6nQYh1JogMBfpR37VJihlZUKN8/dM84mHtr RKA4GiKIpHBLjvnXQfZG6uTdT0bFT2xOFDA11POYDkARUtfd3Y2Rhufy08RDbI2COR DdDeZDwucv828hgfq8B1ds7OulkKdmVPp4sxz3yY5sprhZiCQLhFI/ZVxAxroTXh1s rf/+m4YYQw0w5KACVam2szzZis9tAh/XjAYW/Prg1XQqwpqZMq6BGNiYSXQmxPoFQf N+MStJEsKoyaO3+NcgzK+Ls4SKpMFdNMwoPGdpvOUR8uyp26Bkl/QFRG6WPqV0YwvH dfowu5ylDdkGA== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 80B3668A89; Sun, 14 Dec 2025 10:55:36 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Sun, 14 Dec 2025 10:54:32 -0700 Message-ID: <20251214175449.3799539-11-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251214175449.3799539-1-sjg@u-boot.org> References: <20251214175449.3799539-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: LIOKTDOC4TTVQMOCP5AK4PHLCCMGOYF4 X-Message-ID-Hash: LIOKTDOC4TTVQMOCP5AK4PHLCCMGOYF4 X-MailFrom: sjg@u-boot.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Heinrich Schuchardt , Simon Glass , Claude X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 10/21] test: Add a helper to check the next line against a regex List-Id: Discussion and patches related to U-Boot Concept Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Simon Glass Add a new ut_assert_nextline_regex() macro and ut_check_console_line_regex() helper to check console output against a regex pattern. This is useful when the exact output varies (e.g., file paths or line numbers in error messages). Co-developed-by: Claude Signed-off-by: Simon Glass --- doc/develop/tests_writing.rst | 3 +++ include/test/ut.h | 29 +++++++++++++++++++++++++++++ test/ut.c | 21 +++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/doc/develop/tests_writing.rst b/doc/develop/tests_writing.rst index 43756989d43..41612a9e21b 100644 --- a/doc/develop/tests_writing.rst +++ b/doc/develop/tests_writing.rst @@ -451,6 +451,9 @@ ut_assert_nextlinen(fmt, args...) Assert that the next console output line matches up to the format string length +ut_assert_nextline_regex(pattern) + Assert that the next console output line matches a regex pattern + ut_assert_nextline_empty() Assert that the next console output line is empty diff --git a/include/test/ut.h b/include/test/ut.h index a2b42cdf414..7098c9be7d6 100644 --- a/include/test/ut.h +++ b/include/test/ut.h @@ -87,6 +87,20 @@ int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...) int ut_check_console_linen(struct unit_test_state *uts, const char *fmt, ...) __attribute__ ((format (__printf__, 2, 3))); +/** + * ut_check_console_line_regex() - Check the next console line against a regex + * + * This checks the next line of console output against a regex pattern. + * + * After the function returns, uts->expect_str holds the regex pattern and + * uts->actual_str holds the actual string read from the console. + * + * @uts: Test state + * @regex: Regular expression pattern to match against + * Return: 0 if OK, other value on error + */ +int ut_check_console_line_regex(struct unit_test_state *uts, const char *regex); + /** * ut_check_skipline() - Check that the next console line exists and skip it * @@ -412,6 +426,21 @@ int ut_check_console_dump(struct unit_test_state *uts, int total_bytes); __ret; \ }) +/* Assert that the next console output line matches a regex pattern */ +#define ut_assert_nextline_regex(pattern) ({ \ + int __ret = 0; \ + \ + if (ut_check_console_line_regex(uts, pattern)) { \ + ut_failf(uts, __FILE__, __LINE__, __func__, \ + "console regex", \ + "\nExpected regex '%s',\n got '%s'", \ + uts->expect_str, uts->actual_str); \ + if (!uts->soft_fail) \ + return CMD_RET_FAILURE; \ + } \ + __ret; \ +}) + /* Assert that there is a 'next' console output line, and skip it */ #define ut_assert_skipline() ({ \ int __ret = 0; \ diff --git a/test/ut.c b/test/ut.c index 94b09364687..aed59cae0b9 100644 --- a/test/ut.c +++ b/test/ut.c @@ -150,6 +150,27 @@ int ut_check_console_linen(struct unit_test_state *uts, const char *fmt, ...) strlen(uts->expect_str)); } +int ut_check_console_line_regex(struct unit_test_state *uts, const char *regex) +{ + char err[UT_REGEX_ERR_SIZE]; + int len; + int ret; + + len = strlcpy(uts->expect_str, regex, sizeof(uts->expect_str)); + if (len >= sizeof(uts->expect_str)) { + ut_fail(uts, __FILE__, __LINE__, __func__, + "unit_test_state->expect_str too small"); + return -EOVERFLOW; + } + ret = readline_check(uts); + if (ret == -ENOENT) + return 1; + + ret = ut_check_regex(regex, uts->actual_str, err); + + return ret; +} + int ut_check_skipline(struct unit_test_state *uts) { int ret;