aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test
diff options
context:
space:
mode:
authorGravatar elenairina <elenairina@google.com>2017-11-24 07:23:55 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2017-11-24 07:25:22 -0800
commita6ed4c01d374bcf98bf39c1efc12a0f4f20fe313 (patch)
treeb0a6f8ffb7d9efd8b9f1ca3f3224366319f1a3fa /src/test
parent263c5e07dd2860a866fd361cd29c03afaf3e019f (diff)
Create the output source jar in java_common.compile
and expose transitive source jars to Skylark. Slightly refactor java classes to take in specific host javabase inputs and host java executable for creating the source jar, instead of always relying on fetching them from native java rules specific attributes. Creating the output source jar in java_common.compile makes the behavior more similar to java_library. Exposing the transitive_source_jars to Skylark helps with the Skylark migration from the old 'java' provider to JavaInfo. Progress on #2614. RELNOTES: transitive_source_jars is now exposed on JavaInfo. PiperOrigin-RevId: 176844750
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiTest.java150
1 files changed, 149 insertions, 1 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiTest.java b/src/test/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiTest.java
index a8389f5630..7df7dc95e7 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiTest.java
@@ -203,11 +203,159 @@ public class JavaSkylarkApiTest extends BuildViewTestCase {
OutputJar outputJar = outputs.getOutputJars().get(0);
assertThat(outputJar.getClassJar().getFilename()).isEqualTo("libdep.jar");
assertThat(outputJar.getIJar().getFilename()).isEqualTo("libdep-hjar.jar");
- assertThat(outputJar.getSrcJars()).isEmpty();
+ assertThat(prettyJarNames(outputJar.getSrcJars())).containsExactly("java/test/libdep-src.jar");
assertThat(outputs.getJdeps().getFilename()).isEqualTo("libdep.jdeps");
}
@Test
+ public void testJavaCommonCompileTransitiveSourceJars() throws Exception {
+ writeBuildFileForJavaToolchain();
+ scratch.file(
+ "java/test/BUILD",
+ "load(':custom_rule.bzl', 'java_custom_library')",
+ "java_custom_library(",
+ " name = 'custom',",
+ " srcs = ['Main.java'],",
+ " deps = [':dep']",
+ ")",
+ "java_library(",
+ " name = 'dep',",
+ " srcs = [ 'Dep.java'],",
+ ")");
+ scratch.file(
+ "java/test/custom_rule.bzl",
+ "def _impl(ctx):",
+ " output_jar = ctx.actions.declare_file('lib' + ctx.label.name + '.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,",
+ " javac_opts = java_common.default_javac_opts(",
+ " ctx, java_toolchain_attr = '_java_toolchain'),",
+ " deps = deps,",
+ " java_toolchain = ctx.attr._java_toolchain,",
+ " host_javabase = ctx.attr._host_javabase",
+ " )",
+ " return struct(",
+ " files = depset([output_jar]),",
+ " providers = [compilation_provider]",
+ " )",
+ "java_custom_library = rule(",
+ " implementation = _impl,",
+ " outputs = {",
+ " 'my_output': 'lib%{name}.jar'",
+ " },",
+ " attrs = {",
+ " 'srcs': attr.label_list(allow_files=['.java']),",
+ " 'deps': attr.label_list(),",
+ " '_java_toolchain': attr.label(default = Label('//java/com/google/test:toolchain')),",
+ " '_host_javabase': attr.label(default = Label('//tools/defaults:jdk'))",
+ " },",
+ " fragments = ['java']",
+ ")");
+
+ ConfiguredTarget configuredTarget = getConfiguredTarget("//java/test:custom");
+ JavaInfo info = configuredTarget.get(JavaInfo.PROVIDER);
+ SkylarkList<Artifact> sourceJars = info.getSourceJars();
+ NestedSet<Artifact> transitiveSourceJars = info.getTransitiveSourceJars();
+ assertThat(artifactFilesNames(sourceJars)).containsExactly("libcustom-src.jar");
+ assertThat(artifactFilesNames(transitiveSourceJars))
+ .containsExactly("libdep-src.jar", "libcustom-src.jar");
+ }
+
+ @Test
+ public void testJavaCommonCompileWithOnlyOneSourceJar() throws Exception {
+ writeBuildFileForJavaToolchain();
+ scratch.file(
+ "java/test/BUILD",
+ "load(':custom_rule.bzl', 'java_custom_library')",
+ "java_custom_library(",
+ " name = 'custom',",
+ " srcs = ['myjar-src.jar'],",
+ ")");
+ scratch.file(
+ "java/test/custom_rule.bzl",
+ "def _impl(ctx):",
+ " output_jar = ctx.actions.declare_file('lib' + ctx.label.name + '.jar')",
+ " compilation_provider = java_common.compile(",
+ " ctx,",
+ " source_jars = ctx.files.srcs,",
+ " output = output_jar,",
+ " javac_opts = java_common.default_javac_opts(",
+ " ctx, java_toolchain_attr = '_java_toolchain'),",
+ " java_toolchain = ctx.attr._java_toolchain,",
+ " host_javabase = ctx.attr._host_javabase",
+ " )",
+ " return struct(",
+ " files = depset([output_jar]),",
+ " providers = [compilation_provider]",
+ " )",
+ "java_custom_library = rule(",
+ " implementation = _impl,",
+ " outputs = {",
+ " 'my_output': 'lib%{name}.jar'",
+ " },",
+ " attrs = {",
+ " 'srcs': attr.label_list(allow_files=['.jar']),",
+ " '_java_toolchain': attr.label(default = Label('//java/com/google/test:toolchain')),",
+ " '_host_javabase': attr.label(default = Label('//tools/defaults:jdk'))",
+ " },",
+ " fragments = ['java']",
+ ")");
+
+ ConfiguredTarget configuredTarget = getConfiguredTarget("//java/test:custom");
+ JavaInfo info = configuredTarget.get(JavaInfo.PROVIDER);
+ SkylarkList<Artifact> sourceJars = info.getSourceJars();
+ assertThat(artifactFilesNames(sourceJars)).containsExactly("myjar-src.jar");
+ }
+
+ @Test
+ public void testJavaCommonCompileWithNoSources() throws Exception {
+ writeBuildFileForJavaToolchain();
+ scratch.file(
+ "java/test/BUILD",
+ "load(':custom_rule.bzl', 'java_custom_library')",
+ "java_custom_library(",
+ " name = 'custom',",
+ ")");
+ scratch.file(
+ "java/test/custom_rule.bzl",
+ "def _impl(ctx):",
+ " output_jar = ctx.actions.declare_file('lib' + ctx.label.name + '.jar')",
+ " compilation_provider = java_common.compile(",
+ " ctx,",
+ " output = output_jar,",
+ " javac_opts = java_common.default_javac_opts(",
+ " ctx, java_toolchain_attr = '_java_toolchain'),",
+ " java_toolchain = ctx.attr._java_toolchain,",
+ " host_javabase = ctx.attr._host_javabase",
+ " )",
+ " return struct(",
+ " files = depset([output_jar]),",
+ " providers = [compilation_provider]",
+ " )",
+ "java_custom_library = rule(",
+ " implementation = _impl,",
+ " outputs = {",
+ " 'my_output': 'lib%{name}.jar'",
+ " },",
+ " attrs = {",
+ " '_java_toolchain': attr.label(default = Label('//java/com/google/test:toolchain')),",
+ " '_host_javabase': attr.label(default = Label('//tools/defaults:jdk'))",
+ " },",
+ " fragments = ['java']",
+ ")");
+ try {
+ getConfiguredTarget("//java/test:custom");
+ } catch (AssertionError e) {
+ assertThat(e.getMessage())
+ .contains("source_jars, sources and exports cannot be simultaneous empty");
+ }
+
+ }
+
+ @Test
public void testExposesJavaSkylarkApiProvider() throws Exception {
scratch.file(
"java/test/BUILD",