aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java
diff options
context:
space:
mode:
authorGravatar tomlu <tomlu@google.com>2018-04-11 06:15:13 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-04-11 06:16:59 -0700
commit11f203748c9fc7d360c53ac62f191346806c39b0 (patch)
treed4115d21d40d951337c7aa59207f652f4bf13d5e /src/test/java
parent0c254e67ef98944897ba7258a8c37300272cdf52 (diff)
Add CommandLinesAndParamFiles class.
This class is currently unused. In order to keep CL sizes down we introduce this class prior to using it in the spawn runners. This class can manage an action's list of command lines and param files. For instance, SpawnAction will contain one of these instances (instead of keeping a single command line object and adding param file write actions to the action graph). At spawn execution time, the spawn runners will use this class to resolve the list of command lines and param files into a master argument list + some number of param files that need to be written. The local spawn runners can simply write the param files using a helper. In the distributed cases the param files are ready-made VirtualActionInputs that can be added to the Spawn's other inputs. RELNOTES: None PiperOrigin-RevId: 192439501
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/com/google/devtools/build/lib/actions/CommandLinesAndParamFilesTest.java154
1 files changed, 154 insertions, 0 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/actions/CommandLinesAndParamFilesTest.java b/src/test/java/com/google/devtools/build/lib/actions/CommandLinesAndParamFilesTest.java
new file mode 100644
index 0000000000..bdf5b52e4e
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/actions/CommandLinesAndParamFilesTest.java
@@ -0,0 +1,154 @@
+// Copyright 2018 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.actions;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.common.collect.ImmutableList;
+import com.google.devtools.build.lib.actions.Artifact.ArtifactExpander;
+import com.google.devtools.build.lib.actions.CommandLinesAndParamFiles.ResolvedCommandLineAndParamFiles;
+import com.google.devtools.build.lib.actions.ParameterFile.ParameterFileType;
+import com.google.devtools.build.lib.vfs.FileSystemUtils;
+import com.google.devtools.build.lib.vfs.Path;
+import com.google.devtools.build.lib.vfs.PathFragment;
+import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
+import java.nio.charset.StandardCharsets;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Tests for {@link CommandLinesAndParamFiles}. */
+@RunWith(JUnit4.class)
+public class CommandLinesAndParamFilesTest {
+
+ private final ArtifactExpander artifactExpander = null;
+ private final PathFragment execPath = PathFragment.create("output.txt");
+
+ @Test
+ public void testSimpleCommandLine() throws Exception {
+ ResolvedCommandLineAndParamFiles resolved =
+ CommandLinesAndParamFiles.builder()
+ .addCommandLine(CommandLine.of(ImmutableList.of("--foo", "--bar")))
+ .build()
+ .resolve(artifactExpander, execPath, 1024, 0);
+ assertThat(resolved.arguments()).containsExactly("--foo", "--bar");
+ assertThat(resolved.getParamFiles()).isEmpty();
+ }
+
+ @Test
+ public void testSimpleParamFileUseAlways() throws Exception {
+ ResolvedCommandLineAndParamFiles resolved =
+ CommandLinesAndParamFiles.builder()
+ .addCommandLine(
+ CommandLine.of(ImmutableList.of("--foo", "--bar")),
+ ParamFileInfo.builder(ParameterFileType.UNQUOTED).setUseAlways(true).build())
+ .build()
+ .resolve(artifactExpander, execPath, 1024, 0);
+ assertThat(resolved.arguments()).containsExactly("@output.txt-0.params");
+ assertThat(resolved.getParamFiles()).hasSize(1);
+ assertThat(resolved.getParamFiles().get(0).arguments).containsExactly("--foo", "--bar");
+ }
+
+ @Test
+ public void testMaybeUseParamsFiles() throws Exception {
+ CommandLinesAndParamFiles commandLinesAndParamFiles =
+ CommandLinesAndParamFiles.builder()
+ .addCommandLine(
+ CommandLine.of(ImmutableList.of("--foo", "--bar")),
+ ParamFileInfo.builder(ParameterFileType.UNQUOTED).setUseAlways(false).build())
+ .build();
+ // Set max length to longer than command line, no param file needed
+ ResolvedCommandLineAndParamFiles resolved =
+ commandLinesAndParamFiles.resolve(artifactExpander, execPath, 1024, 0);
+ assertThat(resolved.arguments()).containsExactly("--foo", "--bar");
+ assertThat(resolved.getParamFiles()).isEmpty();
+
+ // Set max length to 0, spill to param file is forced
+ resolved = commandLinesAndParamFiles.resolve(artifactExpander, execPath, 0, 0);
+ assertThat(resolved.arguments()).containsExactly("@output.txt-0.params");
+ assertThat(resolved.getParamFiles()).hasSize(1);
+ assertThat(resolved.getParamFiles().get(0).arguments).containsExactly("--foo", "--bar");
+ }
+
+ @Test
+ public void testMixOfCommandLinesAndParamFiles() throws Exception {
+ ResolvedCommandLineAndParamFiles resolved =
+ CommandLinesAndParamFiles.builder()
+ .addCommandLine(CommandLine.of(ImmutableList.of("a", "b")))
+ .addCommandLine(
+ CommandLine.of(ImmutableList.of("c", "d")),
+ ParamFileInfo.builder(ParameterFileType.UNQUOTED).setUseAlways(true).build())
+ .addCommandLine(CommandLine.of(ImmutableList.of("e", "f")))
+ .addCommandLine(
+ CommandLine.of(ImmutableList.of("g", "h")),
+ ParamFileInfo.builder(ParameterFileType.UNQUOTED).setUseAlways(true).build())
+ .build()
+ .resolve(artifactExpander, execPath, 1024, 0);
+ assertThat(resolved.arguments())
+ .containsExactly("a", "b", "@output.txt-0.params", "e", "f", "@output.txt-1.params");
+ assertThat(resolved.getParamFiles()).hasSize(2);
+ assertThat(resolved.getParamFiles().get(0).arguments).containsExactly("c", "d");
+ assertThat(resolved.getParamFiles().get(0).paramFileExecPath.getPathString())
+ .isEqualTo("output.txt-0.params");
+ assertThat(resolved.getParamFiles().get(1).arguments).containsExactly("g", "h");
+ assertThat(resolved.getParamFiles().get(1).paramFileExecPath.getPathString())
+ .isEqualTo("output.txt-1.params");
+ }
+
+ @Test
+ public void testFirstParamFilePassesButSecondFailsLengthTest() throws Exception {
+ ResolvedCommandLineAndParamFiles resolved =
+ CommandLinesAndParamFiles.builder()
+ .addCommandLine(
+ CommandLine.of(ImmutableList.of("a", "b")),
+ ParamFileInfo.builder(ParameterFileType.UNQUOTED).setUseAlways(false).build())
+ .addCommandLine(
+ CommandLine.of(ImmutableList.of("c", "d")),
+ ParamFileInfo.builder(ParameterFileType.UNQUOTED).setUseAlways(false).build())
+ .build()
+ .resolve(artifactExpander, execPath, 4, 0);
+ assertThat(resolved.arguments()).containsExactly("a", "b", "@output.txt-0.params");
+ assertThat(resolved.getParamFiles()).hasSize(1);
+ assertThat(resolved.getParamFiles().get(0).arguments).containsExactly("c", "d");
+ }
+
+ @Test
+ public void testWriteParamFiles() throws Exception {
+ CommandLinesAndParamFiles commandLinesAndParamFiles =
+ CommandLinesAndParamFiles.builder()
+ .addCommandLine(
+ CommandLine.of(ImmutableList.of("--foo", "--bar")),
+ ParamFileInfo.builder(ParameterFileType.UNQUOTED).setUseAlways(true).build())
+ .addCommandLine(
+ CommandLine.of(ImmutableList.of("--baz")),
+ ParamFileInfo.builder(ParameterFileType.UNQUOTED).setUseAlways(true).build())
+ .build();
+ InMemoryFileSystem inMemoryFileSystem = new InMemoryFileSystem();
+ Path execRoot = inMemoryFileSystem.getPath("/exec");
+ execRoot.createDirectoryAndParents();
+ ResolvedCommandLineAndParamFiles resolved =
+ commandLinesAndParamFiles.resolve(
+ artifactExpander, PathFragment.create("my/param/file/out"), 0, 0);
+ resolved.writeParamFiles(execRoot);
+
+ assertThat(
+ FileSystemUtils.readLines(
+ execRoot.getRelative("my/param/file/out-0.params"), StandardCharsets.ISO_8859_1))
+ .containsExactly("--foo", "--bar");
+ assertThat(
+ FileSystemUtils.readLines(
+ execRoot.getRelative("my/param/file/out-1.params"), StandardCharsets.ISO_8859_1))
+ .containsExactly("--baz");
+ }
+}