aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build
diff options
context:
space:
mode:
authorGravatar elenairina <elenairina@google.com>2017-11-29 08:17:47 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2017-11-29 08:28:57 -0800
commit1e3d568ea68e3d4b597dd40e6100295548f44e8a (patch)
tree9d50bdd1b940678de107f4511063421a18d736f0 /src/test/java/com/google/devtools/build
parentbd24c6bba0e6254d01bb92053847d0d396a8b77d (diff)
Create the output source jar in java_common.compile
and expose transitive source jars to Skylark. Initial change was rolled back (unknown commit). java_common.compile wouldn't fill the JavaRuleOutputJarsProvider when there was only one input source jar. Fixed that and added test (there was one before, but didn't check this particular provider). 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: 177311138
Diffstat (limited to 'src/test/java/com/google/devtools/build')
-rw-r--r--src/test/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiTest.java157
1 files changed, 156 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..e3cc4b87e8 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,166 @@ 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");
+ JavaRuleOutputJarsProvider outputJars = info.getOutputJars();
+ assertThat(outputJars.getOutputJars()).hasSize(1);
+ OutputJar outputJar = outputJars.getOutputJars().get(0);
+ assertThat((outputJar.getClassJar().getFilename())).isEqualTo("libcustom.jar");
+ assertThat((outputJar.getSrcJar().getFilename())).isEqualTo("myjar-src.jar");
+ assertThat((outputJar.getIJar().getFilename())).isEqualTo("libcustom-hjar.jar");
+ assertThat(outputJars.getJdeps().getFilename()).isEqualTo("libcustom.jdeps");
+ }
+
+ @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",