Skylark: File object can now retrieve dirname/basename.
--
MOS_MIGRATED_REVID=94478925
diff --git a/src/main/java/com/google/devtools/build/lib/actions/Artifact.java b/src/main/java/com/google/devtools/build/lib/actions/Artifact.java
index c39c228..8220bd2 100644
--- a/src/main/java/com/google/devtools/build/lib/actions/Artifact.java
+++ b/src/main/java/com/google/devtools/build/lib/actions/Artifact.java
@@ -208,10 +208,26 @@
return path;
}
+
/**
- * Returns the base file name of this artifact.
+ * Returns the directory name of this artifact, similar to dirname(1).
+ *
+ * <p> The directory name is always a relative path to the execution directory.
+ */
+ @SkylarkCallable(name = "dirname", structField = true,
+ doc = "The directory name of this artifact.")
+ public final String getDirname() {
+ PathFragment parent = getExecPath().getParentDirectory();
+
+ return (parent == null) ? "/" : parent.getSafePathString();
+ }
+
+ /**
+ * Returns the base file name of this artifact, similar to basename(1).
*/
@Override
+ @SkylarkCallable(name = "basename", structField = true,
+ doc = "The base file name of this artifact.")
public final String getFilename() {
return getExecPath().getBaseName();
}
@@ -230,8 +246,8 @@
* for source artifacts if created without specifying the owner, or for special derived artifacts,
* such as target completion middleman artifacts, build info artifacts, and the like.
*
- * When deserializing artifacts we end up with a dummy owner. In that case, it must be set using
- * {@link #setArtifactOwner} before this method is called.
+ * <p>When deserializing artifacts we end up with a dummy owner. In that case,
+ * it must be set using {@link #setArtifactOwner} before this method is called.
*/
public final ArtifactOwner getArtifactOwner() {
Preconditions.checkState(owner != DESERIALIZED_MARKER_OWNER, this);
diff --git a/src/test/java/com/google/devtools/build/lib/actions/ArtifactTest.java b/src/test/java/com/google/devtools/build/lib/actions/ArtifactTest.java
index c4b3f53..d0b4b2b 100644
--- a/src/test/java/com/google/devtools/build/lib/actions/ArtifactTest.java
+++ b/src/test/java/com/google/devtools/build/lib/actions/ArtifactTest.java
@@ -309,4 +309,32 @@
new PathFragment("b/c"),
new LabelArtifactOwner(Label.parseAbsoluteUnchecked("//foo:bar"))).serializeToString());
}
+
+ @Test
+ public void testLongDirname() throws Exception {
+ String dirName = createDirNameArtifact().getDirname();
+
+ assertThat(dirName).isEqualTo("aaa/bbb/ccc");
+ }
+
+ @Test
+ public void testDirnameInExecutionDir() throws Exception {
+ Artifact artifact = new Artifact(scratch.file("/foo/bar.txt"),
+ Root.asDerivedRoot(scratch.dir("/foo")));
+
+ assertThat(artifact.getDirname()).isEqualTo(".");
+ }
+
+ @Test
+ public void testCanConstructPathFromDirAndFilename() throws Exception {
+ Artifact artifact = createDirNameArtifact();
+ String constructed =
+ String.format("%s/%s", artifact.getDirname(), artifact.getFilename());
+
+ assertThat(constructed).isEqualTo("aaa/bbb/ccc/ddd");
+ }
+
+ private Artifact createDirNameArtifact() throws Exception {
+ return new Artifact(scratch.file("/aaa/bbb/ccc/ddd"), Root.asDerivedRoot(scratch.dir("/")));
+ }
}