Automate code coverage measurement 49/312649/3 accepted/tizen/unified/20240620.050722 accepted/tizen/unified/20240621.010432 accepted/tizen/unified/dev/20240701.072857 accepted/tizen/unified/toolchain/20240624.121359 accepted/tizen/unified/x/20240624.031926 accepted/tizen/unified/x/asan/20240625.092220
authorDariusz Michaluk <d.michaluk@samsung.com>
Wed, 12 Jun 2024 12:50:15 +0000 (14:50 +0200)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Thu, 13 Jun 2024 09:49:19 +0000 (09:49 +0000)
To gather unit tests coverage report:
- build with: --define "gcov 1",
- instal webauthn-gcov rpm,
- run webauthn-coverage.sh script.

Change-Id: I9d3cdb38733a412e64bcfea9924b9292d43d633a

CMakeLists.txt
packaging/webauthn.spec
tests/CMakeLists.txt
tests/webauthn-coverage.sh.in [new file with mode: 0644]

index 1554d7a7135b85ae2d996796a8d110d5c7dc1d03..8fc57f5a2aaacbca57683de4cc4b4aace8358a16 100644 (file)
@@ -43,6 +43,10 @@ SET(LDFLAGS "${LDFLAGS} -lgcov")
 ADD_DEFINITIONS("-DGCOV_BUILD=\"${GCOV_BUILD}\"")
 ENDIF(GCOV_BUILD)
 
+IF(NOT DEFINED COVERAGE_DIR)
+    SET(COVERAGE_DIR "${SHARE_INSTALL_PREFIX}/${PROJECT_NAME}-coverage")
+ENDIF(NOT DEFINED COVERAGE_DIR)
+
 # Force PIE
 SET(CMAKE_POSITION_INDEPENDENT_CODE "True")
 SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie")
index 7759763f04ba495a8fd2fffcae4cb7591591c09a..ac9ddc7c89724383a47fffdf39fe63dbf0ee804f 100644 (file)
@@ -20,9 +20,6 @@ Requires(postun): /sbin/ldconfig
 BuildRequires: cmake
 BuildRequires: pkgconfig(dlog)
 BuildRequires: pkgconfig(libsystemd)
-%if 0%{?gcov:1}
-BuildRequires: lcov
-%endif
 
 %description
 Web Authentication Service
@@ -93,6 +90,9 @@ Web Authentication Service (manual test)
 %if 0%{?gcov:1}
 %package gcov
 Summary:    %{name} gcov data
+BuildRequires: lcov
+Requires:   %{name}-unittests = %{version}-%{release}
+Requires:   %{name}-debugsource = %{version}-%{release}
 
 %description gcov
 Base utils gcov objects
@@ -121,8 +121,11 @@ Base utils gcov objects
 %prep
 %setup -q
 
+%global coverage_dir %{_datadir}/%{name}-coverage
+
 %build
 %cmake . -DGCOV_BUILD=%{?gcov:1}%{!?gcov:0} \
+         -DCOVERAGE_DIR=%{coverage_dir} \
          -DPREFIX=%{_prefix} \
          -DEXEC_PREFIX=%{_exec_prefix} \
          -DINCLUDEDIR=%{_includedir} \
@@ -250,6 +253,8 @@ fi
 %if 0%{?gcov:1}
 %files gcov
 %{_datadir}/gcov/obj/*
+%{_bindir}/%{name}-coverage.sh
+%coverage_dir
 %endif
 
 %files -n %{name}-unittests
index aac330f58446f8b0e6aed0a0118d44972b5b3b67..163bd1e917764bd977b0e4800a8bcd36de5bdc5b 100644 (file)
@@ -22,6 +22,27 @@ PKG_CHECK_MODULES(UNIT_TESTS_DEPS
     libsystemd
     )
 
+IF(GCOV_BUILD)
+    # coverage data
+    SET(COVERAGE_BUILD_DIR
+        ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${TARGET_WEBAUTHN_UNIT_TESTS}.dir/
+    )
+
+    # install gcno files
+    INSTALL(
+        DIRECTORY ${COVERAGE_BUILD_DIR}/
+        DESTINATION ${COVERAGE_DIR}
+        FILES_MATCHING PATTERN "*.gcno"
+    )
+
+    # install code coverage automation script
+    CONFIGURE_FILE(${PROJECT_NAME}-coverage.sh.in ${PROJECT_NAME}-coverage.sh @ONLY)
+    INSTALL(
+        PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-coverage.sh
+        DESTINATION ${BIN_DIR}
+    )
+ENDIF(GCOV_BUILD)
+
 SET(UNIT_TESTS_SOURCES
     ${CMAKE_CURRENT_SOURCE_DIR}/unittests.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/test-common.cpp
diff --git a/tests/webauthn-coverage.sh.in b/tests/webauthn-coverage.sh.in
new file mode 100644 (file)
index 0000000..2c7305a
--- /dev/null
@@ -0,0 +1,51 @@
+#!/bin/bash
+
+set -exuo pipefail
+
+REPORT="@PROJECT_NAME@-coverage.info"
+STDERR="@PROJECT_NAME@-coverage.stderr"
+HTML_DIR="@PROJECT_NAME@-coverage"
+
+SRCS_DIR="/usr/src/debug/@PROJECT_NAME@-@VERSION@"
+
+# create dir for the report
+mkdir $HTML_DIR
+
+# remove old gcda files
+find / -iname "*.gcda" -exec rm {} \;
+
+# launch unit tests
+webauthn-unittests
+
+# copy source files
+cp -rp $SRCS_DIR*/* "@CMAKE_BINARY_DIR@"
+
+# copy gcda files
+cp -r "@COVERAGE_BUILD_DIR@"/* "@COVERAGE_DIR@"
+
+# prepare report
+rm -f $STDERR
+
+# Due to mismatch in the version of gcc/gcov and lcov, we need to ignore few errors
+IGNORE_ERRORS=" --ignore-errors unused,unused \
+                               --ignore-errors empty,empty \
+                               --ignore-errors mismatch,mismatch \
+                               --ignore-errors gcov,gcov "
+
+lcov $IGNORE_ERRORS --no-external -c -d "@COVERAGE_DIR@" -b "@CMAKE_BINARY_DIR@" -o $REPORT 2>$STDERR
+lcov $IGNORE_ERRORS -r $REPORT "@CMAKE_BINARY_DIR@/tests/*" -o $REPORT
+
+# Let's skip lcov warning
+# geninfo: WARNING: using JSON module "JSON::PP" - which is much slower than some alternatives.
+# Consider installing one of JSON::XS or Cpanel::JSON::XS
+sed -i '/WARNING/d' $STDERR
+
+# check errors
+if [ -s $STDERR ]
+then
+       echo "Errors detected (see $STDERR). Aborting."
+       exit 1
+fi
+
+# html
+genhtml $REPORT --output-directory $HTML_DIR/