blob: 4d57313faff9360a32bd0c1d374d421edb2dabee [file] [log] [blame]
tomlu11f20372018-04-11 06:15:13 -07001// Copyright 2018 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package com.google.devtools.build.lib.actions;
15
16import static com.google.common.truth.Truth.assertThat;
17
18import com.google.common.collect.ImmutableList;
19import com.google.devtools.build.lib.actions.Artifact.ArtifactExpander;
tomlu32b27e22018-04-13 00:51:14 -070020import com.google.devtools.build.lib.actions.CommandLines.ExpandedCommandLines;
tomlu11f20372018-04-11 06:15:13 -070021import com.google.devtools.build.lib.actions.ParameterFile.ParameterFileType;
22import com.google.devtools.build.lib.vfs.FileSystemUtils;
23import com.google.devtools.build.lib.vfs.Path;
24import com.google.devtools.build.lib.vfs.PathFragment;
25import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
26import java.nio.charset.StandardCharsets;
27import org.junit.Test;
28import org.junit.runner.RunWith;
29import org.junit.runners.JUnit4;
30
tomlu32b27e22018-04-13 00:51:14 -070031/** Tests for {@link CommandLines}. */
tomlu11f20372018-04-11 06:15:13 -070032@RunWith(JUnit4.class)
tomlu32b27e22018-04-13 00:51:14 -070033public class CommandLinesTest {
tomlu11f20372018-04-11 06:15:13 -070034
35 private final ArtifactExpander artifactExpander = null;
36 private final PathFragment execPath = PathFragment.create("output.txt");
37
38 @Test
39 public void testSimpleCommandLine() throws Exception {
tomlu32b27e22018-04-13 00:51:14 -070040 ExpandedCommandLines expanded =
41 CommandLines.builder()
tomlu11f20372018-04-11 06:15:13 -070042 .addCommandLine(CommandLine.of(ImmutableList.of("--foo", "--bar")))
43 .build()
tomlu32b27e22018-04-13 00:51:14 -070044 .expand(artifactExpander, execPath, 1024, 0);
45 assertThat(expanded.arguments()).containsExactly("--foo", "--bar");
46 assertThat(expanded.getParamFiles()).isEmpty();
tomlu11f20372018-04-11 06:15:13 -070047 }
48
49 @Test
50 public void testSimpleParamFileUseAlways() throws Exception {
tomlu32b27e22018-04-13 00:51:14 -070051 ExpandedCommandLines expanded =
52 CommandLines.builder()
tomlu11f20372018-04-11 06:15:13 -070053 .addCommandLine(
54 CommandLine.of(ImmutableList.of("--foo", "--bar")),
55 ParamFileInfo.builder(ParameterFileType.UNQUOTED).setUseAlways(true).build())
56 .build()
tomlu32b27e22018-04-13 00:51:14 -070057 .expand(artifactExpander, execPath, 1024, 0);
58 assertThat(expanded.arguments()).containsExactly("@output.txt-0.params");
59 assertThat(expanded.getParamFiles()).hasSize(1);
60 assertThat(expanded.getParamFiles().get(0).arguments).containsExactly("--foo", "--bar");
tomlu11f20372018-04-11 06:15:13 -070061 }
62
63 @Test
64 public void testMaybeUseParamsFiles() throws Exception {
tomlu32b27e22018-04-13 00:51:14 -070065 CommandLines commandLines =
66 CommandLines.builder()
tomlu11f20372018-04-11 06:15:13 -070067 .addCommandLine(
68 CommandLine.of(ImmutableList.of("--foo", "--bar")),
69 ParamFileInfo.builder(ParameterFileType.UNQUOTED).setUseAlways(false).build())
70 .build();
71 // Set max length to longer than command line, no param file needed
tomlu32b27e22018-04-13 00:51:14 -070072 ExpandedCommandLines expanded = commandLines.expand(artifactExpander, execPath, 1024, 0);
73 assertThat(expanded.arguments()).containsExactly("--foo", "--bar");
74 assertThat(expanded.getParamFiles()).isEmpty();
tomlu11f20372018-04-11 06:15:13 -070075
76 // Set max length to 0, spill to param file is forced
tomlu32b27e22018-04-13 00:51:14 -070077 expanded = commandLines.expand(artifactExpander, execPath, 0, 0);
78 assertThat(expanded.arguments()).containsExactly("@output.txt-0.params");
79 assertThat(expanded.getParamFiles()).hasSize(1);
80 assertThat(expanded.getParamFiles().get(0).arguments).containsExactly("--foo", "--bar");
tomlu11f20372018-04-11 06:15:13 -070081 }
82
83 @Test
84 public void testMixOfCommandLinesAndParamFiles() throws Exception {
tomlu32b27e22018-04-13 00:51:14 -070085 ExpandedCommandLines expanded =
86 CommandLines.builder()
tomlu11f20372018-04-11 06:15:13 -070087 .addCommandLine(CommandLine.of(ImmutableList.of("a", "b")))
88 .addCommandLine(
89 CommandLine.of(ImmutableList.of("c", "d")),
90 ParamFileInfo.builder(ParameterFileType.UNQUOTED).setUseAlways(true).build())
91 .addCommandLine(CommandLine.of(ImmutableList.of("e", "f")))
92 .addCommandLine(
93 CommandLine.of(ImmutableList.of("g", "h")),
94 ParamFileInfo.builder(ParameterFileType.UNQUOTED).setUseAlways(true).build())
95 .build()
tomlu32b27e22018-04-13 00:51:14 -070096 .expand(artifactExpander, execPath, 1024, 0);
97 assertThat(expanded.arguments())
tomlu11f20372018-04-11 06:15:13 -070098 .containsExactly("a", "b", "@output.txt-0.params", "e", "f", "@output.txt-1.params");
tomlu32b27e22018-04-13 00:51:14 -070099 assertThat(expanded.getParamFiles()).hasSize(2);
100 assertThat(expanded.getParamFiles().get(0).arguments).containsExactly("c", "d");
101 assertThat(expanded.getParamFiles().get(0).paramFileExecPath.getPathString())
tomlu11f20372018-04-11 06:15:13 -0700102 .isEqualTo("output.txt-0.params");
tomlu32b27e22018-04-13 00:51:14 -0700103 assertThat(expanded.getParamFiles().get(1).arguments).containsExactly("g", "h");
104 assertThat(expanded.getParamFiles().get(1).paramFileExecPath.getPathString())
tomlu11f20372018-04-11 06:15:13 -0700105 .isEqualTo("output.txt-1.params");
106 }
107
108 @Test
109 public void testFirstParamFilePassesButSecondFailsLengthTest() throws Exception {
tomlu32b27e22018-04-13 00:51:14 -0700110 ExpandedCommandLines expanded =
111 CommandLines.builder()
tomlu11f20372018-04-11 06:15:13 -0700112 .addCommandLine(
113 CommandLine.of(ImmutableList.of("a", "b")),
114 ParamFileInfo.builder(ParameterFileType.UNQUOTED).setUseAlways(false).build())
115 .addCommandLine(
116 CommandLine.of(ImmutableList.of("c", "d")),
117 ParamFileInfo.builder(ParameterFileType.UNQUOTED).setUseAlways(false).build())
118 .build()
tomlu32b27e22018-04-13 00:51:14 -0700119 .expand(artifactExpander, execPath, 4, 0);
120 assertThat(expanded.arguments()).containsExactly("a", "b", "@output.txt-0.params");
121 assertThat(expanded.getParamFiles()).hasSize(1);
122 assertThat(expanded.getParamFiles().get(0).arguments).containsExactly("c", "d");
tomlu11f20372018-04-11 06:15:13 -0700123 }
124
125 @Test
126 public void testWriteParamFiles() throws Exception {
tomlu32b27e22018-04-13 00:51:14 -0700127 CommandLines commandLines =
128 CommandLines.builder()
tomlu11f20372018-04-11 06:15:13 -0700129 .addCommandLine(
130 CommandLine.of(ImmutableList.of("--foo", "--bar")),
131 ParamFileInfo.builder(ParameterFileType.UNQUOTED).setUseAlways(true).build())
132 .addCommandLine(
133 CommandLine.of(ImmutableList.of("--baz")),
134 ParamFileInfo.builder(ParameterFileType.UNQUOTED).setUseAlways(true).build())
135 .build();
136 InMemoryFileSystem inMemoryFileSystem = new InMemoryFileSystem();
137 Path execRoot = inMemoryFileSystem.getPath("/exec");
138 execRoot.createDirectoryAndParents();
tomlu32b27e22018-04-13 00:51:14 -0700139 ExpandedCommandLines expanded =
140 commandLines.expand(artifactExpander, PathFragment.create("my/param/file/out"), 0, 0);
141 expanded.writeParamFiles(execRoot);
tomlu11f20372018-04-11 06:15:13 -0700142
143 assertThat(
144 FileSystemUtils.readLines(
145 execRoot.getRelative("my/param/file/out-0.params"), StandardCharsets.ISO_8859_1))
146 .containsExactly("--foo", "--bar");
147 assertThat(
148 FileSystemUtils.readLines(
149 execRoot.getRelative("my/param/file/out-1.params"), StandardCharsets.ISO_8859_1))
150 .containsExactly("--baz");
151 }
152}