I want to generate a code coverage report using Bazel for a Python project (my environment: macOS Ventura, M1 arm64, Python 3.10, Bazel 6.0).
The documentation states that for this task Bazel 6.0 and a modified version of coverage.py
should work.
It suggests to create a requiremnts.txt
with the following content:
git+https://github.com/ulfjack/coveragepy.git@lcov-support
extend the WORKSPACE
file this way:
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "rules_python",
url = "https://github.com/bazelbuild/rules_python/releases/download/0.5.0/rules_python-0.5.0.tar.gz",
sha256 = "cd6730ed53a002c56ce4e2f396ba3b3be262fd7cb68339f0377a45e8227fe332",
)
load("@rules_python//python:pip.bzl", "pip_install")
pip_install(
name = "python_deps",
requirements = "//:requirements.txt",
)
And modify py_test
s this way:
load("@python_deps//:requirements.bzl", "entry_point")
alias(
name = "python_coverage_tools",
actual = entry_point("coverage"),
)
py_test(
name = "test",
srcs = ["test.py"],
env = {
"PYTHON_COVERAGE": "$(location :python_coverage_tools)",
},
deps = [
":main",
":python_coverage_tools",
],
)
I followed this approach but get this error message:
ERROR: An error occurred during the fetch of repository 'python_deps':
Traceback (most recent call last):
File "/private/var/tmp/_bazel_vertexwahn/401174f4ee3f8d1aff86fa2a0c8c5dbe/external/rules_python/python/pip_install/pip_repository.bzl", line 345, column 13, in _pip_repository_impl
fail("rules_python failed: %s (%s)" % (result.stdout, result.stderr))
Error in fail: rules_python failed: (Traceback (most recent call last):
File "/opt/homebrew/Cellar/[email protected]/3.10.9/Frameworks/Python.framework/Versions/3.10/lib/python3.10/runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/opt/homebrew/Cellar/[email protected]/3.10.9/Frameworks/Python.framework/Versions/3.10/lib/python3.10/runpy.py", line 86, in _run_code
exec(code, run_globals)
File "/private/var/tmp/_bazel_vertexwahn/401174f4ee3f8d1aff86fa2a0c8c5dbe/external/rules_python/python/pip_install/extract_wheels/parse_requirements_to_bzl.py", line 322, in <module>
main(requirement_file)
File "/private/var/tmp/_bazel_vertexwahn/401174f4ee3f8d1aff86fa2a0c8c5dbe/external/rules_python/python/pip_install/extract_wheels/parse_requirements_to_bzl.py", line 309, in main
generate_parsed_requirements_contents(
File "/private/var/tmp/_bazel_vertexwahn/401174f4ee3f8d1aff86fa2a0c8c5dbe/external/rules_python/python/pip_install/extract_wheels/parse_requirements_to_bzl.py", line 114, in generate_parsed_requirements_contents
repo_names_and_reqs = repo_names_and_requirements(
File "/private/var/tmp/_bazel_vertexwahn/401174f4ee3f8d1aff86fa2a0c8c5dbe/external/rules_python/python/pip_install/extract_wheels/parse_requirements_to_bzl.py", line 69, in repo_names_and_requirements
return [
File "/private/var/tmp/_bazel_vertexwahn/401174f4ee3f8d1aff86fa2a0c8c5dbe/external/rules_python/python/pip_install/extract_wheels/parse_requirements_to_bzl.py", line 71, in <listcomp>
bazel.sanitise_name(ir.name, prefix=repo_prefix),
File "/private/var/tmp/_bazel_vertexwahn/401174f4ee3f8d1aff86fa2a0c8c5dbe/external/rules_python/python/pip_install/extract_wheels/bazel.py", line 25, in sanitise_name
return prefix + name.replace("-", "_").replace(".", "_").lower()
AttributeError: 'NoneType' object has no attribute 'replace'
Any ideas how to get proper code coverage for a Bazel based Python project are welcome.
I tried also to run the code coverage command this way:
bazel coverage --test_env=PYTHON_COVERAGE=/Users/vertexahn/dev/coveragepy/__main__.py //tests:test_main
This results in:
exec ${PAGER:-/usr/bin/less} "$0" || exit 1
Executing tests from //tests:test_main
-----------------------------------------------------------------------------
Unrecognized option '[run] relative_files=' in config file /private/var/tmp/_bazel_vertexwahn/401174f4ee3f8d1aff86fa2a0c8c5dbe/sandbox/darwin-sandbox/79/execroot/__main__/bazel-out/darwin_arm64-fastbuild/testlogs/_coverage/tests/test_main/test/.coveragerc
Unknown command: 'lcov'
Use 'coverage help' for help.
--
Coverage runner: Not collecting coverage for failed test.
The following commands failed with status 1
/private/var/tmp/_bazel_vertexwahn/401174f4ee3f8d1aff86fa2a0c8c5dbe/sandbox/darwin-sandbox/79/execroot/__main__/bazel-out/darwin_arm64-fastbuild/bin/tests/test_main.runfiles/__main__/tests/test_main
Similar questions on StackOverflow with no answer: