aboutsummaryrefslogtreecommitdiff
path: root/go/tools/bazel_testing/def.bzl
blob: d097027b08e52a624196264e8d21280acd6f8d73 (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
# Copyright 2019 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.

load("//go:def.bzl", "go_test")

def go_bazel_test(rule_files = None, **kwargs):
    """go_bazel_test is a wrapper for go_test that simplifies the use of
    //go/tools/bazel_testing. Tests may be written
    that don't explicitly depend on bazel_testing or rules_go files.
    """

    if not rule_files:
        rule_files = [Label("//:all_files")]

    # Add dependency on bazel_testing library.
    kwargs.setdefault("deps", [])

    bazel_testing_library = "@io_bazel_rules_go//go/tools/bazel_testing"
    if bazel_testing_library not in kwargs["deps"]:
        kwargs["deps"].append(bazel_testing_library)

    # Add data dependency on rules_go files. bazel_testing will copy or link
    # these files in an external repo.
    kwargs.setdefault("data", [])
    kwargs["data"] += rule_files

    # Add paths to rules_go files to arguments. bazel_testing will copy or link
    # these files.
    kwargs.setdefault("args", [])
    kwargs["args"] = (["-begin_files"] +
                      ["$(locations {})".format(t) for t in rule_files] +
                      ["-end_files"] +
                      kwargs["args"])

    # Set rundir to the workspace root directory to ensure relative paths
    # are interpreted correctly.
    kwargs.setdefault("rundir", ".")

    # Set tags.
    # local: don't run in sandbox or on remote executor.
    # exclusive: run one test at a time, since they share a Bazel
    #   output directory. If we don't do this, tests must extract the bazel
    #   installation and start with a fresh cache every time, making them
    #   much slower.
    kwargs.setdefault("tags", [])
    if "local" not in kwargs["tags"]:
        kwargs["tags"].append("local")
    if "exclusive" not in kwargs["tags"]:
        kwargs["tags"].append("exclusive")

    go_test(**kwargs)