Skip to content

Commit fe7b293

Browse files
committed
feat(toolchain) Add coveragepy configuration attribute
1 parent 63114a3 commit fe7b293

13 files changed

+82
-14
lines changed

examples/bzlmod/.coveragerc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[report]
2+
include_namespace_packages=True
3+
skip_covered = True
4+
[run]
5+
relative_files = True
6+
branch = True
7+
omit =
8+
*/external/*

examples/bzlmod/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,5 @@ build_test(
8282
name = "all_requirements",
8383
targets = all_requirements,
8484
)
85+
86+
exports_files([".coveragerc"])

examples/bzlmod/MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ bazel_dep(name = "protobuf", version = "24.4", repo_name = "com_google_protobuf"
2222
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
2323
python.toolchain(
2424
configure_coverage_tool = True,
25+
coverage_rc = "//:.coveragerc",
2526
# Only set when you have mulitple toolchain versions.
2627
is_default = True,
2728
python_version = "3.9",

examples/bzlmod/MODULE.bazel.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/private/common/providers.bzl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def _PyRuntimeInfo_init(
7373
interpreter = None,
7474
files = None,
7575
coverage_tool = None,
76+
coverage_rc = None,
7677
coverage_files = None,
7778
pyc_tag = None,
7879
python_version,
@@ -121,6 +122,7 @@ def _PyRuntimeInfo_init(
121122
"bootstrap_template": bootstrap_template,
122123
"coverage_files": coverage_files,
123124
"coverage_tool": coverage_tool,
125+
"coverage_rc": coverage_rc,
124126
"files": files,
125127
"implementation_name": implementation_name,
126128
"interpreter": interpreter,
@@ -202,6 +204,12 @@ The files required at runtime for using `coverage_tool`. Will be `None` if no
202204
203205
If set, this field is a `File` representing tool used for collecting code
204206
coverage information from python tests. Otherwise, this is `None`.
207+
""",
208+
"coverage_rc": """
209+
:type: File | None
210+
211+
If set, this field is a `File` representing the configuration file used by the
212+
coverage information from python tests. Otherwise, this is `None`.
205213
""",
206214
"files": """
207215
:type: depset[File] | None

python/private/common/py_executable.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,8 @@ def _get_runtime_details(ctx, semantics):
320320
direct.append(effective_runtime.coverage_tool)
321321
if effective_runtime.coverage_files:
322322
transitive.append(effective_runtime.coverage_files)
323+
if effective_runtime.coverage_rc:
324+
direct.append(effective_runtime.coverage_rc)
323325
runtime_files = depset(direct = direct, transitive = transitive)
324326
else:
325327
runtime_files = depset()

python/private/common/py_executable_bazel.bzl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,10 @@ def _create_stage2_bootstrap(
343343
)
344344
else:
345345
coverage_tool_runfiles_path = ""
346+
if runtime and runtime.coverage_rc:
347+
coverage_rc_path = runtime.coverage_rc.path
348+
else:
349+
coverage_rc_path = ""
346350

347351
template = runtime.stage2_bootstrap_template
348352

@@ -351,6 +355,7 @@ def _create_stage2_bootstrap(
351355
output = output,
352356
substitutions = {
353357
"%coverage_tool%": coverage_tool_runfiles_path,
358+
"%coverage_rc%": coverage_rc_path,
354359
"%import_all%": "True" if ctx.fragments.bazel_py.python_import_all_repositories else "False",
355360
"%imports%": ":".join(imports.to_list()),
356361
"%main%": "{}/{}".format(ctx.workspace_name, main_py.short_path),
@@ -403,6 +408,12 @@ def _create_stage1_bootstrap(
403408
subs["%shebang%"] = DEFAULT_STUB_SHEBANG
404409
template = ctx.file._bootstrap_template
405410

411+
if runtime and runtime.coverage_rc:
412+
coverage_rc_path = runtime.coverage_rc.path
413+
else:
414+
coverage_rc_path = ""
415+
416+
subs["%coverage_rc%"] = coverage_rc_path
406417
subs["%coverage_tool%"] = coverage_tool_runfiles_path
407418
subs["%import_all%"] = ("True" if ctx.fragments.bazel_py.python_import_all_repositories else "False")
408419
subs["%imports%"] = ":".join(imports.to_list())

python/private/common/py_runtime_rule.bzl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ def _py_runtime_impl(ctx):
7777
else:
7878
coverage_tool = None
7979
coverage_files = None
80+
if ctx.attr.coverage_rc:
81+
coverage_rc = ctx.attr.coverage_rc.files.to_list()[0]
82+
else:
83+
coverage_rc = None
8084

8185
python_version = ctx.attr.python_version
8286

@@ -117,6 +121,7 @@ def _py_runtime_impl(ctx):
117121
py_runtime_info_kwargs.update(dict(
118122
implementation_name = ctx.attr.implementation_name,
119123
interpreter_version_info = interpreter_version_info,
124+
coverage_rc = coverage_rc,
120125
pyc_tag = pyc_tag,
121126
stage2_bootstrap_template = ctx.file.stage2_bootstrap_template,
122127
zip_main_template = ctx.file.zip_main_template,
@@ -216,6 +221,12 @@ of coverage.py (https://coverage.readthedocs.io), at least including
216221
the `run` and `lcov` subcommands.
217222
""",
218223
),
224+
"coverage_rc": attr.label(
225+
allow_single_file = True,
226+
doc = ".converage or pyproject.toml or " +
227+
"for configure coverage tool",
228+
mandatory = False,
229+
),
219230
"files": attr.label_list(
220231
allow_files = True,
221232
doc = """

python/private/hermetic_runtime_repo_setup.bzl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ def define_hermetic_runtime_toolchain_impl(
2727
extra_files_glob_exclude,
2828
python_version,
2929
python_bin,
30-
coverage_tool):
30+
coverage_tool,
31+
coverage_rc):
3132
"""Define a toolchain implementation for a python-build-standalone repo.
3233
3334
It expected this macro is called in the top-level package of an extracted
@@ -47,6 +48,8 @@ def define_hermetic_runtime_toolchain_impl(
4748
repositoroy.
4849
coverage_tool: {type}`str` optional target to the coverage tool to
4950
use.
51+
coverage_rc: {type}`str` optional target to the coverage rc file to
52+
use.
5053
"""
5154
_ = name # @unused
5255
version_info = semver(python_version)
@@ -134,6 +137,7 @@ def define_hermetic_runtime_toolchain_impl(
134137
},
135138
# Convert empty string to None
136139
coverage_tool = coverage_tool or None,
140+
coverage_rc = coverage_rc or None,
137141
python_version = "PY3",
138142
implementation_name = "cpython",
139143
# See https://peps.python.org/pep-3147/ for pyc tag infix format

python/private/python.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ def _create_toolchain_attrs_struct(*, tag = None, python_version = None, toolcha
319319
python_version = python_version if python_version else tag.python_version,
320320
configure_coverage_tool = getattr(tag, "configure_coverage_tool", False),
321321
ignore_root_user_error = getattr(tag, "ignore_root_user_error", False),
322+
coverage_rc = getattr(tag, "coverage_rc", None),
322323
)
323324

324325
def _get_bazel_version_specific_kwargs():
@@ -375,6 +376,10 @@ A toolchain's repository name uses the format `python_{major}_{minor}`, e.g.
375376
mandatory = False,
376377
doc = "Whether or not to configure the default coverage tool for the toolchains.",
377378
),
379+
"coverage_rc": attr.label(
380+
mandatory = False,
381+
doc = "The coverage configuration file to use for the toolchains.",
382+
),
378383
"ignore_root_user_error": attr.bool(
379384
default = False,
380385
doc = """\

0 commit comments

Comments
 (0)