diff --git a/.bazelrc b/.bazelrc index c44124d961..66a644e289 100644 --- a/.bazelrc +++ b/.bazelrc @@ -4,8 +4,8 @@ # (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it) # To update these lines, execute # `bazel run @rules_bazel_integration_test//tools:update_deleted_packages` -build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered -query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered +build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/python/private,gazelle/pythonconfig,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered +query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/python/private,gazelle/pythonconfig,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered test --test_output=errors diff --git a/CHANGELOG.md b/CHANGELOG.md index 004d92c7bb..aa198ea0b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,9 @@ A brief description of the categories of changes: {#v0-0-0-added} ### Added +* (toolchain) Using testing toolchain to configure py_test coverage. + This opens the potential to configure differnt test runners. + ([#2246](https://github.com/bazelbuild/rules_python/pull/2246)). * (publish) The requirements file for the `twine` publishing rules have been updated to have a new convention: `requirements_darwin.txt`, `requirements_linux.txt`, `requirements_windows.txt` for each respective OS diff --git a/examples/bzlmod/MODULE.bazel.lock b/examples/bzlmod/MODULE.bazel.lock index 369e050cbb..b8ce0dc1de 100644 --- a/examples/bzlmod/MODULE.bazel.lock +++ b/examples/bzlmod/MODULE.bazel.lock @@ -6297,26 +6297,6 @@ ] } }, - "@@rules_python~//python/extensions:python_test.bzl%python_test": { - "general": { - "bzlTransitiveDigest": "SEH8ZrdlM2SCRF7dzdR/sGS1tbitMAmMMJ4ibAgTE+0=", - "usagesDigest": "aGmuY9IjHfwnuGdDvQH7QBfq7RWp2wTNV6oueQMpky0=", - "recordedFileInputs": {}, - "recordedDirentsInputs": {}, - "envVariables": {}, - "generatedRepoSpecs": { - "py_test_toolchain": { - "bzlFile": "@@rules_python~//python/private:py_test_toolchain.bzl", - "ruleClassName": "py_test_toolchain_repo", - "attributes": { - "coverage_rc": "@@//:.coveragerc", - "toolchain_type": "'@@rules_python~//python:py_test_toolchain_type'" - } - } - }, - "recordedRepoMappingEntries": [] - } - }, "@@rules_python~//python/private/pypi:pip.bzl%pip_internal": { "general": { "bzlTransitiveDigest": "pgIPI+ouKZGvpe1ZWE13QCQ5n0E4veB3tzWhx5XURJ0=", diff --git a/examples/bzlmod/tests/BUILD.bazel b/examples/bzlmod/tests/BUILD.bazel index 03d287a556..944108678e 100644 --- a/examples/bzlmod/tests/BUILD.bazel +++ b/examples/bzlmod/tests/BUILD.bazel @@ -54,6 +54,18 @@ py_test( main = "coverage_rc_is_set_test.py", ) +py_test_3_9( + name = "coverage_rc_is_set_3_9_test", + srcs = ["coverage_rc_is_set_test.py"], + main = "coverage_rc_is_set_test.py", +) + +py_test_3_11( + name = "coverage_rc_is_set_3_11_test", + srcs = ["coverage_rc_is_set_test.py"], + main = "coverage_rc_is_set_test.py", +) + py_test_3_9( name = "my_lib_3_9_test", srcs = ["my_lib_test.py"], diff --git a/examples/bzlmod/tests/coverage_rc_is_set_test.py b/examples/bzlmod/tests/coverage_rc_is_set_test.py index 7b156973b4..b228f0868a 100644 --- a/examples/bzlmod/tests/coverage_rc_is_set_test.py +++ b/examples/bzlmod/tests/coverage_rc_is_set_test.py @@ -22,7 +22,7 @@ def test_coverage_rc_file_exists(self): 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", + 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 diff --git a/python/config_settings/transition.bzl b/python/config_settings/transition.bzl index a7646dcda3..ca74369f2e 100644 --- a/python/config_settings/transition.bzl +++ b/python/config_settings/transition.bzl @@ -81,6 +81,10 @@ def _transition_py_impl(ctx): for k, v in ctx.attr.env.items(): env[k] = ctx.expand_location(v) + # Add the environment from the target if it has RunEnvironmentInfo. + # RunEnvironmentInfo contains environment variables configured by exec_group toolchain + if RunEnvironmentInfo in target: + env.update(target[RunEnvironmentInfo].environment) providers = [ DefaultInfo( executable = executable, diff --git a/python/extensions/python_test.bzl b/python/extensions/python_test.bzl index 1345012bc1..c437b5e431 100644 --- a/python/extensions/python_test.bzl +++ b/python/extensions/python_test.bzl @@ -26,12 +26,6 @@ python_test.configure( 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") diff --git a/python/private/py_executable.bzl b/python/private/py_executable.bzl index 214037b0b3..b181045121 100644 --- a/python/private/py_executable.bzl +++ b/python/private/py_executable.bzl @@ -848,6 +848,7 @@ def _create_providers( runtime_details: struct of runtime information; see _get_runtime_details() output_groups: dict[str, depset[File]]; used to create OutputGroupInfo semantics: BinarySemantics struct; see create_binary_semantics() + is_test: bool; True if the rule is a test rule, Returns: A list of modern providers. diff --git a/python/private/py_test_toolchain.bzl b/python/private/py_test_toolchain.bzl index 0515fdace9..d8bc80ae6d 100644 --- a/python/private/py_test_toolchain.bzl +++ b/python/private/py_test_toolchain.bzl @@ -22,7 +22,8 @@ load( "PY_TEST_TOOLCHAIN_TYPE", ) -PytestProvider = provider( +PyTestProviderInfo = provider( + doc = "Information about the pytest toolchain", fields = [ "coverage_rc", ], @@ -31,7 +32,7 @@ PytestProvider = provider( def _py_test_toolchain_impl(ctx): return [ platform_common.ToolchainInfo( - py_test_info = PytestProvider( + py_test_info = PyTestProviderInfo( coverage_rc = ctx.attr.coverage_rc, ), ), @@ -56,6 +57,10 @@ py_test_toolchain_macro( def py_test_toolchain_macro(*, name, coverage_rc, toolchain_type): """ Macro to create a py_test_toolchain rule and a native toolchain rule. + + name: The name of the toolchain. + coverage_rc: The coverage rc file. + toolchain_type: The toolchain type. """ py_test_toolchain( name = "{}_toolchain".format(name), @@ -73,7 +78,7 @@ def _toolchains_repo_impl(repository_ctx): kwargs = dict( name = repository_ctx.name, coverage_rc = str(repository_ctx.attr.coverage_rc), - toolchain_type = repository_ctx.attr.toolchain_type, + toolchain_type = str(repository_ctx.attr.toolchain_type), ) build_content = _TOOLCHAIN_TEMPLATE.format( @@ -88,21 +93,27 @@ 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, ), + "toolchain_type": attr.label(doc = "Toolchain type", mandatory = True), }, ) -def register_py_test_toolchain(coverage_rc, register_toolchains = True): - # Need to create a repository rule for this to work. +def register_py_test_toolchain(name, coverage_rc, register_toolchains = True): + """ Register the py_test_toolchain and native toolchain rules. + + name: The name of the toolchain. + coverage_rc: The coverage rc file. + register_toolchains: Whether to register the toolchains. + + """ py_test_toolchain_repo( - name = "py_test_toolchain", + name = name, coverage_rc = coverage_rc, - toolchain_type = str(PY_TEST_TOOLCHAIN_TYPE), + toolchain_type = PY_TEST_TOOLCHAIN_TYPE, ) if register_toolchains: native.toolchain(name = "py_test_toolchain") diff --git a/python/private/py_toolchain_suite.bzl b/python/private/py_toolchain_suite.bzl index 0c9f4d2fc3..a69be376b4 100644 --- a/python/private/py_toolchain_suite.bzl +++ b/python/private/py_toolchain_suite.bzl @@ -20,7 +20,6 @@ load( ":toolchain_types.bzl", "EXEC_TOOLS_TOOLCHAIN_TYPE", "PY_CC_TOOLCHAIN_TYPE", - "PY_TEST_TOOLCHAIN_TYPE", "TARGET_TOOLCHAIN_TYPE", ) diff --git a/python/private/python_test.bzl b/python/private/python_test.bzl index 8f4b6a9cb4..3371b55a4b 100644 --- a/python/private/python_test.bzl +++ b/python/private/python_test.bzl @@ -14,8 +14,8 @@ "Python test toolchain module extensions for use with bzlmod." +load("@bazel_features//:features.bzl", "bazel_features") load("//python/private:py_test_toolchain.bzl", "register_py_test_toolchain") -load(":text_util.bzl", "render") def _python_test_impl(module_ctx): """Implementation of the `coverage` extension. @@ -26,35 +26,21 @@ def _python_test_impl(module_ctx): for mod in module_ctx.modules: for tag in mod.tags.configure: register_py_test_toolchain( + name = "py_test_toolchain", coverage_rc = tag.coveragerc, register_toolchains = False, ) + if bazel_features.external_deps.extension_metadata_has_reproducible: + return module_ctx.extension_metadata(reproducible = True) + else: + return None configure = tag_class( doc = """Tag class used to register Python toolchains.""", attrs = { # TODO: Add testrunner and potentially coverage_tool "coveragerc": attr.label( - doc = """Tag class used to register Python toolchains. - -:::{topic} Toolchains in the Root Module - -:::{tip} -In order to use a different name than the above, you can use the following `MODULE.bazel` -syntax: -```starlark -python = use_extension("@rules_python//python/extensions:python.bzl", "python") -python.toolchain( - is_default = True, - python_version = "3.11", -) - -use_repo(python, my_python_name = "python_3_11") -``` - -Then the python interpreter will be available as `my_python_name`. -::: -""", + doc = """Tag class used to register Python toolchains.""", mandatory = True, ), },