xref: /aosp_15_r20/external/bazelbuild-rules_go/go/tools/bazel_testing/def.bzl (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
1# Copyright 2019 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
15load("//go:def.bzl", "go_test")
16
17def go_bazel_test(rule_files = None, **kwargs):
18    """go_bazel_test is a wrapper for go_test that simplifies the use of
19    //go/tools/bazel_testing. Tests may be written
20    that don't explicitly depend on bazel_testing or rules_go files.
21    """
22
23    if not rule_files:
24        rule_files = [Label("//:all_files")]
25
26    # Add dependency on bazel_testing library.
27    kwargs.setdefault("deps", [])
28
29    bazel_testing_library = "@io_bazel_rules_go//go/tools/bazel_testing"
30    if bazel_testing_library not in kwargs["deps"]:
31        kwargs["deps"].append(bazel_testing_library)
32
33    # Add data dependency on rules_go files. bazel_testing will copy or link
34    # these files in an external repo.
35    kwargs.setdefault("data", [])
36    kwargs["data"] += rule_files
37
38    # Add paths to rules_go files to arguments. bazel_testing will copy or link
39    # these files.
40    kwargs.setdefault("args", [])
41    kwargs["args"] = (["-begin_files"] +
42                      ["$(locations {})".format(t) for t in rule_files] +
43                      ["-end_files"] +
44                      kwargs["args"])
45
46    # Set rundir to the workspace root directory to ensure relative paths
47    # are interpreted correctly.
48    kwargs.setdefault("rundir", ".")
49
50    # Set tags.
51    # local: don't run in sandbox or on remote executor.
52    # exclusive: run one test at a time, since they share a Bazel
53    #   output directory. If we don't do this, tests must extract the bazel
54    #   installation and start with a fresh cache every time, making them
55    #   much slower.
56    kwargs.setdefault("tags", [])
57    if "local" not in kwargs["tags"]:
58        kwargs["tags"].append("local")
59    if "exclusive" not in kwargs["tags"]:
60        kwargs["tags"].append("exclusive")
61
62    go_test(**kwargs)
63