[libubootenv,v2,6/8] tests: add unit test infrastructure based on cmocka

Message ID 20261113171737.470894-7-dario.binacchi@amarulasolutions.com
State New
Headers
Series Add the FWU metadata storage format |

Commit Message

Dario Binacchi Nov. 13, 2026, 5:17 p.m. UTC
Add the BUILD_TESTS option, disabled by default, to build unit tests
based on cmocka and run them with ctest, and enable it in the CI.

Signed-off-by: Dario Binacchi <dario.binacchi@amarulasolutions.com>
---
Changes in v2:
- New patch

 .gitlab-ci.yml       |  4 +++-
 CMakeLists.txt       |  7 +++++++
 tests/CMakeLists.txt | 11 +++++++++++
 3 files changed, 21 insertions(+), 1 deletion(-)
 create mode 100644 tests/CMakeLists.txt

-- 
2.43.0
  

Patch

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index b546fa42e1cf..9b4d8102a894 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -18,11 +18,12 @@  ubuntu-jammy:
     - apt-get install -y zlib1g-dev
     - apt-get install -y curl
     - apt-get install -y git
+    - apt-get install -y libcmocka-dev
  
   script:
     - if [ ! -d Debug ];then mkdir Debug;fi
     - cd Debug
-    - cmake -DCMAKE_BUILD_TYPE=Debug ..
+    - cmake -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTS=ON ..
     - if [ $CI_COMMIT_BRANCH == "coverity_scan" ]; then
         curl -o /tmp/cov-analysis-linux64.tgz https://scan.coverity.com/download/linux64 --form project=$COVERITY_SCAN_PROJECT_NAME --form token=$COVERITY_SCAN_TOKEN;
         tar xfz /tmp/cov-analysis-linux64.tgz;
@@ -31,6 +32,7 @@  ubuntu-jammy:
         curl https://scan.coverity.com/builds?project=$COVERITY_SCAN_PROJECT_NAME --form token=$COVERITY_SCAN_TOKEN --form email=$GITLAB_USER_EMAIL --form file=@cov-int.tar.gz --form version="`git describe --tags`" --form description="`git describe --tags` / $CI_COMMIT_TITLE / $CI_COMMIT_REF_NAME:$CI_PIPELINE_ID ";
       fi
     - make VERBOSE=1
+    - ctest --output-on-failure
 
 reuse:
   needs: []
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4a2fcb3768e1..071b8e7604e9 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -32,6 +32,13 @@  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
 include_directories ("${PROJECT_SOURCE_DIR}/src")
 add_subdirectory (src)
 
+option(BUILD_TESTS "Build the unit tests" OFF)
+
+if(BUILD_TESTS)
+    enable_testing()
+    add_subdirectory(tests)
+endif(BUILD_TESTS)
+
 # first we can indicate the documentation build as an option and set it to ON by default
 option(BUILD_DOC "Build documentation" ON)
 
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
new file mode 100644
index 000000000000..67626a492436
--- /dev/null
+++ b/tests/CMakeLists.txt
@@ -0,0 +1,11 @@ 
+# SPDX-FileCopyrightText: 2026 Amarula Solutions, Dario Binacchi <dario.binacchi@amarulasolutions.com>
+#
+# SPDX-License-Identifier:     LGPL-2.1-or-later
+
+find_path(CMOCKA_INCLUDE_DIR cmocka.h)
+find_library(CMOCKA_LIBRARY cmocka)
+if(NOT CMOCKA_INCLUDE_DIR OR NOT CMOCKA_LIBRARY)
+  message(FATAL_ERROR "cmocka is required to build the tests")
+endif()
+
+include_directories(${CMOCKA_INCLUDE_DIR})