tests: Restructure test 89/298589/2
authorYoungjae Cho <y0.cho@samsung.com>
Thu, 7 Sep 2023 08:03:56 +0000 (17:03 +0900)
committerYoungjae Cho <y0.cho@samsung.com>
Mon, 16 Oct 2023 07:43:37 +0000 (16:43 +0900)
Instead of building testsuite into a shared object and the main()
dlopens it, the macro
 * #define TESTSUITE()
has been introduced for testsuite. It runs the testsuite and report
to the main() how many testcases have failed.

Change-Id: I6a9330601b96f7795442a85631dc666a97fe6d47
Signed-off-by: Youngjae Cho <y0.cho@samsung.com>
tests/CMakeLists.txt
tests/libcommon/CMakeLists.txt [deleted file]
tests/libcommon/test-common.c
tests/test-main.c
tests/test-main.h
tests/test-mock.c

index bfd44ea..132c796 100644 (file)
@@ -1,20 +1,29 @@
 SET(TEST_DRIVER "test-driver")
 
-FILE(GLOB SRCS "${CMAKE_CURRENT_SOURCE_DIR}/*.c")
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)
 
-SET(TEST_SUITE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
-ADD_DEFINITIONS("-DTEST_SUITE_DIRECTORY=\"${TEST_SUITE_DIRECTORY}\"")
+FILE(GLOB TEST_DRIVER_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/*.c")
+FILE(GLOB LIBCOMMON_SRCS
+       "${CMAKE_SOURCE_DIR}/tests/libcommon/*.c"
+       "${CMAKE_SOURCE_DIR}/src/libcommon/*.c")
+
+SET(SRCS
+       ${TEST_DRIVER_SRCS}
+       ${LIBCOMMON_SRCS}
+       )
+
+SET(WRAP_FLAGS "${WRAP_FLAGS} -Wl,--wrap=open")
+SET(WRAP_FLAGS "${WRAP_FLAGS} -Wl,--wrap=open64")
 
 INCLUDE(FindPkgConfig)
 PKG_CHECK_MODULES(REQUIRED_PKGS REQUIRED
+       glib-2.0
+       capi-system-info
        cmocka)
 
 ADD_EXECUTABLE(${TEST_DRIVER} ${SRCS})
-TARGET_LINK_LIBRARIES(${TEST_DRIVER} cmocka dl)
-SET_TARGET_PROPERTIES(${TEST_DRIVER} PROPERTIES LINK_FLAGS "-Wl,--export-dynamic")
+TARGET_LINK_LIBRARIES(${TEST_DRIVER} ${REQUIRED_PKGS_LDFLAGS})
+SET_TARGET_PROPERTIES(${TEST_DRIVER} PROPERTIES LINK_FLAGS "${WRAP_FLAGS} -Wl,--export-dynamic")
 
 ADD_CUSTOM_TARGET(run-test ALL "./${TEST_DRIVER}")
 ADD_DEPENDENCIES(run-test ${TEST_DRIVER})
-
-# add test suites
-ADD_SUBDIRECTORY(libcommon)
diff --git a/tests/libcommon/CMakeLists.txt b/tests/libcommon/CMakeLists.txt
deleted file mode 100644 (file)
index 9fe27d3..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-SET(TEST_SUITE "test-driver-libcommon")
-
-INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src)
-
-FILE(GLOB SRCS "${CMAKE_CURRENT_SOURCE_DIR}/*.c")
-FILE(GLOB ORIG_SRCS "${CMAKE_SOURCE_DIR}/src/libcommon/*.c")
-
-ADD_LIBRARY(${TEST_SUITE} MODULE ${SRCS} ${ORIG_SRCS})
-SET_TARGET_PROPERTIES(${TEST_SUITE} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${TEST_SUITE_DIRECTORY})
-ADD_DEPENDENCIES(${TEST_DRIVER} ${TEST_SUITE})
-
-SET(WRAP_FLAGS "${WRAP_FLAGS} -Wl,--wrap=open")
-SET(WRAP_FLAGS "${WRAP_FLAGS} -Wl,--wrap=open64")
-SET_TARGET_PROPERTIES(${TEST_SUITE} PROPERTIES LINK_FLAGS ${WRAP_FLAGS})
-
-TARGET_LINK_LIBRARIES(${TEST_SUITE} cmocka glib-2.0)
index f34ce27..9f97bd2 100644 (file)
@@ -6,11 +6,11 @@
 #include <stdbool.h>
 #include <glib.h>
 
-#include "libsyscommon/file.h"
-#include "libsyscommon/list.h"
-#include "libsyscommon/ini-parser.h"
-#include "libsyscommon/bitmap.h"
-#include "libsyscommon/notifier.h"
+#include <libsyscommon/file.h>
+#include <libsyscommon/list.h>
+#include <libsyscommon/ini-parser.h>
+#include <libsyscommon/bitmap.h>
+#include <libsyscommon/notifier.h>
 #include "../test-main.h"
 #include "../test-mock.h"
 
@@ -501,28 +501,24 @@ static void test_syscommon_notifier_emit_notify_priority_p1(void **state)
        syscommon_notifier_emit_notify(SYSCOMMON_NOTIFIER_8, (void *)(intptr_t)0x1234);
 }
 
-int run_test_suite(void)
-{
-       const struct CMUnitTest testsuite[] = {
-               cmocka_unit_test_setup_teardown(test_sys_get_str_p1, setup, teardown),
-               cmocka_unit_test_setup_teardown(test_sys_get_str_n1, setup, teardown),
-               cmocka_unit_test(test_sys_get_str_n2),
-               cmocka_unit_test(test_list_append_p),
-               cmocka_unit_test(test_list_prepend_p),
-               cmocka_unit_test(test_config_parse_p),
-               cmocka_unit_test(test_init_bitmap_p),
-               cmocka_unit_test(test_init_bitmap_n),
-               cmocka_unit_test(test_test_bit),
-               cmocka_unit_test(test_all_bit),
-               cmocka_unit_test(test_syscommon_notifier_emit_notify_p1),
-               cmocka_unit_test(test_syscommon_notifier_emit_notify_p2),
-               cmocka_unit_test(test_syscommon_notifier_emit_notify_p3),
-               cmocka_unit_test(test_syscommon_notifier_emit_notify_once_p),
-               cmocka_unit_test(test_destroy_callback_p1),
-               cmocka_unit_test(test_destroy_callback_p2),
-               cmocka_unit_test(test_destroy_callback_p3),
-               cmocka_unit_test(test_syscommon_notifier_emit_notify_priority_p1),
-       };
-
-       return cmocka_run_group_tests(testsuite, NULL, NULL);
-}
+static const struct CMUnitTest testsuite_libcommon[] = {
+       cmocka_unit_test_setup_teardown(test_sys_get_str_p1, setup, teardown),
+       cmocka_unit_test_setup_teardown(test_sys_get_str_n1, setup, teardown),
+       cmocka_unit_test(test_sys_get_str_n2),
+       cmocka_unit_test(test_list_append_p),
+       cmocka_unit_test(test_list_prepend_p),
+       cmocka_unit_test(test_config_parse_p),
+       cmocka_unit_test(test_init_bitmap_p),
+       cmocka_unit_test(test_init_bitmap_n),
+       cmocka_unit_test(test_test_bit),
+       cmocka_unit_test(test_all_bit),
+       cmocka_unit_test(test_syscommon_notifier_emit_notify_p1),
+       cmocka_unit_test(test_syscommon_notifier_emit_notify_p2),
+       cmocka_unit_test(test_syscommon_notifier_emit_notify_p3),
+       cmocka_unit_test(test_syscommon_notifier_emit_notify_once_p),
+       cmocka_unit_test(test_destroy_callback_p1),
+       cmocka_unit_test(test_destroy_callback_p2),
+       cmocka_unit_test(test_destroy_callback_p3),
+       cmocka_unit_test(test_syscommon_notifier_emit_notify_priority_p1),
+};
+TESTSUITE(testsuite_libcommon)
index 5af3e94..aa899b4 100644 (file)
@@ -6,46 +6,20 @@
 
 #include "test-main.h"
 
-int main(int argc, char *argv[])
-{
-       int fail = 0;
-       DIR *dir;
-       struct dirent *ent;
-
-       dir = opendir(TEST_SUITE_DIRECTORY);
-       if (!dir) {
-               printf("Failed to opendir, %m\n");
-               return 1;
-       }
-
-       while ((ent = readdir(dir))) {
-               void *handle;
-               char sopath[512] = {0, };
-               int (*run_test_suite) (void);
-
-               if (!strstr(ent->d_name, ".so"))
-                       continue;
-
-               snprintf(sopath, sizeof(sopath), "%s/%s", TEST_SUITE_DIRECTORY, ent->d_name);
-
-               handle = dlopen(sopath, RTLD_LAZY);
-               if (!handle) {
-                       printf("Failed to dlopen, %s\n", dlerror());
-                       continue;
-               }
+static int totalfail = 0;
 
-               run_test_suite = dlsym(handle, "run_test_suite");
-               if (!run_test_suite) {
-                       printf("Failed to dlsym, %s\n", dlerror());
-                       dlclose(handle);
-                       continue;
-               }
-
-               fail += run_test_suite();
-               dlclose(handle);
-       }
-
-       closedir(dir);
+void testsuite_report_failcount(int failcount)
+{
+       totalfail += failcount;
+}
 
-       return fail;
+int main(int argc, char *argv[])
+{
+       /**
+        * All test suites will run by constructor before the main() and the totalfail
+        * will be updated when each testsuite calls testsuite_record_failcount(),
+        * notifying about how many testcases they have failed. Therefore, the main()
+        * just need to return totalfail.
+        */
+       return totalfail;
 }
index 4b04a85..0430e4d 100644 (file)
@@ -6,5 +6,13 @@
 #include <setjmp.h>
 #include <cmocka.h>
 
+void testsuite_report_failcount(int failcount);
+
+#define TESTSUITE(cmocka_unittest)                                                             \
+static void __attribute__((constructor)) run(void)                                             \
+{                                                                                              \
+       testsuite_report_failcount(cmocka_run_group_tests(cmocka_unittest, NULL, NULL));        \
+}
+
 #endif //__TEST_MAIN_H__
 
index 4e7f260..58eb80b 100644 (file)
@@ -29,7 +29,6 @@ int __wrap_open64(const char *pathname, int flags)
        return __wrap_open(pathname, flags);
 }
 
-
 void mock_open(const char *pathname, int flags)
 {
        will_return(__wrap_open, pathname);