aboutsummaryrefslogtreecommitdiff
path: root/tests/py_runtime_pair/py_runtime_pair_tests.bzl
blob: 236f1ba3a5db3cff87b393647e9bf55080c235cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# 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.
"""Starlark tests for py_runtime_pair rule."""

load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
load("@rules_testing//lib:test_suite.bzl", "test_suite")
load("@rules_testing//lib:truth.bzl", "matching", "subjects")
load("@rules_testing//lib:util.bzl", rt_util = "util")
load("//python:py_binary.bzl", "py_binary")
load("//python:py_runtime.bzl", "py_runtime")
load("//python:py_runtime_pair.bzl", "py_runtime_pair")
load("//python/private:reexports.bzl", "BuiltinPyRuntimeInfo")  # buildifier: disable=bzl-visibility
load("//tests:py_runtime_info_subject.bzl", "py_runtime_info_subject")

def _toolchain_factory(value, meta):
    return subjects.struct(
        value,
        meta = meta,
        attrs = {
            "py3_runtime": py_runtime_info_subject,
        },
    )

def _provides_builtin_py_runtime_info_impl(ctx):  # @unused
    return [BuiltinPyRuntimeInfo(
        python_version = "PY3",
        interpreter_path = "builtin",
    )]

_provides_builtin_py_runtime_info = rule(
    implementation = _provides_builtin_py_runtime_info_impl,
)

_tests = []

def _test_basic(name):
    rt_util.helper_target(
        py_runtime,
        name = name + "_runtime",
        interpreter = "fake_interpreter",
        python_version = "PY3",
        files = ["file1.txt"],
    )
    rt_util.helper_target(
        py_runtime_pair,
        name = name + "_subject",
        py3_runtime = name + "_runtime",
    )
    analysis_test(
        name = name,
        target = name + "_subject",
        impl = _test_basic_impl,
    )

def _test_basic_impl(env, target):
    toolchain = env.expect.that_target(target).provider(
        platform_common.ToolchainInfo,
        factory = _toolchain_factory,
    )
    toolchain.py3_runtime().python_version().equals("PY3")
    toolchain.py3_runtime().files().contains_predicate(matching.file_basename_equals("file1.txt"))
    toolchain.py3_runtime().interpreter().path().contains("fake_interpreter")

_tests.append(_test_basic)

def _test_builtin_py_info_accepted(name):
    rt_util.helper_target(
        _provides_builtin_py_runtime_info,
        name = name + "_runtime",
    )
    rt_util.helper_target(
        py_runtime_pair,
        name = name + "_subject",
        py3_runtime = name + "_runtime",
    )
    analysis_test(
        name = name,
        target = name + "_subject",
        impl = _test_builtin_py_info_accepted_impl,
    )

def _test_builtin_py_info_accepted_impl(env, target):
    toolchain = env.expect.that_target(target).provider(
        platform_common.ToolchainInfo,
        factory = _toolchain_factory,
    )
    toolchain.py3_runtime().python_version().equals("PY3")
    toolchain.py3_runtime().interpreter_path().equals("builtin")

_tests.append(_test_builtin_py_info_accepted)

def _test_py_runtime_pair_and_binary(name):
    rt_util.helper_target(
        py_runtime,
        name = name + "_runtime",
        interpreter_path = "/fake_interpreter",
        python_version = "PY3",
    )
    rt_util.helper_target(
        py_runtime_pair,
        name = name + "_pair",
        py3_runtime = name + "_runtime",
    )
    native.toolchain(
        name = name + "_toolchain",
        toolchain = name + "_pair",
        toolchain_type = "//python:toolchain_type",
    )
    rt_util.helper_target(
        py_binary,
        name = name + "_subject",
        srcs = [name + "_subject.py"],
    )
    analysis_test(
        name = name,
        target = name + "_subject",
        impl = _test_py_runtime_pair_and_binary_impl,
        config_settings = {
            "//command_line_option:extra_toolchains": [
                "//tests/py_runtime_pair:{}_toolchain".format(name),
                "//tests/cc:all",
            ],
        },
    )

def _test_py_runtime_pair_and_binary_impl(env, target):
    # Building indicates success, so nothing to assert
    _ = env, target  # @unused

_tests.append(_test_py_runtime_pair_and_binary)

def py_runtime_pair_test_suite(name):
    test_suite(
        name = name,
        tests = _tests,
    )