Skip to content

Commit efeaa9b

Browse files
committed
Improve example with tests and 3rd party dependencies
- Fake install dependencies into $HOME/.cache (cached) - Move test files into own folder - Exclude test files and 3rd party files from coverage
1 parent 46a1cd0 commit efeaa9b

File tree

5 files changed

+38
-23
lines changed

5 files changed

+38
-23
lines changed

.travis.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,34 @@ language: cpp
22
os: linux
33
compiler: gcc
44

5+
cache:
6+
- directories:
7+
- $HOME/.cache
8+
59
addons:
610
apt:
711
packages: lcov
812

913
before_script:
1014
- mkdir -p build && cd build
1115

16+
install:
17+
# (fake) install dependencies
18+
- mkdir -p $HOME/.cache
19+
- touch $HOME/.cache/mydepency.cpp
20+
1221
script:
1322
- cmake -DCODE_COVERAGE=ON ..
1423
- cmake --build . --config Debug -- -j $(nproc)
1524
- ctest -j $(nproc) --output-on-failure
1625

1726
after_success:
1827
# Create lcov report
19-
- lcov --capture --directory . --output-file coverage.info
20-
- lcov --remove coverage.info '/usr/*' --output-file coverage.info # filter system-files
28+
- lcov --directory . --capture --output-file coverage.info # capture coverage info
29+
- lcov --remove coverage.info '/usr/*' '*/tests/*' "${HOME}"'/.cache/*' --output-file coverage.info # filter out system
2130
- lcov --list coverage.info
2231
# Uploading to CodeCov
32+
# '-f' specifies file(s) to use and disables manual coverage gathering and file search which has already been done above
2333
- bash <(curl -s https://codecov.io/bash) -f coverage.info || echo "Codecov did not collect coverage reports"
2434

2535
notifications:

CMakeLists.txt

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
cmake_minimum_required (VERSION 3.0)
1+
cmake_minimum_required (VERSION 3.1)
22
project(Example LANGUAGES CXX)
33

4-
option(CODE_COVERAGE "Enable coverage reporting" OFF)
5-
6-
# Common configuration and force C++ standard
7-
add_compile_options(-Wall -pedantic)
8-
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.1)
9-
set(CMAKE_CXX_STANDARD 11)
10-
else()
11-
add_compile_options(-std=c++11)
12-
endif()
13-
4+
# Use C++11
5+
set(CMAKE_CXX_STANDARD 11)
6+
# Require (at least) it
7+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
8+
# Don't use e.g. GNU extension (like -std=gnu++11) for portability
9+
set(CMAKE_CXX_EXTENSIONS OFF)
1410

1511
# Code Coverage Configuration
1612
add_library(coverage_config INTERFACE)
1713

18-
if(CODE_COVERAGE)
14+
option(CODE_COVERAGE "Enable coverage reporting" OFF)
15+
if(CODE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
1916
# Add required flags (GCC & LLVM/Clang)
2017
target_compile_options(coverage_config INTERFACE
2118
-O0 # no optimization
@@ -30,5 +27,13 @@ if(CODE_COVERAGE)
3027
endif(CODE_COVERAGE)
3128

3229

33-
enable_testing() # CTest
3430
add_subdirectory(src)
31+
32+
option (BUILD_TESTING "Build the testing tree." ON)
33+
# Only build tests if we are the top-level project
34+
# Allows this to be used by super projects with `add_subdirectory`
35+
if (BUILD_TESTING AND (PROJECT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR))
36+
enable_testing()
37+
add_subdirectory(tests)
38+
endif()
39+

src/CMakeLists.txt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
cmake_minimum_required(VERSION 3.0)
2-
3-
# The library
41
add_library(example complex.cpp)
52
target_include_directories(example PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
3+
# Setting this to the library only avoids errors in 3rd party dependencies which are compile with e.g. -Werror
4+
target_compile_options(example PUBLIC -Wall -pedantic)
65
# Include code-coverage settings:
76
target_link_libraries(example PUBLIC coverage_config)
87

9-
10-
# Tests for the library
11-
add_executable(tests complex_main.cpp)
12-
target_link_libraries(tests PRIVATE example) # inherits coverage_config
13-
add_test(NAME example_test COMMAND tests)

tests/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
add_executable(tests complex_main.cpp)
2+
# Linking up all libraries (includes the coverage settings set by 'example' library)
3+
target_link_libraries(tests PRIVATE example)
4+
5+
add_test(NAME example_test COMMAND tests)
6+
File renamed without changes.

0 commit comments

Comments
 (0)