From 2f0f64b2b084d7c26347e7d590a7295aa19dd0dc Mon Sep 17 00:00:00 2001 From: DavidKorczynski Date: Fri, 17 Feb 2023 14:45:39 +0000 Subject: [PATCH] infra: base-runner: add filtering to python cov reports (#9698) This is to avoid reporting coverage on files that are essentially not part of a given project, which butchers the summarised coverage stats at the moment. Signed-off-by: David Korczynski Signed-off-by: David Korczynski --- .../python_coverage_runner_help.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/infra/base-images/base-runner/python_coverage_runner_help.py b/infra/base-images/base-runner/python_coverage_runner_help.py index 51576c7ab..1b97e3fa9 100755 --- a/infra/base-images/base-runner/python_coverage_runner_help.py +++ b/infra/base-images/base-runner/python_coverage_runner_help.py @@ -25,6 +25,23 @@ from coverage.cmdline import main as coverage_main from coverage.data import CoverageData +def should_exclude_file(filepath): + """Returns whether the path should be excluded from the coverage report.""" + # Skip all atheris code + if "atheris" in filepath: + return True + + # Filter out all standard python libraries + if '/usr/local/lib/python' in filepath and 'site-packages' not in filepath: + return True + + # Avoid all PyInstaller modules. + if 'PyInstaller' in filepath: + return True + + return False + + def translate_lines(cov_data, new_cov_data, all_file_paths): """ Translate lines in a .coverage file created by coverage.py such that @@ -40,6 +57,8 @@ def translate_lines(cov_data, new_cov_data, all_file_paths): # Check if this file exists in our file paths: for local_file_path in all_file_paths: + if should_exclude_file(local_file_path): + continue if local_file_path.endswith(stripped_py_file_path): print('Found matching: %s' % (local_file_path)) new_cov_data.add_lines(