Automate code coverage measurement 27/243027/6
authorDariusz Michaluk <d.michaluk@samsung.com>
Wed, 2 Sep 2020 12:48:36 +0000 (14:48 +0200)
committerDariusz Michaluk <d.michaluk@samsung.com>
Tue, 3 Nov 2020 15:35:53 +0000 (16:35 +0100)
To gather unit tests coverage report:
- use COVERAGE build_type,
- instal askuser-notification-coverage rpm,
- run askuser-notification-coverage.sh script.

Change-Id: I64f5fc86391f432906532d2e0dcd216c085df4c7

CMakeLists.txt
packaging/askuser-notification.spec
test/unit-tests/CMakeLists.txt
test/unit-tests/askuser-notification-coverage.sh.in [new file with mode: 0644]

index d029880a290e10b5fdf296582c454ecb68d31895..4c51562597a143f93c4fcc3035b3a647c643810b 100644 (file)
@@ -20,7 +20,7 @@
 ############################# Check minimum CMake version #####################
 
 CMAKE_MINIMUM_REQUIRED(VERSION 2.8.3)
-PROJECT("askuser")
+PROJECT("askuser-notification")
 SET(ASKUSER_VERSION_MAJOR 0)
 SET(ASKUSER_VERSION ${ASKUSER_VERSION_MAJOR}.6.0)
 
@@ -36,12 +36,20 @@ SET(BIN_INSTALL_DIR
     CACHE PATH
     "Binary installation directory")
 
+IF(NOT DEFINED COVERAGE_DIR)
+SET(COVERAGE_DIR
+    "${SHARE_INSTALL_PREFIX}/${PROJECT_NAME}-coverage"
+    CACHE PATH
+    "Read-only coverage data directory")
+ENDIF(NOT DEFINED COVERAGE_DIR)
+ADD_DEFINITIONS("-DCOVERAGE_DIR=\"${COVERAGE_DIR}\"")
+
 ############################# compiler flags ##################################
 
 SET(CMAKE_CXX_FLAGS_PROFILING  "-O0 -g -pg")
-SET(CMAKE_CXX_FLAGS_DEBUG      "-O0 -g -ggdb")
+SET(CMAKE_CXX_FLAGS_DEBUG      "-O0 -g -ggdb -Wp,-U_FORTIFY_SOURCE")
 SET(CMAKE_CXX_FLAGS_RELEASE    "-O2 -g")
-SET(CMAKE_CXX_FLAGS_CCOV       "-O2 -g --coverage")
+SET(CMAKE_CXX_FLAGS_COVERAGE   "-O0 -g -ggdb --coverage -Wp,-U_FORTIFY_SOURCE")
 
 # Enable C++17
 SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
@@ -78,7 +86,7 @@ SET(TARGET_ASKUSER_NOTIFICATION_LIB_TEST "askuser-notification-test")
 SET(TARGET_ASKUSER_NOTIFICATION_CLIENT_LIB "askuser-notification-client")
 SET(TARGET_PRIVACY_PRIVILEGE_MANAGER_CAPI_LIB "capi-privacy-privilege-manager")
 SET(TARGET_PRIVACY_PRIVILEGE_MANAGER_TEST "capi-privacy-privilege-manager-test")
-SET(TARGET_ASKUSER_UNIT_TESTS "askuser-unit-tests")
+SET(TARGET_ASKUSER_UNIT_TESTS "askuser-notification-unit-tests")
 
 ADD_SUBDIRECTORY(src/plugin)
 ADD_SUBDIRECTORY(src/notification-daemon)
index de5eb032de94b254f900d7f00d04d2e950a5f597..bb97fee9b9b6d3b79db87d7e139a2b472ad2cd70 100644 (file)
@@ -1,3 +1,5 @@
+%{!?build_type:%global build_type RELEASE}
+
 %if !%{defined with_systemd_daemon}
 %define with_systemd_daemon 1
 %endif
@@ -29,9 +31,8 @@ BuildRequires: pkgconfig(capi-base-common)
 BuildRequires: pkgconfig(libsmack)
 BuildRequires: pkgconfig(aul)
 BuildRequires: boost-devel
-
-%if !%{defined build_type}
-%define build_type RELEASE
+%if "%{build_type}" == "COVERAGE"
+BuildRequires: lcov
 %endif
 
 %description
@@ -60,11 +61,11 @@ Summary: Tools for testing askuser-notification client and C API
 Provides tests for checking the API provided by capi-privacy-privilege-manager
 and askuser-notification-client
 
-%package -n askuser-unit-tests
+%package unit-tests
 Summary:    Askuser unit test binaries
 Group:      Security/Development
 
-%description -n askuser-unit-tests
+%description unit-tests
 Internal test for Askuser implementation.
 
 %package client
@@ -90,6 +91,21 @@ Additionally, for privacy privileges, it provides an ability to determine
 their statuses by displaying an appropriate pop-up dialog box. A user
 can make a decision whether to give a privilege to an application or not.
 
+%if "%{build_type}" == "COVERAGE"
+
+%package coverage
+Summary:    Askuser code coverage data
+Group:      Security/Testing
+Requires:   askuser-notification-unit-tests = %{version}-%{release}
+Requires:   askuser-notification-debugsource = %{version}-%{release}
+Requires:   lcov
+Requires:   gcc
+
+%description coverage
+Askuser code coverage data
+
+%endif
+
 %prep
 %setup -q
 
@@ -98,17 +114,16 @@ can make a decision whether to give a privilege to an application or not.
 export CXXFLAGS="$CXXFLAGS -DTIZEN_DEBUG_ENABLE"
 %endif
 
-%if %{?build_type} == "DEBUG"
-export CXXFLAGS="$CXXFLAGS -Wp,-U_FORTIFY_SOURCE"
-%endif
-
 export CXXFLAGS="$CXXFLAGS -Wno-implicit-fallthrough"
 export LDFLAGS+="-Wl,--rpath=%{_libdir}"
 
+%global coverage_dir %{_datadir}/askuser-notification-coverage
+
 %cmake . \
         -DCMAKE_BUILD_TYPE=%{?build_type} \
         -DBUILD_WITH_SYSTEMD_DAEMON=%{?with_systemd_daemon} \
-        -DCMAKE_VERBOSE_MAKEFILE=ON
+        -DCMAKE_VERBOSE_MAKEFILE=ON \
+        -DCOVERAGE_DIR=%{coverage_dir}
 make %{?jobs:-j%jobs}
 
 %install
@@ -180,10 +195,10 @@ systemctl restart cynara.service
 %attr(755,root,root) %{_bindir}/askuser-notification-test
 %attr(755,root,root) %{_bindir}/capi-privacy-privilege-manager-test
 
-%files -n askuser-unit-tests
+%files unit-tests
 %manifest %{_datadir}/default.manifest
 %license LICENSE
-%attr(755,root,root) %{_bindir}/askuser-unit-tests
+%attr(755,root,root) %{_bindir}/askuser-notification-unit-tests
 
 %files client
 %manifest %{_datadir}/default.manifest
@@ -194,3 +209,13 @@ systemctl restart cynara.service
 %manifest %{_datadir}/default.manifest
 %license LICENSE
 %attr(644,-,-) %{_libdir}/libcapi-privacy-privilege-manager.so.*
+
+%if "%{build_type}" == "COVERAGE"
+
+%files coverage
+%manifest %{_datadir}/default.manifest
+%license LICENSE
+%{_bindir}/askuser-notification-coverage.sh
+%coverage_dir
+
+%endif
index 7f733db8417f1c814037222b8dd31dc1ca04462e..4b2a08a252e5e959887c8520172aac07a6f9222c 100644 (file)
@@ -14,6 +14,29 @@ SET(ASKUSER_TESTS_PATH ${PROJECT_SOURCE_DIR}/test/unit-tests)
 
 ADD_DEFINITIONS("-DBOOST_TEST_DYN_LINK")
 
+IF (CMAKE_BUILD_TYPE MATCHES "COVERAGE")
+
+    # coverage data
+    SET(COVERAGE_BUILD_DIR
+        ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${TARGET_ASKUSER_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(askuser-notification-coverage.sh.in askuser-notification-coverage.sh @ONLY)
+    INSTALL(
+        PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/askuser-notification-coverage.sh
+        DESTINATION ${BIN_INSTALL_DIR}
+    )
+
+ENDIF (CMAKE_BUILD_TYPE MATCHES "COVERAGE")
+
 INCLUDE_DIRECTORIES(SYSTEM ${ASKUSER_UNIT_TESTS_DEP_INCLUDE_DIRS})
 INCLUDE_DIRECTORIES(
     ${ASKUSER_SRC_PATH}/common
@@ -41,4 +64,4 @@ TARGET_LINK_LIBRARIES(${TARGET_ASKUSER_UNIT_TESTS}
     ${Boost_LIBRARIES}
 )
 
-INSTALL(TARGETS ${TARGET_ASKUSER_UNIT_TESTS} DESTINATION bin)
+INSTALL(TARGETS ${TARGET_ASKUSER_UNIT_TESTS} DESTINATION ${BIN_INSTALL_DIR})
diff --git a/test/unit-tests/askuser-notification-coverage.sh.in b/test/unit-tests/askuser-notification-coverage.sh.in
new file mode 100644 (file)
index 0000000..6bcb43e
--- /dev/null
@@ -0,0 +1,39 @@
+#!/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@-@ASKUSER_VERSION@"
+
+# create dir for the report
+mkdir $HTML_DIR
+
+# remove old gcda files
+find / -iname "*.gcda" -exec rm {} \;
+
+# launch unit tests
+askuser-notification-unit-tests
+
+# 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
+lcov --no-external -c -d "@COVERAGE_DIR@" -b "@CMAKE_BINARY_DIR@" -o $REPORT 2>$STDERR
+lcov -r $REPORT "@CMAKE_BINARY_DIR@/test/unit-tests/*" -o $REPORT
+
+# check warnings
+if [ -s $STDERR ]
+then
+       echo "Warnings detected (see $STDERR). Aborting."
+       exit 1
+fi
+
+# html
+genhtml $REPORT --output-directory $HTML_DIR/