-
-
Notifications
You must be signed in to change notification settings - Fork 610
feat(toolchain): Extend Python Testing Toolchain with COVERAGE_RC Support #2246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[report] | ||
include_namespace_packages=True | ||
skip_covered=True | ||
[run] | ||
relative_files=True | ||
branch=True |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Copyright 2024 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import os | ||
import tempfile | ||
import unittest | ||
|
||
|
||
class TestEnvironmentVariables(unittest.TestCase): | ||
def test_coverage_rc_file_exists(self): | ||
# Assert that the environment variable is set and points to a valid file | ||
coverage_rc_path = os.environ.get("COVERAGE_RC") | ||
if coverage_rc_path: | ||
coverage_rc_path = os.path.abspath(coverage_rc_path) | ||
self.assertTrue( | ||
os.path.isfile(coverage_rc_path), | ||
f"COVERAGE_RC does not point to a valid file, {coverage_rc_path}", | ||
) | ||
|
||
# Read the content of the file and assert it matches the expected content | ||
expected_content = ( | ||
"[report]\n" | ||
"include_namespace_packages=True\n" | ||
"skip_covered=True\n" | ||
"[run]\n" | ||
"relative_files=True\n" | ||
"branch=True\n" | ||
) | ||
|
||
with open(coverage_rc_path, "r") as file: | ||
file_content = file.read() | ||
|
||
self.assertEqual( | ||
file_content, | ||
expected_content, | ||
"COVERAGE_RC file content does not match the expected content", | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Copyright 2023 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Python toolchain module extensions for use with bzlmod. | ||
|
||
::::{topic} Basic usage | ||
|
||
The simplest way to configure the toolchain with `rules_python` is as follows. | ||
|
||
```starlark | ||
python_test = use_extension("@rules_python//python/extensions:python_test.bzl", "python_test") | ||
python_test.configure( | ||
coveragerc = ".coveragerc", | ||
) | ||
use_repo(python_test, "py_test_toolchain") | ||
register_toolchains("@py_test_toolchain//:all") | ||
Comment on lines
+26
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
``` | ||
""" | ||
|
||
load("//python/private:python_test.bzl", _python_test = "python_test") | ||
|
||
python_test = _python_test |
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -14,6 +14,7 @@ | |||||||||||||||||
"""Implementation of py_test rule.""" | ||||||||||||||||||
|
||||||||||||||||||
load("@bazel_skylib//lib:dicts.bzl", "dicts") | ||||||||||||||||||
load("//python/private:toolchain_types.bzl", "PY_TEST_TOOLCHAIN_TYPE") | ||||||||||||||||||
load(":attributes.bzl", "AGNOSTIC_TEST_ATTRS") | ||||||||||||||||||
load(":common.bzl", "maybe_add_test_execution_info") | ||||||||||||||||||
load( | ||||||||||||||||||
|
@@ -40,16 +41,44 @@ _BAZEL_PY_TEST_ATTRS = { | |||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
def _py_test_impl(ctx): | ||||||||||||||||||
providers = py_executable_impl( | ||||||||||||||||||
providers, binary_info, environment_info = py_executable_impl( | ||||||||||||||||||
ctx = ctx, | ||||||||||||||||||
is_test = True, | ||||||||||||||||||
inherited_environment = ctx.attr.env_inherit, | ||||||||||||||||||
) | ||||||||||||||||||
maybe_add_test_execution_info(providers, ctx) | ||||||||||||||||||
return providers | ||||||||||||||||||
py_test_toolchain = ctx.exec_groups["test"].toolchains[PY_TEST_TOOLCHAIN_TYPE] | ||||||||||||||||||
if py_test_toolchain: | ||||||||||||||||||
py_test_info = py_test_toolchain.py_test_info | ||||||||||||||||||
else: | ||||||||||||||||||
providers.extend( | ||||||||||||||||||
[ | ||||||||||||||||||
DefaultInfo( | ||||||||||||||||||
executable = binary_info.executable, | ||||||||||||||||||
files = binary_info.files, | ||||||||||||||||||
default_runfiles = binary_info.default_runfiles, | ||||||||||||||||||
data_runfiles = binary_info.data_runfiles, | ||||||||||||||||||
), | ||||||||||||||||||
RunEnvironmentInfo( | ||||||||||||||||||
environment = environment_info.environment, | ||||||||||||||||||
inherited_environment = environment_info.inherited_environment, | ||||||||||||||||||
), | ||||||||||||||||||
], | ||||||||||||||||||
) | ||||||||||||||||||
return providers | ||||||||||||||||||
test_providers = py_test_info.get_runner.func( | ||||||||||||||||||
ctx, | ||||||||||||||||||
binary_info, | ||||||||||||||||||
environment_info, | ||||||||||||||||||
**py_test_info.get_runner.args | ||||||||||||||||||
) | ||||||||||||||||||
return test_providers + providers | ||||||||||||||||||
|
||||||||||||||||||
py_test = create_executable_rule( | ||||||||||||||||||
implementation = _py_test_impl, | ||||||||||||||||||
attrs = dicts.add(AGNOSTIC_TEST_ATTRS, _BAZEL_PY_TEST_ATTRS), | ||||||||||||||||||
test = True, | ||||||||||||||||||
exec_groups = { | ||||||||||||||||||
"test": exec_group(toolchains = [config_common.toolchain_type(PY_TEST_TOOLCHAIN_TYPE, mandatory = False)]), | ||||||||||||||||||
}, | ||||||||||||||||||
Comment on lines
+81
to
+83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally I would love to be able to express this as
Suggested change
My goal is for us to extend Do you have any ideas? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think I don't understand how |
||||||||||||||||||
) |
Uh oh!
There was an error while loading. Please reload this page.