blob: a61ed0ade88ed61a7ac232c57eec67ba80e68b32 [file] [log] [blame]
Ulf Adamsc0a84442017-03-21 10:08:03 +00001// Copyright 2017 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.exec;
15
16import static com.google.common.truth.Truth.assertThat;
ulfjack1973be42018-05-30 03:25:35 -070017import static com.google.devtools.build.lib.exec.FilesetManifest.RelativeSymlinkBehavior.ERROR;
18import static com.google.devtools.build.lib.exec.FilesetManifest.RelativeSymlinkBehavior.IGNORE;
19import static com.google.devtools.build.lib.exec.FilesetManifest.RelativeSymlinkBehavior.RESOLVE;
jcaterfb5ed302019-04-29 13:30:34 -070020import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
Ulf Adamsc0a84442017-03-21 10:08:03 +000021import static org.junit.Assert.fail;
22
ulfjack1973be42018-05-30 03:25:35 -070023import com.google.common.collect.ImmutableList;
24import com.google.common.collect.ImmutableMap;
Ulf Adamsc0a84442017-03-21 10:08:03 +000025import com.google.devtools.build.lib.actions.ActionInput;
Ulf Adamsc0a84442017-03-21 10:08:03 +000026import com.google.devtools.build.lib.actions.ActionInputHelper;
27import com.google.devtools.build.lib.actions.Artifact;
buchgrd4d3d502018-08-02 06:47:19 -070028import com.google.devtools.build.lib.actions.Artifact.ArtifactExpander;
29import com.google.devtools.build.lib.actions.Artifact.SpecialArtifact;
30import com.google.devtools.build.lib.actions.Artifact.SpecialArtifactType;
31import com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact;
32import com.google.devtools.build.lib.actions.ArtifactOwner;
felly56fd4fe2018-09-05 10:54:54 -070033import com.google.devtools.build.lib.actions.ArtifactPathResolver;
tomlu1cdcdf92018-01-16 11:07:51 -080034import com.google.devtools.build.lib.actions.ArtifactRoot;
Ulf Adamsc0a84442017-03-21 10:08:03 +000035import com.google.devtools.build.lib.actions.EmptyRunfilesSupplier;
shahan602cc852018-06-06 20:09:57 -070036import com.google.devtools.build.lib.actions.FileArtifactValue;
ulfjack1973be42018-05-30 03:25:35 -070037import com.google.devtools.build.lib.actions.FilesetOutputSymlink;
Ulf Adamsc0a84442017-03-21 10:08:03 +000038import com.google.devtools.build.lib.actions.RunfilesSupplier;
buchgrd4d3d502018-08-02 06:47:19 -070039import com.google.devtools.build.lib.actions.Spawn;
Ulf Adamsc0a84442017-03-21 10:08:03 +000040import com.google.devtools.build.lib.analysis.Runfiles;
41import com.google.devtools.build.lib.analysis.RunfilesSupplierImpl;
ulfjack3903c922017-07-06 04:30:34 -040042import com.google.devtools.build.lib.exec.util.FakeActionInputFileCache;
buchgrd4d3d502018-08-02 06:47:19 -070043import com.google.devtools.build.lib.exec.util.SpawnBuilder;
Ulf Adamsc0a84442017-03-21 10:08:03 +000044import com.google.devtools.build.lib.vfs.FileSystem;
45import com.google.devtools.build.lib.vfs.FileSystemUtils;
46import com.google.devtools.build.lib.vfs.Path;
47import com.google.devtools.build.lib.vfs.PathFragment;
tomluee6a6862018-01-17 14:36:26 -080048import com.google.devtools.build.lib.vfs.Root;
Ulf Adamsc0a84442017-03-21 10:08:03 +000049import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
50import java.io.IOException;
buchgrd4d3d502018-08-02 06:47:19 -070051import java.util.Arrays;
52import java.util.Collection;
ulfjack1973be42018-05-30 03:25:35 -070053import java.util.HashMap;
Ulf Adamsc0a84442017-03-21 10:08:03 +000054import java.util.Map;
Ulf Adamsc0a84442017-03-21 10:08:03 +000055import org.junit.Test;
56import org.junit.runner.RunWith;
57import org.junit.runners.JUnit4;
Ulf Adamsc0a84442017-03-21 10:08:03 +000058
buchgrd4d3d502018-08-02 06:47:19 -070059/** Tests for {@link SpawnInputExpander}. */
Ulf Adamsc0a84442017-03-21 10:08:03 +000060@RunWith(JUnit4.class)
61public class SpawnInputExpanderTest {
ulfjack3903c922017-07-06 04:30:34 -040062 private static final byte[] FAKE_DIGEST = new byte[] {1, 2, 3, 4};
63
buchgrd4d3d502018-08-02 06:47:19 -070064 private static final ArtifactExpander NO_ARTIFACT_EXPANDER =
65 (a, b) -> fail("expected no interactions");
66
Googler12498fd2018-10-10 10:47:00 -070067 private final FileSystem fs = new InMemoryFileSystem();
68 private final Path execRoot = fs.getPath("/root");
69 private final ArtifactRoot rootDir =
70 ArtifactRoot.asDerivedRoot(execRoot, fs.getPath("/root/out"));
Ulf Adamsc0a84442017-03-21 10:08:03 +000071
Googler12498fd2018-10-10 10:47:00 -070072 private SpawnInputExpander expander = new SpawnInputExpander(execRoot, /*strict=*/ true);
73 private Map<PathFragment, ActionInput> inputMappings = new HashMap<>();
Ulf Adamsc0a84442017-03-21 10:08:03 +000074
75 @Test
76 public void testEmptyRunfiles() throws Exception {
77 RunfilesSupplier supplier = EmptyRunfilesSupplier.INSTANCE;
buchgrd4d3d502018-08-02 06:47:19 -070078 FakeActionInputFileCache mockCache = new FakeActionInputFileCache();
felly56fd4fe2018-09-05 10:54:54 -070079 expander.addRunfilesToInputs(inputMappings, supplier, mockCache, NO_ARTIFACT_EXPANDER,
80 ArtifactPathResolver.IDENTITY, true);
Ulf Adamsc0a84442017-03-21 10:08:03 +000081 assertThat(inputMappings).isEmpty();
82 }
83
84 @Test
85 public void testRunfilesSingleFile() throws Exception {
86 Artifact artifact =
tomluee6a6862018-01-17 14:36:26 -080087 new Artifact(
88 fs.getPath("/root/dir/file"),
89 ArtifactRoot.asSourceRoot(Root.fromPath(fs.getPath("/root"))));
Ulf Adamsc0a84442017-03-21 10:08:03 +000090 Runfiles runfiles = new Runfiles.Builder("workspace").addArtifact(artifact).build();
nharmatab4060b62017-04-04 17:11:39 +000091 RunfilesSupplier supplier = new RunfilesSupplierImpl(PathFragment.create("runfiles"), runfiles);
ulfjack3903c922017-07-06 04:30:34 -040092 FakeActionInputFileCache mockCache = new FakeActionInputFileCache();
janakr39969b12019-03-03 16:25:48 -080093 mockCache.put(
94 artifact,
95 FileArtifactValue.createNormalFile(
96 FAKE_DIGEST, /*proxy=*/ null, 0L, /*isShareable=*/ true));
Ulf Adamsc0a84442017-03-21 10:08:03 +000097
felly56fd4fe2018-09-05 10:54:54 -070098 expander.addRunfilesToInputs(inputMappings, supplier, mockCache, NO_ARTIFACT_EXPANDER,
99 ArtifactPathResolver.IDENTITY, true);
Ulf Adamsc0a84442017-03-21 10:08:03 +0000100 assertThat(inputMappings).hasSize(1);
101 assertThat(inputMappings)
nharmatab4060b62017-04-04 17:11:39 +0000102 .containsEntry(PathFragment.create("runfiles/workspace/dir/file"), artifact);
Ulf Adamsc0a84442017-03-21 10:08:03 +0000103 }
104
105 @Test
buchgrd4d3d502018-08-02 06:47:19 -0700106 public void testRunfilesDirectoryStrict() {
Ulf Adamsc0a84442017-03-21 10:08:03 +0000107 Artifact artifact =
tomluee6a6862018-01-17 14:36:26 -0800108 new Artifact(
109 fs.getPath("/root/dir/file"),
110 ArtifactRoot.asSourceRoot(Root.fromPath(fs.getPath("/root"))));
Ulf Adamsc0a84442017-03-21 10:08:03 +0000111 Runfiles runfiles = new Runfiles.Builder("workspace").addArtifact(artifact).build();
nharmatab4060b62017-04-04 17:11:39 +0000112 RunfilesSupplier supplier = new RunfilesSupplierImpl(PathFragment.create("runfiles"), runfiles);
ulfjack3903c922017-07-06 04:30:34 -0400113 FakeActionInputFileCache mockCache = new FakeActionInputFileCache();
114 mockCache.put(artifact, FileArtifactValue.createDirectory(-1));
Ulf Adamsc0a84442017-03-21 10:08:03 +0000115
jcaterfb5ed302019-04-29 13:30:34 -0700116 IOException expected =
117 assertThrows(
118 IOException.class,
119 () ->
120 expander.addRunfilesToInputs(
121 inputMappings,
122 supplier,
123 mockCache,
124 NO_ARTIFACT_EXPANDER,
125 ArtifactPathResolver.IDENTITY,
126 true));
127 assertThat(expected).hasMessageThat().isEqualTo("Not a file: dir/file");
Ulf Adamsc0a84442017-03-21 10:08:03 +0000128 }
129
130 @Test
131 public void testRunfilesDirectoryNonStrict() throws Exception {
132 Artifact artifact =
tomluee6a6862018-01-17 14:36:26 -0800133 new Artifact(
134 fs.getPath("/root/dir/file"),
135 ArtifactRoot.asSourceRoot(Root.fromPath(fs.getPath("/root"))));
Ulf Adamsc0a84442017-03-21 10:08:03 +0000136 Runfiles runfiles = new Runfiles.Builder("workspace").addArtifact(artifact).build();
nharmatab4060b62017-04-04 17:11:39 +0000137 RunfilesSupplier supplier = new RunfilesSupplierImpl(PathFragment.create("runfiles"), runfiles);
ulfjack3903c922017-07-06 04:30:34 -0400138 FakeActionInputFileCache mockCache = new FakeActionInputFileCache();
139 mockCache.put(artifact, FileArtifactValue.createDirectory(-1));
Ulf Adamsc0a84442017-03-21 10:08:03 +0000140
tomlu1a19b622018-01-11 15:17:28 -0800141 expander = new SpawnInputExpander(execRoot, /*strict=*/ false);
felly56fd4fe2018-09-05 10:54:54 -0700142 expander.addRunfilesToInputs(inputMappings, supplier, mockCache, NO_ARTIFACT_EXPANDER,
143 ArtifactPathResolver.IDENTITY, true);
Ulf Adamsc0a84442017-03-21 10:08:03 +0000144 assertThat(inputMappings).hasSize(1);
145 assertThat(inputMappings)
nharmatab4060b62017-04-04 17:11:39 +0000146 .containsEntry(PathFragment.create("runfiles/workspace/dir/file"), artifact);
Ulf Adamsc0a84442017-03-21 10:08:03 +0000147 }
148
149 @Test
150 public void testRunfilesTwoFiles() throws Exception {
151 Artifact artifact1 =
tomluee6a6862018-01-17 14:36:26 -0800152 new Artifact(
153 fs.getPath("/root/dir/file"),
154 ArtifactRoot.asSourceRoot(Root.fromPath(fs.getPath("/root"))));
Ulf Adamsc0a84442017-03-21 10:08:03 +0000155 Artifact artifact2 =
tomluee6a6862018-01-17 14:36:26 -0800156 new Artifact(
157 fs.getPath("/root/dir/baz"),
158 ArtifactRoot.asSourceRoot(Root.fromPath(fs.getPath("/root"))));
buchgrd4d3d502018-08-02 06:47:19 -0700159 Runfiles runfiles =
160 new Runfiles.Builder("workspace").addArtifact(artifact1).addArtifact(artifact2).build();
nharmatab4060b62017-04-04 17:11:39 +0000161 RunfilesSupplier supplier = new RunfilesSupplierImpl(PathFragment.create("runfiles"), runfiles);
ulfjack3903c922017-07-06 04:30:34 -0400162 FakeActionInputFileCache mockCache = new FakeActionInputFileCache();
janakr39969b12019-03-03 16:25:48 -0800163 mockCache.put(
164 artifact1,
165 FileArtifactValue.createNormalFile(
166 FAKE_DIGEST, /*proxy=*/ null, 1L, /*isShareable=*/ true));
167 mockCache.put(
168 artifact2,
169 FileArtifactValue.createNormalFile(
170 FAKE_DIGEST, /*proxy=*/ null, 12L, /*isShareable=*/ true));
Ulf Adamsc0a84442017-03-21 10:08:03 +0000171
felly56fd4fe2018-09-05 10:54:54 -0700172 expander.addRunfilesToInputs(inputMappings, supplier, mockCache, NO_ARTIFACT_EXPANDER,
173 ArtifactPathResolver.IDENTITY, true);
Ulf Adamsc0a84442017-03-21 10:08:03 +0000174 assertThat(inputMappings).hasSize(2);
175 assertThat(inputMappings)
nharmatab4060b62017-04-04 17:11:39 +0000176 .containsEntry(PathFragment.create("runfiles/workspace/dir/file"), artifact1);
Ulf Adamsc0a84442017-03-21 10:08:03 +0000177 assertThat(inputMappings)
nharmatab4060b62017-04-04 17:11:39 +0000178 .containsEntry(PathFragment.create("runfiles/workspace/dir/baz"), artifact2);
Ulf Adamsc0a84442017-03-21 10:08:03 +0000179 }
180
181 @Test
182 public void testRunfilesSymlink() throws Exception {
183 Artifact artifact =
tomluee6a6862018-01-17 14:36:26 -0800184 new Artifact(
185 fs.getPath("/root/dir/file"),
186 ArtifactRoot.asSourceRoot(Root.fromPath(fs.getPath("/root"))));
buchgrd4d3d502018-08-02 06:47:19 -0700187 Runfiles runfiles =
188 new Runfiles.Builder("workspace")
189 .addSymlink(PathFragment.create("symlink"), artifact)
190 .build();
nharmatab4060b62017-04-04 17:11:39 +0000191 RunfilesSupplier supplier = new RunfilesSupplierImpl(PathFragment.create("runfiles"), runfiles);
ulfjack3903c922017-07-06 04:30:34 -0400192 FakeActionInputFileCache mockCache = new FakeActionInputFileCache();
janakr39969b12019-03-03 16:25:48 -0800193 mockCache.put(
194 artifact,
195 FileArtifactValue.createNormalFile(
196 FAKE_DIGEST, /*proxy=*/ null, 1L, /*isShareable=*/ true));
Ulf Adamsc0a84442017-03-21 10:08:03 +0000197
felly56fd4fe2018-09-05 10:54:54 -0700198 expander.addRunfilesToInputs(inputMappings, supplier, mockCache, NO_ARTIFACT_EXPANDER,
199 ArtifactPathResolver.IDENTITY, true);
Ulf Adamsc0a84442017-03-21 10:08:03 +0000200 assertThat(inputMappings).hasSize(1);
201 assertThat(inputMappings)
nharmatab4060b62017-04-04 17:11:39 +0000202 .containsEntry(PathFragment.create("runfiles/workspace/symlink"), artifact);
Ulf Adamsc0a84442017-03-21 10:08:03 +0000203 }
204
205 @Test
206 public void testRunfilesRootSymlink() throws Exception {
207 Artifact artifact =
tomluee6a6862018-01-17 14:36:26 -0800208 new Artifact(
209 fs.getPath("/root/dir/file"),
210 ArtifactRoot.asSourceRoot(Root.fromPath(fs.getPath("/root"))));
buchgrd4d3d502018-08-02 06:47:19 -0700211 Runfiles runfiles =
212 new Runfiles.Builder("workspace")
213 .addRootSymlink(PathFragment.create("symlink"), artifact)
214 .build();
nharmatab4060b62017-04-04 17:11:39 +0000215 RunfilesSupplier supplier = new RunfilesSupplierImpl(PathFragment.create("runfiles"), runfiles);
ulfjack3903c922017-07-06 04:30:34 -0400216 FakeActionInputFileCache mockCache = new FakeActionInputFileCache();
janakr39969b12019-03-03 16:25:48 -0800217 mockCache.put(
218 artifact,
219 FileArtifactValue.createNormalFile(
220 FAKE_DIGEST, /*proxy=*/ null, 1L, /*isShareable=*/ true));
Ulf Adamsc0a84442017-03-21 10:08:03 +0000221
felly56fd4fe2018-09-05 10:54:54 -0700222 expander.addRunfilesToInputs(inputMappings, supplier, mockCache, NO_ARTIFACT_EXPANDER,
223 ArtifactPathResolver.IDENTITY, true);
Ulf Adamsc0a84442017-03-21 10:08:03 +0000224 assertThat(inputMappings).hasSize(2);
nharmatab4060b62017-04-04 17:11:39 +0000225 assertThat(inputMappings).containsEntry(PathFragment.create("runfiles/symlink"), artifact);
Ulf Adamsc0a84442017-03-21 10:08:03 +0000226 // If there's no other entry, Runfiles adds an empty file in the workspace to make sure the
227 // directory gets created.
228 assertThat(inputMappings)
229 .containsEntry(
nharmatab4060b62017-04-04 17:11:39 +0000230 PathFragment.create("runfiles/workspace/.runfile"), SpawnInputExpander.EMPTY_FILE);
Ulf Adamsc0a84442017-03-21 10:08:03 +0000231 }
232
233 @Test
buchgrd4d3d502018-08-02 06:47:19 -0700234 public void testRunfilesWithTreeArtifacts() throws Exception {
235 SpecialArtifact treeArtifact = createTreeArtifact("treeArtifact");
236 assertThat(treeArtifact.isTreeArtifact()).isTrue();
237 TreeFileArtifact file1 = ActionInputHelper.treeFileArtifact(treeArtifact, "file1");
238 TreeFileArtifact file2 = ActionInputHelper.treeFileArtifact(treeArtifact, "file2");
239 FileSystemUtils.writeContentAsLatin1(file1.getPath(), "foo");
240 FileSystemUtils.writeContentAsLatin1(file2.getPath(), "bar");
241
242 Runfiles runfiles = new Runfiles.Builder("workspace").addArtifact(treeArtifact).build();
243 ArtifactExpander artifactExpander =
244 (Artifact artifact, Collection<? super Artifact> output) -> {
245 if (artifact.equals(treeArtifact)) {
246 output.addAll(Arrays.asList(file1, file2));
247 }
248 };
249 RunfilesSupplier supplier = new RunfilesSupplierImpl(PathFragment.create("runfiles"), runfiles);
250 FakeActionInputFileCache fakeCache = new FakeActionInputFileCache();
251 fakeCache.put(file1, FileArtifactValue.create(file1));
252 fakeCache.put(file2, FileArtifactValue.create(file2));
253
felly56fd4fe2018-09-05 10:54:54 -0700254 expander.addRunfilesToInputs(inputMappings, supplier, fakeCache, artifactExpander,
255 ArtifactPathResolver.IDENTITY, true);
buchgrd4d3d502018-08-02 06:47:19 -0700256 assertThat(inputMappings).hasSize(2);
257 assertThat(inputMappings)
258 .containsEntry(PathFragment.create("runfiles/workspace/treeArtifact/file1"), file1);
259 assertThat(inputMappings)
260 .containsEntry(PathFragment.create("runfiles/workspace/treeArtifact/file2"), file2);
261 }
262
263 @Test
264 public void testRunfilesWithTreeArtifactsInSymlinks() throws Exception {
265 SpecialArtifact treeArtifact = createTreeArtifact("treeArtifact");
266 assertThat(treeArtifact.isTreeArtifact()).isTrue();
267 TreeFileArtifact file1 = ActionInputHelper.treeFileArtifact(treeArtifact, "file1");
268 TreeFileArtifact file2 = ActionInputHelper.treeFileArtifact(treeArtifact, "file2");
269 FileSystemUtils.writeContentAsLatin1(file1.getPath(), "foo");
270 FileSystemUtils.writeContentAsLatin1(file2.getPath(), "bar");
271 Runfiles runfiles =
272 new Runfiles.Builder("workspace")
273 .addSymlink(PathFragment.create("symlink"), treeArtifact)
274 .build();
275
276 ArtifactExpander artifactExpander =
277 (Artifact artifact, Collection<? super Artifact> output) -> {
278 if (artifact.equals(treeArtifact)) {
279 output.addAll(Arrays.asList(file1, file2));
280 }
281 };
282 RunfilesSupplier supplier = new RunfilesSupplierImpl(PathFragment.create("runfiles"), runfiles);
283 FakeActionInputFileCache fakeCache = new FakeActionInputFileCache();
284 fakeCache.put(file1, FileArtifactValue.create(file1));
285 fakeCache.put(file2, FileArtifactValue.create(file2));
286
felly56fd4fe2018-09-05 10:54:54 -0700287 expander.addRunfilesToInputs(inputMappings, supplier, fakeCache, artifactExpander,
288 ArtifactPathResolver.IDENTITY, true);
buchgrd4d3d502018-08-02 06:47:19 -0700289 assertThat(inputMappings).hasSize(2);
290 assertThat(inputMappings)
291 .containsEntry(PathFragment.create("runfiles/workspace/symlink/file1"), file1);
292 assertThat(inputMappings)
293 .containsEntry(PathFragment.create("runfiles/workspace/symlink/file2"), file2);
294 }
295
296 @Test
297 public void testTreeArtifactsInInputs() throws Exception {
298 SpecialArtifact treeArtifact = createTreeArtifact("treeArtifact");
299 assertThat(treeArtifact.isTreeArtifact()).isTrue();
300 TreeFileArtifact file1 = ActionInputHelper.treeFileArtifact(treeArtifact, "file1");
301 TreeFileArtifact file2 = ActionInputHelper.treeFileArtifact(treeArtifact, "file2");
302 FileSystemUtils.writeContentAsLatin1(file1.getPath(), "foo");
303 FileSystemUtils.writeContentAsLatin1(file2.getPath(), "bar");
304
305 ArtifactExpander artifactExpander =
306 (Artifact artifact, Collection<? super Artifact> output) -> {
307 if (artifact.equals(treeArtifact)) {
308 output.addAll(Arrays.asList(file1, file2));
309 }
310 };
311 FakeActionInputFileCache fakeCache = new FakeActionInputFileCache();
312 fakeCache.put(file1, FileArtifactValue.create(file1));
313 fakeCache.put(file2, FileArtifactValue.create(file2));
314
315 Spawn spawn = new SpawnBuilder("/bin/echo", "Hello World").withInput(treeArtifact).build();
felly56fd4fe2018-09-05 10:54:54 -0700316 inputMappings = expander.getInputMapping(spawn, artifactExpander, ArtifactPathResolver.IDENTITY,
317 fakeCache, true);
buchgrd4d3d502018-08-02 06:47:19 -0700318 assertThat(inputMappings).hasSize(2);
319 assertThat(inputMappings).containsEntry(PathFragment.create("treeArtifact/file1"), file1);
320 assertThat(inputMappings).containsEntry(PathFragment.create("treeArtifact/file2"), file2);
321 }
322
323 private SpecialArtifact createTreeArtifact(String relPath) throws IOException {
324 Path outputDir = fs.getPath("/root");
325 Path outputPath = execRoot.getRelative(relPath);
326 outputPath.createDirectoryAndParents();
327 ArtifactRoot derivedRoot = ArtifactRoot.asSourceRoot(Root.fromPath(outputDir));
328 return new SpecialArtifact(
329 derivedRoot,
330 derivedRoot.getExecPath().getRelative(derivedRoot.getRoot().relativize(outputPath)),
331 ArtifactOwner.NullArtifactOwner.INSTANCE,
332 SpecialArtifactType.TREE);
333 }
334
335 @Test
Ulf Adamsc0a84442017-03-21 10:08:03 +0000336 public void testEmptyManifest() throws Exception {
Googler12498fd2018-10-10 10:47:00 -0700337 Map<Artifact, ImmutableList<FilesetOutputSymlink>> filesetMappings =
338 ImmutableMap.of(createFileset("out"), ImmutableList.of());
Ulf Adamsc0a84442017-03-21 10:08:03 +0000339
Googler12498fd2018-10-10 10:47:00 -0700340 expander.addFilesetManifests(filesetMappings, inputMappings);
341
Ulf Adamsc0a84442017-03-21 10:08:03 +0000342 assertThat(inputMappings).isEmpty();
343 }
344
345 @Test
346 public void testManifestWithSingleFile() throws Exception {
Googler12498fd2018-10-10 10:47:00 -0700347 Map<Artifact, ImmutableList<FilesetOutputSymlink>> filesetMappings =
348 ImmutableMap.of(
349 createFileset("out"), ImmutableList.of(filesetSymlink("foo/bar", "/dir/file")));
Ulf Adamsc0a84442017-03-21 10:08:03 +0000350
Googler12498fd2018-10-10 10:47:00 -0700351 expander.addFilesetManifests(filesetMappings, inputMappings);
352
Ulf Adamsc0a84442017-03-21 10:08:03 +0000353 assertThat(inputMappings)
Googler12498fd2018-10-10 10:47:00 -0700354 .containsExactly(
355 PathFragment.create("out/foo/bar"), ActionInputHelper.fromPath("/dir/file"));
Ulf Adamsc0a84442017-03-21 10:08:03 +0000356 }
357
358 @Test
359 public void testManifestWithTwoFiles() throws Exception {
Googler12498fd2018-10-10 10:47:00 -0700360 Map<Artifact, ImmutableList<FilesetOutputSymlink>> filesetMappings =
361 ImmutableMap.of(
362 createFileset("out"),
363 ImmutableList.of(
364 filesetSymlink("foo/bar", "/dir/file"), filesetSymlink("foo/baz", "/dir/file")));
Ulf Adamsc0a84442017-03-21 10:08:03 +0000365
Googler12498fd2018-10-10 10:47:00 -0700366 expander.addFilesetManifests(filesetMappings, inputMappings);
367
Ulf Adamsc0a84442017-03-21 10:08:03 +0000368 assertThat(inputMappings)
Googler12498fd2018-10-10 10:47:00 -0700369 .containsExactly(
370 PathFragment.create("out/foo/bar"), ActionInputHelper.fromPath("/dir/file"),
371 PathFragment.create("out/foo/baz"), ActionInputHelper.fromPath("/dir/file"));
Ulf Adamsc0a84442017-03-21 10:08:03 +0000372 }
373
374 @Test
375 public void testManifestWithDirectory() throws Exception {
Googler12498fd2018-10-10 10:47:00 -0700376 Map<Artifact, ImmutableList<FilesetOutputSymlink>> filesetMappings =
377 ImmutableMap.of(createFileset("out"), ImmutableList.of(filesetSymlink("foo/bar", "/some")));
Ulf Adamsc0a84442017-03-21 10:08:03 +0000378
Googler12498fd2018-10-10 10:47:00 -0700379 expander.addFilesetManifests(filesetMappings, inputMappings);
380
Ulf Adamsc0a84442017-03-21 10:08:03 +0000381 assertThat(inputMappings)
Googler12498fd2018-10-10 10:47:00 -0700382 .containsExactly(PathFragment.create("out/foo/bar"), ActionInputHelper.fromPath("/some"));
Ulf Adamsc0a84442017-03-21 10:08:03 +0000383 }
ulfjack1973be42018-05-30 03:25:35 -0700384
Googler18b08442018-10-04 09:55:06 -0700385 private static FilesetOutputSymlink filesetSymlink(String from, String to) {
kush4b120e72018-07-11 16:21:27 -0700386 return FilesetOutputSymlink.createForTesting(
Googler18b08442018-10-04 09:55:06 -0700387 PathFragment.create(from), PathFragment.create(to), PathFragment.create("/root"));
ulfjack1973be42018-05-30 03:25:35 -0700388 }
389
tomlu73eccc22018-09-06 08:02:37 -0700390 private ImmutableMap<Artifact, ImmutableList<FilesetOutputSymlink>> simpleFilesetManifest() {
ulfjack1973be42018-05-30 03:25:35 -0700391 return ImmutableMap.of(
tomlu73eccc22018-09-06 08:02:37 -0700392 createFileset("out"),
ulfjack1973be42018-05-30 03:25:35 -0700393 ImmutableList.of(
Googler18b08442018-10-04 09:55:06 -0700394 filesetSymlink("workspace/bar", "foo"), filesetSymlink("workspace/foo", "/root/bar")));
ulfjack1973be42018-05-30 03:25:35 -0700395 }
396
tomlu73eccc22018-09-06 08:02:37 -0700397 private SpecialArtifact createFileset(String execPath) {
398 return new SpecialArtifact(
399 rootDir,
400 PathFragment.create(execPath),
401 ArtifactOwner.NullArtifactOwner.INSTANCE,
402 SpecialArtifactType.FILESET);
403 }
404
ulfjack1973be42018-05-30 03:25:35 -0700405 @Test
406 public void testManifestWithErrorOnRelativeSymlink() throws Exception {
407 expander = new SpawnInputExpander(execRoot, /*strict=*/ true, ERROR);
jcaterfb5ed302019-04-29 13:30:34 -0700408 IOException e =
409 assertThrows(
410 IOException.class,
411 () -> expander.addFilesetManifests(simpleFilesetManifest(), inputMappings));
412 assertThat(e).hasMessageThat().contains("runfiles target is not absolute: foo");
ulfjack1973be42018-05-30 03:25:35 -0700413 }
414
415 @Test
416 public void testManifestWithIgnoredRelativeSymlink() throws Exception {
417 expander = new SpawnInputExpander(execRoot, /*strict=*/ true, IGNORE);
Googler12498fd2018-10-10 10:47:00 -0700418 expander.addFilesetManifests(simpleFilesetManifest(), inputMappings);
419 assertThat(inputMappings)
ulfjack1973be42018-05-30 03:25:35 -0700420 .containsExactly(
Googler18b08442018-10-04 09:55:06 -0700421 PathFragment.create("out/workspace/foo"), ActionInputHelper.fromPath("/root/bar"));
ulfjack1973be42018-05-30 03:25:35 -0700422 }
423
424 @Test
425 public void testManifestWithResolvedRelativeSymlink() throws Exception {
426 expander = new SpawnInputExpander(execRoot, /*strict=*/ true, RESOLVE);
Googler12498fd2018-10-10 10:47:00 -0700427 expander.addFilesetManifests(simpleFilesetManifest(), inputMappings);
428 assertThat(inputMappings)
ulfjack1973be42018-05-30 03:25:35 -0700429 .containsExactly(
Googler18b08442018-10-04 09:55:06 -0700430 PathFragment.create("out/workspace/bar"), ActionInputHelper.fromPath("/root/bar"),
431 PathFragment.create("out/workspace/foo"), ActionInputHelper.fromPath("/root/bar"));
ulfjack1973be42018-05-30 03:25:35 -0700432 }
Ulf Adamsc0a84442017-03-21 10:08:03 +0000433}