This is an automated email from the ASF dual-hosted git repository.

pkarashchenko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new 28d4c5844 run testsuites where name match pattern value of --suite
28d4c5844 is described below

commit 28d4c5844877be32885329d9434bdfc54f2a214b
Author: zhangchao53 <[email protected]>
AuthorDate: Fri Sep 1 18:25:43 2023 +0800

    run testsuites where name match pattern value of --suite
---
 testing/cmocka/cmocka_main.c | 79 ++++++++++++++++++++++++--------------------
 1 file changed, 44 insertions(+), 35 deletions(-)

diff --git a/testing/cmocka/cmocka_main.c b/testing/cmocka/cmocka_main.c
index d4aa15852..b13dbbd84 100644
--- a/testing/cmocka/cmocka_main.c
+++ b/testing/cmocka/cmocka_main.c
@@ -30,18 +30,18 @@
 #include <stddef.h>
 #include <setjmp.h>
 #include <stdint.h>
-#include <cmocka.h>
-#include <sys/wait.h>
 #include <stdio.h>
 #include <syslog.h>
-
+#include <sys/wait.h>
 #include <builtin/builtin.h>
+#include <regex.h>
+#include <cmocka.h>
 
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
 
-static void cm_usage (void)
+static void cm_usage(void)
 {
     char *mesg =
         "an elegant unit testing framework for C "
@@ -51,14 +51,34 @@ static void cm_usage (void)
         "     --list       display only the names of testcases "
         "and testsuite,\n"
         "                  don't execute them\n"
-        "     --test A     only run cases where name matches A pattern\n"
-        "     --skip B     don't run cases where name matches B pattern\n"
-        "     --case C     specifies testsuite C to run\n"
-        "Example: cmocka --case mm --case sched "
+        "     --test A     only run cases where case function "
+        "name matches A pattern\n"
+        "     --skip B     don't run cases where case function "
+        "name matches B pattern\n"
+        "     --suite C    only run suites where PROGNAME "
+        "matches C pattern\n"
+        "Example: cmocka --suite mm|sched "
         "--test Test* --skip TestNuttxMm0[123]\n\n";
     printf("%s", mesg);
 }
 
+static int cm_regexmatch(FAR const char *pattern, FAR const char *str)
+{
+  regex_t regex;
+  int ret;
+
+  ret = regcomp(&regex, pattern, REG_EXTENDED | REG_NOSUB);
+  if (ret != 0)
+    {
+      return 0;
+    }
+
+  ret = regexec(&regex, str, 0, NULL, 0);
+  regfree(&regex);
+
+  return ret == 0;
+}
+
 /****************************************************************************
  * cmocka_main
  ****************************************************************************/
@@ -67,30 +87,32 @@ int main(int argc, FAR char *argv[])
 {
   const char prefix[] = CONFIG_TESTING_CMOCKA_PROGNAME"_";
   FAR const struct builtin_s *builtin;
-  int len = strlen(prefix);
+  int prefix_len  = strlen(prefix);
   FAR char *testcase = NULL;
   FAR char *bypass[argc + 1];
-  FAR char *cases[argc + 1];
+  FAR char *suite = NULL;
   FAR char *skip = NULL;
   int num_bypass = 1;
-  int num_cases = 0;
   int ret;
   int i;
-  int j;
   int list_tests = 0;
 
-  if (strlen(argv[0]) < len - 1 ||
-      strncmp(argv[0], prefix, len - 1))
+  if (strlen(argv[0]) < prefix_len  - 1 ||
+      strncmp(argv[0], prefix, prefix_len  - 1))
     {
       return 0;
     }
 
-  memset(cases, 0, sizeof(cases));
   memset(bypass, 0, sizeof(bypass));
 
   for (i = 1; i < argc; i++)
     {
-      if (strcmp("--list", argv[i]) == 0)
+      if (strcmp("--help", argv[i]) == 0 || strcmp("-?", argv[i]) == 0)
+        {
+          cm_usage();
+          return 0;
+        }
+      else if (strcmp("--list", argv[i]) == 0)
         {
           list_tests = 1;
         }
@@ -98,14 +120,9 @@ int main(int argc, FAR char *argv[])
         {
           testcase = argv[++i];
         }
-      else if (strcmp("--help", argv[i]) == 0 || strcmp("-?", argv[i]) == 0)
+      else if (strcmp("--suite", argv[i]) == 0)
         {
-          cm_usage();
-          return 0;
-        }
-      else if (strcmp("--case", argv[i]) == 0)
-        {
-          cases[num_cases++] = argv[++i];
+          suite = argv[++i];
         }
       else if (strcmp("--skip", argv[i]) == 0)
         {
@@ -131,22 +148,14 @@ int main(int argc, FAR char *argv[])
   for (i = 0; (builtin = builtin_for_index(i)) != NULL; i++)
     {
       if (builtin->main == NULL ||
-          strlen(builtin->name) < len ||
-          strncmp(builtin->name, prefix, len))
+          strlen(builtin->name) < prefix_len  ||
+          strncmp(builtin->name, prefix, prefix_len))
         {
           continue;
         }
 
-      for (j = 0; cases[j]; j++)
-        {
-          if (strncmp(builtin->name + len,
-                cases[j], strlen(cases[j])) == 0)
-            {
-              break;
-            }
-        }
-
-      if (j && cases[j] == NULL)
+      if (suite != NULL &&
+          !cm_regexmatch(suite, builtin->name))
         {
           continue;
         }

Reply via email to