Skip to content

Commit 7ec81bd

Browse files
committed
fix(coverage): missing files in the coverage report if they have no tests
This ensures that un-executed files (i.e. files that aren't tested) are included in the coverage report. The current behavior is that coverage.py excludes them by default. See https://coverage.readthedocs.io/en/7.6.10/source.html#execution
1 parent edfb4b3 commit 7ec81bd

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Unreleased changes template.
8484
{obj}`--venvs_use_declare_symlink=no` to have it not create symlinks at
8585
build time (they will be created at runtime instead).
8686
(Fixes [#2489](https://github.com/bazelbuild/rules_python/issues/2489))
87+
* (coverage) Fix missing files in the coverage report if they have no tests.
8788

8889
{#v0-0-0-added}
8990
### Added

python/private/python_bootstrap_template.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,14 @@ def _RunForCoverage(python_program, main_filename, args, env,
433433
relative_files = True
434434
''')
435435
PrintVerboseCoverage('Coverage entrypoint:', coverage_entrypoint)
436+
437+
instrumented_files = [abs_path for abs_path, _ in InstrumentedFilePaths()]
438+
unique_dirs = {os.path.dirname(file) for file in instrumented_files}
439+
source = ",".join(unique_dirs)
440+
441+
PrintVerboseCoverage("[coveragepy] Instrumented Files:\n" + "\n".join(instrumented_files))
442+
PrintVerboseCoverage("[coveragepy] Sources:\n" + "\n".join(unique_dirs))
443+
436444
# First run the target Python file via coveragepy to create a .coverage
437445
# database file, from which we can later export lcov.
438446
ret_code = subprocess.call(
@@ -443,6 +451,7 @@ relative_files = True
443451
"--rcfile=" + rcfile_name,
444452
"--append",
445453
"--branch",
454+
"--source=" + source,
446455
main_filename
447456
] + args,
448457
env=env,

0 commit comments

Comments
 (0)