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

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

commit 37ac7f923ab37a69ca5079b33861ebe5e5ca9318
Author: yangjiao <[email protected]>
AuthorDate: Tue Aug 15 20:55:12 2023 +0800

    Update cmocka_main.c to support following functions:
    1. list all the testcases with "--list";
    2. run some testcases with "--test";
    3. show cmocka help message;
    4. skip some testcases with "--skip";
    
    Signed-off-by: yangjiao <[email protected]>
---
 testing/cmocka/cmocka_main.c | 53 ++++++++++++++++++++++++++++++++++++++------
 1 file changed, 46 insertions(+), 7 deletions(-)

diff --git a/testing/cmocka/cmocka_main.c b/testing/cmocka/cmocka_main.c
index 7a189fcb4..d4aa15852 100644
--- a/testing/cmocka/cmocka_main.c
+++ b/testing/cmocka/cmocka_main.c
@@ -32,6 +32,8 @@
 #include <stdint.h>
 #include <cmocka.h>
 #include <sys/wait.h>
+#include <stdio.h>
+#include <syslog.h>
 
 #include <builtin/builtin.h>
 
@@ -39,6 +41,24 @@
  * Public Functions
  ****************************************************************************/
 
+static void cm_usage (void)
+{
+    char *mesg =
+        "an elegant unit testing framework for C "
+        "with support for mock objects\n"
+        "Usage: cmocka [OPTION [ARG]] ...\n"
+        " -?, --help       show this help statement\n"
+        "     --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 Test* --skip TestNuttxMm0[123]\n\n";
+    printf("%s", mesg);
+}
+
 /****************************************************************************
  * cmocka_main
  ****************************************************************************/
@@ -48,15 +68,16 @@ int main(int argc, FAR char *argv[])
   const char prefix[] = CONFIG_TESTING_CMOCKA_PROGNAME"_";
   FAR const struct builtin_s *builtin;
   int len = strlen(prefix);
+  FAR char *testcase = NULL;
   FAR char *bypass[argc + 1];
   FAR char *cases[argc + 1];
-  FAR char *skip[argc + 1];
+  FAR char *skip = NULL;
   int num_bypass = 1;
   int num_cases = 0;
-  int num_skip = 0;
   int ret;
   int i;
   int j;
+  int list_tests = 0;
 
   if (strlen(argv[0]) < len - 1 ||
       strncmp(argv[0], prefix, len - 1))
@@ -65,18 +86,30 @@ int main(int argc, FAR char *argv[])
     }
 
   memset(cases, 0, sizeof(cases));
-  memset(skip, 0, sizeof(skip));
   memset(bypass, 0, sizeof(bypass));
 
   for (i = 1; i < argc; i++)
     {
-      if (strcmp("--case", argv[i]) == 0)
+      if (strcmp("--list", argv[i]) == 0)
+        {
+          list_tests = 1;
+        }
+      else if (strcmp("--test", argv[i]) == 0)
+        {
+          testcase = argv[++i];
+        }
+      else if (strcmp("--help", argv[i]) == 0 || strcmp("-?", argv[i]) == 0)
+        {
+          cm_usage();
+          return 0;
+        }
+      else if (strcmp("--case", argv[i]) == 0)
         {
           cases[num_cases++] = argv[++i];
         }
       else if (strcmp("--skip", argv[i]) == 0)
         {
-          skip[num_skip++] = argv[++i];
+          skip = argv[++i];
         }
       else
         {
@@ -84,12 +117,17 @@ int main(int argc, FAR char *argv[])
         }
     }
 
+  cmocka_set_test_filter(NULL);
   cmocka_set_skip_filter(NULL);
-  for (i = 0; skip[i]; i++)
+  cmocka_set_list_test(list_tests);
+
+  if (list_tests == 0)
     {
-      cmocka_set_skip_filter(skip[i]);
+      cmocka_set_test_filter(testcase);
+      cmocka_set_skip_filter(skip);
     }
 
+  print_message("Cmocka Test Start.");
   for (i = 0; (builtin = builtin_for_index(i)) != NULL; i++)
     {
       if (builtin->main == NULL ||
@@ -121,5 +159,6 @@ int main(int argc, FAR char *argv[])
         }
     }
 
+  print_message("Cmocka Test Completed.");
   return 0;
 }

Reply via email to