diff --git a/.gitignore b/.gitignore index d59a62b7e1..fa397f98d2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ build tmp +*.gcov diff --git a/testing/Makefile b/testing/Makefile index e34aaca939..c5610fc97b 100644 --- a/testing/Makefile +++ b/testing/Makefile @@ -8,7 +8,7 @@ brief: make-brief coverage distclean: @rm -f coverage.log $(MAKE) -C btest $@ - $(MAKE) -C bro-core-coverage $@ + $(MAKE) -C coverage $@ make-verbose: @for repo in $(DIRS); do (cd $$repo && make -s ); done @@ -23,5 +23,5 @@ coverage: @echo "Complete test suite code coverage:" @./scripts/coverage-calc "brocov.tmp.*" coverage.log `pwd`/../scripts @rm -f brocov.tmp.* - @cd bro-core-coverage && make coverage + @cd coverage && make coverage diff --git a/testing/bro-core-coverage/Makefile b/testing/coverage/Makefile similarity index 52% rename from testing/bro-core-coverage/Makefile rename to testing/coverage/Makefile index e7db4894fd..1aa8036bbf 100644 --- a/testing/bro-core-coverage/Makefile +++ b/testing/coverage/Makefile @@ -7,3 +7,9 @@ cleanup: distclean: cleanup @find ../../ -name "*.gcno" -exec rm {} \; + +html: + @(cd ../../; if which lcov; then\ + lcov --capture --directory . --output-file coverage.info \ + && genhtml coverage.info && rm coverage.info; \ + fi) diff --git a/testing/coverage/README b/testing/coverage/README new file mode 100644 index 0000000000..0c19e14d51 --- /dev/null +++ b/testing/coverage/README @@ -0,0 +1,13 @@ +On a Bro build configured with --enable-coverage, this script produces a code coverage report after Bro has been invoked. The intended application of this script is after the btest testsuite has run. This combination (btests first, coverage computation afterward) happens automatically when running "make" in the testing directory. This script puts .gcov files (which are included in .gitignore) alongside the corresponding source files. + +This depends on gcov, which should come with your gcc. If gcov is not installed, the script will abort with an error message. + +TODO: Use `make html` as make target in this directory to output the html files that gcov can create (must have lcov on system). + +The goal of code-coverage.sh script is to automate code coverage testing. See the following steps for the code structure: + 1. Run test suite + 2. Check for .gcda files existing. + 3a. Run gcov (-p to preserve path) + 3b. Prune .gcov files for objects outside of the Bro tree + 4a. Analyze .gcov files generated and create summary file + 4b. Send .gcov files to appropriate path diff --git a/testing/bro-core-coverage/code_coverage.sh b/testing/coverage/code_coverage.sh similarity index 90% rename from testing/bro-core-coverage/code_coverage.sh rename to testing/coverage/code_coverage.sh index b1eb8c599a..ec583b1e5e 100755 --- a/testing/bro-core-coverage/code_coverage.sh +++ b/testing/coverage/code_coverage.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # # On a Bro build configured with --enable-coverage, this script # produces a code coverage report after Bro has been invoked. The @@ -17,7 +17,7 @@ # 4b. Send .gcov files to appropriate path # CURR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" # Location of script -BASE="$(realpath "${CURR}/../../")" +BASE="$(readlink -f "${CURR}/../../")" TMP="${CURR}/tmp.$$" mkdir -p $TMP @@ -96,14 +96,20 @@ done echo "ok" # 3a. Run gcov (-p to preserve path) and move into tmp directory +# ... if system does not have gcov installed, exit with message. echo -n "Creating coverage files... " -( cd "$TMP" && find "$BASE" -name "*.o" -exec gcov -p {} > /dev/null 2>&1 \; ) -NUM_GCOVS=$(ls "$TMP"/*.gcov | wc -l) -if [ $NUM_GCOVS -eq 0 ]; then - echo "no gcov files produced, aborting" +if which gcov; then + ( cd "$TMP" && find "$BASE" -name "*.o" -exec gcov -p {} > /dev/null 2>&1 \; ) + NUM_GCOVS=$(find "$TMP" -name *.gcov | wc -l) + if [ $NUM_GCOVS -eq 0 ]; then + echo "no gcov files produced, aborting" + exit 1 + fi + echo "ok, $NUM_GCOVS coverage files" +else + echo "gcov is not installed on system, aborting" exit 1 fi -echo "ok, $NUM_GCOVS coverage files" # 3b. Prune gcov files that fall outside of the Bro tree: # Look for files containing gcov's slash substitution character "#"