-
Notifications
You must be signed in to change notification settings - Fork 550
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(toolchain): Python testing toolchain
Inspired by https://github.com/trybka/scraps/blob/master/cc_test.md This PR extends Test Runner enviroment to provide a coveragerc enviroment variable COVERAGE_RC, allowing user to provide coverage resource in what ever format
- Loading branch information
Showing
15 changed files
with
333 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# 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") | ||
self.assertTrue( | ||
os.path.isfile(coverage_rc_path), | ||
"COVERAGE_RC does not point to a valid file", | ||
) | ||
|
||
# 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# 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") | ||
``` | ||
:::{seealso} | ||
For more in-depth documentation see the {obj}`python.toolchain`. | ||
::: | ||
:::: | ||
""" | ||
|
||
load("//python/private:python_test.bzl", _python_test = "python_test") | ||
|
||
python_test = _python_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# 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. | ||
|
||
""" | ||
Simple toolchain which overrides env and exec requirements. | ||
""" | ||
|
||
load(":text_util.bzl", "render") | ||
load( | ||
":toolchain_types.bzl", | ||
"PY_TEST_TOOLCHAIN_TYPE", | ||
) | ||
|
||
PytestProvider = provider( | ||
fields = [ | ||
"coverage_rc", | ||
], | ||
) | ||
|
||
def _py_test_toolchain_impl(ctx): | ||
return [ | ||
platform_common.ToolchainInfo( | ||
py_test_info = PytestProvider( | ||
coverage_rc = ctx.attr.coverage_rc, | ||
), | ||
), | ||
] | ||
|
||
py_test_toolchain = rule( | ||
implementation = _py_test_toolchain_impl, | ||
attrs = { | ||
"coverage_rc": attr.label( | ||
allow_single_file = True, | ||
), | ||
}, | ||
) | ||
_TOOLCHAIN_TEMPLATE = """ | ||
load("@rules_python//python/private:py_test_toolchain.bzl", "py_test_toolchain") | ||
py_test_toolchain( | ||
name = "{name}_toolchain", | ||
coverage_rc = "{coverage_rc}", | ||
) | ||
toolchain( | ||
name = "{name}", | ||
target_compatible_with = [], | ||
exec_compatible_with = [], | ||
toolchain = "{name}_toolchain", | ||
toolchain_type = "{toolchain_type}", | ||
) | ||
""" | ||
|
||
def _toolchains_repo_impl(repository_ctx): | ||
build_content = _TOOLCHAIN_TEMPLATE.format( | ||
name = "py_test_toolchain", | ||
toolchain_type = repository_ctx.attr.toolchain_type, | ||
coverage_rc = repository_ctx.attr.coverage_rc, | ||
) | ||
repository_ctx.file("BUILD.bazel", build_content) | ||
|
||
py_test_toolchain_repo = repository_rule( | ||
_toolchains_repo_impl, | ||
doc = "Generates a toolchain hub repository", | ||
attrs = { | ||
"toolchain_type": attr.string(doc = "Toolchain type", mandatory = True), | ||
"coverage_rc": attr.label( | ||
allow_single_file = True, | ||
doc = "The coverage rc file", | ||
mandatory = True, | ||
), | ||
}, | ||
) | ||
|
||
def register_py_test_toolchain(coverage_rc, register_toolchains = True): | ||
# Need to create a repository rule for this to work. | ||
py_test_toolchain_repo( | ||
name = "py_test_toolchain", | ||
coverage_rc = coverage_rc, | ||
toolchain_type = str(PY_TEST_TOOLCHAIN_TYPE), | ||
) | ||
if register_toolchains: | ||
native.toolchain(name = "py_test_toolchain") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.