bzlmod: Refactor repository mapping with a new class
To support strict repository dependency in Bzlmod, we need to distinguish repo mapping behaviour between repos generated from Bzlmod and WORKSPACE, therefore we have to refactor the repo mapping from a ImmutableMap to the RepositoryMapping class.
Working towards: https://github.com/bazelbuild/bazel/issues/13793
Related: https://github.com/bazelbuild/bazel/issues/13316
RELNOTES: None
PiperOrigin-RevId: 394180770
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/LocationExpanderTest.java b/src/test/java/com/google/devtools/build/lib/analysis/LocationExpanderTest.java
index 2e92de0..fdf64e6 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/LocationExpanderTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/LocationExpanderTest.java
@@ -18,6 +18,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.analysis.LocationExpander.LocationFunction;
+import com.google.devtools.build.lib.cmdline.RepositoryMapping;
import com.google.devtools.build.lib.cmdline.RepositoryName;
import java.util.ArrayList;
import java.util.List;
@@ -73,7 +74,7 @@
ImmutableMap.<String, LocationFunction>of(
"location", f1,
"locations", f2),
- ImmutableMap.of());
+ RepositoryMapping.ALWAYS_FALLBACK);
}
private String expand(String input) throws Exception {
@@ -133,10 +134,11 @@
RepositoryName.create("@foo"),
RepositoryName.create("@bar"));
- LocationExpander locationExpander = new LocationExpander(
- new Capture(),
- ImmutableMap.<String, LocationFunction>of("location", f1),
- repositoryMapping);
+ LocationExpander locationExpander =
+ new LocationExpander(
+ new Capture(),
+ ImmutableMap.<String, LocationFunction>of("location", f1),
+ RepositoryMapping.createAllowingFallback(repositoryMapping));
String value = locationExpander.expand("$(location @foo//a)");
assertThat(value).isEqualTo("src/a");
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/LocationFunctionTest.java b/src/test/java/com/google/devtools/build/lib/analysis/LocationFunctionTest.java
index 311ae2e..c333ad7 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/LocationFunctionTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/LocationFunctionTest.java
@@ -25,6 +25,7 @@
import com.google.devtools.build.lib.actions.util.ActionsTestUtil;
import com.google.devtools.build.lib.analysis.LocationExpander.LocationFunction;
import com.google.devtools.build.lib.cmdline.Label;
+import com.google.devtools.build.lib.cmdline.RepositoryMapping;
import com.google.devtools.build.lib.cmdline.RepositoryName;
import com.google.devtools.build.lib.vfs.DigestHashFunction;
import com.google.devtools.build.lib.vfs.FileSystem;
@@ -47,23 +48,25 @@
public void absoluteAndRelativeLabels() throws Exception {
LocationFunction func =
new LocationFunctionBuilder("//foo", false).add("//foo", "/exec/src/bar").build();
- assertThat(func.apply("//foo", ImmutableMap.of())).isEqualTo("src/bar");
- assertThat(func.apply(":foo", ImmutableMap.of())).isEqualTo("src/bar");
- assertThat(func.apply("foo", ImmutableMap.of())).isEqualTo("src/bar");
+ assertThat(func.apply("//foo", RepositoryMapping.ALWAYS_FALLBACK)).isEqualTo("src/bar");
+ assertThat(func.apply(":foo", RepositoryMapping.ALWAYS_FALLBACK)).isEqualTo("src/bar");
+ assertThat(func.apply("foo", RepositoryMapping.ALWAYS_FALLBACK)).isEqualTo("src/bar");
}
@Test
public void pathUnderExecRootUsesDotSlash() throws Exception {
LocationFunction func =
new LocationFunctionBuilder("//foo", false).add("//foo", "/exec/bar").build();
- assertThat(func.apply("//foo", ImmutableMap.of())).isEqualTo("./bar");
+ assertThat(func.apply("//foo", RepositoryMapping.ALWAYS_FALLBACK)).isEqualTo("./bar");
}
@Test
public void noSuchLabel() throws Exception {
LocationFunction func = new LocationFunctionBuilder("//foo", false).build();
IllegalStateException expected =
- assertThrows(IllegalStateException.class, () -> func.apply("//bar", ImmutableMap.of()));
+ assertThrows(
+ IllegalStateException.class,
+ () -> func.apply("//bar", RepositoryMapping.ALWAYS_FALLBACK));
assertThat(expected)
.hasMessageThat()
.isEqualTo(
@@ -75,7 +78,9 @@
public void emptyList() throws Exception {
LocationFunction func = new LocationFunctionBuilder("//foo", false).add("//foo").build();
IllegalStateException expected =
- assertThrows(IllegalStateException.class, () -> func.apply("//foo", ImmutableMap.of()));
+ assertThrows(
+ IllegalStateException.class,
+ () -> func.apply("//foo", RepositoryMapping.ALWAYS_FALLBACK));
assertThat(expected)
.hasMessageThat()
.isEqualTo("label '//foo:foo' in $(location) expression expands to no files");
@@ -86,7 +91,9 @@
LocationFunction func =
new LocationFunctionBuilder("//foo", false).add("//foo", "/exec/1", "/exec/2").build();
IllegalStateException expected =
- assertThrows(IllegalStateException.class, () -> func.apply("//foo", ImmutableMap.of()));
+ assertThrows(
+ IllegalStateException.class,
+ () -> func.apply("//foo", RepositoryMapping.ALWAYS_FALLBACK));
assertThat(expected)
.hasMessageThat()
.isEqualTo(
@@ -99,7 +106,9 @@
public void noSuchLabelMultiple() throws Exception {
LocationFunction func = new LocationFunctionBuilder("//foo", true).build();
IllegalStateException expected =
- assertThrows(IllegalStateException.class, () -> func.apply("//bar", ImmutableMap.of()));
+ assertThrows(
+ IllegalStateException.class,
+ () -> func.apply("//bar", RepositoryMapping.ALWAYS_FALLBACK));
assertThat(expected)
.hasMessageThat()
.isEqualTo(
@@ -111,7 +120,8 @@
public void fileWithSpace() throws Exception {
LocationFunction func =
new LocationFunctionBuilder("//foo", false).add("//foo", "/exec/file/with space").build();
- assertThat(func.apply("//foo", ImmutableMap.of())).isEqualTo("'file/with space'");
+ assertThat(func.apply("//foo", RepositoryMapping.ALWAYS_FALLBACK))
+ .isEqualTo("'file/with space'");
}
@Test
@@ -119,7 +129,8 @@
LocationFunction func = new LocationFunctionBuilder("//foo", true)
.add("//foo", "/exec/foo/bar", "/exec/out/foo/foobar")
.build();
- assertThat(func.apply("//foo", ImmutableMap.of())).isEqualTo("foo/bar foo/foobar");
+ assertThat(func.apply("//foo", RepositoryMapping.ALWAYS_FALLBACK))
+ .isEqualTo("foo/bar foo/foobar");
}
@Test
@@ -127,7 +138,7 @@
LocationFunction func = new LocationFunctionBuilder("//foo", true)
.add("//foo", "/exec/file/with space", "/exec/file/with spaces ")
.build();
- assertThat(func.apply("//foo", ImmutableMap.of()))
+ assertThat(func.apply("//foo", RepositoryMapping.ALWAYS_FALLBACK))
.isEqualTo("'file/with space' 'file/with spaces '");
}
@@ -137,7 +148,8 @@
.setExecPaths(true)
.add("//foo", "/exec/bar", "/exec/out/foobar")
.build();
- assertThat(func.apply("//foo", ImmutableMap.of())).isEqualTo("./bar out/foobar");
+ assertThat(func.apply("//foo", RepositoryMapping.ALWAYS_FALLBACK))
+ .isEqualTo("./bar out/foobar");
}
@Test
@@ -147,7 +159,8 @@
ImmutableMap<RepositoryName, RepositoryName> repositoryMapping = ImmutableMap.of(a, b);
LocationFunction func =
new LocationFunctionBuilder("//foo", false).add("@b//foo", "/exec/src/bar").build();
- assertThat(func.apply("@a//foo", repositoryMapping)).isEqualTo("src/bar");
+ assertThat(func.apply("@a//foo", RepositoryMapping.createAllowingFallback(repositoryMapping)))
+ .isEqualTo("src/bar");
}
@Test
@@ -157,7 +170,9 @@
ImmutableMap<RepositoryName, RepositoryName> repositoryMapping = ImmutableMap.of(a, b);
LocationFunction func =
new LocationFunctionBuilder("//foo", false).add("@potato//foo", "/exec/src/bar").build();
- assertThat(func.apply("@potato//foo", repositoryMapping)).isEqualTo("src/bar");
+ assertThat(
+ func.apply("@potato//foo", RepositoryMapping.createAllowingFallback(repositoryMapping)))
+ .isEqualTo("src/bar");
}
}