Skip to content

Commit

Permalink
Add code coverage
Browse files Browse the repository at this point in the history
- close #20

- makefile gained targets to estimate code coverage
* 'make cov' = same as 'make test' but with code coverage support
* 'make cov_run' = run unit tests and gcov on each source file (which
were in a previous step compiled with 'make cov')

- new bash script 'run_gcov.sh' which executes 'gcov' for each source
file; used by 'make cov_run'

- updated travis-ci yml to run code coverage unit tests and connect to
'codecov' server
  • Loading branch information
dschlaep committed Nov 29, 2017
1 parent 8e17910 commit 76af7c5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ language: c
compiler:
- clang
- gcc

script:
- make bin
- make test test_run
- make cleaner
- make cov cov_run

after_success:
- bash <(curl -s https://codecov.io/bash) || echo "Codecov did not collect coverage reports"
34 changes: 28 additions & 6 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
#-----------------------------------------------------------------------------------
# 05/24/2012 (DLM)
# 07/14/2017 (drs)
# for use in terminal while in the project source directory to make compiling easier
#-----------------------------------------------------------------------------------
# commands explanations
#-----------------------------------------------------------------------------------
# make all creates a shared object for use in rSOILWAT2 ('all' target is required
Expand All @@ -14,8 +10,12 @@
# make lib create SOILWAT2 library
# make test compile unit tests in 'test/ folder with googletest
# make test_run run unit tests (in a previous step compiled with 'make test')
# make test_clean delete test files and libraries
# make cov same as 'make test' but with code coverage support
# make cov_run run unit tests and gcov on each source file (in a previous step
# compiled with 'make cov')
# make clean delete all of the o files
# make test_clean delete test files and libraries
# make cov_clean delete files associated with code coverage
# make cleaner delete all of the o files, the shared object file(s), test files and
# libraries, and the binary exe
#-----------------------------------------------------------------------------------
Expand All @@ -26,6 +26,7 @@ uname_m = $(shell uname -m)
# CXX = g++
CFLAGS = -O3 -Wall -Wextra -pedantic -std=c11
CXXFLAGS = -Wall -Wextra -std=gnu++11 # gnu++11 required for googletest on Windows/cygwin
CovFlags = -coverage -g -O0
LDFLAGS = -L.
LDLIBS = -l$(target) -lm # order of libraries is important for GNU gcc (libSOILWAT2 depends on libm)

Expand Down Expand Up @@ -57,6 +58,7 @@ target = SOILWAT2
bin_test = sw_test
lib_target = lib$(target).a
lib_target++ = lib$(target)++.a
lib_covtarget++ = libcov$(target)++.a
SHLIB = r$(target)$(SHLIB_EXT)

gtest = gtest
Expand All @@ -66,6 +68,7 @@ GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)
GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
$(GTEST_DIR)/include/gtest/internal/*.h
gtest_LDLIBS = -l$(gtest) -l$(target)++ -lm
cov_LDLIBS = -l$(gtest) -lcov$(target)++ -lm


all: $(SHLIB)
Expand All @@ -86,6 +89,10 @@ $(lib_target++) :
ar -rcsu $(lib_target++) $(objects_tests)
@rm -f $(objects_tests)

$(lib_covtarget++) :
$(CXX) $(CXXFLAGS) $(CovFlags) -c $(sources_tests)
ar -rcsu $(lib_covtarget++) $(objects_tests)
@rm -f $(objects_tests)

bin : $(target)

Expand Down Expand Up @@ -116,9 +123,19 @@ test : $(lib_gtest) $(lib_target++)
$(CXX) $(CXXFLAGS) $(LDFLAGS) -isystem ${GTEST_DIR}/include -pthread \
test/*.cc -o $(bin_test) $(gtest_LDLIBS)

.PHONY : test_run
test_run : test
./$(bin_test)

cov : cov_clean $(lib_gtest) $(lib_covtarget++)
$(CXX) $(CXXFLAGS) $(CovFlags) $(LDFLAGS) -isystem ${GTEST_DIR}/include \
-pthread test/*.cc -o $(bin_test) $(cov_LDLIBS)

.PHONY : cov_run
cov_run : cov
./$(bin_test)
./run_gcov.sh


.PHONY : clean
clean :
Expand All @@ -134,5 +151,10 @@ clean2 :
test_clean :
@rm -f gtest-all.o $(lib_gtest) $(bin_test)

.PHONY : cov_clean
cov_clean :
@rm -f $(lib_covtarget++) *.gcda *.gcno *.gcov
@rm -fr *.dSYM

.PHONY : cleaner
cleaner : clean clean2 test_clean
cleaner : clean clean2 test_clean cov_clean
12 changes: 12 additions & 0 deletions run_gcov.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

sources_tests='SW_Main_lib.c SW_VegEstab.c SW_Control.c generic.c
rands.c Times.c mymemory.c filefuncs.c
SW_Files.c SW_Model.c SW_Site.c SW_SoilWater.c
SW_Markov.c SW_Weather.c SW_Sky.c SW_Output_mock.c
SW_VegProd.c SW_Flow_lib.c SW_Flow.c SW_Carbon.c'

for sf in $sources_tests
do
gcov $sf
done

0 comments on commit 76af7c5

Please sign in to comment.