aboutsummaryrefslogtreecommitdiff
path: root/tests/base_rules/py_executable_base_tests.bzl
blob: 396057997d48de90f88c1d06fae4e1077ee7795a (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# 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.
"""Tests common to py_binary and py_test (executable rules)."""

load("@rules_python_internal//:rules_python_config.bzl", rp_config = "config")
load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
load("@rules_testing//lib:truth.bzl", "matching")
load("@rules_testing//lib:util.bzl", rt_util = "util")
load("//tests/base_rules:base_tests.bzl", "create_base_tests")
load("//tests/base_rules:util.bzl", "WINDOWS_ATTR", pt_util = "util")
load("//tests/support:test_platforms.bzl", "WINDOWS")

_tests = []

def _test_basic_windows(name, config):
    if rp_config.enable_pystar:
        target_compatible_with = []
    else:
        target_compatible_with = ["@platforms//:incompatible"]
    rt_util.helper_target(
        config.rule,
        name = name + "_subject",
        srcs = ["main.py"],
        main = "main.py",
    )
    analysis_test(
        name = name,
        impl = _test_basic_windows_impl,
        target = name + "_subject",
        config_settings = {
            # NOTE: The default for this flag is based on the Bazel host OS, not
            # the target platform. For windows, it defaults to true, so force
            # it to that to match behavior when this test runs on other
            # platforms.
            "//command_line_option:build_python_zip": "true",
            "//command_line_option:cpu": "windows_x86_64",
            "//command_line_option:crosstool_top": Label("//tests/cc:cc_toolchain_suite"),
            "//command_line_option:extra_toolchains": [str(Label("//tests/cc:all"))],
            "//command_line_option:platforms": [WINDOWS],
        },
        attr_values = {"target_compatible_with": target_compatible_with},
    )

def _test_basic_windows_impl(env, target):
    target = env.expect.that_target(target)
    target.executable().path().contains(".exe")
    target.runfiles().contains_predicate(matching.str_endswith(
        target.meta.format_str("/{name}.zip"),
    ))
    target.runfiles().contains_predicate(matching.str_endswith(
        target.meta.format_str("/{name}.exe"),
    ))

_tests.append(_test_basic_windows)

def _test_executable_in_runfiles(name, config):
    rt_util.helper_target(
        config.rule,
        name = name + "_subject",
        srcs = [name + "_subject.py"],
    )
    analysis_test(
        name = name,
        impl = _test_executable_in_runfiles_impl,
        target = name + "_subject",
        attrs = WINDOWS_ATTR,
    )

_tests.append(_test_executable_in_runfiles)

def _test_executable_in_runfiles_impl(env, target):
    if pt_util.is_windows(env):
        exe = ".exe"
    else:
        exe = ""

    env.expect.that_target(target).runfiles().contains_at_least([
        "{workspace}/{package}/{test_name}_subject" + exe,
    ])

def _test_default_main_can_be_generated(name, config):
    rt_util.helper_target(
        config.rule,
        name = name + "_subject",
        srcs = [rt_util.empty_file(name + "_subject.py")],
    )
    analysis_test(
        name = name,
        impl = _test_default_main_can_be_generated_impl,
        target = name + "_subject",
    )

_tests.append(_test_default_main_can_be_generated)

def _test_default_main_can_be_generated_impl(env, target):
    env.expect.that_target(target).default_outputs().contains(
        "{package}/{test_name}_subject.py",
    )

def _test_default_main_can_have_multiple_path_segments(name, config):
    rt_util.helper_target(
        config.rule,
        name = name + "/subject",
        srcs = [name + "/subject.py"],
    )
    analysis_test(
        name = name,
        impl = _test_default_main_can_have_multiple_path_segments_impl,
        target = name + "/subject",
    )

_tests.append(_test_default_main_can_have_multiple_path_segments)

def _test_default_main_can_have_multiple_path_segments_impl(env, target):
    env.expect.that_target(target).default_outputs().contains(
        "{package}/{test_name}/subject.py",
    )

def _test_default_main_must_be_in_srcs(name, config):
    # Bazel 5 will crash with a Java stacktrace when the native Python
    # rules have an error.
    if not pt_util.is_bazel_6_or_higher():
        rt_util.skip_test(name = name)
        return
    rt_util.helper_target(
        config.rule,
        name = name + "_subject",
        srcs = ["other.py"],
    )
    analysis_test(
        name = name,
        impl = _test_default_main_must_be_in_srcs_impl,
        target = name + "_subject",
        expect_failure = True,
    )

_tests.append(_test_default_main_must_be_in_srcs)

def _test_default_main_must_be_in_srcs_impl(env, target):
    env.expect.that_target(target).failures().contains_predicate(
        matching.str_matches("default*does not appear in srcs"),
    )

def _test_default_main_cannot_be_ambiguous(name, config):
    # Bazel 5 will crash with a Java stacktrace when the native Python
    # rules have an error.
    if not pt_util.is_bazel_6_or_higher():
        rt_util.skip_test(name = name)
        return
    rt_util.helper_target(
        config.rule,
        name = name + "_subject",
        srcs = [name + "_subject.py", "other/{}_subject.py".format(name)],
    )
    analysis_test(
        name = name,
        impl = _test_default_main_cannot_be_ambiguous_impl,
        target = name + "_subject",
        expect_failure = True,
    )

_tests.append(_test_default_main_cannot_be_ambiguous)

def _test_default_main_cannot_be_ambiguous_impl(env, target):
    env.expect.that_target(target).failures().contains_predicate(
        matching.str_matches("default main*matches multiple files"),
    )

def _test_explicit_main(name, config):
    rt_util.helper_target(
        config.rule,
        name = name + "_subject",
        srcs = ["custom.py"],
        main = "custom.py",
    )
    analysis_test(
        name = name,
        impl = _test_explicit_main_impl,
        target = name + "_subject",
    )

_tests.append(_test_explicit_main)

def _test_explicit_main_impl(env, target):
    # There isn't a direct way to ask what main file was selected, so we
    # rely on it being in the default outputs.
    env.expect.that_target(target).default_outputs().contains(
        "{package}/custom.py",
    )

def _test_explicit_main_cannot_be_ambiguous(name, config):
    # Bazel 5 will crash with a Java stacktrace when the native Python
    # rules have an error.
    if not pt_util.is_bazel_6_or_higher():
        rt_util.skip_test(name = name)
        return
    rt_util.helper_target(
        config.rule,
        name = name + "_subject",
        srcs = ["x/foo.py", "y/foo.py"],
        main = "foo.py",
    )
    analysis_test(
        name = name,
        impl = _test_explicit_main_cannot_be_ambiguous_impl,
        target = name + "_subject",
        expect_failure = True,
    )

_tests.append(_test_explicit_main_cannot_be_ambiguous)

def _test_explicit_main_cannot_be_ambiguous_impl(env, target):
    env.expect.that_target(target).failures().contains_predicate(
        matching.str_matches("foo.py*matches multiple"),
    )

def _test_files_to_build(name, config):
    rt_util.helper_target(
        config.rule,
        name = name + "_subject",
        srcs = [name + "_subject.py"],
    )
    analysis_test(
        name = name,
        impl = _test_files_to_build_impl,
        target = name + "_subject",
        attrs = WINDOWS_ATTR,
    )

_tests.append(_test_files_to_build)

def _test_files_to_build_impl(env, target):
    default_outputs = env.expect.that_target(target).default_outputs()
    if pt_util.is_windows(env):
        default_outputs.contains("{package}/{test_name}_subject.exe")
    else:
        default_outputs.contains_exactly([
            "{package}/{test_name}_subject",
            "{package}/{test_name}_subject.py",
        ])

def _test_name_cannot_end_in_py(name, config):
    # Bazel 5 will crash with a Java stacktrace when the native Python
    # rules have an error.
    if not pt_util.is_bazel_6_or_higher():
        rt_util.skip_test(name = name)
        return
    rt_util.helper_target(
        config.rule,
        name = name + "_subject.py",
        srcs = ["main.py"],
    )
    analysis_test(
        name = name,
        impl = _test_name_cannot_end_in_py_impl,
        target = name + "_subject.py",
        expect_failure = True,
    )

_tests.append(_test_name_cannot_end_in_py)

def _test_name_cannot_end_in_py_impl(env, target):
    env.expect.that_target(target).failures().contains_predicate(
        matching.str_matches("name must not end in*.py"),
    )

# Can't test this -- mandatory validation happens before analysis test
# can intercept it
# TODO(#1069): Once re-implemented in Starlark, modify rule logic to make this
# testable.
# def _test_srcs_is_mandatory(name, config):
#     rt_util.helper_target(
#         config.rule,
#         name = name + "_subject",
#     )
#     analysis_test(
#         name = name,
#         impl = _test_srcs_is_mandatory,
#         target = name + "_subject",
#         expect_failure = True,
#     )
#
# _tests.append(_test_srcs_is_mandatory)
#
# def _test_srcs_is_mandatory_impl(env, target):
#     env.expect.that_target(target).failures().contains_predicate(
#         matching.str_matches("mandatory*srcs"),
#     )

# =====
# You were gonna add a test at the end, weren't you?
# Nope. Please keep them sorted; put it in its alphabetical location.
# Here's the alphabet so you don't have to sing that song in your head:
# A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
# =====

def create_executable_tests(config):
    def _executable_with_srcs_wrapper(name, **kwargs):
        if not kwargs.get("srcs"):
            kwargs["srcs"] = [name + ".py"]
        config.rule(name = name, **kwargs)

    config = pt_util.struct_with(config, base_test_rule = _executable_with_srcs_wrapper)
    return pt_util.create_tests(_tests, config = config) + create_base_tests(config = config)