Skip to content

Commit 9c738e2

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 9c738e2

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-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,

python/private/stage2_bootstrap_template.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,14 @@ def _maybe_collect_coverage(enable):
293293
relative_files = True
294294
"""
295295
)
296+
297+
instrumented_files = [abs_path for abs_path, _ in instrumented_file_paths()]
298+
unique_dirs = {os.path.dirname(file) for file in instrumented_files}
299+
source = ",".join(unique_dirs)
300+
301+
print_verbose_coverage("[coveragepy] Instrumented Files:\n" + "\n".join(instrumented_files))
302+
print_verbose_coverage("[coveragepy] Sources:\n" + "\n".join(unique_dirs))
303+
296304
try:
297305
cov = coverage.Coverage(
298306
config_file=rcfile_name,
@@ -314,6 +322,7 @@ def _maybe_collect_coverage(enable):
314322
# see https://github.com/nedbat/coveragepy/blob/bfb0c708fdd8182b2a9f0fc403596693ef65e475/coverage/inorout.py#L153-L164
315323
"*/external/*",
316324
],
325+
source=source,
317326
)
318327
cov.start()
319328
try:

0 commit comments

Comments
 (0)