Repository navigation
Expand file tree
/
Copy pathjava_test_tests.bzl
More file actions
125 lines (107 loc) · 3.86 KB
/
Copy pathjava_test_tests.bzl
File metadata and controls
125 lines (107 loc) · 3.86 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""Tests for the Bazel java_test rule"""
load("@bazel_features//private:util.bzl", _bazel_version_ge = "ge")
load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
load("@rules_testing//lib:truth.bzl", "matching", "subjects")
load("@rules_testing//lib:util.bzl", "util")
load("//java:java_test.bzl", "java_test")
load("//test/java/testutil:helper.bzl", "always_passes")
def _test_deduced_test_class(name):
if not _bazel_version_ge("8.0.0"):
always_passes(name)
return
util.helper_target(
java_test,
name = name + "/foo",
srcs = [name + "/Foo.java"],
)
analysis_test(
name = name,
impl = _test_deduced_test_class_impl,
target = name + "/foo",
)
def _test_deduced_test_class_impl(env, target):
executable = target[DefaultInfo].files_to_run.executable.short_path
assert_action = env.expect.that_target(target).action_generating(executable)
if assert_action.actual.substitutions:
# TemplateExpansion action on linux/mac
assert_jvm_flags = assert_action.substitutions().get(
"%jvm_flags%",
factory = lambda v, meta: subjects.collection([v], meta),
)
else:
# Windows
assert_jvm_flags = assert_action.argv()
assert_jvm_flags.contains_predicate(
matching.str_matches("-Dbazel.test_suite=bazel.rules.test_deduced_test_class.Foo"),
)
# regression test for https://github.com/bazelbuild/bazel/issues/20378
def _test_invalid_test_class_at_repo_root(name):
analysis_test(
name = name,
impl = _test_invalid_test_class_at_repo_root_impl,
target = "//:invalid_test_at_repo_root",
expect_failure = True,
)
def _test_invalid_test_class_at_repo_root_impl(env, target):
env.expect.that_target(target).failures().contains_predicate(
matching.str_matches("cannot determine test class."),
)
def _test_test_classes(name):
if not _bazel_version_ge("8.0.0"):
always_passes(name)
return
util.helper_target(
java_test,
name = name + "/foo",
test_classes = ["com.example.FooTest", "com.example.BarTest"],
)
analysis_test(
name = name,
impl = _test_test_classes_impl,
target = name + "/foo",
)
def _test_test_classes_impl(env, target):
executable = target[DefaultInfo].files_to_run.executable.short_path
assert_action = env.expect.that_target(target).action_generating(executable)
if assert_action.actual.substitutions:
# TemplateExpansion action on linux/mac
assert_jvm_flags = assert_action.substitutions().get(
"%jvm_flags%",
factory = lambda v, meta: subjects.collection([v], meta),
)
else:
# Windows
assert_jvm_flags = assert_action.argv()
assert_jvm_flags.contains_predicate(
matching.str_matches("-Dbazel.test_suite=com.example.FooTest,com.example.BarTest"),
)
def _test_both_test_class_and_test_classes_fails(name):
if not _bazel_version_ge("8.0.0"):
always_passes(name)
return
util.helper_target(
java_test,
name = name + "/foo",
test_class = "FooTest",
test_classes = ["FooTest"],
)
analysis_test(
name = name,
impl = _test_both_test_class_and_test_classes_fails_impl,
target = name + "/foo",
expect_failure = True,
)
def _test_both_test_class_and_test_classes_fails_impl(env, target):
env.expect.that_target(target).failures().contains_predicate(
matching.str_matches("cannot specify both 'test_class' and 'test_classes'"),
)
def java_test_tests(name):
test_suite(
name = name,
tests = [
_test_deduced_test_class,
_test_invalid_test_class_at_repo_root,
_test_test_classes,
_test_both_test_class_and_test_classes_fails,
],
)