Skip to content

Coverage now fails if there are no covered Python files even if test succeeds #2762

Open
@phst

Description

@phst

🐞 bug report

Affected Rule

The issue is caused by the rule: py_test

Is this a regression?

Yes, the previous version in which this bug was not present was: 1.2.0

Description

If there are no covered source files, the coverage.py lcov step fails, causing the entire test to fail when run under coverage (see repro below).

🔬 Minimal Reproduction

See https://github.com/phst/py-cover-bug, and the next section.

🔥 Exception or Error

After cloning the above repository, run bazel coverage --test_output=all //a:test. This fails with an error

[...]
INFO: Using default value for --instrumentation_filter: "^//a[/:]".
[...]
INFO: From Testing //a:test:
==================== Test output for //a:test:
Hello world
[...]/coverage/inorout.py:504: CoverageWarning: Module [...]/_main/bazel-out/darwin_arm64-fastbuild/bin/b was never imported. (module-not-imported)
  self.warn(f"Module {pkg} was never imported.", slug="module-not-imported")
[...]/coverage/control.py:894: CoverageWarning: No data was collected. (no-data-collected)
  self._warn("No data was collected.", slug="no-data-collected")
--
Coverage runner: Not collecting coverage for failed test.
The following commands failed with status 1
[...]/bin/a/test.runfiles/_main/a/test
================================================================================
[...]

Running this with bazel test instead of coverage succeeds. Downgrading to rules_python 1.2.0 also makes it succeed.

Relevant additional output with verbose coverage enabled:

[coveragepy] Instrumented Files:
[...]/test.runfiles/_main/bazel-out/darwin_arm64-fastbuild/bin/b/bin
[...]/test.runfiles/_main/bazel-out/darwin_arm64-fastbuild/bin/b/binruntime_objects_list.txt
[coveragepy] Sources:
[...]/test.runfiles/_main/bazel-out/darwin_arm64-fastbuild/bin/b
Coverage entrypoint: [...]/coverage/__main__.py
[...]
Converting coveragepy database to lcov: /private/var/tmp/_bazel_p/35f69852d1c41a85f4769530320f22fe/sandbox/darwin-sandbox/26/execroot/_main/bazel-out/darwin_arm64-fastbuild/testlogs/_coverage/a/test/test/pylcov.dat
No data to report.
+ TEST_STATUS=1
[...]

Note that no directories containing Python files are listed in "Sources", so no coverage is generated for them. This is expected given the instrumentation filter, but causes coverage.py lcov to err out since it doesn't find any coverage data. My guess is that this is fallout from #2607.

🌍 Your Environment

Operating System:

macOS

Output of bazel version:

8.1.1

Rules_python version:

1.3.0

Anything else relevant?

Metadata

Metadata

Assignees

No one assigned

    Labels

    core-rulesIssues concerning core bin/test/lib rules

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions