Skip to content

Commit

Permalink
Add ability to filter coverage during combine operations
Browse files Browse the repository at this point in the history
Description:
- Fix #48
- Filter options now apply during combine operations
  • Loading branch information
RPGillespie6 committed Jun 4, 2020
1 parent 42baa84 commit 4328737
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 18 deletions.
44 changes: 26 additions & 18 deletions fastcov.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,38 +155,45 @@ def processGcdas(args, coverage_files, gcov_filter_options):

return base_fastcov

def processGcov(cwd, gcov, files, gcov_filter_options):
# Add absolute path
gcov["file_abs"] = os.path.abspath(os.path.join(cwd, gcov["file"]))

def shouldFilterSource(source, gcov_filter_options):
"""Returns true if the provided source file should be filtered due to CLI options, otherwise returns false"""
# If explicit sources were passed, check for match
if gcov_filter_options["sources"]:
if gcov["file_abs"] in gcov_filter_options["sources"]:
files.append(gcov)
logging.debug("Accepted coverage for '%s'", gcov["file_abs"])
else:
logging.debug("Skipping coverage for '%s' due to option '--source-files'", gcov["file_abs"])
return
if source not in gcov_filter_options["sources"]:
logging.debug("Filtering coverage for '%s' due to option '--source-files'", source)
return True

# Check exclude filter
for ex in gcov_filter_options["exclude"]:
if ex in gcov["file_abs"]:
logging.debug("Skipping coverage for '%s' due to option '--exclude %s'", gcov["file_abs"], ex)
return
if ex in source:
logging.debug("Filtering coverage for '%s' due to option '--exclude %s'", source, ex)
return True

# Check include filter
if gcov_filter_options["include"]:
included = False
for ex in gcov_filter_options["include"]:
if ex in gcov["file_abs"]:
for inc in gcov_filter_options["include"]:
if inc in source:
included = True
files.append(gcov)
logging.debug("Accepted coverage for '%s'", gcov["file_abs"])
break

if not included:
logging.debug("Skipping coverage for '%s' due to option '--include %s'", gcov["file_abs"], " ".join(gcov_filter_options["include"]))
logging.debug("Filtering coverage for '%s' due to option '--include %s'", source, " ".join(gcov_filter_options["include"]))
return True

return False

def filterFastcov(fastcov_json, args):
gcov_filter_options = getGcovFilterOptions(args)
for source in list(fastcov_json["sources"].keys()):
if shouldFilterSource(source, gcov_filter_options):
del fastcov_json["sources"][source]

def processGcov(cwd, gcov, files, gcov_filter_options):
# Add absolute path
gcov["file_abs"] = os.path.abspath(os.path.join(cwd, gcov["file"]))

if shouldFilterSource(gcov["file_abs"], gcov_filter_options):
return

files.append(gcov)
Expand Down Expand Up @@ -544,6 +551,7 @@ def parseAndCombine(paths):
def combineCoverageFiles(args):
logging.info("Performing combine operation")
fastcov_json = parseAndCombine(args.combine)
filterFastcov(fastcov_json, args)
dumpFile(fastcov_json, args)

def dumpFile(fastcov_json, args):
Expand Down
41 changes: 41 additions & 0 deletions test/functional/expected_results/basic.fastcov.expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"sources": {
"/mnt/workspace/test/functional/cmake_project/src/source2.cpp": {
"": {
"functions": {
"_Z3barbii": {
"start_line": 3,
"execution_count": 10
}
},
"branches": {},
"lines": {
"3": 10,
"5": 10,
"6": 10,
"8": 0
}
}
},
"/mnt/workspace/test/functional/cmake_project/src/source1.cpp": {
"": {
"functions": {
"_Z3foob": {
"start_line": 3,
"execution_count": 1
}
},
"branches": {},
"lines": {
"3": 1,
"5": 1,
"7": 1001,
"8": 1000,
"10": 1000,
"11": 0,
"14": 1
}
}
}
}
}
13 changes: 13 additions & 0 deletions test/functional/run_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ test `find . -name *.gcda | wc -l` -eq 0
${TEST_DIR}/fastcov.py --gcov gcov-9 --zerocounters # Clear previous test coverage
ctest -R ctest_1

# Create a fastcov JSON to be filtered later
coverage run -a ${TEST_DIR}/fastcov.py --gcov gcov-9 --verbose -o basic.fastcov.json

# Test (basic report generation - no branches)
coverage run -a ${TEST_DIR}/fastcov.py --gcov gcov-9 --verbose --exclude cmake_project/test/ --lcov -o test1.actual.info
cmp test1.actual.info ${TEST_DIR}/expected_results/test1.expected.info
Expand Down Expand Up @@ -80,6 +83,16 @@ if coverage run -a ${TEST_DIR}/fastcov.py --gcov ${TEST_DIR}/fake-gcov.sh ; then
exit 1
fi

# Combine operation - filtering
coverage run -a ${TEST_DIR}/fastcov.py -C basic.fastcov.json --exclude cmake_project/test/ -o basic.fastcov.actual.json
${TEST_DIR}/json_cmp.py basic.fastcov.actual.json ${TEST_DIR}/expected_results/basic.fastcov.expected.json

coverage run -a ${TEST_DIR}/fastcov.py -C basic.fastcov.json --include cmake_project/src/ -o basic.fastcov2.actual.json
${TEST_DIR}/json_cmp.py basic.fastcov2.actual.json ${TEST_DIR}/expected_results/basic.fastcov.expected.json

coverage run -a ${TEST_DIR}/fastcov.py -C basic.fastcov.json --source-files /mnt/workspace/test/functional/cmake_project/src/source2.cpp /mnt/workspace/test/functional/cmake_project/src/source1.cpp -o basic.fastcov3.actual.json
${TEST_DIR}/json_cmp.py basic.fastcov3.actual.json ${TEST_DIR}/expected_results/basic.fastcov.expected.json

# Combine operation
coverage run -a ${TEST_DIR}/fastcov.py -C ${TEST_DIR}/expected_results/test2.expected.info ${TEST_DIR}/expected_results/test1.tn.expected.info --lcov -o combine1.actual.info
cmp combine1.actual.info ${TEST_DIR}/expected_results/combine1.expected.info
Expand Down

0 comments on commit 4328737

Please sign in to comment.