1*60517a1eSAndroid Build Coastguard Worker# Copyright 2023 The Bazel Authors. All rights reserved. 2*60517a1eSAndroid Build Coastguard Worker# 3*60517a1eSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 4*60517a1eSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 5*60517a1eSAndroid Build Coastguard Worker# You may obtain a copy of the License at 6*60517a1eSAndroid Build Coastguard Worker# 7*60517a1eSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 8*60517a1eSAndroid Build Coastguard Worker# 9*60517a1eSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 10*60517a1eSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 11*60517a1eSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*60517a1eSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 13*60517a1eSAndroid Build Coastguard Worker# limitations under the License. 14*60517a1eSAndroid Build Coastguard Worker"""Helpers and utilities multiple tests re-use.""" 15*60517a1eSAndroid Build Coastguard Worker 16*60517a1eSAndroid Build Coastguard Workerload("@bazel_skylib//lib:structs.bzl", "structs") 17*60517a1eSAndroid Build Coastguard Workerload("//python/private:util.bzl", "IS_BAZEL_6_OR_HIGHER") # buildifier: disable=bzl-visibility 18*60517a1eSAndroid Build Coastguard Worker 19*60517a1eSAndroid Build Coastguard Worker# Use this with is_windows() 20*60517a1eSAndroid Build Coastguard WorkerWINDOWS_ATTR = {"windows": attr.label(default = "@platforms//os:windows")} 21*60517a1eSAndroid Build Coastguard Worker 22*60517a1eSAndroid Build Coastguard Workerdef _create_tests(tests, **kwargs): 23*60517a1eSAndroid Build Coastguard Worker test_names = [] 24*60517a1eSAndroid Build Coastguard Worker for func in tests: 25*60517a1eSAndroid Build Coastguard Worker test_name = _test_name_from_function(func) 26*60517a1eSAndroid Build Coastguard Worker func(name = test_name, **kwargs) 27*60517a1eSAndroid Build Coastguard Worker test_names.append(test_name) 28*60517a1eSAndroid Build Coastguard Worker return test_names 29*60517a1eSAndroid Build Coastguard Worker 30*60517a1eSAndroid Build Coastguard Workerdef _test_name_from_function(func): 31*60517a1eSAndroid Build Coastguard Worker """Derives the name of the given rule implementation function. 32*60517a1eSAndroid Build Coastguard Worker 33*60517a1eSAndroid Build Coastguard Worker Args: 34*60517a1eSAndroid Build Coastguard Worker func: the function whose name to extract 35*60517a1eSAndroid Build Coastguard Worker 36*60517a1eSAndroid Build Coastguard Worker Returns: 37*60517a1eSAndroid Build Coastguard Worker The name of the given function. Note it will have leading and trailing 38*60517a1eSAndroid Build Coastguard Worker "_" stripped -- this allows passing a private function and having the 39*60517a1eSAndroid Build Coastguard Worker name of the test not start with "_". 40*60517a1eSAndroid Build Coastguard Worker """ 41*60517a1eSAndroid Build Coastguard Worker 42*60517a1eSAndroid Build Coastguard Worker # Starlark currently stringifies a function as "<function NAME>", so we use 43*60517a1eSAndroid Build Coastguard Worker # that knowledge to parse the "NAME" portion out. 44*60517a1eSAndroid Build Coastguard Worker # NOTE: This is relying on an implementation detail of Bazel 45*60517a1eSAndroid Build Coastguard Worker func_name = str(func) 46*60517a1eSAndroid Build Coastguard Worker func_name = func_name.partition("<function ")[-1] 47*60517a1eSAndroid Build Coastguard Worker func_name = func_name.rpartition(">")[0] 48*60517a1eSAndroid Build Coastguard Worker func_name = func_name.partition(" ")[0] 49*60517a1eSAndroid Build Coastguard Worker return func_name.strip("_") 50*60517a1eSAndroid Build Coastguard Worker 51*60517a1eSAndroid Build Coastguard Workerdef _struct_with(s, **kwargs): 52*60517a1eSAndroid Build Coastguard Worker struct_dict = structs.to_dict(s) 53*60517a1eSAndroid Build Coastguard Worker struct_dict.update(kwargs) 54*60517a1eSAndroid Build Coastguard Worker return struct(**struct_dict) 55*60517a1eSAndroid Build Coastguard Worker 56*60517a1eSAndroid Build Coastguard Workerdef _is_bazel_6_or_higher(): 57*60517a1eSAndroid Build Coastguard Worker return IS_BAZEL_6_OR_HIGHER 58*60517a1eSAndroid Build Coastguard Worker 59*60517a1eSAndroid Build Coastguard Workerdef _is_windows(env): 60*60517a1eSAndroid Build Coastguard Worker """Tell if the target platform is windows. 61*60517a1eSAndroid Build Coastguard Worker 62*60517a1eSAndroid Build Coastguard Worker This assumes the `WINDOWS_ATTR` attribute was added. 63*60517a1eSAndroid Build Coastguard Worker 64*60517a1eSAndroid Build Coastguard Worker Args: 65*60517a1eSAndroid Build Coastguard Worker env: The test env struct 66*60517a1eSAndroid Build Coastguard Worker Returns: 67*60517a1eSAndroid Build Coastguard Worker True if the target is Windows, False if not. 68*60517a1eSAndroid Build Coastguard Worker """ 69*60517a1eSAndroid Build Coastguard Worker constraint = env.ctx.attr.windows[platform_common.ConstraintValueInfo] 70*60517a1eSAndroid Build Coastguard Worker return env.ctx.target_platform_has_constraint(constraint) 71*60517a1eSAndroid Build Coastguard Worker 72*60517a1eSAndroid Build Coastguard Workerutil = struct( 73*60517a1eSAndroid Build Coastguard Worker create_tests = _create_tests, 74*60517a1eSAndroid Build Coastguard Worker struct_with = _struct_with, 75*60517a1eSAndroid Build Coastguard Worker is_bazel_6_or_higher = _is_bazel_6_or_higher, 76*60517a1eSAndroid Build Coastguard Worker is_windows = _is_windows, 77*60517a1eSAndroid Build Coastguard Worker) 78