Skip to content

Commit 3bed9dc

Browse files
hvadehrarules_java Copybara
authored andcommitted
Add a test for extending compile time jdeps
PiperOrigin-RevId: 729517655 Change-Id: I6eb39583a60cd964621b2499925e4d2bcca5c158
1 parent 5d4442e commit 3bed9dc

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

java/test/common/java_common_tests.bzl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
44
load("@rules_testing//lib:util.bzl", "util")
55
load("//java:java_library.bzl", "java_library")
66
load("//java/common:java_common.bzl", "java_common")
7+
load("//java/common:java_info.bzl", "JavaInfo")
78
load("//java/common:java_plugin_info.bzl", "JavaPluginInfo")
89
load("//java/test/testutil:java_info_subject.bzl", "java_info_subject")
910
load("//java/test/testutil:rules/custom_library.bzl", "custom_library")
11+
load("//java/test/testutil:rules/custom_library_extended_compile_jdeps.bzl", "CompileJdepsInfo", "custom_library_extended_jdeps")
1012
load("//java/test/testutil:rules/custom_library_with_exports.bzl", "custom_library_with_exports")
1113
load("//java/test/testutil:rules/custom_library_with_sourcepaths.bzl", "custom_library_with_sourcepaths")
1214

@@ -74,6 +76,32 @@ def _test_java_plugin_info_impl(env, _target):
7476
"java_common.JavaPluginInfo == JavaPluginInfo",
7577
).equals(True)
7678

79+
# Tests that extended 'compile time jdeps' are consistently updated.
80+
def _test_compile_extend_compile_time_jdeps(name):
81+
util.helper_target(
82+
custom_library_extended_jdeps,
83+
name = name + "/foo",
84+
srcs = ["Foo.java"],
85+
extra_jdeps = "Foo.jdeps",
86+
)
87+
88+
analysis_test(
89+
name = name,
90+
impl = _test_compile_extend_compile_time_jdeps_impl,
91+
target = name + "/foo",
92+
attr_values = {"tags": ["min_bazel_7"]},
93+
)
94+
95+
def _test_compile_extend_compile_time_jdeps_impl(env, target):
96+
before = target[CompileJdepsInfo].before.to_list()
97+
assert_that_before = env.expect.that_collection(before)
98+
assert_that_after = env.expect.that_collection(target[CompileJdepsInfo].after.to_list())
99+
100+
assert_that_before.has_size(1)
101+
assert_that_after.has_size(2)
102+
assert_that_after.contains_at_least(before)
103+
assert_that_after.contains_exactly(target[JavaInfo]._compile_time_java_dependencies)
104+
77105
def java_common_tests(name):
78106
test_suite(
79107
name = name,
@@ -82,5 +110,6 @@ def java_common_tests(name):
82110
_test_compile_sourcepath,
83111
_test_compile_exports_no_sources,
84112
_test_java_plugin_info,
113+
_test_compile_extend_compile_time_jdeps,
85114
],
86115
)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""Helper rule to test extending compile time jdeps"""
2+
3+
load("//java/common:java_common.bzl", "java_common")
4+
load("//java/common:java_info.bzl", "JavaInfo")
5+
load("//java/common:java_semantics.bzl", "semantics")
6+
7+
CompileJdepsInfo = provider("Provider to testing compile jdeps", fields = ["before", "after"])
8+
9+
def _compile_time_jdeps(info):
10+
return depset([outputs.compile_jdeps for outputs in info.java_outputs if outputs.compile_jdeps != None])
11+
12+
def _impl(ctx):
13+
output_jar = ctx.actions.declare_file("lib" + ctx.label.name + ".jar")
14+
info = java_common.compile(
15+
ctx,
16+
source_files = ctx.files.srcs,
17+
output = output_jar,
18+
java_toolchain = semantics.find_java_toolchain(ctx),
19+
)
20+
jdeps_info = JavaInfo(
21+
output_jar = output_jar,
22+
compile_jar = None,
23+
compile_jdeps = ctx.file.extra_jdeps,
24+
)
25+
extra_info = java_common.merge([info, jdeps_info])
26+
return [
27+
extra_info,
28+
CompileJdepsInfo(
29+
before = _compile_time_jdeps(info),
30+
after = _compile_time_jdeps(extra_info),
31+
),
32+
]
33+
34+
custom_library_extended_jdeps = rule(
35+
implementation = _impl,
36+
outputs = {
37+
"my_output": "lib%{name}.jar",
38+
},
39+
attrs = {
40+
"srcs": attr.label_list(allow_files = [".java"]),
41+
"extra_jdeps": attr.label(allow_single_file = True),
42+
},
43+
toolchains = [semantics.JAVA_TOOLCHAIN_TYPE],
44+
fragments = ["java"],
45+
)

0 commit comments

Comments
 (0)