From 5024353a32625209808c3a7b8c16062d0a7c62c5 Mon Sep 17 00:00:00 2001 From: dacek Date: Tue, 8 Feb 2022 10:48:25 -0800 Subject: [PATCH] Add JavaCompileActionBuilderTest. PiperOrigin-RevId: 427235097 --- .../devtools/build/lib/rules/java/BUILD | 16 ++ .../java/JavaCompileActionBuilderTest.java | 147 ++++++++++++++++++ 2 files changed, 163 insertions(+) create mode 100644 src/test/java/com/google/devtools/build/lib/rules/java/JavaCompileActionBuilderTest.java diff --git a/src/test/java/com/google/devtools/build/lib/rules/java/BUILD b/src/test/java/com/google/devtools/build/lib/rules/java/BUILD index 12f2e94754a3c0..217dadfdbd1484 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/java/BUILD +++ b/src/test/java/com/google/devtools/build/lib/rules/java/BUILD @@ -14,6 +14,22 @@ filegroup( visibility = ["//src:__subpackages__"], ) +java_test( + name = "JavaCompileActionBuilderTest", + srcs = ["JavaCompileActionBuilderTest.java"], + deps = [ + "//src/main/java/com/google/devtools/build/lib/actions:artifacts", + "//src/main/java/com/google/devtools/build/lib/cmdline:cmdline-primitives", + "//src/main/java/com/google/devtools/build/lib/rules/java:java-compilation", + "//src/main/protobuf:deps_java_proto", + "//src/test/java/com/google/devtools/build/lib/analysis/util", + "//src/test/java/com/google/devtools/build/lib/rules/java:java_compile_action_test_helper", + "//src/test/java/com/google/devtools/build/lib/testutil:JunitUtils", + "//third_party:junit4", + "//third_party:truth", + ], +) + java_test( name = "JavaCompilationArgsTest", srcs = ["JavaCompilationArgsTest.java"], diff --git a/src/test/java/com/google/devtools/build/lib/rules/java/JavaCompileActionBuilderTest.java b/src/test/java/com/google/devtools/build/lib/rules/java/JavaCompileActionBuilderTest.java new file mode 100644 index 00000000000000..6431a53ce8fa27 --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/rules/java/JavaCompileActionBuilderTest.java @@ -0,0 +1,147 @@ +// Copyright 2022 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.devtools.build.lib.rules.java; + +import static com.google.common.truth.Truth.assertThat; +import static com.google.devtools.build.lib.rules.java.JavaCompileActionTestHelper.getJavacArguments; + +import com.google.devtools.build.lib.actions.Artifact; +import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; +import com.google.devtools.build.lib.cmdline.RepositoryName; +import com.google.devtools.build.lib.testutil.MoreAsserts; +import com.google.devtools.build.lib.view.proto.Deps; +import com.google.devtools.build.lib.view.proto.Deps.Dependency.Kind; +import java.util.ArrayList; +import java.util.List; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for {@link JavaCompileActionBuilder}. */ +@RunWith(JUnit4.class) +public final class JavaCompileActionBuilderTest extends BuildViewTestCase { + + @Test + public void testClassdirIsInBlazeOut() throws Exception { + scratch.file("java/com/google/test/BUILD", + "java_binary(name = 'a',", + " srcs = ['a.java'])"); + JavaCompileAction action = + (JavaCompileAction) getGeneratingActionForLabel("//java/com/google/test:a.jar"); + List command = new ArrayList<>(); + command.addAll(getJavacArguments(action)); + MoreAsserts.assertContainsSublist( + command, + "--output", + targetConfig + .getBinFragment(RepositoryName.MAIN) + .getRelative("java/com/google/test/a.jar") + .getPathString()); + } + + @Test + public void progressMessage() throws Exception { + scratch.file( + "java/com/google/test/BUILD", + "java_library(name = 'a',", + " srcs = ['a.java', 'b.java'])"); + JavaCompileAction action = + (JavaCompileAction) getGeneratingActionForLabel("//java/com/google/test:liba.jar"); + assertThat(action.getProgressMessage()) + .isEqualTo("Building java/com/google/test/liba.jar (2 source files)"); + } + + @Test + public void progressMessageWithSourceJars() throws Exception { + scratch.file( + "java/com/google/test/BUILD", + "java_library(name = 'a',", + " srcs = ['a.java', 'b.java', 'archive.srcjar'])"); + JavaCompileAction action = + (JavaCompileAction) getGeneratingActionForLabel("//java/com/google/test:liba.jar"); + assertThat(action.getProgressMessage()) + .isEqualTo("Building java/com/google/test/liba.jar (2 source files, 1 source jar)"); + } + + @Test + public void progressMessageAnnotationProcessors() throws Exception { + scratch.file( + "java/com/google/test/BUILD", + "java_plugin(", + " name = 'foo',", + " srcs = ['Foo.java'],", + " processor_class = 'Foo',", + ")", + "java_plugin(", + " name = 'bar',", + " srcs = ['Bar.java'],", + " processor_class = 'com.google.Bar',", + ")", + "java_library(", + " name = 'a',", + " srcs = ['a.java', 'b.java', 'archive.srcjar'],", + " plugins = [':foo', ':bar'],", + ")"); + JavaCompileAction action = + (JavaCompileAction) getGeneratingActionForLabel("//java/com/google/test:liba.jar"); + assertThat(action.getProgressMessage()) + .isEqualTo( + "Building java/com/google/test/liba.jar (2 source files, 1 source jar)" + + " and running annotation processors (Foo, Bar)"); + } + + @Test + public void testLocale() throws Exception { + scratch.file( + "java/com/google/test/BUILD", // + "java_library(", + " name = 'a',", + " srcs = ['A.java'],", + ")"); + JavaCompileAction action = + (JavaCompileAction) getGeneratingActionForLabel("//java/com/google/test:liba.jar"); + assertThat(action.getIncompleteEnvironmentForTesting()) + .containsEntry("LC_CTYPE", "en_US.UTF-8"); + } + + @Test + public void testClasspathReduction() throws Exception { + scratch.file( + "java/com/google/test/BUILD", + "java_library(name = 'a', srcs = ['A.java'], deps = [':b'])", + "java_library(name = 'b', srcs = ['B.java'], deps = [':c', ':d'])", + "java_library(name = 'c', srcs = ['C.java'])", + "java_library(name = 'd', srcs = ['D.java'])"); + Artifact bJdeps = + getBinArtifact("libb-hjar.jdeps", getConfiguredTarget("//java/com/google/test:b")); + Artifact cHjar = + getBinArtifact("libc-hjar.jar", getConfiguredTarget("//java/com/google/test:libc.jar")); + JavaCompileAction action = + (JavaCompileAction) getGeneratingActionForLabel("//java/com/google/test:liba.jar"); + JavaCompileActionContext context = new JavaCompileActionContext(); + Deps.Dependency dep = + Deps.Dependency.newBuilder() + .setKind(Kind.EXPLICIT) + .setPath(cHjar.getExecPathString()) + .build(); + context.insertDependencies(bJdeps, Deps.Dependencies.newBuilder().addDependency(dep).build()); + assertThat( + artifactsToStrings( + action.getReducedClasspath(new ActionExecutionContextBuilder().build(), context) + .reducedJars)) + .containsExactly( + "bin java/com/google/test/libb-hjar.jar", "bin java/com/google/test/libc-hjar.jar"); + } +}