diff --git a/fastcov.py b/fastcov.py index 30d013d..dbf719b 100755 --- a/fastcov.py +++ b/fastcov.py @@ -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) @@ -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): diff --git a/test/functional/expected_results/basic.fastcov.expected.json b/test/functional/expected_results/basic.fastcov.expected.json new file mode 100644 index 0000000..aaeea78 --- /dev/null +++ b/test/functional/expected_results/basic.fastcov.expected.json @@ -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 + } + } + } + } +} \ No newline at end of file diff --git a/test/functional/run_all.sh b/test/functional/run_all.sh index 43f7e9f..c9d211c 100755 --- a/test/functional/run_all.sh +++ b/test/functional/run_all.sh @@ -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 @@ -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