Skip to content

Commit abeb1af

Browse files
committed
add coverage
1 parent 80c01d4 commit abeb1af

File tree

3 files changed

+106
-6
lines changed

3 files changed

+106
-6
lines changed

.github/workflows/test-linux.yml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ jobs:
3131
make
3232
sudo make install
3333
34-
- name: Install Doxygen
34+
- name: Install Doxygen, clang-format, lcov
3535
run: |
3636
sudo apt update
37-
sudo apt-get install doxygen graphviz clang-format-9
37+
sudo apt-get install doxygen graphviz clang-format-9 lcov
3838
3939
- name: Configure build
40-
run: cmake -Bbuild -DBUILD_SHARED_LIBS=ON -DWARNINGS_AS_ERRORS=ON .
40+
run: cmake -Bbuild -DCMAKE_BUILD_TYPE=Debug -DBUILD_SHARED_LIBS=ON -DWARNINGS_AS_ERRORS=ON .
4141

4242
- name: Formatting check
4343
working-directory: build
@@ -50,6 +50,11 @@ jobs:
5050
working-directory: build
5151
run: make
5252

53-
- name: Test SQLite3
53+
- name: Tests
5454
working-directory: build
55-
run: sqlite3 -echo -bail < "../test/test.sql"
55+
run: make coverage
56+
57+
- uses: coverallsapp/github-action@master
58+
with:
59+
path-to-lcov: ./build/coverage.cleaned.info
60+
github-token: ${{ secrets.GITHUB_TOKEN }}

CMakeLists.txt

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ project(h3-sqlite3 LANGUAGES C)
1010

1111
set(H3EXT_VERSION 4.0.0)
1212

13+
option(ENABLE_COVERAGE "Enable compiling tests with coverage." ON)
14+
1315
set(LIB_SOURCE_FILES src/h3ext.c src/h3ext.h)
1416
set(OTHER_SOURCE_FILES "")
1517

@@ -28,6 +30,13 @@ if(NOT WIN32)
2830

2931
target_compile_options(h3ext PRIVATE $<$<CONFIG:Debug>:-gdwarf-2 -g3 -O0 -fno-inline -fno-eliminate-unused-debug-types>)
3032

33+
if(ENABLE_COVERAGE)
34+
target_compile_options(h3ext PRIVATE $<$<CONFIG:Debug>:--coverage>)
35+
# --coverage is not passed to the linker, so this option is needed
36+
# to fully enable coverage.
37+
target_link_options(h3ext PRIVATE $<$<CONFIG:Debug>:--coverage>)
38+
endif()
39+
3140
option(WARNINGS_AS_ERRORS "Warnings are treated as errors" OFF)
3241
if(WARNINGS_AS_ERRORS)
3342
target_compile_options(h3ext PRIVATE -Werror)
@@ -93,5 +102,37 @@ else()
93102
)
94103
endif()
95104

96-
# TODO: Testing
105+
if(ENABLE_COVERAGE)
106+
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/scripts/coverage.sh"
107+
INPUT "${CMAKE_CURRENT_SOURCE_DIR}/scripts/coverage.sh.in")
108+
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "coverage.info")
109+
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "coverage.cleaned.info")
110+
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "coverage")
111+
add_custom_target(coverage
112+
COMMAND bash "${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/scripts/coverage.sh" "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}")
113+
add_custom_target(clean-coverage
114+
# Before running coverage, clear all counters
115+
COMMAND lcov --rc lcov_branch_coverage=1 --directory '${CMAKE_CURRENT_BINARY_DIR}' --zerocounters
116+
COMMENT "Zeroing counters"
117+
)
118+
endif()
119+
120+
enable_testing()
121+
122+
macro(add_h3sqlite_test name srcfile)
123+
add_test(NAME ${name} COMMAND sh -c "sqlite3 -echo -bail < ${CMAKE_CURRENT_SOURCE_DIR}/${srcfile}")
124+
125+
if(ENABLE_COVERAGE)
126+
add_custom_target(${name}_coverage
127+
COMMAND sh -c "sqlite3 -bail <${CMAKE_CURRENT_SOURCE_DIR}/${srcfile}"
128+
COMMENT "Running ${name}_coverage"
129+
)
130+
131+
add_dependencies(coverage ${name}_coverage)
132+
add_dependencies(${name}_coverage clean-coverage)
133+
endif()
134+
endmacro()
135+
136+
add_h3sqlite_test(test_sql test/test.sql)
137+
97138
# TODO: Installation

scripts/coverage.sh.in

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2018-2019 Uber Technologies, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# To invoke this script, use `bash coverage.sh <source-dir> <binary-dir>`,
18+
# where `source-dir` corresponds to CMake source directory and `binary-dir`
19+
# corresponds to the CMake build directory. This script is used to run the
20+
# `coverage` target. In order to guarantee that `coverage` is only a valid
21+
# target in debug builds, we must know the build mode. However, CMake tries to
22+
# delay the selection of the build mode until the build itself. This allows
23+
# tools like Xcode to choose to build in debug mode or release mode without
24+
# rerunning CMake. In order to control the `coverage` target based on the build
25+
# mode, this script is regenerated whenever a new build mode is selected,
26+
# regardless of whether or not the `cmake` command is invoked again.
27+
#
28+
# Example:
29+
#
30+
# ```
31+
# $ cmake -DCMAKE_BUILD_TYPE=Debug ..
32+
# $ xcodebuild -configuration Release # coverage.sh generated
33+
# $ xcodebuild -configuration Release # coverage.sh not regenerated
34+
# $ xcodebuild -configuration Debug # coverage.sh regenerated
35+
# ```
36+
37+
set -e
38+
39+
if [[ $<BOOL:$<CONFIG:Debug>> != 1 ]]; then
40+
echo "Cannot run coverage for non-debug build" 1>&2
41+
exit 1
42+
fi
43+
44+
src_dir=${1:-"Missing source directory"}
45+
binary_dir=${2:-"Missing binary directory"}
46+
47+
# Exclude the usual LCOV exclusion comment, and also
48+
# do not require branch coverage for assertions.
49+
br_exclusion='LCOV_EXCL_BR_LINE|assert\('
50+
51+
cd "${binary_dir}"
52+
lcov --rc lcov_branch_coverage=1 --rc "lcov_excl_br_line=$br_exclusion" --directory . --capture --output-file coverage.info
53+
lcov --rc lcov_branch_coverage=1 --rc "lcov_excl_br_line=$br_exclusion" --extract coverage.info "${src_dir}/src/*" --output-file coverage.cleaned.info
54+
genhtml --branch-coverage -o coverage coverage.cleaned.info --title 'h3 coverage'

0 commit comments

Comments
 (0)