diff --git a/tools/java/runfiles/Runfiles.java b/tools/java/runfiles/Runfiles.java
index 81fa0b37e3481f..b704847b5a784e 100644
--- a/tools/java/runfiles/Runfiles.java
+++ b/tools/java/runfiles/Runfiles.java
@@ -78,67 +78,84 @@
* Process p = pb.start();
*
*/
-public abstract class Runfiles {
+public final class Runfiles {
- private static final String MAIN_REPOSITORY_NAME = "";
+ public abstract static class Preloaded {
- /**
- * See {@link com.google.devtools.build.lib.analysis.RepoMappingManifestAction.Entry}.
- */
- private static class RepoMappingKey {
+ /**
+ * See {@link com.google.devtools.build.lib.analysis.RepoMappingManifestAction.Entry}.
+ */
+ static class RepoMappingKey {
- public final String sourceRepo;
- public final String targetRepoApparentName;
+ public final String sourceRepo;
+ public final String targetRepoApparentName;
- public RepoMappingKey(String sourceRepo, String targetRepoApparentName) {
- this.sourceRepo = sourceRepo;
- this.targetRepoApparentName = targetRepoApparentName;
- }
+ public RepoMappingKey(String sourceRepo, String targetRepoApparentName) {
+ this.sourceRepo = sourceRepo;
+ this.targetRepoApparentName = targetRepoApparentName;
+ }
- @Override
- public boolean equals(Object o) {
- if (this == o) {
- return true;
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ RepoMappingKey that = (RepoMappingKey) o;
+ return sourceRepo.equals(that.sourceRepo) && targetRepoApparentName.equals(
+ that.targetRepoApparentName);
}
- if (o == null || getClass() != o.getClass()) {
- return false;
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(sourceRepo, targetRepoApparentName);
}
- RepoMappingKey that = (RepoMappingKey) o;
- return sourceRepo.equals(that.sourceRepo) && targetRepoApparentName.equals(
- that.targetRepoApparentName);
}
- @Override
- public int hashCode() {
- return Objects.hash(sourceRepo, targetRepoApparentName);
+ /**
+ * Returns a new instance that uses the provided source repository as a default for all calls to
+ * {@link #rlocation(String)}.
+ *
+ *
This is useful when receiving a {@link Runfiles} instance from a different Bazel
+ * repository. In this case, while the runfiles manifest or directory encoded in the instance
+ * should be used for runfiles lookups, the repository from which apparent repository names
+ * should be resolved needs to change.
+ *
+ * @param sourceRepository the canonical name of the Bazel repository relative to which apparent
+ * repository names should be resolved
+ * @return a new {@link Runfiles} instance identical to this one, except that calls to
+ * {@link #rlocation(String)} use the provided source repository.
+ */
+ public final Runfiles withSourceRepository(String sourceRepository) {
+ Util.checkArgument(sourceRepository != null);
+ return new Runfiles(this, sourceRepository);
}
- }
- private final Map repoMapping;
- private final String sourceRepository;
+ public final Runfiles unmapped() {
+ return new Runfiles(this, null);
+ }
- // Package-private constructor, so only package-private classes may extend it.
- private Runfiles(String repoMappingPath, String sourceRepository) throws IOException {
- this.repoMapping = loadRepositoryMapping(repoMappingPath);
- this.sourceRepository = sourceRepository;
- }
+ protected abstract Map getEnvVars();
- protected Runfiles(Runfiles runfiles, String sourceRepository) {
- this.repoMapping = runfiles.repoMapping;
- this.sourceRepository = sourceRepository;
+ protected abstract String rlocationChecked(String path);
+
+ protected abstract Map getRepoMapping();
+
+ // Private constructor, so only nested classes may extend it.
+ private Preloaded() {
+ }
}
- /**
- * Returns a new {@link Runfiles} instance.
- *
- * Deprecated: With {@code --enable_bzlmod}, this function can only resolve runfiles
- * correctly if called from the main repository. Use {@link #create(String)} instead.
- *
- *
This method passes the JVM's environment variable map to {@link #create(Map)}.
- */
- @Deprecated
- public static Runfiles create() throws IOException {
- return create(System.getenv());
+ private static final String MAIN_REPOSITORY = "";
+
+ private final Preloaded preloadedRunfiles;
+ private final String sourceRepository;
+
+ private Runfiles(Preloaded preloadedRunfiles, String sourceRepository) {
+ this.preloadedRunfiles = preloadedRunfiles;
+ this.sourceRepository = sourceRepository;
}
/**
@@ -150,17 +167,13 @@ public static Runfiles create() throws IOException {
* lookups should be performed. This can be obtained using
* {@link AutoBazelRepository} (see class documentation).
*/
- public static Runfiles create(String sourceRepository) throws IOException {
- return create(System.getenv(), sourceRepository);
+ public static Preloaded preload() throws IOException {
+ return preload(System.getenv());
}
/**
* Returns a new {@link Runfiles} instance.
*
- *
Deprecated: With {@code --enable_bzlmod}, this function can only resolve runfiles
- * correctly if called from the main repository. Use {@link #create(Map, String)} instead.
- *
- *
*
The returned object is either:
*
*
@@ -179,18 +192,46 @@ public static Runfiles create(String sourceRepository) throws IOException {
* Note about performance: the manifest-based implementation eagerly reads and caches the whole
* manifest file upon instantiation.
*
+ * @param sourceRepository the canonical name of the Bazel repository relative to which apparent
+ * repository names in runfiles paths should be resolved. This can be
+ * obtained using {@link AutoBazelRepository} (see class documentation).
+ *
* @throws IOException if RUNFILES_MANIFEST_ONLY=1 is in {@code env} but there's no
* "RUNFILES_MANIFEST_FILE", "RUNFILES_DIR", or "JAVA_RUNFILES" key in
* {@code env} or their values are empty, or some IO error occurs
*/
+ public static Preloaded preload(Map env) throws IOException {
+ if (isManifestOnly(env)) {
+ // On Windows, Bazel sets RUNFILES_MANIFEST_ONLY=1.
+ // On every platform, Bazel also sets RUNFILES_MANIFEST_FILE, but on Linux and macOS it's
+ // faster to use RUNFILES_DIR.
+ return new ManifestBased(getManifestPath(env));
+ } else {
+ return new DirectoryBased(getRunfilesDir(env));
+ }
+ }
+
+ /**
+ * Returns a new {@link Runfiles} instance.
+ *
+ * Deprecated: With {@code --enable_bzlmod}, this function can only resolve runfiles
+ * correctly if called from the main repository. Use {@link #preload()}}
+ * instead.
+ *
+ *
This method passes the JVM's environment variable map to {@link #create(Map)}.
+ */
@Deprecated
- public static Runfiles create(Map env) throws IOException {
- return create(env, MAIN_REPOSITORY_NAME);
+ public static Runfiles create() throws IOException {
+ return preload().withSourceRepository(MAIN_REPOSITORY);
}
/**
* Returns a new {@link Runfiles} instance.
*
+ * Deprecated: With {@code --enable_bzlmod}, this function can only resolve runfiles
+ * correctly if called from the main repository. Use {@link #preload(Map)} instead.
+ *
+ *
*
The returned object is either:
*
*
@@ -209,42 +250,15 @@ public static Runfiles create(Map env) throws IOException {
* Note about performance: the manifest-based implementation eagerly reads and caches the whole
* manifest file upon instantiation.
*
- * @param sourceRepository the canonical name of the Bazel repository relative to which apparent
- * repository names in runfiles paths should be resolved. This can be
- * obtained using {@link AutoBazelRepository} (see class documentation).
- *
* @throws IOException if RUNFILES_MANIFEST_ONLY=1 is in {@code env} but there's no
* "RUNFILES_MANIFEST_FILE", "RUNFILES_DIR", or "JAVA_RUNFILES" key in
* {@code env} or their values are empty, or some IO error occurs
*/
- public static Runfiles create(Map env, String sourceRepository)
- throws IOException {
- if (isManifestOnly(env)) {
- // On Windows, Bazel sets RUNFILES_MANIFEST_ONLY=1.
- // On every platform, Bazel also sets RUNFILES_MANIFEST_FILE, but on Linux and macOS it's
- // faster to use RUNFILES_DIR.
- return new ManifestBased(getManifestPath(env), sourceRepository);
- } else {
- return new DirectoryBased(getRunfilesDir(env), sourceRepository);
- }
+ @Deprecated
+ public static Runfiles create(Map env) throws IOException {
+ return preload(env).withSourceRepository(MAIN_REPOSITORY);
}
- /**
- * Returns a new instance that uses the provided source repository as a default for all calls to
- * {@link #rlocation(String)}.
- *
- * This is useful when receiving a {@link Runfiles} instance from a different Bazel
- * repository. In this case, while the runfiles manifest or directory encoded in the instance
- * should be used for runfiles lookups, the repository from which apparent repository names should
- * be resolved needs to change.
- *
- * @param sourceRepository the canonical name of the Bazel repository relative to which apparent
- * repository names should be resolved
- * @return a new {@link Runfiles} instance identical to this one, except that calls to
- * {@link #rlocation(String)} use the provided source repository.
- */
- public abstract Runfiles withSourceRepository(String sourceRepository);
-
/**
* Returns the runtime path of a runfile (a Bazel-built binary's/test's data-dependency).
*
@@ -258,27 +272,7 @@ public static Runfiles create(Map env, String sourceRepository)
* @throws IllegalArgumentException if {@code path} fails validation, for example if it's null or
* empty, or not normalized (contains "./", "../", or "//")
*/
- public final String rlocation(String path) {
- return rlocation(path, this.sourceRepository);
- }
-
- /**
- * Returns the runtime path of a runfile (a Bazel-built binary's/test's data-dependency).
- *
- * The returned path may not be valid. The caller should check the path's validity and that
- * the path exists.
- *
- *
The function may return null. In that case the caller can be sure that the rule does not
- * know about this data-dependency.
- *
- * @param path runfiles-root-relative path of the runfile
- * @param sourceRepository the canonical name of the Bazel repository relative to which apparent
- * repository names in runfiles paths should be resolved. Overrides the
- * repository set when creating this {@link Runfiles} instance.
- * @throws IllegalArgumentException if {@code path} fails validation, for example if it's null or
- * empty, or not normalized (contains "./", "../", or "//")
- */
- public final String rlocation(String path, String sourceRepository) {
+ public String rlocation(String path) {
Util.checkArgument(path != null);
Util.checkArgument(!path.isEmpty());
Util.checkArgument(
@@ -296,14 +290,18 @@ public final String rlocation(String path, String sourceRepository) {
return path;
}
+ if (sourceRepository == null) {
+ return preloadedRunfiles.rlocationChecked(path);
+ }
String[] apparentTargetAndRemainder = path.split("/", 2);
if (apparentTargetAndRemainder.length < 2) {
- return rlocationChecked(path);
+ return preloadedRunfiles.rlocationChecked(path);
}
- String targetCanonical = repoMapping.getOrDefault(
- new RepoMappingKey(sourceRepository, apparentTargetAndRemainder[0]),
+ String targetCanonical = preloadedRunfiles.getRepoMapping().getOrDefault(
+ new Preloaded.RepoMappingKey(sourceRepository, apparentTargetAndRemainder[0]),
apparentTargetAndRemainder[0]);
- return rlocationChecked(targetCanonical + "/" + apparentTargetAndRemainder[1]);
+ return preloadedRunfiles.rlocationChecked(
+ targetCanonical + "/" + apparentTargetAndRemainder[1]);
}
/**
@@ -312,7 +310,9 @@ public final String rlocation(String path, String sourceRepository) {
*
The caller should add the returned key-value pairs to the environment of subprocesses in
* case those subprocesses are also Bazel-built binaries that need to use runfiles.
*/
- public abstract Map getEnvVars();
+ public Map getEnvVars() {
+ return preloadedRunfiles.getEnvVars();
+ }
/**
* Returns true if the platform supports runfiles only via manifests.
@@ -343,7 +343,7 @@ private static String getRunfilesDir(Map env) throws IOException
return value;
}
- private static Map loadRepositoryMapping(String path)
+ private static Map loadRepositoryMapping(String path)
throws IOException {
if (path == null) {
return Collections.emptyMap();
@@ -361,38 +361,62 @@ private static Map loadRepositoryMapping(String path)
return split;
})
.collect(Collectors.toMap(
- split -> new RepoMappingKey(split[0], split[1]),
+ split -> new Preloaded.RepoMappingKey(split[0], split[1]),
split -> split[2])));
}
}
- abstract String rlocationChecked(String path);
-
/**
* {@link Runfiles} implementation that parses a runfiles-manifest file to look up runfiles.
*/
- private static final class ManifestBased extends Runfiles {
+ private static final class ManifestBased extends Runfiles.Preloaded {
private final Map runfiles;
private final String manifestPath;
+ private final Map repoMapping;
- ManifestBased(String manifestPath, String sourceRepository) throws IOException {
- super(findRepositoryMapping(manifestPath), sourceRepository);
+ ManifestBased(String manifestPath) throws IOException {
Util.checkArgument(manifestPath != null);
Util.checkArgument(!manifestPath.isEmpty());
this.manifestPath = manifestPath;
this.runfiles = loadRunfiles(manifestPath);
+ this.repoMapping = loadRepositoryMapping(findRepositoryMapping(manifestPath));
+ }
+
+ @Override
+ protected String rlocationChecked(String path) {
+ String exactMatch = runfiles.get(path);
+ if (exactMatch != null) {
+ return exactMatch;
+ }
+ // If path references a runfile that lies under a directory that itself is a runfile, then
+ // only the directory is listed in the manifest. Look up all prefixes of path in the manifest
+ // and append the relative path from the prefix if there is a match.
+ int prefixEnd = path.length();
+ while ((prefixEnd = path.lastIndexOf('/', prefixEnd - 1)) != -1) {
+ String prefixMatch = runfiles.get(path.substring(0, prefixEnd));
+ if (prefixMatch != null) {
+ return prefixMatch + '/' + path.substring(prefixEnd + 1);
+ }
+ }
+ return null;
}
- private ManifestBased(ManifestBased existing, String sourceRepository) {
- super(existing, sourceRepository);
- this.manifestPath = existing.manifestPath;
- this.runfiles = existing.runfiles;
+ @Override
+ protected Map getEnvVars() {
+ HashMap result = new HashMap<>(4);
+ result.put("RUNFILES_MANIFEST_ONLY", "1");
+ result.put("RUNFILES_MANIFEST_FILE", manifestPath);
+ String runfilesDir = findRunfilesDir(manifestPath);
+ result.put("RUNFILES_DIR", runfilesDir);
+ // TODO(laszlocsomor): remove JAVA_RUNFILES once the Java launcher can pick up RUNFILES_DIR.
+ result.put("JAVA_RUNFILES", runfilesDir);
+ return result;
}
@Override
- public Runfiles withSourceRepository(String sourceRepository) {
- return new ManifestBased(this, sourceRepository);
+ protected Map getRepoMapping() {
+ return repoMapping;
}
private static Map loadRunfiles(String path) throws IOException {
@@ -434,61 +458,40 @@ private static String findRepositoryMapping(String manifestPath) {
}
return null;
}
-
- @Override
- public String rlocationChecked(String path) {
- String exactMatch = runfiles.get(path);
- if (exactMatch != null) {
- return exactMatch;
- }
- // If path references a runfile that lies under a directory that itself is a runfile, then
- // only the directory is listed in the manifest. Look up all prefixes of path in the manifest
- // and append the relative path from the prefix if there is a match.
- int prefixEnd = path.length();
- while ((prefixEnd = path.lastIndexOf('/', prefixEnd - 1)) != -1) {
- String prefixMatch = runfiles.get(path.substring(0, prefixEnd));
- if (prefixMatch != null) {
- return prefixMatch + '/' + path.substring(prefixEnd + 1);
- }
- }
- return null;
- }
-
- @Override
- public Map getEnvVars() {
- HashMap result = new HashMap<>(4);
- result.put("RUNFILES_MANIFEST_ONLY", "1");
- result.put("RUNFILES_MANIFEST_FILE", manifestPath);
- String runfilesDir = findRunfilesDir(manifestPath);
- result.put("RUNFILES_DIR", runfilesDir);
- // TODO(laszlocsomor): remove JAVA_RUNFILES once the Java launcher can pick up RUNFILES_DIR.
- result.put("JAVA_RUNFILES", runfilesDir);
- return result;
- }
}
/**
* {@link Runfiles} implementation that appends runfiles paths to the runfiles root.
*/
- private static final class DirectoryBased extends Runfiles {
+ private static final class DirectoryBased extends Preloaded {
private final String runfilesRoot;
+ private final Map repoMapping;
- DirectoryBased(String runfilesDir, String sourceRepository) throws IOException {
- super(findRepositoryMapping(runfilesDir), sourceRepository);
+ DirectoryBased(String runfilesDir) throws IOException {
Util.checkArgument(!Util.isNullOrEmpty(runfilesDir));
Util.checkArgument(new File(runfilesDir).isDirectory());
this.runfilesRoot = runfilesDir;
+ this.repoMapping = loadRepositoryMapping(findRepositoryMapping(runfilesRoot));
}
- private DirectoryBased(DirectoryBased existing, String sourceRepository) {
- super(existing, sourceRepository);
- this.runfilesRoot = existing.runfilesRoot;
+ @Override
+ protected String rlocationChecked(String path) {
+ return runfilesRoot + "/" + path;
}
@Override
- public Runfiles withSourceRepository(String sourceRepository) {
- return new DirectoryBased(this, sourceRepository);
+ protected Map getRepoMapping() {
+ return repoMapping;
+ }
+
+ @Override
+ protected Map getEnvVars() {
+ HashMap result = new HashMap<>(2);
+ result.put("RUNFILES_DIR", runfilesRoot);
+ // TODO(laszlocsomor): remove JAVA_RUNFILES once the Java launcher can pick up RUNFILES_DIR.
+ result.put("JAVA_RUNFILES", runfilesRoot);
+ return result;
}
private static String findRepositoryMapping(String runfilesRoot) {
@@ -500,29 +503,13 @@ private static String findRepositoryMapping(String runfilesRoot) {
}
return null;
}
-
- @Override
- String rlocationChecked(String path) {
- return runfilesRoot + "/" + path;
- }
-
- @Override
- public Map getEnvVars() {
- HashMap result = new HashMap<>(2);
- result.put("RUNFILES_DIR", runfilesRoot);
- // TODO(laszlocsomor): remove JAVA_RUNFILES once the Java launcher can pick up RUNFILES_DIR.
- result.put("JAVA_RUNFILES", runfilesRoot);
- return result;
- }
}
- static Runfiles createManifestBasedForTesting(String manifestPath, String sourceRepository)
- throws IOException {
- return new ManifestBased(manifestPath, sourceRepository);
+ static Preloaded createManifestBasedForTesting(String manifestPath) throws IOException {
+ return new ManifestBased(manifestPath);
}
- static Runfiles createDirectoryBasedForTesting(String runfilesDir, String sourceRepository)
- throws IOException {
- return new DirectoryBased(runfilesDir, sourceRepository);
+ static Preloaded createDirectoryBasedForTesting(String runfilesDir) throws IOException {
+ return new DirectoryBased(runfilesDir);
}
}
diff --git a/tools/java/runfiles/testing/RunfilesTest.java b/tools/java/runfiles/testing/RunfilesTest.java
index 10cb1d644c2061..c645efbb1ee8ee 100644
--- a/tools/java/runfiles/testing/RunfilesTest.java
+++ b/tools/java/runfiles/testing/RunfilesTest.java
@@ -233,7 +233,7 @@ public void testDirectoryBasedRlocation() throws IOException {
// exists.
File dir = new File(System.getenv("TEST_TMPDIR"), "mock/runfiles");
assertThat(dir.mkdirs()).isTrue();
- Runfiles r = Runfiles.createDirectoryBasedForTesting(dir.toString(), "");
+ Runfiles r = Runfiles.createDirectoryBasedForTesting(dir.toString()).withSourceRepository("");
// Escaping for "\": once for string and once for regex.
assertThat(r.rlocation("arg")).matches(".*[/\\\\]mock[/\\\\]runfiles[/\\\\]arg");
}
@@ -244,7 +244,7 @@ public void testManifestBasedRlocation() throws Exception {
"Foo/runfile1 C:/Actual Path\\runfile1",
"Foo/Bar/runfile2 D:\\the path\\run file 2.txt",
"Foo/Bar/Dir E:\\Actual Path\\Directory"));
- Runfiles r = Runfiles.createManifestBasedForTesting(mf.toString(), "");
+ Runfiles r = Runfiles.createManifestBasedForTesting(mf.toString()).withSourceRepository("");
assertThat(r.rlocation("Foo/runfile1")).isEqualTo("C:/Actual Path\\runfile1");
assertThat(r.rlocation("Foo/Bar/runfile2")).isEqualTo("D:\\the path\\run file 2.txt");
assertThat(r.rlocation("Foo/Bar/Dir")).isEqualTo("E:\\Actual Path\\Directory");
@@ -266,7 +266,7 @@ public void testManifestBasedRlocationWithRepoMapping_fromMain() throws Exceptio
",my_protobuf,protobuf~3.19.2",
",my_workspace,_main",
"protobuf~3.19.2,protobuf,protobuf~3.19.2"));
- Runfiles r = Runfiles.createManifestBasedForTesting(mf.toString(), "");
+ Runfiles r = Runfiles.createManifestBasedForTesting(mf.toString()).withSourceRepository("");
assertThat(r.rlocation("my_module/bar/runfile")).isEqualTo(
"/the/path/./to/other//other runfile.txt");
@@ -302,8 +302,8 @@ public void testManifestBasedRlocationWithRepoMapping_fromMain() throws Exceptio
}
@Test
- public void testManifestBasedRlocationWithRepoMapping_fromOtherRepo() throws Exception {
- Path mf = tempFile("foo.runfiles/MANIFEST", ImmutableList.of(
+ public void testManifestBasedRlocationUnmapped() throws Exception {
+ Path mf = tempFile("foo.runfiles_manifest", ImmutableList.of(
"config.json /etc/config.json",
"protobuf~3.19.2/foo/runfile C:/Actual Path\\protobuf\\runfile",
"_main/bar/runfile /the/path/./to/other//other runfile.txt",
@@ -313,19 +313,19 @@ public void testManifestBasedRlocationWithRepoMapping_fromOtherRepo() throws Exc
",my_protobuf,protobuf~3.19.2",
",my_workspace,_main",
"protobuf~3.19.2,protobuf,protobuf~3.19.2"));
- Runfiles r = Runfiles.createManifestBasedForTesting(mf.toString(), "protobuf~3.19.2");
-
- assertThat(r.rlocation("protobuf/foo/runfile")).isEqualTo("C:/Actual Path\\protobuf\\runfile");
- assertThat(r.rlocation("protobuf/bar/dir")).isEqualTo("E:\\Actual Path\\Directory");
- assertThat(r.rlocation("protobuf/bar/dir/file")).isEqualTo("E:\\Actual Path\\Directory/file");
- assertThat(r.rlocation("protobuf/bar/dir/de eply/nes ted/fi~le")).isEqualTo(
- "E:\\Actual Path\\Directory/de eply/nes ted/fi~le");
+ Runfiles r = Runfiles.createManifestBasedForTesting(mf.toString()).unmapped();
assertThat(r.rlocation("my_module/bar/runfile")).isNull();
+ assertThat(r.rlocation("my_workspace/bar/runfile")).isNull();
assertThat(r.rlocation("my_protobuf/foo/runfile")).isNull();
assertThat(r.rlocation("my_protobuf/bar/dir")).isNull();
assertThat(r.rlocation("my_protobuf/bar/dir/file")).isNull();
- assertThat(r.rlocation("my_protobuf/bar/dir/de eply/nes ted/fi~le")).isNull();
+ assertThat(r.rlocation("my_protobuf/bar/dir/de eply/nes ted/fi~le")).isNull();
+
+ assertThat(r.rlocation("protobuf/foo/runfile")).isNull();
+ assertThat(r.rlocation("protobuf/bar/dir")).isNull();
+ assertThat(r.rlocation("protobuf/bar/dir/file")).isNull();
+ assertThat(r.rlocation("protobuf/bar/dir/dir/de eply/nes ted/fi~le")).isNull();
assertThat(r.rlocation("_main/bar/runfile")).isEqualTo(
"/the/path/./to/other//other runfile.txt");
@@ -344,8 +344,7 @@ public void testManifestBasedRlocationWithRepoMapping_fromOtherRepo() throws Exc
}
@Test
- public void testManifestBasedRlocationWithRepoMapping_fromOtherRepo_withSourceRepository()
- throws Exception {
+ public void testManifestBasedRlocationWithRepoMapping_fromOtherRepo() throws Exception {
Path mf = tempFile("foo.runfiles/MANIFEST", ImmutableList.of(
"config.json /etc/config.json",
"protobuf~3.19.2/foo/runfile C:/Actual Path\\protobuf\\runfile",
@@ -356,7 +355,7 @@ public void testManifestBasedRlocationWithRepoMapping_fromOtherRepo_withSourceRe
",my_protobuf,protobuf~3.19.2",
",my_workspace,_main",
"protobuf~3.19.2,protobuf,protobuf~3.19.2"));
- Runfiles r = Runfiles.createManifestBasedForTesting(mf.toString(), "")
+ Runfiles r = Runfiles.createManifestBasedForTesting(mf.toString())
.withSourceRepository("protobuf~3.19.2");
assertThat(r.rlocation("protobuf/foo/runfile")).isEqualTo("C:/Actual Path\\protobuf\\runfile");
@@ -395,7 +394,7 @@ public void testDirectoryBasedRlocationWithRepoMapping_fromMain() throws Excepti
",my_protobuf,protobuf~3.19.2",
",my_workspace,_main",
"protobuf~3.19.2,protobuf,protobuf~3.19.2"));
- Runfiles r = Runfiles.createDirectoryBasedForTesting(dir.toString(), "");
+ Runfiles r = Runfiles.createDirectoryBasedForTesting(dir.toString()).withSourceRepository("");
assertThat(r.rlocation("my_module/bar/runfile")).isEqualTo(
dir + "/_main/bar/runfile");
@@ -427,25 +426,30 @@ public void testDirectoryBasedRlocationWithRepoMapping_fromMain() throws Excepti
}
@Test
- public void testDirectoryBasedRlocationWithRepoMapping_fromOtherRepo() throws Exception {
+ public void testDirectoryBasedRlocationUnmapped() throws Exception {
Path dir = tempDir.newFolder("foo.runfiles").toPath();
- Path rm = tempFile(dir.getParent().resolve("foo.repo_mapping").toString(), ImmutableList.of(
+ tempFile(dir.getParent().resolve("foo.repo_mapping").toString(), ImmutableList.of(
",my_module,_main",
",my_protobuf,protobuf~3.19.2",
",my_workspace,_main",
"protobuf~3.19.2,protobuf,protobuf~3.19.2"));
- Runfiles r = Runfiles.createDirectoryBasedForTesting(dir.toString(), "protobuf~3.19.2");
+ Runfiles r = Runfiles.createDirectoryBasedForTesting(dir.toString()).unmapped();
- assertThat(r.rlocation("protobuf/foo/runfile")).isEqualTo(dir + "/protobuf~3.19.2/foo/runfile");
- assertThat(r.rlocation("protobuf/bar/dir")).isEqualTo(dir + "/protobuf~3.19.2/bar/dir");
- assertThat(r.rlocation("protobuf/bar/dir/file")).isEqualTo(
- dir + "/protobuf~3.19.2/bar/dir/file");
- assertThat(r.rlocation("protobuf/bar/dir/de eply/nes ted/fi~le")).isEqualTo(
- dir + "/protobuf~3.19.2/bar/dir/de eply/nes ted/fi~le");
+ assertThat(r.rlocation("my_module/bar/runfile")).isEqualTo(
+ dir + "/my_module/bar/runfile");
+ assertThat(r.rlocation("my_workspace/bar/runfile")).isEqualTo(
+ dir + "/my_workspace/bar/runfile");
+ assertThat(r.rlocation("my_protobuf/foo/runfile")).isEqualTo(
+ dir + "/my_protobuf/foo/runfile");
+ assertThat(r.rlocation("my_protobuf/bar/dir")).isEqualTo(dir + "/my_protobuf/bar/dir");
+ assertThat(r.rlocation("my_protobuf/bar/dir/file")).isEqualTo(
+ dir + "/my_protobuf/bar/dir/file");
+ assertThat(r.rlocation("my_protobuf/bar/dir/de eply/nes ted/fi~le")).isEqualTo(
+ dir + "/my_protobuf/bar/dir/de eply/nes ted/fi~le");
- assertThat(r.rlocation("my_module/bar/runfile")).isEqualTo(dir + "/my_module/bar/runfile");
- assertThat(r.rlocation("my_protobuf/bar/dir/de eply/nes ted/fi~le")).isEqualTo(
- dir + "/my_protobuf/bar/dir/de eply/nes ted/fi~le");
+ assertThat(r.rlocation("protobuf/foo/runfile")).isEqualTo(dir + "/protobuf/foo/runfile");
+ assertThat(r.rlocation("protobuf/bar/dir/dir/de eply/nes ted/fi~le")).isEqualTo(
+ dir + "/protobuf/bar/dir/dir/de eply/nes ted/fi~le");
assertThat(r.rlocation("_main/bar/runfile")).isEqualTo(
dir + "/_main/bar/runfile");
@@ -461,15 +465,14 @@ public void testDirectoryBasedRlocationWithRepoMapping_fromOtherRepo() throws Ex
}
@Test
- public void testDirectoryBasedRlocationWithRepoMapping_fromOtherRepo_withSourceRepository()
- throws Exception {
+ public void testDirectoryBasedRlocationWithRepoMapping_fromOtherRepo() throws Exception {
Path dir = tempDir.newFolder("foo.runfiles").toPath();
Path rm = tempFile(dir.getParent().resolve("foo.repo_mapping").toString(), ImmutableList.of(
",my_module,_main",
",my_protobuf,protobuf~3.19.2",
",my_workspace,_main",
"protobuf~3.19.2,protobuf,protobuf~3.19.2"));
- Runfiles r = Runfiles.createDirectoryBasedForTesting(dir.toString(), "")
+ Runfiles r = Runfiles.createDirectoryBasedForTesting(dir.toString())
.withSourceRepository("protobuf~3.19.2");
assertThat(r.rlocation("protobuf/foo/runfile")).isEqualTo(dir + "/protobuf~3.19.2/foo/runfile");
@@ -498,29 +501,31 @@ public void testDirectoryBasedRlocationWithRepoMapping_fromOtherRepo_withSourceR
@Test
public void testDirectoryBasedCtorArgumentValidation() throws IOException {
- assertThrows(
- IllegalArgumentException.class, () -> Runfiles.createDirectoryBasedForTesting(null, ""));
+ assertThrows(IllegalArgumentException.class,
+ () -> Runfiles.createDirectoryBasedForTesting(null).withSourceRepository(""));
- assertThrows(IllegalArgumentException.class, () -> Runfiles.createDirectoryBasedForTesting("",
- ""));
+ assertThrows(IllegalArgumentException.class,
+ () -> Runfiles.createDirectoryBasedForTesting("").withSourceRepository(""));
assertThrows(
IllegalArgumentException.class,
- () -> Runfiles.createDirectoryBasedForTesting("non-existent directory is bad", ""));
+ () -> Runfiles.createDirectoryBasedForTesting("non-existent directory is bad")
+ .withSourceRepository(""));
- Runfiles.createDirectoryBasedForTesting(System.getenv("TEST_TMPDIR"), "");
+ Runfiles.createDirectoryBasedForTesting(System.getenv("TEST_TMPDIR")).withSourceRepository("");
}
@Test
public void testManifestBasedCtorArgumentValidation() throws Exception {
assertThrows(
- IllegalArgumentException.class, () -> Runfiles.createManifestBasedForTesting(null, ""));
+ IllegalArgumentException.class,
+ () -> Runfiles.createManifestBasedForTesting(null).withSourceRepository(""));
- assertThrows(IllegalArgumentException.class, () -> Runfiles.createManifestBasedForTesting("",
- ""));
+ assertThrows(IllegalArgumentException.class,
+ () -> Runfiles.createManifestBasedForTesting("").withSourceRepository(""));
Path mf = tempFile("foobar", ImmutableList.of("a b"));
- Runfiles.createManifestBasedForTesting(mf.toString(), "");
+ Runfiles.createManifestBasedForTesting(mf.toString()).withSourceRepository("");
}
@Test
@@ -528,7 +533,7 @@ public void testInvalidRepoMapping() throws Exception {
Path mf = tempFile("foo.runfiles/MANIFEST", ImmutableList.of());
tempFile("foo.repo_mapping", ImmutableList.of("a,b,c,d"));
assertThrows(IllegalArgumentException.class,
- () -> Runfiles.createManifestBasedForTesting(mf.toString(), ""));
+ () -> Runfiles.createManifestBasedForTesting(mf.toString()).withSourceRepository(""));
}
private Path tempFile(String path, ImmutableList lines) throws IOException {