Skip to content

Commit 488a037

Browse files
authored
feat(coverage): Register coverage.py to hermetic toolchains (bazel-contrib#977)
This allows including the coverage package as part of the toolchain dependencies, which is mixed into a test's dependencies when `bazel coverage` is run (if coverage is not enabled, no extra dependency is added) For now, it's disabled by default because enabling it poses the risk of having two versions of coverage installed (one from the toolchain, one from the user's dependencies). The user can turn the coverage_tool setting by passing `register_coverage_tool=(True|False)` to `python_register_toolchains` or `python_register_multi_toolchains` call or specifying the `coverage_tool` label as described in the `versions.bzl` file. Use coverage.py v6.5.0 because the latest has `types.py` in the package directory, which imports from Python's stdlib `types` [1]. Somehow the Python interpreter is thinking that the `from types import FrameType` is referring to the currently interpreted file and everything breaks. I would have expected the package to use absolute imports and only attempt to import from `coverage.types` if we use `coverage.types` and not just a plain `types` import. NOTE: Coverage is only for non-windows platforms. Update tests to: - ensure that we can still use the toolchain as previously. - ensure that we are not downloading extra deps if they are not needed. * Also changes the projects bazelrc to use a remotejdk, which makes it easier for contributors because they don't have to locally install a jdk to get going. [1]: https://github.com/nedbat/coveragepy/blob/master/coverage/types.py [3]: bazelbuild/bazel#15835
1 parent 3fe06a1 commit 488a037

18 files changed

Lines changed: 663 additions & 5 deletions

File tree

.bazelci/presubmit.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ buildifier:
3434
.reusable_build_test_all: &reusable_build_test_all
3535
build_targets: ["..."]
3636
test_targets: ["..."]
37+
.coverage_targets_example_bzlmod: &coverage_targets_example_bzlmod
38+
coverage_targets: ["//:test"]
39+
.coverage_targets_example_multi_python: &coverage_targets_example_multi_python
40+
coverage_targets:
41+
- //tests:my_lib_3_10_test
42+
- //tests:my_lib_3_11_test
43+
- //tests:my_lib_3_8_test
44+
- //tests:my_lib_3_9_test
45+
- //tests:my_lib_default_test
46+
- //tests:version_3_10_test
47+
- //tests:version_3_11_test
48+
- //tests:version_3_8_test
49+
- //tests:version_3_9_test
50+
- //tests:version_default_test
3751
tasks:
3852
gazelle_extension:
3953
name: Test the Gazelle extension
@@ -89,42 +103,50 @@ tasks:
89103

90104
integration_test_bzlmod_ubuntu:
91105
<<: *reusable_build_test_all
106+
<<: *coverage_targets_example_bzlmod
92107
name: bzlmod integration tests on Ubuntu
93108
working_directory: examples/bzlmod
94109
platform: ubuntu2004
95110
integration_test_bzlmod_debian:
96111
<<: *reusable_build_test_all
112+
<<: *coverage_targets_example_bzlmod
97113
name: bzlmod integration tests on Debian
98114
working_directory: examples/bzlmod
99115
platform: debian11
100116
integration_test_bzlmod_macos:
101117
<<: *reusable_build_test_all
118+
<<: *coverage_targets_example_bzlmod
102119
name: bzlmod integration tests on macOS
103120
working_directory: examples/bzlmod
104121
platform: macos
105122
integration_test_bzlmod_windows:
106123
<<: *reusable_build_test_all
124+
# coverage is not supported on Windows
107125
name: bzlmod integration tests on Windows
108126
working_directory: examples/bzlmod
109127
platform: windows
110128

111129
integration_test_multi_python_versions_ubuntu:
112130
<<: *reusable_build_test_all
131+
<<: *coverage_targets_example_multi_python
113132
name: multi_python_versions integration tests on Ubuntu
114133
working_directory: examples/multi_python_versions
115134
platform: ubuntu2004
116135
integration_test_multi_python_versions_debian:
117136
<<: *reusable_build_test_all
137+
<<: *coverage_targets_example_multi_python
118138
name: multi_python_versions integration tests on Debian
119139
working_directory: examples/multi_python_versions
120140
platform: debian11
121141
integration_test_multi_python_versions_macos:
122142
<<: *reusable_build_test_all
143+
<<: *coverage_targets_example_multi_python
123144
name: multi_python_versions integration tests on macOS
124145
working_directory: examples/multi_python_versions
125146
platform: macos
126147
integration_test_multi_python_versions_windows:
127148
<<: *reusable_build_test_all
149+
# coverage is not supported on Windows
128150
name: multi_python_versions integration tests on Windows
129151
working_directory: examples/multi_python_versions
130152
platform: windows

MODULE.bazel

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,20 @@ use_repo(
2929
"pypi__tomli",
3030
"pypi__wheel",
3131
"pypi__zipp",
32+
# coverage_deps managed by running ./tools/update_coverage_deps.py <version>
33+
"pypi__coverage_cp310_aarch64-apple-darwin",
34+
"pypi__coverage_cp310_aarch64-unknown-linux-gnu",
35+
"pypi__coverage_cp310_x86_64-apple-darwin",
36+
"pypi__coverage_cp310_x86_64-unknown-linux-gnu",
37+
"pypi__coverage_cp311_aarch64-unknown-linux-gnu",
38+
"pypi__coverage_cp311_x86_64-apple-darwin",
39+
"pypi__coverage_cp311_x86_64-unknown-linux-gnu",
40+
"pypi__coverage_cp38_aarch64-apple-darwin",
41+
"pypi__coverage_cp38_aarch64-unknown-linux-gnu",
42+
"pypi__coverage_cp38_x86_64-apple-darwin",
43+
"pypi__coverage_cp38_x86_64-unknown-linux-gnu",
44+
"pypi__coverage_cp39_aarch64-apple-darwin",
45+
"pypi__coverage_cp39_aarch64-unknown-linux-gnu",
46+
"pypi__coverage_cp39_x86_64-apple-darwin",
47+
"pypi__coverage_cp39_x86_64-unknown-linux-gnu",
3248
)

examples/bzlmod/.bazelrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
common --experimental_enable_bzlmod
2+
3+
coverage --java_runtime_version=remotejdk_11

examples/bzlmod/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ compile_pip_requirements(
1212

1313
py_library(
1414
name = "lib",
15-
srcs = ["__init__.py"],
15+
srcs = ["lib.py"],
1616
deps = [
1717
requirement("pylint"),
1818
requirement("tabulate"),

examples/bzlmod/MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ local_path_override(
1313
python = use_extension("@rules_python//python:extensions.bzl", "python")
1414
python.toolchain(
1515
name = "python3_9",
16+
configure_coverage_tool = True,
1617
python_version = "3.9",
1718
)
1819
use_repo(python, "python3_9_toolchains")

examples/bzlmod/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from __init__ import main
15+
from lib import main
1616

1717
if __name__ == "__main__":
1818
print(main([["A", 1], ["B", 2]]))

examples/bzlmod/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import unittest
1616

17-
from __init__ import main
17+
from lib import main
1818

1919

2020
class ExampleTest(unittest.TestCase):

examples/multi_python_versions/.bazelrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ test --test_output=errors
33
# Windows requires these for multi-python support:
44
build --enable_runfiles
55
startup --windows_enable_symlinks
6+
7+
coverage --java_runtime_version=remotejdk_11

examples/multi_python_versions/WORKSPACE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ python_register_multi_toolchains(
2222
"3.8",
2323
"3.9",
2424
"3.10",
25+
"3.11",
2526
],
27+
register_coverage_tool = True,
2628
)
2729

2830
load("@python//:pip.bzl", "multi_pip_parse")
2931
load("@python//3.10:defs.bzl", interpreter_3_10 = "interpreter")
32+
load("@python//3.11:defs.bzl", interpreter_3_11 = "interpreter")
3033
load("@python//3.8:defs.bzl", interpreter_3_8 = "interpreter")
3134
load("@python//3.9:defs.bzl", interpreter_3_9 = "interpreter")
3235

@@ -35,11 +38,13 @@ multi_pip_parse(
3538
default_version = default_python_version,
3639
python_interpreter_target = {
3740
"3.10": interpreter_3_10,
41+
"3.11": interpreter_3_11,
3842
"3.8": interpreter_3_8,
3943
"3.9": interpreter_3_9,
4044
},
4145
requirements_lock = {
4246
"3.10": "//requirements:requirements_lock_3_10.txt",
47+
"3.11": "//requirements:requirements_lock_3_11.txt",
4348
"3.8": "//requirements:requirements_lock_3_8.txt",
4449
"3.9": "//requirements:requirements_lock_3_9.txt",
4550
},

0 commit comments

Comments
 (0)