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
|
# 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.
"""Helpers and utilities multiple tests re-use."""
load("@bazel_skylib//lib:structs.bzl", "structs")
# Use this with is_windows()
WINDOWS_ATTR = {"windows": attr.label(default = "@platforms//os:windows")}
def _create_tests(tests, **kwargs):
test_names = []
for func in tests:
test_name = _test_name_from_function(func)
func(name = test_name, **kwargs)
test_names.append(test_name)
return test_names
def _test_name_from_function(func):
"""Derives the name of the given rule implementation function.
Args:
func: the function whose name to extract
Returns:
The name of the given function. Note it will have leading and trailing
"_" stripped -- this allows passing a private function and having the
name of the test not start with "_".
"""
# Starlark currently stringifies a function as "<function NAME>", so we use
# that knowledge to parse the "NAME" portion out.
# NOTE: This is relying on an implementation detail of Bazel
func_name = str(func)
func_name = func_name.partition("<function ")[-1]
func_name = func_name.rpartition(">")[0]
func_name = func_name.partition(" ")[0]
return func_name.strip("_")
def _struct_with(s, **kwargs):
struct_dict = structs.to_dict(s)
struct_dict.update(kwargs)
return struct(**struct_dict)
def _is_bazel_6_or_higher():
# Bazel 5.4 has a bug where every access of testing.ExecutionInfo is a
# different object that isn't equal to any other. This is fixed in bazel 6+.
return testing.ExecutionInfo == testing.ExecutionInfo
def _is_windows(env):
"""Tell if the target platform is windows.
This assumes the `WINDOWS_ATTR` attribute was added.
Args:
env: The test env struct
Returns:
True if the target is Windows, False if not.
"""
constraint = env.ctx.attr.windows[platform_common.ConstraintValueInfo]
return env.ctx.target_platform_has_constraint(constraint)
util = struct(
create_tests = _create_tests,
struct_with = _struct_with,
is_bazel_6_or_higher = _is_bazel_6_or_higher,
is_windows = _is_windows,
)
|