forked from bazelbuild/rules_java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava_binary_tests.bzl
More file actions
183 lines (166 loc) · 5.95 KB
/
Copy pathjava_binary_tests.bzl
File metadata and controls
183 lines (166 loc) · 5.95 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"""Tests for the java_binary rule"""
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
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_binary.bzl", "java_binary")
load("//java:java_library.bzl", "java_library")
load("//test/java/testutil:java_info_subject.bzl", "java_info_subject")
load("//test/java/testutil:rules/custom_java_info_rule.bzl", "custom_java_info_rule")
load("//test/java/testutil:rules/forward_java_info.bzl", "java_info_forwarding_rule")
def _test_java_binary_provides_binary_java_info(name):
util.helper_target(java_binary, name = "bin", srcs = ["Main.java"])
analysis_test(
name = name,
impl = _test_java_binary_provides_binary_java_info_impl,
target = Label(":bin"),
attr_values = {"tags": ["min_bazel_7"]},
)
def _test_java_binary_provides_binary_java_info_impl(env, target):
assert_java_info = java_info_subject.from_target(env, target)
assert_java_info.compilation_args().equals(None)
assert_java_info.is_binary().equals(True)
def _test_stamp_conversion_does_not_override_int(name):
util.helper_target(
java_binary,
name = name + "/bin",
srcs = ["Main.java"],
stamp = -1,
)
analysis_test(
name = name,
impl = _test_stamp_conversion_does_not_override_int_impl,
target = name + "/bin",
config_settings = {
"//command_line_option:stamp": False,
},
# deploy jars are in a separate rule in Bazel 7, Bazel 6 generated build-info differently
attr_values = {"tags": ["min_bazel_8"]},
)
def _test_stamp_conversion_does_not_override_int_impl(env, target):
assert_deploy_jar_action = env.expect.that_target(target).action_generating(
"{package}/{name}_deploy.jar",
)
assert_deploy_jar_action.inputs().not_contains_predicate(
matching.file_basename_equals("non_volatile_file.properties"),
)
assert_deploy_jar_action.inputs().contains_predicate(
matching.file_basename_equals("redacted_file.properties"),
)
def _test_java_binary_attributes(name):
util.helper_target(
java_library,
name = name + "/jl_bottom_for_deps",
srcs = ["java/A.java"],
)
util.helper_target(
java_library,
name = name + "/jl_bottom_for_runtime_deps",
srcs = ["java/A2.java"],
)
util.helper_target(
java_info_forwarding_rule,
name = name + "/mya",
dep = name + "/jl_bottom_for_deps",
)
util.helper_target(
java_info_forwarding_rule,
name = name + "/myb",
dep = name + "/jl_bottom_for_runtime_deps",
)
util.helper_target(
java_binary,
name = name + "/binary",
srcs = ["java/B.java"],
main_class = "foo.A",
deps = [name + "/mya"],
runtime_deps = [name + "/myb"],
)
analysis_test(
name = name,
impl = _test_java_binary_attributes_impl,
target = name + "/binary",
)
def _test_java_binary_attributes_impl(env, target):
assert_runtime_classpath = java_info_subject.from_target(env, target).compilation_info().runtime_classpath()
# Test that all bottom jars are on the runtime classpath.
assert_runtime_classpath.contains_at_least_predicates([
matching.file_basename_equals("jl_bottom_for_deps.jar"),
matching.file_basename_equals("jl_bottom_for_runtime_deps.jar"),
])
def _test_java_binary_propagates_direct_native_libraries(name):
util.helper_target(
cc_library,
name = name + "/cclib",
srcs = ["z.cc"],
)
util.helper_target(
cc_binary,
name = name + "/native",
srcs = ["cc/x.cc"],
deps = [name + "/cclib"],
linkshared = 1,
linkstatic = 1,
)
util.helper_target(
java_library,
name = name + "/jl",
srcs = ["java/A.java"],
deps = [name + "/native"],
)
util.helper_target(
cc_binary,
name = name + "/ccl",
srcs = ["cc/x.cc"],
deps = [name + "/cclib"],
linkshared = 1,
linkstatic = 1,
)
util.helper_target(
custom_java_info_rule,
name = name + "/r",
output_jar = name + "-out.jar",
cc_dep = [name + "/ccl"],
dep = [name + "/jl"],
)
util.helper_target(
java_binary,
name = name + "/binary",
srcs = ["java/C.java"],
deps = [name + "/r"],
main_class = "C",
)
analysis_test(
name = name,
impl = _test_java_binary_propagates_direct_native_libraries_impl,
target = name + "/binary",
# in Bazel 6, the windows stub was created by a bespoke, native and
# opaque-to-Starlark LauncherFileWriteAction
attr_values = {"tags": ["min_bazel_7"]},
)
def _test_java_binary_propagates_direct_native_libraries_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("-Djava.library.path=${JAVA_RUNFILES}/*/test_java_binary_propagates_direct_native_libraries"),
)
def java_binary_tests(name):
test_suite(
name = name,
tests = [
_test_java_binary_provides_binary_java_info,
_test_stamp_conversion_does_not_override_int,
_test_java_binary_attributes,
_test_java_binary_propagates_direct_native_libraries,
],
)