Symlink creation: pass symlinks to OutputService
This allows implementations of the OutputService to skip reading the
manifest file and instead use the in-memory data.
This is the second attempt for https://github.com/bazelbuild/bazel/commit/83b70caca5952e75489b43cae21837c047a475af including a fix and tests
for null artifacts.
The reason for the breakage is that Python runfiles use null to indicate
that we should create empty files (in addition to the symlinks). However,
this was not caught due to insufficient test coverage for the case of
using an active OutputService.
PiperOrigin-RevId: 282526946
diff --git a/src/test/java/com/google/devtools/build/lib/exec/SymlinkTreeStrategyTest.java b/src/test/java/com/google/devtools/build/lib/exec/SymlinkTreeStrategyTest.java
new file mode 100644
index 0000000..b81ad7a
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/exec/SymlinkTreeStrategyTest.java
@@ -0,0 +1,100 @@
+// Copyright 2019 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.exec;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import com.google.common.collect.ImmutableList;
+import com.google.devtools.build.lib.actions.ActionEnvironment;
+import com.google.devtools.build.lib.actions.ActionExecutionContext;
+import com.google.devtools.build.lib.actions.Artifact;
+import com.google.devtools.build.lib.actions.ArtifactPathResolver;
+import com.google.devtools.build.lib.actions.util.ActionsTestUtil;
+import com.google.devtools.build.lib.analysis.Runfiles;
+import com.google.devtools.build.lib.analysis.actions.SymlinkTreeAction;
+import com.google.devtools.build.lib.analysis.actions.SymlinkTreeActionContext;
+import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
+import com.google.devtools.build.lib.events.StoredEventHandler;
+import com.google.devtools.build.lib.vfs.OutputService;
+import com.google.devtools.build.lib.vfs.Path;
+import com.google.devtools.build.lib.vfs.PathFragment;
+import java.util.Map;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+import org.mockito.ArgumentCaptor;
+
+/** Unit tests for {@link SymlinkTreeStrategy}. */
+@RunWith(JUnit4.class)
+public final class SymlinkTreeStrategyTest extends BuildViewTestCase {
+ @Test
+ public void testArtifactToPathConversion() {
+ Artifact artifact = getBinArtifactWithNoOwner("dir/foo");
+ assertThat(SymlinkTreeStrategy.TO_PATH.apply(artifact)).isEqualTo(artifact.getPath());
+ assertThat(SymlinkTreeStrategy.TO_PATH.apply(null)).isEqualTo(null);
+ }
+
+ @Test
+ public void outputServiceInteraction() throws Exception {
+ ActionExecutionContext context = mock(ActionExecutionContext.class);
+ OutputService outputService = mock(OutputService.class);
+ StoredEventHandler eventHandler = new StoredEventHandler();
+
+ when(context.getContext(SymlinkTreeActionContext.class))
+ .thenReturn(new SymlinkTreeStrategy(outputService, null));
+ when(context.getInputPath(any())).thenAnswer((i) -> ((Artifact) i.getArgument(0)).getPath());
+ when(context.getPathResolver()).thenReturn(ArtifactPathResolver.IDENTITY);
+ when(context.getEventHandler()).thenReturn(eventHandler);
+ when(outputService.canCreateSymlinkTree()).thenReturn(true);
+
+ Artifact inputManifest = getBinArtifactWithNoOwner("dir/manifest.in");
+ Artifact outputManifest = getBinArtifactWithNoOwner("dir/MANIFEST");
+ Artifact runfile = getBinArtifactWithNoOwner("dir/runfile");
+
+ Runfiles runfiles =
+ new Runfiles.Builder("TESTING", false)
+ .setEmptyFilesSupplier((paths) -> ImmutableList.of(PathFragment.create("dir/empty")))
+ .addArtifact(runfile)
+ .build();
+ SymlinkTreeAction action =
+ new SymlinkTreeAction(
+ ActionsTestUtil.NULL_ACTION_OWNER,
+ inputManifest,
+ runfiles,
+ outputManifest,
+ /*filesetTree=*/ false,
+ ActionEnvironment.EMPTY,
+ /*enableRunfiles=*/ true);
+
+ action.execute(context);
+
+ @SuppressWarnings("unchecked")
+ ArgumentCaptor<Map<PathFragment, Path>> capture = ArgumentCaptor.forClass(Map.class);
+ verify(outputService, times(1))
+ .createSymlinkTree(any(), capture.capture(), any(), anyBoolean(), any());
+ assertThat(capture.getValue())
+ .containsExactly(
+ PathFragment.create("TESTING/dir/runfile"),
+ runfile.getPath(),
+ PathFragment.create("TESTING/dir/empty"),
+ null);
+ }
+}