]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Set unit test group name in CMocka tests
authorŠtěpán Balážik <stepan@isc.org>
Wed, 15 Oct 2025 19:37:52 +0000 (21:37 +0200)
committerŠtěpán Balážik <stepan@isc.org>
Fri, 19 Dec 2025 18:26:22 +0000 (18:26 +0000)
CMocka uses group names in the JUnit output.

Use dirname_filename as the group name, as there duplicate testnames
(e.g. time exists both in isc/ and dns/)

tests/include/tests/isc.h
tests/libtest/isc.c

index 78e7e10614786b081b81c47d7664ed1cfd8979b5..3e77f7f1b56f5e17a9db9c1c8a7e0684a367e92a 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <isc/buffer.h>
 #include <isc/commandline.h>
+#include <isc/file.h>
 #include <isc/hash.h>
 #include <isc/log.h>
 #include <isc/mem.h>
@@ -57,6 +58,16 @@ setup_managers(void **state);
 int
 teardown_managers(void **state);
 
+isc_result_t
+file_path_to_groupname(const char *path, char *out, size_t outlen);
+/*%<
+ * Example:
+ *   "/.../tests/dns/dbdiff" -> "dns_dbdiff"
+ *
+ * Returns ISC_R_SUCCESS on success, ISC_R_NOSPACE if outlen is too small,
+ * or other error codes returned by isc_file_splitpath.
+ */
+
 #ifndef TESTS_DIR
 #define TESTS_DIR "./"
 #endif
@@ -240,10 +251,18 @@ teardown_managers(void **state);
                        }                                                               \
                }                                                                       \
                                                                                         \
+               char         group_name[1024];                                          \
+               isc_result_t res = file_path_to_groupname(argv[0], group_name,          \
+                                                         sizeof(group_name));          \
+               if (res != ISC_R_SUCCESS) {                                             \
+                       strncpy(group_name, "tests", sizeof(group_name));               \
+               }                                                                       \
                if (selected[0].name != NULL) {                                         \
-                       r = cmocka_run_group_tests(selected, setup, teardown);          \
+                       r = cmocka_run_group_tests_name(group_name, selected,           \
+                                                       setup, teardown);               \
                } else {                                                                \
-                       r = cmocka_run_group_tests(tests, setup, teardown);             \
+                       r = cmocka_run_group_tests_name(group_name, tests,              \
+                                                       setup, teardown);               \
                }                                                                       \
                                                                                         \
                teardown_mctx(NULL);                                                    \
index 5b6334a64d3192f3bdf63ef22d28cd49ed507f71..433ea1487afedbdcbdf38cf845d44be6acef1163 100644 (file)
@@ -26,6 +26,7 @@
 #include <isc/managers.h>
 #include <isc/mem.h>
 #include <isc/os.h>
+#include <isc/result.h>
 #include <isc/string.h>
 #include <isc/timer.h>
 #include <isc/util.h>
@@ -126,3 +127,29 @@ teardown_managers(void **state) {
 
        return 0;
 }
+
+isc_result_t
+file_path_to_groupname(const char *path, char *out, size_t outlen) {
+       char *dir = NULL;
+       const char *base = NULL;
+       const char *parent_basename;
+       const char *file_basename;
+
+       REQUIRE(path != NULL);
+       REQUIRE(out != NULL);
+
+       RETERR(isc_file_splitpath(isc_g_mctx, path, &dir, &base));
+
+       parent_basename = isc_file_basename(dir);
+       file_basename = isc_file_basename(base);
+
+       if (strlen(parent_basename) + 1 + strlen(file_basename) + 1 > outlen) {
+               isc_mem_free(isc_g_mctx, dir);
+               return ISC_R_NOSPACE;
+       }
+
+       snprintf(out, outlen, "%s_%s", parent_basename, file_basename);
+
+       isc_mem_free(isc_g_mctx, dir);
+       return ISC_R_SUCCESS;
+}