22

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.

I_love_coding_93
  • 251
  • 1
  • 2
  • 7
  • 1
    Modern lcov's (>=1.14) has --exclude. See http://ltp.sourceforge.net/coverage/lcov/lcov.1.php. – thoni56 Aug 23 '20 at 06:51

1 Answers1

25

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.

Inian
  • 80,270
  • 14
  • 142
  • 161
  • this might be a dumb question but how come you have in single quotes '/usr/mydirToExclude/*' and how does that relate to the code /tmp/mylibfiletoExclude.info ? – I_love_coding_93 Jul 18 '16 at 13:59
  • Refer the embedded link for actual example! – Inian Jul 18 '16 at 14:00
  • @I_love_coding_93: Were you able to use the option for your need? – Inian Jul 18 '16 at 14:21
  • Did you go through this link, https://wiki.documentfoundation.org/Development/Lcov#Remove_.2F_filter_out_remaining_unwanted_stuff_from_tracefil, If I understand right, removing directories from code coverage report is what you wanted – Inian Jul 18 '16 at 14:27
  • @I_love_coding_93, yes @Inian shows to you how to remove coverage data from existed report and put coverage data in new (filtered) report. Other way to do the similar is use `extract` option. – Gluttton Jul 18 '16 at 14:27
  • 2
    @I_love_coding_93, just put your and @Inian code together: `lcov -c -d $TARGET_DIR/.. -o $TARGET_DIR/cov_test.info && lcov --remove $TARGET_DIR/cov_test.info '/usr/include/*' -o $TARGET_DIR/cov_test_filtered.info`. – Gluttton Jul 18 '16 at 14:29
  • @Gluttton: Appreciate your comments! – Inian Jul 18 '16 at 14:30
  • @Inian how comes single quotes are being used instead of double quotes for the directories like '/usr/include/*' -o $TARGET_DIR/cov_test_filtered.info – I_love_coding_93 Jul 18 '16 at 14:35
  • @I_love_coding_93: Why is that a problem when `lcov` treats it such a way? All you need to get is, if the option is helping you solve the problem as expected right? – Inian Jul 18 '16 at 14:36
  • Please note that --remove is a separated option which cannot be combined with --capture, so one needs to capture the data first, then call lcov --remove on it and only then generate HTML. – The Godfather Jun 28 '18 at 13:51