|
1 | 1 | """Tests for the java_plugin rule""" |
2 | 2 |
|
3 | 3 | load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite") |
| 4 | +load("@rules_testing//lib:truth.bzl", "subjects") |
4 | 5 | load("@rules_testing//lib:util.bzl", "util") |
5 | 6 | load("//java:java_library.bzl", "java_library") |
6 | 7 | load("//java:java_plugin.bzl", "java_plugin") |
7 | 8 | load("//java/common:java_plugin_info.bzl", "JavaPluginInfo") |
| 9 | +load("//java/common:proguard_spec_info.bzl", "ProguardSpecInfo") |
8 | 10 | load("//test/java/testutil:java_info_subject.bzl", "java_plugin_info_subject") |
9 | 11 |
|
10 | 12 | def _test_exposes_plugins_to_starlark(name): |
@@ -75,11 +77,313 @@ def _test_exposes_api_generating_plugins_to_starlark_impl(env, target): |
75 | 77 | assert_api_plugin_data.processor_data().contains_exactly(["{package}/pluginfile.dat"]) |
76 | 78 | assert_api_plugin_data.equals(target[JavaPluginInfo].plugins) |
77 | 79 |
|
| 80 | +def _test_not_empty_processor_class(name): |
| 81 | + util.helper_target( |
| 82 | + java_library, |
| 83 | + name = name + "/deps", |
| 84 | + srcs = ["Deps.java"], |
| 85 | + ) |
| 86 | + util.helper_target( |
| 87 | + java_plugin, |
| 88 | + name = name + "/processor", |
| 89 | + srcs = ["Processor.java"], |
| 90 | + processor_class = "com.google.test.Processor", |
| 91 | + deps = [name + "/deps"], |
| 92 | + ) |
| 93 | + |
| 94 | + analysis_test( |
| 95 | + name = name, |
| 96 | + impl = _test_not_empty_processor_class_impl, |
| 97 | + target = name + "/processor", |
| 98 | + ) |
| 99 | + |
| 100 | +def _test_not_empty_processor_class_impl(env, target): |
| 101 | + plugin_info = java_plugin_info_subject.from_target(env, target) |
| 102 | + plugin_info.plugins().processor_classes().contains_exactly(["com.google.test.Processor"]) |
| 103 | + |
| 104 | + plugin_info.plugins().processor_jars().contains_exactly([ |
| 105 | + "{package}/lib{name}.jar", |
| 106 | + "{package}/lib{test_name}/deps.jar", |
| 107 | + ]) |
| 108 | + |
| 109 | +def _test_empty_processor_class(name): |
| 110 | + util.helper_target( |
| 111 | + java_library, |
| 112 | + name = name + "/deps", |
| 113 | + srcs = ["Deps.java"], |
| 114 | + ) |
| 115 | + util.helper_target( |
| 116 | + java_plugin, |
| 117 | + name = name + "/bugchecker", |
| 118 | + srcs = ["BugChecker.java"], |
| 119 | + deps = [":" + name + "/deps"], |
| 120 | + ) |
| 121 | + |
| 122 | + analysis_test( |
| 123 | + name = name, |
| 124 | + impl = _test_empty_processor_class_impl, |
| 125 | + target = name + "/bugchecker", |
| 126 | + ) |
| 127 | + |
| 128 | +def _test_empty_processor_class_impl(env, target): |
| 129 | + plugin_info = java_plugin_info_subject.from_target(env, target) |
| 130 | + plugin_info.plugins().processor_classes().contains_exactly([]) |
| 131 | + plugin_info.plugins().processor_jars().contains_exactly([ |
| 132 | + "{package}/lib{name}.jar", |
| 133 | + "{package}/lib{test_name}/deps.jar", |
| 134 | + ]) |
| 135 | + |
| 136 | +def _test_empty_processor_class_target(name): |
| 137 | + util.helper_target( |
| 138 | + java_library, |
| 139 | + name = name + "/deps", |
| 140 | + srcs = ["Deps.java"], |
| 141 | + ) |
| 142 | + util.helper_target( |
| 143 | + java_plugin, |
| 144 | + name = name + "/bugchecker", |
| 145 | + srcs = ["BugChecker.java"], |
| 146 | + deps = [":" + name + "/deps"], |
| 147 | + ) |
| 148 | + util.helper_target( |
| 149 | + java_library, |
| 150 | + name = name + "/empty", |
| 151 | + plugins = [":" + name + "/bugchecker"], |
| 152 | + ) |
| 153 | + |
| 154 | + analysis_test( |
| 155 | + name = name, |
| 156 | + impl = _test_empty_processor_class_target_impl, |
| 157 | + target = name + "/empty", |
| 158 | + ) |
| 159 | + |
| 160 | +def _test_empty_processor_class_target_impl(env, target): |
| 161 | + env.expect.that_target(target).action_generating("{package}/lib{name}.jar").inputs().contains_at_least([ |
| 162 | + "{package}/lib{test_name}/bugchecker.jar", |
| 163 | + "{package}/lib{test_name}/deps.jar", |
| 164 | + ]) |
| 165 | + |
| 166 | +def _new_proguard_info_subject(info, meta): |
| 167 | + return struct( |
| 168 | + specs = lambda: subjects.depset_file(info.specs, meta.derive("specs")), |
| 169 | + ) |
| 170 | + |
| 171 | +def _test_java_plugin_exports_transitive_proguard_specs(name): |
| 172 | + util.helper_target( |
| 173 | + java_plugin, |
| 174 | + name = name + "/plugin", |
| 175 | + srcs = ["Plugin.java"], |
| 176 | + proguard_specs = ["plugin.pro"], |
| 177 | + ) |
| 178 | + util.helper_target( |
| 179 | + java_library, |
| 180 | + name = name + "/dep", |
| 181 | + srcs = ["Dep.java"], |
| 182 | + proguard_specs = ["dep.pro"], |
| 183 | + ) |
| 184 | + util.helper_target( |
| 185 | + java_plugin, |
| 186 | + name = name + "/top", |
| 187 | + srcs = ["Top.java"], |
| 188 | + plugins = [":" + name + "/plugin"], |
| 189 | + proguard_specs = ["top.pro"], |
| 190 | + deps = [":" + name + "/dep"], |
| 191 | + ) |
| 192 | + |
| 193 | + analysis_test( |
| 194 | + name = name, |
| 195 | + impl = _test_java_plugin_exports_transitive_proguard_specs_impl, |
| 196 | + target = name + "/top", |
| 197 | + # Before Bazel 8, native rules use the native ProguardSpecProvider |
| 198 | + attr_values = {"tags": ["min_bazel_8"]}, |
| 199 | + provider_subject_factories = [struct( |
| 200 | + type = ProguardSpecInfo, |
| 201 | + name = "ProguardInfo", |
| 202 | + factory = _new_proguard_info_subject, |
| 203 | + )], |
| 204 | + ) |
| 205 | + |
| 206 | +def _test_java_plugin_exports_transitive_proguard_specs_impl(env, target): |
| 207 | + env.expect.that_target(target).provider(ProguardSpecInfo).specs().contains_exactly( |
| 208 | + [ |
| 209 | + "{package}/validated_proguard/{test_name}/top/{package}/top.pro_valid", |
| 210 | + "{package}/validated_proguard/{test_name}/dep/{package}/dep.pro_valid", |
| 211 | + ], |
| 212 | + ) |
| 213 | + |
| 214 | +def _test_java_plugin_validates_proguard_specs(name): |
| 215 | + util.helper_target( |
| 216 | + java_plugin, |
| 217 | + name = name + "/plugin", |
| 218 | + srcs = ["Plugin.java"], |
| 219 | + proguard_specs = ["plugin.pro"], |
| 220 | + ) |
| 221 | + |
| 222 | + analysis_test( |
| 223 | + name = name, |
| 224 | + impl = _test_java_plugin_validates_proguard_specs_impl, |
| 225 | + target = name + "/plugin", |
| 226 | + ) |
| 227 | + |
| 228 | +def _test_java_plugin_validates_proguard_specs_impl(env, target): |
| 229 | + output_file = None |
| 230 | + for f in target.output_groups["_hidden_top_level_INTERNAL_"].to_list(): |
| 231 | + if f.basename == "plugin.pro_valid": |
| 232 | + output_file = f |
| 233 | + break |
| 234 | + env.expect.that_target(target).action_generating( |
| 235 | + output_file.short_path, |
| 236 | + ).inputs().contains_at_least( |
| 237 | + ["{package}/plugin.pro"], |
| 238 | + ) |
| 239 | + |
| 240 | +def _test_java_plugin_validates_transitive_proguard_specs(name): |
| 241 | + util.helper_target( |
| 242 | + java_library, |
| 243 | + name = name + "/transitive", |
| 244 | + srcs = ["Transitive.java"], |
| 245 | + proguard_specs = ["transitive.pro"], |
| 246 | + ) |
| 247 | + util.helper_target( |
| 248 | + java_plugin, |
| 249 | + name = name + "/plugin", |
| 250 | + srcs = ["Plugin.java"], |
| 251 | + deps = [":" + name + "/transitive"], |
| 252 | + ) |
| 253 | + |
| 254 | + analysis_test( |
| 255 | + name = name, |
| 256 | + impl = _test_java_plugin_validates_transitive_proguard_specs_impl, |
| 257 | + targets = { |
| 258 | + "transitive": name + "/transitive", |
| 259 | + "plugin": name + "/plugin", |
| 260 | + }, |
| 261 | + ) |
| 262 | + |
| 263 | +def _test_java_plugin_validates_transitive_proguard_specs_impl(env, targets): |
| 264 | + output_file = None |
| 265 | + for f in targets.plugin.output_groups["_hidden_top_level_INTERNAL_"].to_list(): |
| 266 | + if f.basename == "transitive.pro_valid": |
| 267 | + output_file = f |
| 268 | + break |
| 269 | + |
| 270 | + env.expect.that_target(targets.transitive).action_generating( |
| 271 | + output_file.short_path, |
| 272 | + ).inputs().contains_at_least(["{package}/transitive.pro"]) |
| 273 | + |
| 274 | +def _test_generates_api(name): |
| 275 | + util.helper_target( |
| 276 | + java_plugin, |
| 277 | + name = name + "/api_generating", |
| 278 | + srcs = ["ApiGeneratingPlugin.java"], |
| 279 | + generates_api = True, |
| 280 | + processor_class = "ApiGeneratingPlugin", |
| 281 | + ) |
| 282 | + |
| 283 | + analysis_test( |
| 284 | + name = name, |
| 285 | + impl = _test_generates_api_impl, |
| 286 | + target = name + "/api_generating", |
| 287 | + ) |
| 288 | + |
| 289 | +def _test_generates_api_impl(env, target): |
| 290 | + plugin_info = java_plugin_info_subject.from_target(env, target) |
| 291 | + plugin_info.plugins().processor_classes().contains_exactly(["ApiGeneratingPlugin"]) |
| 292 | + plugin_info.api_generating_plugins().processor_classes().contains_exactly(["ApiGeneratingPlugin"]) |
| 293 | + plugin_info.plugins().processor_jars().contains_exactly([ |
| 294 | + "{package}/lib{name}.jar", |
| 295 | + ]) |
| 296 | + plugin_info.api_generating_plugins().processor_jars().contains_exactly([ |
| 297 | + "{package}/lib{name}.jar", |
| 298 | + ]) |
| 299 | + |
| 300 | +def _test_generates_implementation(name): |
| 301 | + util.helper_target( |
| 302 | + java_plugin, |
| 303 | + name = name + "/impl_generating", |
| 304 | + srcs = ["ImplGeneratingPlugin.java"], |
| 305 | + generates_api = False, |
| 306 | + processor_class = "ImplGeneratingPlugin", |
| 307 | + ) |
| 308 | + |
| 309 | + analysis_test( |
| 310 | + name = name, |
| 311 | + impl = _test_generates_implementation_impl, |
| 312 | + target = name + "/impl_generating", |
| 313 | + ) |
| 314 | + |
| 315 | +def _test_generates_implementation_impl(env, target): |
| 316 | + plugin_info = java_plugin_info_subject.from_target(env, target) |
| 317 | + plugin_info.plugins().processor_classes().contains_exactly(["ImplGeneratingPlugin"]) |
| 318 | + plugin_info.api_generating_plugins().processor_classes().contains_exactly([]) |
| 319 | + plugin_info.plugins().processor_jars().contains_exactly([ |
| 320 | + "{package}/lib{test_name}/impl_generating.jar", |
| 321 | + ]) |
| 322 | + plugin_info.api_generating_plugins().processor_jars().contains_exactly([]) |
| 323 | + |
| 324 | +def _test_plugin_data_in_provider(name): |
| 325 | + util.helper_target( |
| 326 | + java_plugin, |
| 327 | + name = name + "/impl_generating", |
| 328 | + srcs = ["ImplGeneratingPlugin.java"], |
| 329 | + data = ["data.txt"], |
| 330 | + generates_api = False, |
| 331 | + processor_class = "ImplGeneratingPlugin", |
| 332 | + ) |
| 333 | + |
| 334 | + analysis_test( |
| 335 | + name = name, |
| 336 | + impl = _test_plugin_data_in_provider_impl, |
| 337 | + target = name + "/impl_generating", |
| 338 | + ) |
| 339 | + |
| 340 | +def _test_plugin_data_in_provider_impl(env, target): |
| 341 | + plugin_info = java_plugin_info_subject.from_target(env, target) |
| 342 | + plugin_info.plugins().processor_data().contains_exactly([ |
| 343 | + "{package}/data.txt", |
| 344 | + ]) |
| 345 | + |
| 346 | +def _test_plugin_data_in_action_inputs(name): |
| 347 | + util.helper_target( |
| 348 | + java_plugin, |
| 349 | + name = name + "/impl_generating_lib", |
| 350 | + srcs = ["ImplGeneratingPlugin.java"], |
| 351 | + data = ["data.txt"], |
| 352 | + generates_api = False, |
| 353 | + processor_class = "ImplGeneratingPlugin", |
| 354 | + ) |
| 355 | + util.helper_target( |
| 356 | + java_library, |
| 357 | + name = name + "/lib", |
| 358 | + plugins = [":" + name + "/impl_generating_lib"], |
| 359 | + ) |
| 360 | + |
| 361 | + analysis_test( |
| 362 | + name = name, |
| 363 | + impl = _test_plugin_data_in_action_inputs_impl, |
| 364 | + target = name + "/lib", |
| 365 | + ) |
| 366 | + |
| 367 | +def _test_plugin_data_in_action_inputs_impl(env, target): |
| 368 | + env.expect.that_target(target).action_generating("{package}/lib{name}.jar").inputs().contains_at_least([ |
| 369 | + "{package}/data.txt", |
| 370 | + ]) |
| 371 | + |
78 | 372 | def java_plugin_tests(name): |
79 | 373 | test_suite( |
80 | 374 | name = name, |
81 | 375 | tests = [ |
82 | 376 | _test_exposes_plugins_to_starlark, |
83 | 377 | _test_exposes_api_generating_plugins_to_starlark, |
| 378 | + _test_not_empty_processor_class, |
| 379 | + _test_empty_processor_class, |
| 380 | + _test_empty_processor_class_target, |
| 381 | + _test_generates_api, |
| 382 | + _test_plugin_data_in_provider, |
| 383 | + _test_plugin_data_in_action_inputs, |
| 384 | + _test_java_plugin_exports_transitive_proguard_specs, |
| 385 | + _test_java_plugin_validates_proguard_specs, |
| 386 | + _test_java_plugin_validates_transitive_proguard_specs, |
| 387 | + _test_generates_implementation, |
84 | 388 | ], |
85 | 389 | ) |
0 commit comments