Skip to content

Commit c1a5885

Browse files
authored
test(gazelle): have a go_test target for each gazelle test (#1537)
This provides better caching and it allows us to have better developer velocity whilst iterating on a single test case and it also solves some of the `timeout` errors I was seeing locally because now each `gazelle` invocation is run at a lower parallelism that `bazel` decides itself.
1 parent 07d530b commit c1a5885

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

gazelle/python/BUILD.bazel

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
load("@bazel_gazelle//:def.bzl", "gazelle_binary")
2-
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
2+
load("@io_bazel_rules_go//go:def.bzl", "go_library")
33
load("@rules_python//python:defs.bzl", "py_binary")
4+
load(":gazelle_test.bzl", "gazelle_test")
45

56
go_library(
67
name = "python",
@@ -53,13 +54,21 @@ filegroup(
5354
output_group = "python_zip_file",
5455
)
5556

56-
go_test(
57+
gazelle_test(
5758
name = "python_test",
5859
srcs = ["python_test.go"],
5960
data = [
6061
":gazelle_binary",
6162
":helper",
62-
] + glob(["testdata/**"]),
63+
],
64+
test_dirs = glob(
65+
# Use this so that we don't need to manually maintain the list.
66+
["testdata/*"],
67+
exclude = ["testdata/*.md"],
68+
# The directories aren't inputs themselves; we just want their
69+
# names.
70+
exclude_directories = 0,
71+
),
6372
deps = [
6473
"@bazel_gazelle//testtools:go_default_library",
6574
"@com_github_ghodss_yaml//:yaml",

gazelle/python/gazelle_test.bzl

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2023 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+
15+
""
16+
17+
load("@io_bazel_rules_go//go:def.bzl", "go_test")
18+
19+
def gazelle_test(*, name, test_dirs, **kwargs):
20+
"""A simple macro to better cache gazelle integration tests
21+
22+
Args:
23+
name (str): The name of the test suite target to be created and
24+
the prefix to all of the individual test targets.
25+
test_dirs (list[str]): The list of dirs in the 'testdata'
26+
directory that we should create separate 'go_test' cases for.
27+
Each of them will be prefixed with '{name}'.
28+
**kwargs: extra arguments passed to 'go_test'.
29+
"""
30+
tests = []
31+
32+
data = kwargs.pop("data", [])
33+
34+
for dir in test_dirs:
35+
_, _, basename = dir.rpartition("/")
36+
37+
test = "{}_{}".format(name, basename)
38+
tests.append(test)
39+
40+
go_test(
41+
name = test,
42+
data = native.glob(["{}/**".format(dir)]) + data,
43+
**kwargs
44+
)
45+
46+
native.test_suite(
47+
name = name,
48+
tests = tests,
49+
)

0 commit comments

Comments
 (0)