Migrate the `JavaStarlarkApiTest.testJavaCommonCompileSourceJarName` test to Starlark The test is removed from Bazel and added in `@rules_java` PiperOrigin-RevId: 738411122 Change-Id: Ibfee767b9e41b89a3cb5b718cd6a9c380c238928
diff --git a/java/test/common/java_common_tests.bzl b/java/test/common/java_common_tests.bzl index ab46253..f01b2d2 100644 --- a/java/test/common/java_common_tests.bzl +++ b/java/test/common/java_common_tests.bzl
@@ -13,6 +13,7 @@ load("//java/test/testutil:rules/custom_library_extended_compile_jdeps.bzl", "CompileJdepsInfo", "custom_library_extended_jdeps") load("//java/test/testutil:rules/custom_library_with_bootclasspath.bzl", "custom_bootclasspath", "custom_library_with_bootclasspath") load("//java/test/testutil:rules/custom_library_with_exports.bzl", "custom_library_with_exports") +load("//java/test/testutil:rules/custom_library_with_named_outputs.bzl", "custom_library_with_named_outputs") load("//java/test/testutil:rules/custom_library_with_sourcepaths.bzl", "custom_library_with_sourcepaths") load("//java/test/testutil:rules/custom_library_with_wrong_plugins_type.bzl", "custom_library_with_wrong_plugins_type") @@ -517,6 +518,39 @@ "{package}/lib{name}-src.jar", ]) +def _test_compile_source_jar_name_derived_from_output_jar(name): + target_name = name + "/custom" + util.helper_target( + custom_library_with_named_outputs, + name = target_name, + srcs = ["Main.java"], + deps = [target_name + "/dep"], + ) + util.helper_target( + java_library, + name = target_name + "/dep", + srcs = ["Dep.java"], + ) + + analysis_test( + name = name, + impl = _test_compile_source_jar_name_derived_from_output_jar_impl, + target = target_name, + ) + +def _test_compile_source_jar_name_derived_from_output_jar_impl(env, target): + assert_java_info = java_info_subject.from_target(env, target) + + assert_java_info.source_jars().contains_exactly_predicates([ + matching.file_basename_equals("amazing-src.jar"), + matching.file_basename_equals("wonderful-src.jar"), + ]) + assert_java_info.transitive_source_jars().contains_exactly([ + "{package}/lib{name}/dep-src.jar", + "{package}/{name}/amazing-src.jar", + "{package}/{name}/wonderful-src.jar", + ]) + def java_common_tests(name): test_suite( name = name, @@ -537,5 +571,6 @@ _test_compile_requires_java_plugin_info, _test_compile_compilation_info, _test_compile_transitive_source_jars, + _test_compile_source_jar_name_derived_from_output_jar, ], )
diff --git a/java/test/testutil/rules/custom_library_with_named_outputs.bzl b/java/test/testutil/rules/custom_library_with_named_outputs.bzl new file mode 100644 index 0000000..ec0a412 --- /dev/null +++ b/java/test/testutil/rules/custom_library_with_named_outputs.bzl
@@ -0,0 +1,40 @@ +"""Custom rule to test that source jar names are derived from the output jar""" + +load("//java/common:java_common.bzl", "java_common") +load("//java/common:java_semantics.bzl", "semantics") + +def _impl(ctx): + output_jar = ctx.actions.declare_file(ctx.attr.name + "/amazing.jar") + other_output_jar = ctx.actions.declare_file(ctx.attr.name + "/wonderful.jar") + deps = [dep[java_common.provider] for dep in ctx.attr.deps] + compilation_provider = java_common.compile( + ctx, + source_files = ctx.files.srcs, + output = output_jar, + deps = deps, + java_toolchain = semantics.find_java_toolchain(ctx), + ) + other_compilation_provider = java_common.compile( + ctx, + source_files = ctx.files.srcs, + output = other_output_jar, + deps = deps, + java_toolchain = semantics.find_java_toolchain(ctx), + ) + result_provider = java_common.merge([compilation_provider, other_compilation_provider]) + return [ + DefaultInfo( + files = depset([output_jar]), + ), + result_provider, + ] + +custom_library_with_named_outputs = rule( + implementation = _impl, + attrs = { + "srcs": attr.label_list(allow_files = [".java"]), + "deps": attr.label_list(), + }, + toolchains = [semantics.JAVA_TOOLCHAIN_TYPE], + fragments = ["java"], +)