Skip to content

Commit 4aa6673

Browse files
hvadehrarules_java Copybara
authored and
rules_java Copybara
committedJan 30, 2025·
Improve android_support_tests.bzl
Adds a custom provider subject for `JavaInfo` and the test now reads better and reports more helpful failure messages. This will also ease migrating more java rules tests out of the Bazel codebase. PiperOrigin-RevId: 721338635 Change-Id: I36269cf04de67698bbf3be3bec9507d12a55d895
1 parent c69bae2 commit 4aa6673

File tree

6 files changed

+95
-32
lines changed

6 files changed

+95
-32
lines changed
 

‎java/test/private/BUILD

+1-19
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,6 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
15-
load("//java:defs.bzl", "java_library", "java_plugin")
16-
load(":android_support_tests.bzl", "android_support_tests", "my_rule")
17-
18-
java_plugin(
19-
name = "my_plugin",
20-
srcs = ["MyPlugin.java"],
21-
)
22-
23-
java_library(
24-
name = "foo",
25-
srcs = ["Foo.java"],
26-
exported_plugins = [":my_plugin"],
27-
)
28-
29-
my_rule(
30-
name = "bar",
31-
dep = ":foo",
32-
)
14+
load(":android_support_tests.bzl", "android_support_tests")
3315

3416
android_support_tests(name = "android_support_tests")

‎java/test/private/Foo.java

-1
This file was deleted.

‎java/test/private/MyPlugin.java

-1
This file was deleted.

‎java/test/private/android_support_tests.bzl

+27-11
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
"""Tests for //java/private:android_support.bzl"""
1515

1616
load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
17+
load("@rules_testing//lib:util.bzl", "util")
18+
load("//java:defs.bzl", "java_library", "java_plugin")
1719
load("//java/common:java_info.bzl", "JavaInfo")
1820
load("//java/private:android_support.bzl", "android_support")
21+
load("//java/test/testutil:java_info_subject.bzl", "java_info_subject")
1922

2023
def _impl(ctx):
2124
return [
@@ -30,25 +33,38 @@ my_rule = rule(
3033
)
3134

3235
def _test_enable_implicit_sourceless_deps_exports_compatibility(name):
36+
util.helper_target(
37+
java_plugin,
38+
name = "my_plugin",
39+
srcs = ["MyPlugin.java"],
40+
)
41+
util.helper_target(
42+
java_library,
43+
name = "base",
44+
srcs = ["Foo.java"],
45+
exported_plugins = [":my_plugin"],
46+
)
47+
util.helper_target(
48+
my_rule,
49+
name = "transformed",
50+
dep = ":base",
51+
)
52+
3353
analysis_test(
3454
name = name,
3555
impl = _test_enable_implicit_sourceless_deps_exports_compatibility_impl,
3656
targets = {
37-
"foo": Label(":foo"),
38-
"bar": Label(":bar"),
57+
"base": Label(":base"),
58+
"transformed": Label(":transformed"),
3959
},
4060
)
4161

4262
def _test_enable_implicit_sourceless_deps_exports_compatibility_impl(env, targets):
43-
# TODO(hvd): write a ProviderSubject for JavaInfo
44-
foo_javainfo = targets.foo[JavaInfo]
45-
bar_javainfo = targets.bar[JavaInfo]
46-
for attr in ["transitive_runtime_jars", "compile_jars", "transitive_compile_time_jars", "full_compile_jars", "_transitive_full_compile_time_jars", "_compile_time_java_dependencies"]:
47-
env.expect.that_bool(getattr(foo_javainfo, attr) == getattr(bar_javainfo, attr)).equals(True)
48-
env.expect.that_depset_of_files(foo_javainfo.plugins.processor_jars).contains_exactly([
49-
"java/test/private/libmy_plugin.jar",
50-
])
51-
env.expect.that_depset_of_files(bar_javainfo.plugins.processor_jars).contains_exactly([])
63+
base_info = java_info_subject.from_target(env, targets.base)
64+
transformed_info = java_info_subject.from_target(env, targets.transformed)
65+
transformed_info.compilation_args().equals(base_info.compilation_args())
66+
base_info.plugins().processor_jars().contains_exactly(["{package}/libmy_plugin.jar"])
67+
transformed_info.plugins().processor_jars().contains_exactly([])
5268

5369
def android_support_tests(name):
5470
test_suite(

‎java/test/testutil/BUILD

Whitespace-only changes.
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""A custom @rules_testing subject for the JavaInfo provider"""
2+
3+
load("@rules_testing//lib:truth.bzl", "subjects", "truth")
4+
load("//java/common:java_info.bzl", "JavaInfo")
5+
6+
def _new_java_info_subject(java_info, meta):
7+
self = struct(actual = java_info, meta = meta.derive("JavaInfo"))
8+
public = struct(
9+
compilation_args = lambda: _new_java_compilation_args_subject(self.actual, self.meta),
10+
plugins = lambda: _new_java_info_plugins_subject(self.actual, self.meta),
11+
)
12+
return public
13+
14+
def _java_info_subject_from_target(env, target):
15+
return _new_java_info_subject(target[JavaInfo], meta = truth.expect(env).meta.derive(
16+
format_str_kwargs = {
17+
"name": target.label.name,
18+
"package": target.label.package,
19+
},
20+
))
21+
22+
def _new_java_compilation_args_subject(java_info, meta):
23+
actual = struct(
24+
transitive_runtime_jars = java_info.transitive_runtime_jars,
25+
compile_jars = java_info.compile_jars,
26+
transitive_compile_time_jars = java_info.transitive_compile_time_jars,
27+
full_compile_jars = java_info.full_compile_jars,
28+
_transitive_full_compile_time_jars = java_info._transitive_full_compile_time_jars,
29+
_compile_time_java_dependencies = java_info._compile_time_java_dependencies,
30+
_is_binary = getattr(java_info, "_is_binary", False),
31+
)
32+
self = struct(
33+
actual = actual,
34+
meta = meta,
35+
)
36+
return struct(
37+
equals = lambda other: _java_compilation_args_equals(self, other),
38+
self = self,
39+
actual = actual,
40+
)
41+
42+
def _java_compilation_args_equals(self, other):
43+
if self.actual == other.actual:
44+
return
45+
for attr in dir(other.actual):
46+
other_attr = getattr(other.actual, attr)
47+
this_attr = getattr(self.actual, attr)
48+
if this_attr != other_attr:
49+
self.meta.derive(attr).add_failure(
50+
"expected: {}".format(other_attr),
51+
"actual: {}".format(this_attr),
52+
)
53+
54+
def _new_java_info_plugins_subject(java_info, meta):
55+
self = struct(
56+
actual = java_info.plugins,
57+
meta = meta.derive("plugins"),
58+
)
59+
public = struct(
60+
processor_jars = lambda: subjects.depset_file(self.actual.processor_jars, meta = self.meta.derive("processor_jars")),
61+
)
62+
return public
63+
64+
java_info_subject = struct(
65+
new = _new_java_info_subject,
66+
from_target = _java_info_subject_from_target,
67+
)

0 commit comments

Comments
 (0)
Please sign in to comment.