• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"""Tests common to py_test, py_binary, and py_library rules."""
15
16load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
17load("@rules_testing//lib:truth.bzl", "matching")
18load("@rules_testing//lib:util.bzl", "PREVENT_IMPLICIT_BUILDING_TAGS", rt_util = "util")
19load("//python:defs.bzl", "PyInfo")
20load("//python/private:reexports.bzl", "BuiltinPyInfo")  # buildifier: disable=bzl-visibility
21load("//tests/base_rules:util.bzl", pt_util = "util")
22load("//tests/support:py_info_subject.bzl", "py_info_subject")
23
24_tests = []
25
26_PRODUCES_PY_INFO_ATTRS = {
27    "imports": attr.string_list(),
28    "srcs": attr.label_list(allow_files = True),
29}
30
31def _create_py_info(ctx, provider_type):
32    return [provider_type(
33        transitive_sources = depset(ctx.files.srcs),
34        imports = depset(ctx.attr.imports),
35    )]
36
37def _produces_builtin_py_info_impl(ctx):
38    return _create_py_info(ctx, BuiltinPyInfo)
39
40_produces_builtin_py_info = rule(
41    implementation = _produces_builtin_py_info_impl,
42    attrs = _PRODUCES_PY_INFO_ATTRS,
43)
44
45def _produces_py_info_impl(ctx):
46    return _create_py_info(ctx, PyInfo)
47
48_produces_py_info = rule(
49    implementation = _produces_py_info_impl,
50    attrs = _PRODUCES_PY_INFO_ATTRS,
51)
52
53def _not_produces_py_info_impl(ctx):
54    _ = ctx  # @unused
55    return [DefaultInfo()]
56
57_not_produces_py_info = rule(
58    implementation = _not_produces_py_info_impl,
59)
60
61def _py_info_propagation_setup(name, config, produce_py_info_rule, test_impl):
62    rt_util.helper_target(
63        config.base_test_rule,
64        name = name + "_subject",
65        deps = [name + "_produces_builtin_py_info"],
66    )
67    rt_util.helper_target(
68        produce_py_info_rule,
69        name = name + "_produces_builtin_py_info",
70        srcs = [rt_util.empty_file(name + "_produce.py")],
71        imports = ["custom-import"],
72    )
73    analysis_test(
74        name = name,
75        target = name + "_subject",
76        impl = test_impl,
77    )
78
79def _py_info_propagation_test_impl(env, target, provider_type):
80    info = env.expect.that_target(target).provider(
81        provider_type,
82        factory = py_info_subject,
83    )
84
85    info.transitive_sources().contains("{package}/{test_name}_produce.py")
86    info.imports().contains("custom-import")
87
88def _test_py_info_propagation_builtin(name, config):
89    if not BuiltinPyInfo:
90        rt_util.skip_test(name = name)
91        return
92    _py_info_propagation_setup(
93        name,
94        config,
95        _produces_builtin_py_info,
96        _test_py_info_propagation_builtin_impl,
97    )
98
99def _test_py_info_propagation_builtin_impl(env, target):
100    _py_info_propagation_test_impl(env, target, BuiltinPyInfo)
101
102_tests.append(_test_py_info_propagation_builtin)
103
104def _test_py_info_propagation(name, config):
105    _py_info_propagation_setup(
106        name,
107        config,
108        _produces_py_info,
109        _test_py_info_propagation_impl,
110    )
111
112def _test_py_info_propagation_impl(env, target):
113    _py_info_propagation_test_impl(env, target, PyInfo)
114
115_tests.append(_test_py_info_propagation)
116
117def _test_requires_provider(name, config):
118    rt_util.helper_target(
119        config.base_test_rule,
120        name = name + "_subject",
121        deps = [name + "_nopyinfo"],
122    )
123    rt_util.helper_target(
124        _not_produces_py_info,
125        name = name + "_nopyinfo",
126    )
127    analysis_test(
128        name = name,
129        target = name + "_subject",
130        impl = _test_requires_provider_impl,
131        expect_failure = True,
132    )
133
134def _test_requires_provider_impl(env, target):
135    env.expect.that_target(target).failures().contains_predicate(
136        matching.str_matches("mandatory*PyInfo"),
137    )
138
139_tests.append(_test_requires_provider)
140
141def _test_data_sets_uses_shared_library(name, config):
142    rt_util.helper_target(
143        config.base_test_rule,
144        name = name + "_subject",
145        data = [rt_util.empty_file(name + "_dso.so")],
146    )
147    analysis_test(
148        name = name,
149        target = name + "_subject",
150        impl = _test_data_sets_uses_shared_library_impl,
151    )
152
153def _test_data_sets_uses_shared_library_impl(env, target):
154    env.expect.that_target(target).provider(
155        PyInfo,
156        factory = py_info_subject,
157    ).uses_shared_libraries().equals(True)
158
159_tests.append(_test_data_sets_uses_shared_library)
160
161def _test_tags_can_be_tuple(name, config):
162    # We don't use a helper because we want to ensure that value passed is
163    # a tuple.
164    config.base_test_rule(
165        name = name + "_subject",
166        tags = ("one", "two") + tuple(PREVENT_IMPLICIT_BUILDING_TAGS),
167    )
168    analysis_test(
169        name = name,
170        target = name + "_subject",
171        impl = _test_tags_can_be_tuple_impl,
172    )
173
174def _test_tags_can_be_tuple_impl(env, target):
175    env.expect.that_target(target).tags().contains_at_least([
176        "one",
177        "two",
178    ])
179
180_tests.append(_test_tags_can_be_tuple)
181
182def create_base_tests(config):
183    return pt_util.create_tests(_tests, config = config)
184