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 | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame^] | 20 | import com.google.devtools.build.lib.actions.CommandLines.ExpandedCommandLines; |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 21 | import com.google.devtools.build.lib.actions.ParameterFile.ParameterFileType; |
| 22 | import com.google.devtools.build.lib.vfs.FileSystemUtils; |
| 23 | import com.google.devtools.build.lib.vfs.Path; |
| 24 | import com.google.devtools.build.lib.vfs.PathFragment; |
| 25 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
| 26 | import java.nio.charset.StandardCharsets; |
| 27 | import org.junit.Test; |
| 28 | import org.junit.runner.RunWith; |
| 29 | import org.junit.runners.JUnit4; |
| 30 | |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame^] | 31 | /** Tests for {@link CommandLines}. */ |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 32 | @RunWith(JUnit4.class) |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame^] | 33 | public class CommandLinesTest { |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 34 | |
| 35 | private final ArtifactExpander artifactExpander = null; |
| 36 | private final PathFragment execPath = PathFragment.create("output.txt"); |
| 37 | |
| 38 | @Test |
| 39 | public void testSimpleCommandLine() throws Exception { |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame^] | 40 | ExpandedCommandLines expanded = |
| 41 | CommandLines.builder() |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 42 | .addCommandLine(CommandLine.of(ImmutableList.of("--foo", "--bar"))) |
| 43 | .build() |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame^] | 44 | .expand(artifactExpander, execPath, 1024, 0); |
| 45 | assertThat(expanded.arguments()).containsExactly("--foo", "--bar"); |
| 46 | assertThat(expanded.getParamFiles()).isEmpty(); |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | @Test |
| 50 | public void testSimpleParamFileUseAlways() throws Exception { |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame^] | 51 | ExpandedCommandLines expanded = |
| 52 | CommandLines.builder() |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 53 | .addCommandLine( |
| 54 | CommandLine.of(ImmutableList.of("--foo", "--bar")), |
| 55 | ParamFileInfo.builder(ParameterFileType.UNQUOTED).setUseAlways(true).build()) |
| 56 | .build() |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame^] | 57 | .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"); |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Test |
| 64 | public void testMaybeUseParamsFiles() throws Exception { |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame^] | 65 | CommandLines commandLines = |
| 66 | CommandLines.builder() |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 67 | .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 |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame^] | 72 | ExpandedCommandLines expanded = commandLines.expand(artifactExpander, execPath, 1024, 0); |
| 73 | assertThat(expanded.arguments()).containsExactly("--foo", "--bar"); |
| 74 | assertThat(expanded.getParamFiles()).isEmpty(); |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 75 | |
| 76 | // Set max length to 0, spill to param file is forced |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame^] | 77 | 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"); |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Test |
| 84 | public void testMixOfCommandLinesAndParamFiles() throws Exception { |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame^] | 85 | ExpandedCommandLines expanded = |
| 86 | CommandLines.builder() |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 87 | .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() |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame^] | 96 | .expand(artifactExpander, execPath, 1024, 0); |
| 97 | assertThat(expanded.arguments()) |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 98 | .containsExactly("a", "b", "@output.txt-0.params", "e", "f", "@output.txt-1.params"); |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame^] | 99 | assertThat(expanded.getParamFiles()).hasSize(2); |
| 100 | assertThat(expanded.getParamFiles().get(0).arguments).containsExactly("c", "d"); |
| 101 | assertThat(expanded.getParamFiles().get(0).paramFileExecPath.getPathString()) |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 102 | .isEqualTo("output.txt-0.params"); |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame^] | 103 | assertThat(expanded.getParamFiles().get(1).arguments).containsExactly("g", "h"); |
| 104 | assertThat(expanded.getParamFiles().get(1).paramFileExecPath.getPathString()) |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 105 | .isEqualTo("output.txt-1.params"); |
| 106 | } |
| 107 | |
| 108 | @Test |
| 109 | public void testFirstParamFilePassesButSecondFailsLengthTest() throws Exception { |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame^] | 110 | ExpandedCommandLines expanded = |
| 111 | CommandLines.builder() |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 112 | .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() |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame^] | 119 | .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"); |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Test |
| 126 | public void testWriteParamFiles() throws Exception { |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame^] | 127 | CommandLines commandLines = |
| 128 | CommandLines.builder() |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 129 | .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(); |
tomlu | 32b27e2 | 2018-04-13 00:51:14 -0700 | [diff] [blame^] | 139 | ExpandedCommandLines expanded = |
| 140 | commandLines.expand(artifactExpander, PathFragment.create("my/param/file/out"), 0, 0); |
| 141 | expanded.writeParamFiles(execRoot); |
tomlu | 11f2037 | 2018-04-11 06:15:13 -0700 | [diff] [blame] | 142 | |
| 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 | } |