-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
test_defs.bzl
100 lines (86 loc) · 3.32 KB
/
test_defs.bzl
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Copyright (C) 2022 Google Inc.
#
# 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.
"""starlark marcors to generate test suites."""
load("@rules_java//java:defs.bzl", "java_test")
_TEMPLATE = """package {VAR_PACKAGE};
import org.junit.runners.Suite;
import org.junit.runner.RunWith;
@RunWith(Suite.class)
@Suite.SuiteClasses({{{VAR_CLASSES}}})
public class {VAR_NAME} {{}}
"""
def _impl(ctx):
classes = ",".join(sorted(ctx.attr.test_classes))
ctx.actions.write(
output = ctx.outputs.out,
content = _TEMPLATE.format(
VAR_PACKAGE = ctx.attr.package_name,
VAR_CLASSES = classes,
VAR_NAME = ctx.attr.name,
),
)
_gen_suite = rule(
attrs = {
"test_classes": attr.string_list(),
"package_name": attr.string(),
},
outputs = {"out": "%{name}.java"},
implementation = _impl,
)
def guice_test_suites(name, deps, srcs = None, args = [], suffix = "", sizes = None, jvm_flags = []):
"""
Generates tests for test file in srcs ending in "Test.java"
Args:
name: name of the test suite to generate
srcs: list of test source files, uses 'glob(["**/*Test.java"])' if not specified
deps: list of runtime dependencies requried to run the test
args: list of flags to pass to the test
jvm_flags: list of JVM flags to pass to the test
suffix: suffix to apend to the generated test name
sizes: not used, exists only so that the opensource guice_test_suites mirror exactly the internal one
"""
flags = []
flags.extend(jvm_flags)
# transform flags to JVM options used externally
for arg in args:
if arg.startswith("--"):
flags.append(arg.replace("--", "-D"))
else:
flags.append(arg)
package_name = native.package_name()
# strip the path prefix from package name so that we get the correct test class name
# "core/test/com/google/inject" becomes "com/google/inject"
# "extensions/service/test/com/google/inject/service" becomes "com/google/inject/service"
if package_name.startswith("core/test/") or package_name.startswith("extensions/"):
package_name = package_name.rpartition("/test/")[2]
test_files = srcs or native.glob(["**/*Test.java"])
test_classes = []
for src in test_files:
test_name = src.replace(".java", "")
test_classes.append((package_name + "/" + test_name + ".class").replace("/", "."))
suite_name = name + suffix
_gen_suite(
name = suite_name,
test_classes = test_classes,
package_name = package_name.replace("/", "."),
)
java_test(
name = "AllTestsSuite" + suffix,
test_class = (package_name + "/" + suite_name).replace("/", "."),
jvm_flags = flags,
srcs = [":" + suite_name],
deps = deps + [
"//third_party/java/junit",
],
)