xref: /aosp_15_r20/external/bazelbuild-rules_python/gazelle/python/gazelle_test.bzl (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
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
15*60517a1eSAndroid Build Coastguard Worker""
16*60517a1eSAndroid Build Coastguard Worker
17*60517a1eSAndroid Build Coastguard Workerload("@io_bazel_rules_go//go:def.bzl", "go_test")
18*60517a1eSAndroid Build Coastguard Worker
19*60517a1eSAndroid Build Coastguard Workerdef gazelle_test(*, name, test_dirs, **kwargs):
20*60517a1eSAndroid Build Coastguard Worker    """A simple macro to better cache gazelle integration tests
21*60517a1eSAndroid Build Coastguard Worker
22*60517a1eSAndroid Build Coastguard Worker    Args:
23*60517a1eSAndroid Build Coastguard Worker        name (str): The name of the test suite target to be created and
24*60517a1eSAndroid Build Coastguard Worker            the prefix to all of the individual test targets.
25*60517a1eSAndroid Build Coastguard Worker        test_dirs (list[str]): The list of dirs in the 'testdata'
26*60517a1eSAndroid Build Coastguard Worker            directory that we should create separate 'go_test' cases for.
27*60517a1eSAndroid Build Coastguard Worker            Each of them will be prefixed with '{name}'.
28*60517a1eSAndroid Build Coastguard Worker        **kwargs: extra arguments passed to 'go_test'.
29*60517a1eSAndroid Build Coastguard Worker    """
30*60517a1eSAndroid Build Coastguard Worker    tests = []
31*60517a1eSAndroid Build Coastguard Worker
32*60517a1eSAndroid Build Coastguard Worker    data = kwargs.pop("data", [])
33*60517a1eSAndroid Build Coastguard Worker
34*60517a1eSAndroid Build Coastguard Worker    for dir in test_dirs:
35*60517a1eSAndroid Build Coastguard Worker        _, _, basename = dir.rpartition("/")
36*60517a1eSAndroid Build Coastguard Worker
37*60517a1eSAndroid Build Coastguard Worker        test = "{}_{}".format(name, basename)
38*60517a1eSAndroid Build Coastguard Worker        tests.append(test)
39*60517a1eSAndroid Build Coastguard Worker
40*60517a1eSAndroid Build Coastguard Worker        go_test(
41*60517a1eSAndroid Build Coastguard Worker            name = test,
42*60517a1eSAndroid Build Coastguard Worker            data = native.glob(["{}/**".format(dir)]) + data,
43*60517a1eSAndroid Build Coastguard Worker            **kwargs
44*60517a1eSAndroid Build Coastguard Worker        )
45*60517a1eSAndroid Build Coastguard Worker
46*60517a1eSAndroid Build Coastguard Worker    native.test_suite(
47*60517a1eSAndroid Build Coastguard Worker        name = name,
48*60517a1eSAndroid Build Coastguard Worker        tests = tests,
49*60517a1eSAndroid Build Coastguard Worker    )
50