How to remove certain directories from lcov code coverage report?

I_love_coding_93 picture I_love_coding_93 · Jul 18, 2016 · Viewed 16k times · Source

I'm not too strong with using lcov and shell scripting so it's a learning process for me. I understand the basics of making a code coverage report but I don't know the line of code to exclude certain directories. In a shell executable file I wrote the following code:

    #!/bin/sh
    ROOT_DIR=$1
    DEST_DIR=$2
    TARGET_DIR=$3
    TARGET=$4

   #init lcov
   lcov -c -i -d $TARGET_DIR/.. -o $TARGET_DIR/cov_init.info

    #run unit test executable
    "$DEST_DIR/$TARGET"

     #capture coverage after running executable
     lcov -c -d $TARGET_DIR/.. -o $TARGET_DIR/cov_test.info

     #I added this in-generate delta of coverage
     lcov -a $TARGET_DIR/cov_init.info -a $TARGET_DIR/cov_test.info -o $TARGET_DIR/cov.info

  # I added this in- Excludes some third party code
                    lcov --remove $TARGET_DIR/cov.info '/opt/*' '/usr/*' '$ROOT_DIR/Common?ExternalLibraries/*'

  #I added this in-generate report
            genhtml $TARGET_DIR/cov.info --ignore-errors source --output-directory $DEST_DIR/CoverageReport/$TARGET
            xdg-open $DEST_DIR/CoverageReport/$TARGET/index.html &

I'm pretty sure I need to exclude the directories before I capture the coverage after running executable.

Answer

Inian picture Inian · Jul 18, 2016

lcov has an option --remove to ignore coverage data for specified files.

--remove tracefile pattern

Remove data from tracefile.

Use this switch if you want to remove coverage data for a par- ticular set of files from a tracefile. Additional command line parameters will be interpreted as shell wildcard patterns (note that they may need to be escaped accordingly to prevent the shell from expanding them first). Every file entry in tracefile which matches at least one of those patterns will be removed.

The result of the remove operation will be written to stdout or the tracefile specified with -o.

Only one of -z, -c, -a, -e, -r, -l, --diff or --summary may be specified at a time.

You can do something like; quoting from the hyper-link below

lcov --remove /tmp/libreoffice_total.info -o /tmp/libreoffice_filtered.info \
    '/usr/include/*' \
    '/usr/lib/*' \
    '/usr/local/src/libreoffice/*/UnpackedTarball/*' \
    '/usr/local/src/libreoffice/workdir/*' \
    '/usr/local/src/libreoffice/instdir/*' \
    '/usr/local/src/libreoffice/external/*' \

Refer to this page for more documentation.