Simplify the ActionInputFileCache
Add a single getMetadata method (matching MetadataHandler), and rewrite
everything in those terms.
This is in preparation for merging ActionInputFileCache and MetadataHandler.
PiperOrigin-RevId: 161053535
diff --git a/src/test/java/com/google/devtools/build/lib/exec/SingleBuildFileCacheTest.java b/src/test/java/com/google/devtools/build/lib/exec/SingleBuildFileCacheTest.java
index 3087837..f9d0372 100644
--- a/src/test/java/com/google/devtools/build/lib/exec/SingleBuildFileCacheTest.java
+++ b/src/test/java/com/google/devtools/build/lib/exec/SingleBuildFileCacheTest.java
@@ -20,10 +20,10 @@
import com.google.common.io.BaseEncoding;
import com.google.devtools.build.lib.actions.ActionInput;
import com.google.devtools.build.lib.actions.ActionInputHelper;
+import com.google.devtools.build.lib.actions.DigestOfDirectoryException;
import com.google.devtools.build.lib.testutil.Suite;
import com.google.devtools.build.lib.testutil.TestSpec;
import com.google.devtools.build.lib.vfs.FileSystem;
-import com.google.devtools.build.lib.vfs.FileSystem.HashFunction;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
import com.google.protobuf.ByteString;
@@ -78,38 +78,43 @@
}
@Test
- public void testExceptionsCached() throws Exception {
+ public void testNonExistentPath() throws Exception {
ActionInput empty = ActionInputHelper.fromPath("/noexist");
- IOException caught = null;
try {
- underTest.getDigest(empty);
+ underTest.getMetadata(empty);
fail("non existent file should raise exception");
} catch (IOException expected) {
- caught = expected;
}
+ }
+
+ @Test
+ public void testDirectory() throws Exception {
+ Path file = fs.getPath("/directory");
+ file.createDirectory();
+ ActionInput input = ActionInputHelper.fromPath(file.getPathString());
try {
- underTest.getSizeInBytes(empty);
- fail("non existent file should raise exception.");
- } catch (IOException expected) {
- assertThat(expected).isSameAs(caught);
+ underTest.getMetadata(input);
+ fail("directory should raise exception");
+ } catch (DigestOfDirectoryException expected) {
+ assertThat(expected).hasMessageThat().isEqualTo("Input is a directory: /directory");
}
}
@Test
public void testCache() throws Exception {
ActionInput empty = ActionInputHelper.fromPath("/empty");
- underTest.getDigest(empty);
+ underTest.getMetadata(empty).getDigest();
assert(calls.containsKey("/empty"));
assertThat((int) calls.get("/empty")).isEqualTo(1);
- underTest.getDigest(empty);
+ underTest.getMetadata(empty).getDigest();
assertThat((int) calls.get("/empty")).isEqualTo(1);
}
@Test
public void testBasic() throws Exception {
ActionInput empty = ActionInputHelper.fromPath("/empty");
- assertThat(underTest.getSizeInBytes(empty)).isEqualTo(0);
- byte[] digestBytes = underTest.getDigest(empty);
+ assertThat(underTest.getMetadata(empty).getSize()).isEqualTo(0);
+ byte[] digestBytes = underTest.getMetadata(empty).getDigest();
ByteString digest = ByteString.copyFromUtf8(
BaseEncoding.base16().lowerCase().encode(digestBytes));
@@ -133,7 +138,7 @@
Path file = fs.getPath("/unreadable");
file.getOutputStream().close();
file.chmod(0);
- ByteString actualDigest = ByteString.copyFrom(underTest.getDigest(input));
+ ByteString actualDigest = ByteString.copyFrom(underTest.getMetadata(input).getDigest());
assertThat(expectedDigest).isEqualTo(actualDigest);
}
}
diff --git a/src/test/java/com/google/devtools/build/lib/exec/SpawnInputExpanderTest.java b/src/test/java/com/google/devtools/build/lib/exec/SpawnInputExpanderTest.java
index 355e914..d577916 100644
--- a/src/test/java/com/google/devtools/build/lib/exec/SpawnInputExpanderTest.java
+++ b/src/test/java/com/google/devtools/build/lib/exec/SpawnInputExpanderTest.java
@@ -18,7 +18,6 @@
import com.google.common.collect.Maps;
import com.google.devtools.build.lib.actions.ActionInput;
-import com.google.devtools.build.lib.actions.ActionInputFileCache;
import com.google.devtools.build.lib.actions.ActionInputHelper;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.EmptyRunfilesSupplier;
@@ -26,6 +25,8 @@
import com.google.devtools.build.lib.actions.RunfilesSupplier;
import com.google.devtools.build.lib.analysis.Runfiles;
import com.google.devtools.build.lib.analysis.RunfilesSupplierImpl;
+import com.google.devtools.build.lib.exec.util.FakeActionInputFileCache;
+import com.google.devtools.build.lib.skyframe.FileArtifactValue;
import com.google.devtools.build.lib.vfs.FileSystem;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
@@ -38,13 +39,14 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
-import org.mockito.Mockito;
/**
* Tests for {@link SpawnInputExpander}.
*/
@RunWith(JUnit4.class)
public class SpawnInputExpanderTest {
+ private static final byte[] FAKE_DIGEST = new byte[] {1, 2, 3, 4};
+
private FileSystem fs;
private SpawnInputExpander expander;
private Map<PathFragment, ActionInput> inputMappings;
@@ -75,8 +77,8 @@
new Artifact(fs.getPath("/root/dir/file"), Root.asSourceRoot(fs.getPath("/root")));
Runfiles runfiles = new Runfiles.Builder("workspace").addArtifact(artifact).build();
RunfilesSupplier supplier = new RunfilesSupplierImpl(PathFragment.create("runfiles"), runfiles);
- ActionInputFileCache mockCache = Mockito.mock(ActionInputFileCache.class);
- Mockito.when(mockCache.isFile(artifact)).thenReturn(true);
+ FakeActionInputFileCache mockCache = new FakeActionInputFileCache();
+ mockCache.put(artifact, FileArtifactValue.createNormalFile(FAKE_DIGEST, 0));
expander.addRunfilesToInputs(inputMappings, supplier, mockCache);
assertThat(inputMappings).hasSize(1);
@@ -90,8 +92,8 @@
new Artifact(fs.getPath("/root/dir/file"), Root.asSourceRoot(fs.getPath("/root")));
Runfiles runfiles = new Runfiles.Builder("workspace").addArtifact(artifact).build();
RunfilesSupplier supplier = new RunfilesSupplierImpl(PathFragment.create("runfiles"), runfiles);
- ActionInputFileCache mockCache = Mockito.mock(ActionInputFileCache.class);
- Mockito.when(mockCache.isFile(artifact)).thenReturn(false);
+ FakeActionInputFileCache mockCache = new FakeActionInputFileCache();
+ mockCache.put(artifact, FileArtifactValue.createDirectory(-1));
try {
expander.addRunfilesToInputs(inputMappings, supplier, mockCache);
@@ -107,8 +109,8 @@
new Artifact(fs.getPath("/root/dir/file"), Root.asSourceRoot(fs.getPath("/root")));
Runfiles runfiles = new Runfiles.Builder("workspace").addArtifact(artifact).build();
RunfilesSupplier supplier = new RunfilesSupplierImpl(PathFragment.create("runfiles"), runfiles);
- ActionInputFileCache mockCache = Mockito.mock(ActionInputFileCache.class);
- Mockito.when(mockCache.isFile(artifact)).thenReturn(false);
+ FakeActionInputFileCache mockCache = new FakeActionInputFileCache();
+ mockCache.put(artifact, FileArtifactValue.createDirectory(-1));
expander = new SpawnInputExpander(/*strict=*/false);
expander.addRunfilesToInputs(inputMappings, supplier, mockCache);
@@ -128,9 +130,9 @@
.addArtifact(artifact2)
.build();
RunfilesSupplier supplier = new RunfilesSupplierImpl(PathFragment.create("runfiles"), runfiles);
- ActionInputFileCache mockCache = Mockito.mock(ActionInputFileCache.class);
- Mockito.when(mockCache.isFile(artifact1)).thenReturn(true);
- Mockito.when(mockCache.isFile(artifact2)).thenReturn(true);
+ FakeActionInputFileCache mockCache = new FakeActionInputFileCache();
+ mockCache.put(artifact1, FileArtifactValue.createNormalFile(FAKE_DIGEST, 1));
+ mockCache.put(artifact2, FileArtifactValue.createNormalFile(FAKE_DIGEST, 2));
expander.addRunfilesToInputs(inputMappings, supplier, mockCache);
assertThat(inputMappings).hasSize(2);
@@ -147,8 +149,8 @@
Runfiles runfiles = new Runfiles.Builder("workspace")
.addSymlink(PathFragment.create("symlink"), artifact).build();
RunfilesSupplier supplier = new RunfilesSupplierImpl(PathFragment.create("runfiles"), runfiles);
- ActionInputFileCache mockCache = Mockito.mock(ActionInputFileCache.class);
- Mockito.when(mockCache.isFile(artifact)).thenReturn(true);
+ FakeActionInputFileCache mockCache = new FakeActionInputFileCache();
+ mockCache.put(artifact, FileArtifactValue.createNormalFile(FAKE_DIGEST, 1));
expander.addRunfilesToInputs(inputMappings, supplier, mockCache);
assertThat(inputMappings).hasSize(1);
@@ -163,8 +165,8 @@
Runfiles runfiles = new Runfiles.Builder("workspace")
.addRootSymlink(PathFragment.create("symlink"), artifact).build();
RunfilesSupplier supplier = new RunfilesSupplierImpl(PathFragment.create("runfiles"), runfiles);
- ActionInputFileCache mockCache = Mockito.mock(ActionInputFileCache.class);
- Mockito.when(mockCache.isFile(artifact)).thenReturn(true);
+ FakeActionInputFileCache mockCache = new FakeActionInputFileCache();
+ mockCache.put(artifact, FileArtifactValue.createNormalFile(FAKE_DIGEST, 1));
expander.addRunfilesToInputs(inputMappings, supplier, mockCache);
assertThat(inputMappings).hasSize(2);
diff --git a/src/test/java/com/google/devtools/build/lib/exec/util/FakeActionInputFileCache.java b/src/test/java/com/google/devtools/build/lib/exec/util/FakeActionInputFileCache.java
new file mode 100644
index 0000000..ac5f9a4
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/exec/util/FakeActionInputFileCache.java
@@ -0,0 +1,58 @@
+// Copyright 2017 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.util;
+
+import com.google.common.base.Preconditions;
+import com.google.devtools.build.lib.actions.ActionInput;
+import com.google.devtools.build.lib.actions.ActionInputFileCache;
+import com.google.devtools.build.lib.actions.cache.Metadata;
+import com.google.devtools.build.lib.vfs.Path;
+import com.google.protobuf.ByteString;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import javax.annotation.Nullable;
+
+/** A fake implementation of the {@link ActionInputFileCache} interface. */
+public final class FakeActionInputFileCache implements ActionInputFileCache {
+ private final Map<ActionInput, Metadata> inputs = new HashMap<>();
+
+ public FakeActionInputFileCache() {
+ }
+
+ public void put(ActionInput artifact, Metadata metadata) {
+ inputs.put(artifact, metadata);
+ }
+
+ @Override
+ public Metadata getMetadata(ActionInput input) throws IOException {
+ return Preconditions.checkNotNull(inputs.get(input));
+ }
+
+ @Override
+ public boolean contentsAvailableLocally(ByteString digest) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ @Nullable
+ public ActionInput getInputFromDigest(ByteString hexDigest) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Path getInputPath(ActionInput input) {
+ throw new UnsupportedOperationException();
+ }
+}
\ No newline at end of file