xref: /aosp_15_r20/external/bazelbuild-rules_python/tests/py_runtime_info/py_runtime_info_tests.bzl (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1# Copyright 2023 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"""Starlark tests for PyRuntimeInfo provider."""
15
16load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
17load("@rules_testing//lib:test_suite.bzl", "test_suite")
18load("//python:py_runtime_info.bzl", "PyRuntimeInfo")
19load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER")  # buildifier: disable=bzl-visibility
20
21def _create_py_runtime_info_without_interpreter_version_info_impl(ctx):
22    kwargs = {}
23    if IS_BAZEL_7_OR_HIGHER:
24        kwargs["bootstrap_template"] = ctx.attr.bootstrap_template
25
26    return [PyRuntimeInfo(
27        interpreter = ctx.file.interpreter,
28        files = depset(ctx.files.files),
29        python_version = "PY3",
30        **kwargs
31    )]
32
33_create_py_runtime_info_without_interpreter_version_info = rule(
34    implementation = _create_py_runtime_info_without_interpreter_version_info_impl,
35    attrs = {
36        "bootstrap_template": attr.label(allow_single_file = True, default = "bootstrap.txt"),
37        "files": attr.label_list(allow_files = True, default = ["data.txt"]),
38        "interpreter": attr.label(allow_single_file = True, default = "interpreter.sh"),
39        "python_version": attr.string(default = "PY3"),
40    },
41)
42
43_tests = []
44
45def _test_can_create_py_runtime_info_without_interpreter_version_info(name):
46    _create_py_runtime_info_without_interpreter_version_info(
47        name = name + "_subject",
48    )
49    analysis_test(
50        name = name,
51        target = name + "_subject",
52        impl = _test_can_create_py_runtime_info_without_interpreter_version_info_impl,
53    )
54
55def _test_can_create_py_runtime_info_without_interpreter_version_info_impl(env, target):
56    # If we get this for, construction succeeded, so nothing to check
57    _ = env, target  # @unused
58
59_tests.append(_test_can_create_py_runtime_info_without_interpreter_version_info)
60
61def py_runtime_info_test_suite(name):
62    test_suite(
63        name = name,
64        tests = _tests,
65    )
66