Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add coverage #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 54 additions & 10 deletions .github/workflows/test-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ on:
branches: [master, stable-*]

jobs:
tests:
name: Test Compile ${{ matrix.compiler }}
test-clang:
name: Test Compile clang
runs-on: ubuntu-latest
env:
CC: ${{ matrix.compiler }}

strategy:
matrix:
compiler: [clang, gcc]
CC: clang

steps:
- uses: actions/checkout@v2.4.0
Expand All @@ -31,7 +27,7 @@ jobs:
make
sudo make install

- name: Install Doxygen
- name: Install Doxygen, clang-format
run: |
sudo apt update
sudo apt-get install doxygen graphviz clang-format-9
Expand All @@ -50,6 +46,54 @@ jobs:
working-directory: build
run: make

- name: Test SQLite3
- name: Tests
working-directory: build
run: sqlite3 -echo -bail < "../test/test.sql"
run: make test

coverage-gcc:
name: Test Compile gcc
runs-on: ubuntu-latest
env:
CC: gcc

steps:
- uses: actions/checkout@v2.4.0

- name: Install H3
run: |
cd /tmp
git clone https://github.com/uber/h3
cd h3
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS=-fPIC -DBUILD_BENCHMARKS​=OFF -DBUILD_FILTERS=OFF -DBUILD_GENERATORS=OFF -DBUILD_TESTING=OFF -DENABLE_DOCS=OFF -DBUILD_FUZZERS=OFF ..
make
sudo make install

- name: Install Doxygen, clang-format, lcov
run: |
sudo apt update
sudo apt-get install doxygen graphviz clang-format-9 lcov

- name: Configure build
run: cmake -Bbuild -DCMAKE_BUILD_TYPE=Debug -DBUILD_SHARED_LIBS=ON -DWARNINGS_AS_ERRORS=ON .

- name: Formatting check
working-directory: build
run: |
clang-format-9 --version
make format
git diff --exit-code

- name: Build
working-directory: build
run: make

- name: Tests
working-directory: build
run: make coverage

- uses: coverallsapp/github-action@master
with:
path-to-lcov: ./build/coverage.cleaned.info
github-token: ${{ secrets.GITHUB_TOKEN }}
43 changes: 42 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ project(h3-sqlite3 LANGUAGES C)

set(H3EXT_VERSION 4.0.0)

option(ENABLE_COVERAGE "Enable compiling tests with coverage." ON)

set(LIB_SOURCE_FILES src/h3ext.c src/h3ext.h)
set(OTHER_SOURCE_FILES "")

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

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

if(ENABLE_COVERAGE)
target_compile_options(h3ext PRIVATE $<$<CONFIG:Debug>:--coverage>)
# --coverage is not passed to the linker, so this option is needed
# to fully enable coverage.
target_link_options(h3ext PRIVATE $<$<CONFIG:Debug>:--coverage>)
endif()

option(WARNINGS_AS_ERRORS "Warnings are treated as errors" OFF)
if(WARNINGS_AS_ERRORS)
target_compile_options(h3ext PRIVATE -Werror)
Expand Down Expand Up @@ -93,5 +102,37 @@ else()
)
endif()

# TODO: Testing
if(ENABLE_COVERAGE)
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/scripts/coverage.sh"
INPUT "${CMAKE_CURRENT_SOURCE_DIR}/scripts/coverage.sh.in")
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "coverage.info")
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "coverage.cleaned.info")
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "coverage")
add_custom_target(coverage
COMMAND bash "${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/scripts/coverage.sh" "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}")
add_custom_target(clean-coverage
# Before running coverage, clear all counters
COMMAND lcov --rc lcov_branch_coverage=1 --directory '${CMAKE_CURRENT_BINARY_DIR}' --zerocounters
COMMENT "Zeroing counters"
)
endif()

enable_testing()

macro(add_h3sqlite_test name srcfile)
add_test(NAME ${name} COMMAND sh -c "sqlite3 -echo -bail < ${CMAKE_CURRENT_SOURCE_DIR}/${srcfile}")

if(ENABLE_COVERAGE)
add_custom_target(${name}_coverage
COMMAND sh -c "sqlite3 -bail <${CMAKE_CURRENT_SOURCE_DIR}/${srcfile}"
COMMENT "Running ${name}_coverage"
)

add_dependencies(coverage ${name}_coverage)
add_dependencies(${name}_coverage clean-coverage)
endif()
endmacro()

add_h3sqlite_test(test_sql test/test.sql)

# TODO: Installation
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# h3-sqlite3

[![Coverage Status](https://coveralls.io/repos/github/isaacbrodsky/h3-sqlite3/badge.svg?branch=master)](https://coveralls.io/github/isaacbrodsky/h3-sqlite3?branch=master)
[![test-linux](https://github.com/isaacbrodsky/h3-sqlite3/workflows/test-linux/badge.svg)](https://github.com/isaacbrodsky/h3-sqlite3/actions)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE)

Expand Down
54 changes: 54 additions & 0 deletions scripts/coverage.sh.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env bash

# Copyright 2018-2019 Uber Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# To invoke this script, use `bash coverage.sh <source-dir> <binary-dir>`,
# where `source-dir` corresponds to CMake source directory and `binary-dir`
# corresponds to the CMake build directory. This script is used to run the
# `coverage` target. In order to guarantee that `coverage` is only a valid
# target in debug builds, we must know the build mode. However, CMake tries to
# delay the selection of the build mode until the build itself. This allows
# tools like Xcode to choose to build in debug mode or release mode without
# rerunning CMake. In order to control the `coverage` target based on the build
# mode, this script is regenerated whenever a new build mode is selected,
# regardless of whether or not the `cmake` command is invoked again.
#
# Example:
#
# ```
# $ cmake -DCMAKE_BUILD_TYPE=Debug ..
# $ xcodebuild -configuration Release # coverage.sh generated
# $ xcodebuild -configuration Release # coverage.sh not regenerated
# $ xcodebuild -configuration Debug # coverage.sh regenerated
# ```

set -e

if [[ $<BOOL:$<CONFIG:Debug>> != 1 ]]; then
echo "Cannot run coverage for non-debug build" 1>&2
exit 1
fi

src_dir=${1:-"Missing source directory"}
binary_dir=${2:-"Missing binary directory"}

# Exclude the usual LCOV exclusion comment, and also
# do not require branch coverage for assertions.
br_exclusion='LCOV_EXCL_BR_LINE|assert\('

cd "${binary_dir}"
lcov --rc lcov_branch_coverage=1 --rc "lcov_excl_br_line=$br_exclusion" --directory . --capture --output-file coverage.info
lcov --rc lcov_branch_coverage=1 --rc "lcov_excl_br_line=$br_exclusion" --extract coverage.info "${src_dir}/src/*" --output-file coverage.cleaned.info
genhtml --branch-coverage -o coverage coverage.cleaned.info --title 'h3 coverage'