Skip to content

Commit

Permalink
Add an ActionInputMap#getTreeMetadataForPrefix method to retrieve the…
Browse files Browse the repository at this point in the history
… metadata for a path's containing tree artifact.

This will be required to implement filesystem operations over tree artifacts in RemoteActionFileSystem.

PiperOrigin-RevId: 551796268
Change-Id: I1a5ba0cee817c2124742bd4cb387e16bd1d1ac9d
  • Loading branch information
tjgq authored and copybara-github committed Jul 28, 2023
1 parent 1f0f667 commit 9d449ab
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,16 @@ public TreeArtifactValue getTreeMetadata(PathFragment execPath) {
return ((TrieArtifact) value).treeArtifactValue;
}

/**
* Returns the {@link TreeArtifactValue} for the shortest prefix of the given path, possibly the
* path itself, that corresponds to a tree artifact; or {@code null} if no such tree artifact
* exists.
*/
@Nullable
public TreeArtifactValue getTreeMetadataForPrefix(PathFragment execPath) {
return treeArtifactsRoot.findTreeArtifactNodeAtPrefix(execPath);
}

@Nullable
@Override
public ActionInput getInput(String execPathString) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,90 @@ public void getInputMetadata_treeFileUnderFile_fails() {
assertThrows(IllegalArgumentException.class, () -> map.getInputMetadata(child));
}

@Test
public void getTreeMetadataForPrefix_nonTree() {
ActionInput file = ActionInputHelper.fromPath("some/file");
map.put(file, TestMetadata.create(1), /* depOwner= */ null);

assertThat(map.getTreeMetadataForPrefix(file.getExecPath())).isNull();
assertThat(map.getTreeMetadataForPrefix(file.getExecPath().getParentDirectory())).isNull();
assertThat(map.getTreeMetadataForPrefix(file.getExecPath().getChild("under"))).isNull();
}

@Test
public void getTreeMetadataForPrefix_emptyTree() {
SpecialArtifact tree = createTreeArtifact("a/tree");
TreeArtifactValue treeValue = TreeArtifactValue.newBuilder(tree).build();
map.putTreeArtifact(tree, treeValue, /* depOwner= */ null);

assertThat(map.getTreeMetadataForPrefix(tree.getExecPath().getParentDirectory())).isNull();
assertThat(map.getTreeMetadataForPrefix(tree.getExecPath())).isEqualTo(treeValue);
assertThat(map.getTreeMetadataForPrefix(tree.getExecPath().getChild("under")))
.isEqualTo(treeValue);
}

@Test
public void getTreeMetadataForPrefix_nonEmptyTree() {
SpecialArtifact tree = createTreeArtifact("a/tree");
TreeFileArtifact child = TreeFileArtifact.createTreeOutput(tree, "some/child");
TreeArtifactValue treeValue =
TreeArtifactValue.newBuilder(tree).putChild(child, TestMetadata.create(1)).build();
map.putTreeArtifact(tree, treeValue, /* depOwner= */ null);

assertThat(map.getTreeMetadataForPrefix(tree.getExecPath().getParentDirectory())).isNull();
assertThat(map.getTreeMetadataForPrefix(tree.getExecPath())).isEqualTo(treeValue);
assertThat(map.getTreeMetadataForPrefix(child.getExecPath())).isEqualTo(treeValue);
assertThat(map.getTreeMetadataForPrefix(child.getExecPath().getParentDirectory()))
.isEqualTo(treeValue);
assertThat(map.getTreeMetadataForPrefix(child.getExecPath().getChild("under")))
.isEqualTo(treeValue);
}

@Test
public void getTreeMetadataForPrefix_treeWithNestedFile() {
SpecialArtifact tree = createTreeArtifact("a/tree");
TreeFileArtifact child = TreeFileArtifact.createTreeOutput(tree, "some/dir/child");
TreeArtifactValue treeValue =
TreeArtifactValue.newBuilder(tree).putChild(child, TestMetadata.create(1)).build();
map.putTreeArtifact(tree, treeValue, /* depOwner= */ null);
map.put(
ActionInputHelper.fromPath(child.getExecPath()),
TestMetadata.create(1),
/* depOwner= */ null);

assertThat(map.getTreeMetadataForPrefix(tree.getExecPath().getParentDirectory())).isNull();
assertThat(map.getTreeMetadataForPrefix(tree.getExecPath())).isEqualTo(treeValue);
assertThat(map.getTreeMetadataForPrefix(child.getExecPath())).isEqualTo(treeValue);
assertThat(map.getTreeMetadataForPrefix(child.getExecPath().getParentDirectory()))
.isEqualTo(treeValue);
assertThat(map.getTreeMetadataForPrefix(child.getExecPath().getChild("under")))
.isEqualTo(treeValue);
}

@Test
public void getTreeMetadataForPrefix_treeWithNestedTree() {
SpecialArtifact tree = createTreeArtifact("a/tree");
TreeFileArtifact child = TreeFileArtifact.createTreeOutput(tree, "some/dir/child");
TreeArtifactValue treeValue =
TreeArtifactValue.newBuilder(tree).putChild(child, TestMetadata.create(1)).build();
map.putTreeArtifact(tree, treeValue, /* depOwner= */ null);
SpecialArtifact nestedTree = createTreeArtifact("a/tree/some/dir");
TreeFileArtifact nestedChild = TreeFileArtifact.createTreeOutput(nestedTree, "child");
TreeArtifactValue nestedTreeValue =
TreeArtifactValue.newBuilder(nestedTree)
.putChild(nestedChild, TestMetadata.create(1))
.build();
map.putTreeArtifact(nestedTree, nestedTreeValue, /* depOwner= */ null);

assertThat(map.getTreeMetadataForPrefix(tree.getExecPath().getParentDirectory())).isNull();
assertThat(map.getTreeMetadataForPrefix(tree.getExecPath())).isEqualTo(treeValue);
assertThat(map.getTreeMetadataForPrefix(child.getExecPath())).isEqualTo(treeValue);
assertThat(map.getTreeMetadataForPrefix(child.getExecPath().getParentDirectory()))
.isEqualTo(treeValue);
assertThat(map.getTreeMetadataForPrefix(child.getExecPath().getChild("under")))
.isEqualTo(treeValue);
}

@Test
public void getters_missingTree_returnNull() {
map.putTreeArtifact(createTreeArtifact("tree"), TreeArtifactValue.empty(), /*depOwner=*/ null);
Expand Down

0 comments on commit 9d449ab

Please sign in to comment.