Skip to content

Commit

Permalink
Add JavaCompileActionBuilderTest.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 427235097
  • Loading branch information
MarkDacek authored and copybara-github committed Feb 8, 2022
1 parent 74b7dd5 commit 5024353
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/test/java/com/google/devtools/build/lib/rules/java/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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");
}
}

0 comments on commit 5024353

Please sign in to comment.