tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 1 | // 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. |
| 14 | package com.google.devtools.build.lib.actions; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.devtools.build.lib.actions.Artifact.ArtifactExpander; |
tomlu | 451cfec | 2018-04-17 06:56:56 -0700 | [diff] [blame] | 20 | import com.google.devtools.build.lib.actions.CommandLines.CommandLineLimits; |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame] | 21 | import com.google.devtools.build.lib.actions.CommandLines.ExpandedCommandLines; |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 22 | import com.google.devtools.build.lib.actions.ParameterFile.ParameterFileType; |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 23 | import com.google.devtools.build.lib.vfs.PathFragment; |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 24 | import org.junit.Test; |
| 25 | import org.junit.runner.RunWith; |
| 26 | import org.junit.runners.JUnit4; |
| 27 | |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame] | 28 | /** Tests for {@link CommandLines}. */ |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 29 | @RunWith(JUnit4.class) |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame] | 30 | public class CommandLinesTest { |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 31 | |
| 32 | private final ArtifactExpander artifactExpander = null; |
| 33 | private final PathFragment execPath = PathFragment.create("output.txt"); |
tomlu | 451cfec | 2018-04-17 06:56:56 -0700 | [diff] [blame] | 34 | private static final CommandLineLimits NO_LIMIT = new CommandLineLimits(10000); |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 35 | |
| 36 | @Test |
| 37 | public void testSimpleCommandLine() throws Exception { |
tomlu | 451cfec | 2018-04-17 06:56:56 -0700 | [diff] [blame] | 38 | CommandLines commandLines = |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame] | 39 | CommandLines.builder() |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 40 | .addCommandLine(CommandLine.of(ImmutableList.of("--foo", "--bar"))) |
tomlu | 451cfec | 2018-04-17 06:56:56 -0700 | [diff] [blame] | 41 | .build(); |
| 42 | ExpandedCommandLines expanded = commandLines.expand(artifactExpander, execPath, NO_LIMIT, 0); |
| 43 | assertThat(commandLines.allArguments()).containsExactly("--foo", "--bar"); |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame] | 44 | assertThat(expanded.arguments()).containsExactly("--foo", "--bar"); |
| 45 | assertThat(expanded.getParamFiles()).isEmpty(); |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | @Test |
tomlu | 451cfec | 2018-04-17 06:56:56 -0700 | [diff] [blame] | 49 | public void testFromArguments() throws Exception { |
tomlu | 936139d | 2018-04-25 10:57:31 -0700 | [diff] [blame] | 50 | CommandLines commandLines = CommandLines.of(ImmutableList.of("--foo", "--bar")); |
tomlu | 451cfec | 2018-04-17 06:56:56 -0700 | [diff] [blame] | 51 | ExpandedCommandLines expanded = commandLines.expand(artifactExpander, execPath, NO_LIMIT, 0); |
| 52 | assertThat(commandLines.allArguments()).containsExactly("--foo", "--bar"); |
tomlu | 451cfec | 2018-04-17 06:56:56 -0700 | [diff] [blame] | 53 | 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")), |
tomlu | 936139d | 2018-04-25 10:57:31 -0700 | [diff] [blame] | 62 | CommandLines.of(ImmutableList.of("--foo", "--bar"))); |
tomlu | 451cfec | 2018-04-17 06:56:56 -0700 | [diff] [blame] | 63 | ExpandedCommandLines expanded = commandLines.expand(artifactExpander, execPath, NO_LIMIT, 0); |
| 64 | assertThat(commandLines.allArguments()).containsExactly("--before", "--foo", "--bar"); |
tomlu | 451cfec | 2018-04-17 06:56:56 -0700 | [diff] [blame] | 65 | assertThat(expanded.arguments()).containsExactly("--before", "--foo", "--bar"); |
| 66 | assertThat(expanded.getParamFiles()).isEmpty(); |
| 67 | } |
| 68 | |
| 69 | @Test |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 70 | public void testSimpleParamFileUseAlways() throws Exception { |
tomlu | 451cfec | 2018-04-17 06:56:56 -0700 | [diff] [blame] | 71 | CommandLines commandLines = |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame] | 72 | CommandLines.builder() |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 73 | .addCommandLine( |
| 74 | CommandLine.of(ImmutableList.of("--foo", "--bar")), |
| 75 | ParamFileInfo.builder(ParameterFileType.UNQUOTED).setUseAlways(true).build()) |
tomlu | 451cfec | 2018-04-17 06:56:56 -0700 | [diff] [blame] | 76 | .build(); |
| 77 | ExpandedCommandLines expanded = commandLines.expand(artifactExpander, execPath, NO_LIMIT, 0); |
| 78 | assertThat(commandLines.allArguments()).containsExactly("--foo", "--bar"); |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame] | 79 | assertThat(expanded.arguments()).containsExactly("@output.txt-0.params"); |
| 80 | assertThat(expanded.getParamFiles()).hasSize(1); |
| 81 | assertThat(expanded.getParamFiles().get(0).arguments).containsExactly("--foo", "--bar"); |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void testMaybeUseParamsFiles() throws Exception { |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame] | 86 | CommandLines commandLines = |
| 87 | CommandLines.builder() |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 88 | .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 |
tomlu | 451cfec | 2018-04-17 06:56:56 -0700 | [diff] [blame] | 93 | ExpandedCommandLines expanded = commandLines.expand(artifactExpander, execPath, NO_LIMIT, 0); |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame] | 94 | assertThat(expanded.arguments()).containsExactly("--foo", "--bar"); |
| 95 | assertThat(expanded.getParamFiles()).isEmpty(); |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 96 | |
| 97 | // Set max length to 0, spill to param file is forced |
tomlu | 451cfec | 2018-04-17 06:56:56 -0700 | [diff] [blame] | 98 | expanded = commandLines.expand(artifactExpander, execPath, new CommandLineLimits(0), 0); |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame] | 99 | assertThat(expanded.arguments()).containsExactly("@output.txt-0.params"); |
| 100 | assertThat(expanded.getParamFiles()).hasSize(1); |
| 101 | assertThat(expanded.getParamFiles().get(0).arguments).containsExactly("--foo", "--bar"); |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Test |
| 105 | public void testMixOfCommandLinesAndParamFiles() throws Exception { |
tomlu | 451cfec | 2018-04-17 06:56:56 -0700 | [diff] [blame] | 106 | CommandLines commandLines = |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame] | 107 | CommandLines.builder() |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 108 | .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()) |
tomlu | 451cfec | 2018-04-17 06:56:56 -0700 | [diff] [blame] | 116 | .build(); |
| 117 | ExpandedCommandLines expanded = commandLines.expand(artifactExpander, execPath, NO_LIMIT, 0); |
| 118 | assertThat(commandLines.allArguments()).containsExactly("a", "b", "c", "d", "e", "f", "g", "h"); |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame] | 119 | assertThat(expanded.arguments()) |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 120 | .containsExactly("a", "b", "@output.txt-0.params", "e", "f", "@output.txt-1.params"); |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame] | 121 | assertThat(expanded.getParamFiles()).hasSize(2); |
| 122 | assertThat(expanded.getParamFiles().get(0).arguments).containsExactly("c", "d"); |
| 123 | assertThat(expanded.getParamFiles().get(0).paramFileExecPath.getPathString()) |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 124 | .isEqualTo("output.txt-0.params"); |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame] | 125 | assertThat(expanded.getParamFiles().get(1).arguments).containsExactly("g", "h"); |
| 126 | assertThat(expanded.getParamFiles().get(1).paramFileExecPath.getPathString()) |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 127 | .isEqualTo("output.txt-1.params"); |
| 128 | } |
| 129 | |
| 130 | @Test |
| 131 | public void testFirstParamFilePassesButSecondFailsLengthTest() throws Exception { |
tomlu | 451cfec | 2018-04-17 06:56:56 -0700 | [diff] [blame] | 132 | CommandLines commandLines = |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame] | 133 | CommandLines.builder() |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 134 | .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()) |
tomlu | 451cfec | 2018-04-17 06:56:56 -0700 | [diff] [blame] | 140 | .build(); |
| 141 | ExpandedCommandLines expanded = |
| 142 | commandLines.expand(artifactExpander, execPath, new CommandLineLimits(4), 0); |
| 143 | assertThat(commandLines.allArguments()).containsExactly("a", "b", "c", "d"); |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame] | 144 | 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"); |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 147 | } |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 148 | } |