1# Copyright 2024 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#    http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Starlark tests for py_runtime rule."""
16
17load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
18load("@rules_testing//lib:test_suite.bzl", "test_suite")
19load("@rules_testing//lib:util.bzl", rt_util = "util")
20load(
21    "//python/private:toolchain_types.bzl",
22    "EXEC_TOOLS_TOOLCHAIN_TYPE",
23    "PY_CC_TOOLCHAIN_TYPE",
24    "TARGET_TOOLCHAIN_TYPE",
25)  # buildifier: disable=bzl-visibility
26load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER")  # buildifier: disable=bzl-visibility
27load("//tests/support:support.bzl", "CC_TOOLCHAIN", "EXEC_TOOLS_TOOLCHAIN", "VISIBLE_FOR_TESTING")
28
29_LookupInfo = provider()  # buildifier: disable=provider-params
30
31def _use_toolchains_impl(ctx):
32    return [
33        _LookupInfo(
34            target = ctx.toolchains[TARGET_TOOLCHAIN_TYPE],
35            exec = ctx.toolchains[EXEC_TOOLS_TOOLCHAIN_TYPE],
36            cc = ctx.toolchains[PY_CC_TOOLCHAIN_TYPE],
37        ),
38    ]
39
40_use_toolchains = rule(
41    implementation = _use_toolchains_impl,
42    toolchains = [
43        TARGET_TOOLCHAIN_TYPE,
44        EXEC_TOOLS_TOOLCHAIN_TYPE,
45        PY_CC_TOOLCHAIN_TYPE,
46    ],
47)
48
49_tests = []
50
51def _test_runtime_env_toolchain_matches(name):
52    rt_util.helper_target(
53        _use_toolchains,
54        name = name + "_subject",
55    )
56    extra_toolchains = [
57        str(Label("//python/runtime_env_toolchains:all")),
58    ]
59
60    # We have to add a cc toolchain because py_cc toolchain depends on it.
61    # However, that package also defines a different fake py_cc toolchain we
62    # don't want to use, so we need to ensure the runtime_env toolchain has
63    # higher precendence.
64    # However, Bazel 6 and Bazel 7 process --extra_toolchains in different
65    # orders:
66    #  * Bazel 6 goes left to right
67    #  * Bazel 7 goes right to left
68    # We could just put our preferred toolchain before *and* after
69    # the undesired toolchain...
70    # However, Bazel 7 has a bug where *duplicate* entries are ignored,
71    # and only the *first* entry is respected.
72    if IS_BAZEL_7_OR_HIGHER:
73        extra_toolchains.insert(0, CC_TOOLCHAIN)
74    else:
75        extra_toolchains.append(CC_TOOLCHAIN)
76    analysis_test(
77        name = name,
78        impl = _test_runtime_env_toolchain_matches_impl,
79        target = name + "_subject",
80        config_settings = {
81            "//command_line_option:extra_toolchains": extra_toolchains,
82            EXEC_TOOLS_TOOLCHAIN: "enabled",
83            VISIBLE_FOR_TESTING: True,
84        },
85    )
86
87def _test_runtime_env_toolchain_matches_impl(env, target):
88    env.expect.that_str(
89        str(target[_LookupInfo].target.toolchain_label),
90    ).contains("runtime_env_py_runtime_pair")
91    env.expect.that_str(
92        str(target[_LookupInfo].exec.toolchain_label),
93    ).contains("runtime_env_py_exec_tools")
94    env.expect.that_str(
95        str(target[_LookupInfo].cc.toolchain_label),
96    ).contains("runtime_env_py_cc")
97
98_tests.append(_test_runtime_env_toolchain_matches)
99
100def runtime_env_toolchain_test_suite(name):
101    test_suite(name = name, tests = _tests)
102