Skip to content

Commit

Permalink
Move StringActionInput to ActionsTestUtil.
Browse files Browse the repository at this point in the history
Move the test-only implementation of `VirtualActionInput` to other helpers in
`ActionsTestUtil` so it can be easily reused by tests outside of `remote`
package. Make the class anonymous.

PiperOrigin-RevId: 340551613
  • Loading branch information
alexjski authored and copybara-github committed Nov 4, 2020
1 parent 5c8e584 commit 1db3a70
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.Streams.stream;
import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
Expand Down Expand Up @@ -58,6 +59,7 @@
import com.google.devtools.build.lib.actions.cache.MetadataHandler;
import com.google.devtools.build.lib.actions.cache.Protos.ActionCacheStatistics.MissDetail;
import com.google.devtools.build.lib.actions.cache.Protos.ActionCacheStatistics.MissReason;
import com.google.devtools.build.lib.actions.cache.VirtualActionInput;
import com.google.devtools.build.lib.analysis.actions.CustomCommandLine;
import com.google.devtools.build.lib.analysis.actions.SpawnActionTemplate;
import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget;
Expand Down Expand Up @@ -96,7 +98,9 @@
import com.google.devtools.build.skyframe.SkyKey;
import com.google.devtools.build.skyframe.SkyValue;
import com.google.devtools.build.skyframe.ValueOrUntypedException;
import com.google.protobuf.ByteString;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -261,6 +265,45 @@ public static ArtifactRoot createArtifactRootFromTwoPaths(Path root, Path execPa
root, execPath.relativeTo(root).getSegments().toArray(new String[0]));
}

/**
* Creates a {@link VirtualActionInput} with given string as contents and provided relative path.
*/
public static VirtualActionInput createVirtualActionInput(String relativePath, String contents) {
return createVirtualActionInput(PathFragment.create(relativePath), contents);
}

/** Creates a {@link VirtualActionInput} with given string as contents and provided path. */
public static VirtualActionInput createVirtualActionInput(PathFragment path, String contents) {
return new VirtualActionInput() {
@Override
public ByteString getBytes() throws IOException {
ByteString.Output out = ByteString.newOutput();
writeTo(out);
return out.toByteString();
}

@Override
public String getExecPathString() {
return path.getPathString();
}

@Override
public PathFragment getExecPath() {
return path;
}

@Override
public boolean isSymlink() {
return false;
}

@Override
public void writeTo(OutputStream out) throws IOException {
out.write(contents.getBytes(UTF_8));
}
};
}

/**
* {@link SkyFunction.Environment} that internally makes a full Skyframe evaluate call for the
* requested keys, blocking until the values are ready.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import com.google.common.util.concurrent.MoreExecutors;
import com.google.devtools.build.lib.actions.ActionInputHelper;
import com.google.devtools.build.lib.actions.cache.VirtualActionInput;
import com.google.devtools.build.lib.actions.util.ActionsTestUtil;
import com.google.devtools.build.lib.authandtls.AuthAndTLSOptions;
import com.google.devtools.build.lib.authandtls.CallCredentialsProvider;
import com.google.devtools.build.lib.authandtls.GoogleAuthUtils;
Expand All @@ -64,7 +65,6 @@
import com.google.devtools.build.lib.remote.merkletree.MerkleTree;
import com.google.devtools.build.lib.remote.options.RemoteOptions;
import com.google.devtools.build.lib.remote.util.DigestUtil;
import com.google.devtools.build.lib.remote.util.StringActionInput;
import com.google.devtools.build.lib.remote.util.TestUtils;
import com.google.devtools.build.lib.remote.util.TracingMetadataUtils;
import com.google.devtools.build.lib.testutil.Scratch;
Expand Down Expand Up @@ -254,7 +254,8 @@ public void testVirtualActionInputSupport() throws Exception {
RemoteExecutionCache client =
new RemoteExecutionCache(newClient(options), options, DIGEST_UTIL);
PathFragment execPath = PathFragment.create("my/exec/path");
VirtualActionInput virtualActionInput = new StringActionInput("hello", execPath);
VirtualActionInput virtualActionInput =
ActionsTestUtil.createVirtualActionInput(execPath, "hello");
MerkleTree merkleTree =
MerkleTree.build(
ImmutableSortedMap.of(execPath, virtualActionInput),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@
import com.google.devtools.build.lib.remote.util.DigestUtil;
import com.google.devtools.build.lib.remote.util.InMemoryCacheClient;
import com.google.devtools.build.lib.remote.util.StaticMetadataProvider;
import com.google.devtools.build.lib.remote.util.StringActionInput;
import com.google.devtools.build.lib.vfs.DigestHashFunction;
import com.google.devtools.build.lib.vfs.FileSystem;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
import com.google.devtools.common.options.Options;
import com.google.protobuf.ByteString;
Expand Down Expand Up @@ -109,7 +107,7 @@ public void testStagingVirtualActionInput() throws Exception {
RemoteCache remoteCache = newCache(options, digestUtil, new HashMap<>());
RemoteActionInputFetcher actionInputFetcher =
new RemoteActionInputFetcher(remoteCache, execRoot, RequestMetadata.getDefaultInstance());
VirtualActionInput a = new StringActionInput("hello world", PathFragment.create("file1"));
VirtualActionInput a = ActionsTestUtil.createVirtualActionInput("file1", "hello world");

// act
actionInputFetcher.prefetchFiles(ImmutableList.of(a), metadataProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.google.devtools.build.lib.actions.util.ActionsTestUtil;
import com.google.devtools.build.lib.remote.merkletree.DirectoryTree.FileNode;
import com.google.devtools.build.lib.remote.util.StaticMetadataProvider;
import com.google.devtools.build.lib.remote.util.StringActionInput;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.PathFragment;
Expand Down Expand Up @@ -133,8 +132,9 @@ public void directoryInputShouldBeExpanded() throws Exception {

private static VirtualActionInput addVirtualFile(
String path, String content, SortedMap<PathFragment, ActionInput> sortedInputs) {
VirtualActionInput input = new StringActionInput(content, PathFragment.create(path));
sortedInputs.put(PathFragment.create(path), input);
PathFragment pathFragment = PathFragment.create(path);
VirtualActionInput input = ActionsTestUtil.createVirtualActionInput(pathFragment, content);
sortedInputs.put(pathFragment, input);
return input;
}

Expand Down

This file was deleted.

0 comments on commit 1db3a70

Please sign in to comment.