blob: c51e236b6f6bfb9def8682d05381178bcd6de5fd [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;
tomlu451cfec2018-04-17 06:56:56 -070020import com.google.devtools.build.lib.actions.CommandLines.CommandLineLimits;
tomlu32b27e22018-04-13 00:51:14 -070021import com.google.devtools.build.lib.actions.CommandLines.ExpandedCommandLines;
tomlu11f20372018-04-11 06:15:13 -070022import com.google.devtools.build.lib.actions.ParameterFile.ParameterFileType;
tomlu11f20372018-04-11 06:15:13 -070023import com.google.devtools.build.lib.vfs.PathFragment;
tomlu11f20372018-04-11 06:15:13 -070024import org.junit.Test;
25import org.junit.runner.RunWith;
26import org.junit.runners.JUnit4;
27
tomlu32b27e22018-04-13 00:51:14 -070028/** Tests for {@link CommandLines}. */
tomlu11f20372018-04-11 06:15:13 -070029@RunWith(JUnit4.class)
tomlu32b27e22018-04-13 00:51:14 -070030public class CommandLinesTest {
tomlu11f20372018-04-11 06:15:13 -070031
32 private final ArtifactExpander artifactExpander = null;
33 private final PathFragment execPath = PathFragment.create("output.txt");
tomlu451cfec2018-04-17 06:56:56 -070034 private static final CommandLineLimits NO_LIMIT = new CommandLineLimits(10000);
tomlu11f20372018-04-11 06:15:13 -070035
36 @Test
37 public void testSimpleCommandLine() throws Exception {
tomlu451cfec2018-04-17 06:56:56 -070038 CommandLines commandLines =
tomlu32b27e22018-04-13 00:51:14 -070039 CommandLines.builder()
tomlu11f20372018-04-11 06:15:13 -070040 .addCommandLine(CommandLine.of(ImmutableList.of("--foo", "--bar")))
tomlu451cfec2018-04-17 06:56:56 -070041 .build();
42 ExpandedCommandLines expanded = commandLines.expand(artifactExpander, execPath, NO_LIMIT, 0);
43 assertThat(commandLines.allArguments()).containsExactly("--foo", "--bar");
tomlu32b27e22018-04-13 00:51:14 -070044 assertThat(expanded.arguments()).containsExactly("--foo", "--bar");
45 assertThat(expanded.getParamFiles()).isEmpty();
tomlu11f20372018-04-11 06:15:13 -070046 }
47
48 @Test
tomlu451cfec2018-04-17 06:56:56 -070049 public void testFromArguments() throws Exception {
tomlu936139d2018-04-25 10:57:31 -070050 CommandLines commandLines = CommandLines.of(ImmutableList.of("--foo", "--bar"));
tomlu451cfec2018-04-17 06:56:56 -070051 ExpandedCommandLines expanded = commandLines.expand(artifactExpander, execPath, NO_LIMIT, 0);
52 assertThat(commandLines.allArguments()).containsExactly("--foo", "--bar");
tomlu451cfec2018-04-17 06:56:56 -070053 assertThat(expanded.arguments()).containsExactly("--foo", "--bar");
54 assertThat(expanded.getParamFiles()).isEmpty();
55 }
56
57 @Test
58 public void testConcat() throws Exception {
59 CommandLines commandLines =
60 CommandLines.concat(
61 CommandLine.of(ImmutableList.of("--before")),
tomlu936139d2018-04-25 10:57:31 -070062 CommandLines.of(ImmutableList.of("--foo", "--bar")));
tomlu451cfec2018-04-17 06:56:56 -070063 ExpandedCommandLines expanded = commandLines.expand(artifactExpander, execPath, NO_LIMIT, 0);
64 assertThat(commandLines.allArguments()).containsExactly("--before", "--foo", "--bar");
tomlu451cfec2018-04-17 06:56:56 -070065 assertThat(expanded.arguments()).containsExactly("--before", "--foo", "--bar");
66 assertThat(expanded.getParamFiles()).isEmpty();
67 }
68
69 @Test
tomlu11f20372018-04-11 06:15:13 -070070 public void testSimpleParamFileUseAlways() throws Exception {
tomlu451cfec2018-04-17 06:56:56 -070071 CommandLines commandLines =
tomlu32b27e22018-04-13 00:51:14 -070072 CommandLines.builder()
tomlu11f20372018-04-11 06:15:13 -070073 .addCommandLine(
74 CommandLine.of(ImmutableList.of("--foo", "--bar")),
75 ParamFileInfo.builder(ParameterFileType.UNQUOTED).setUseAlways(true).build())
tomlu451cfec2018-04-17 06:56:56 -070076 .build();
77 ExpandedCommandLines expanded = commandLines.expand(artifactExpander, execPath, NO_LIMIT, 0);
78 assertThat(commandLines.allArguments()).containsExactly("--foo", "--bar");
tomlu32b27e22018-04-13 00:51:14 -070079 assertThat(expanded.arguments()).containsExactly("@output.txt-0.params");
80 assertThat(expanded.getParamFiles()).hasSize(1);
81 assertThat(expanded.getParamFiles().get(0).arguments).containsExactly("--foo", "--bar");
tomlu11f20372018-04-11 06:15:13 -070082 }
83
84 @Test
85 public void testMaybeUseParamsFiles() throws Exception {
tomlu32b27e22018-04-13 00:51:14 -070086 CommandLines commandLines =
87 CommandLines.builder()
tomlu11f20372018-04-11 06:15:13 -070088 .addCommandLine(
89 CommandLine.of(ImmutableList.of("--foo", "--bar")),
90 ParamFileInfo.builder(ParameterFileType.UNQUOTED).setUseAlways(false).build())
91 .build();
92 // Set max length to longer than command line, no param file needed
tomlu451cfec2018-04-17 06:56:56 -070093 ExpandedCommandLines expanded = commandLines.expand(artifactExpander, execPath, NO_LIMIT, 0);
tomlu32b27e22018-04-13 00:51:14 -070094 assertThat(expanded.arguments()).containsExactly("--foo", "--bar");
95 assertThat(expanded.getParamFiles()).isEmpty();
tomlu11f20372018-04-11 06:15:13 -070096
97 // Set max length to 0, spill to param file is forced
tomlu451cfec2018-04-17 06:56:56 -070098 expanded = commandLines.expand(artifactExpander, execPath, new CommandLineLimits(0), 0);
tomlu32b27e22018-04-13 00:51:14 -070099 assertThat(expanded.arguments()).containsExactly("@output.txt-0.params");
100 assertThat(expanded.getParamFiles()).hasSize(1);
101 assertThat(expanded.getParamFiles().get(0).arguments).containsExactly("--foo", "--bar");
tomlu11f20372018-04-11 06:15:13 -0700102 }
103
104 @Test
105 public void testMixOfCommandLinesAndParamFiles() throws Exception {
tomlu451cfec2018-04-17 06:56:56 -0700106 CommandLines commandLines =
tomlu32b27e22018-04-13 00:51:14 -0700107 CommandLines.builder()
tomlu11f20372018-04-11 06:15:13 -0700108 .addCommandLine(CommandLine.of(ImmutableList.of("a", "b")))
109 .addCommandLine(
110 CommandLine.of(ImmutableList.of("c", "d")),
111 ParamFileInfo.builder(ParameterFileType.UNQUOTED).setUseAlways(true).build())
112 .addCommandLine(CommandLine.of(ImmutableList.of("e", "f")))
113 .addCommandLine(
114 CommandLine.of(ImmutableList.of("g", "h")),
115 ParamFileInfo.builder(ParameterFileType.UNQUOTED).setUseAlways(true).build())
tomlu451cfec2018-04-17 06:56:56 -0700116 .build();
117 ExpandedCommandLines expanded = commandLines.expand(artifactExpander, execPath, NO_LIMIT, 0);
118 assertThat(commandLines.allArguments()).containsExactly("a", "b", "c", "d", "e", "f", "g", "h");
tomlu32b27e22018-04-13 00:51:14 -0700119 assertThat(expanded.arguments())
tomlu11f20372018-04-11 06:15:13 -0700120 .containsExactly("a", "b", "@output.txt-0.params", "e", "f", "@output.txt-1.params");
tomlu32b27e22018-04-13 00:51:14 -0700121 assertThat(expanded.getParamFiles()).hasSize(2);
122 assertThat(expanded.getParamFiles().get(0).arguments).containsExactly("c", "d");
123 assertThat(expanded.getParamFiles().get(0).paramFileExecPath.getPathString())
tomlu11f20372018-04-11 06:15:13 -0700124 .isEqualTo("output.txt-0.params");
tomlu32b27e22018-04-13 00:51:14 -0700125 assertThat(expanded.getParamFiles().get(1).arguments).containsExactly("g", "h");
126 assertThat(expanded.getParamFiles().get(1).paramFileExecPath.getPathString())
tomlu11f20372018-04-11 06:15:13 -0700127 .isEqualTo("output.txt-1.params");
128 }
129
130 @Test
131 public void testFirstParamFilePassesButSecondFailsLengthTest() throws Exception {
tomlu451cfec2018-04-17 06:56:56 -0700132 CommandLines commandLines =
tomlu32b27e22018-04-13 00:51:14 -0700133 CommandLines.builder()
tomlu11f20372018-04-11 06:15:13 -0700134 .addCommandLine(
135 CommandLine.of(ImmutableList.of("a", "b")),
136 ParamFileInfo.builder(ParameterFileType.UNQUOTED).setUseAlways(false).build())
137 .addCommandLine(
138 CommandLine.of(ImmutableList.of("c", "d")),
139 ParamFileInfo.builder(ParameterFileType.UNQUOTED).setUseAlways(false).build())
tomlu451cfec2018-04-17 06:56:56 -0700140 .build();
141 ExpandedCommandLines expanded =
142 commandLines.expand(artifactExpander, execPath, new CommandLineLimits(4), 0);
143 assertThat(commandLines.allArguments()).containsExactly("a", "b", "c", "d");
tomlu32b27e22018-04-13 00:51:14 -0700144 assertThat(expanded.arguments()).containsExactly("a", "b", "@output.txt-0.params");
145 assertThat(expanded.getParamFiles()).hasSize(1);
146 assertThat(expanded.getParamFiles().get(0).arguments).containsExactly("c", "d");
tomlu11f20372018-04-11 06:15:13 -0700147 }
tomlu11f20372018-04-11 06:15:13 -0700148}