Skip to content

Commit

Permalink
bzlmod: Change FakeRegistry to return real, local_repository RepoSpecs
Browse files Browse the repository at this point in the history
    (bazelbuild/bazel#13316)

    This allows us to more easily set up some tests that actually want to fetch modules, without relying on local_path_override all the time. (Useful for RegisteredExecutionPlatformsFunctionTest, for example.)

    PiperOrigin-RevId: 387064920
  • Loading branch information
Luca Di Grazia committed Sep 4, 2022
1 parent bdc31aa commit 2b7d95f
Show file tree
Hide file tree
Showing 6 changed files with 1,128 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,25 +134,25 @@ public void getRepoSpec_bazelModule() throws Exception {
"bazel_dep(name='B',version='1.0')");
FakeRegistry registry =
registryFactory
.newFakeRegistry()
.newFakeRegistry("/usr/local/modules")
.addModule(
createModuleKey("B", "1.0"),
"module(name='B', version='1.0');bazel_dep(name='C',version='2.0')")
.addModule(createModuleKey("C", "2.0"), "module(name='C', version='2.0')");
ModuleFileFunction.REGISTRIES.set(differencer, ImmutableList.of(registry.getUrl()));

EvaluationResult<GetRepoSpecByNameValue> result =
driver.evaluate(ImmutableList.of(getRepoSpecByNameKey("C")), evaluationContext);
driver.evaluate(ImmutableList.of(getRepoSpecByNameKey("C.2.0")), evaluationContext);
if (result.hasError()) {
fail(result.getError().toString());
}

Optional<RepoSpec> repoSpec = result.get(getRepoSpecByNameKey("C")).rule();
Optional<RepoSpec> repoSpec = result.get(getRepoSpecByNameKey("C.2.0")).rule();
assertThat(repoSpec)
.hasValue(
RepoSpec.builder()
.setRuleClassName("fake_http_archive_rule")
.setAttributes(ImmutableMap.of("repo_name", "C"))
.setRuleClassName("local_repository")
.setAttributes(ImmutableMap.of("name", "C.2.0", "path", "/usr/local/modules/C.2.0"))
.build());
}

Expand All @@ -165,27 +165,27 @@ public void getRepoSpec_nonRegistryOverride() throws Exception {
"local_path_override(module_name='C',path='/foo/bar/C')");
FakeRegistry registry =
registryFactory
.newFakeRegistry()
.newFakeRegistry("/usr/local/modules")
.addModule(
createModuleKey("B", "1.0"),
"module(name='B', version='1.0');bazel_dep(name='C',version='2.0')")
.addModule(createModuleKey("C", "2.0"), "module(name='C', version='2.0')");
ModuleFileFunction.REGISTRIES.set(differencer, ImmutableList.of(registry.getUrl()));

EvaluationResult<GetRepoSpecByNameValue> result =
driver.evaluate(ImmutableList.of(getRepoSpecByNameKey("C")), evaluationContext);
driver.evaluate(ImmutableList.of(getRepoSpecByNameKey("C.")), evaluationContext);
if (result.hasError()) {
fail(result.getError().toString());
}

Optional<RepoSpec> repoSpec = result.get(getRepoSpecByNameKey("C")).rule();
Optional<RepoSpec> repoSpec = result.get(getRepoSpecByNameKey("C.")).rule();
assertThat(repoSpec)
.hasValue(
RepoSpec.builder()
.setRuleClassName("local_repository")
.setAttributes(
ImmutableMap.of(
"name", "C",
"name", "C.",
"path", "/foo/bar/C"))
.build());
}
Expand All @@ -200,7 +200,7 @@ public void getRepoSpec_singleVersionOverride() throws Exception {
" module_name='C',version='3.0',patches=['//:foo.patch'],patch_strip=1)");
FakeRegistry registry =
registryFactory
.newFakeRegistry()
.newFakeRegistry("/usr/local/modules")
.addModule(
createModuleKey("B", "1.0"),
"module(name='B', version='1.0');bazel_dep(name='C',version='2.0')")
Expand All @@ -209,27 +209,68 @@ public void getRepoSpec_singleVersionOverride() throws Exception {
ModuleFileFunction.REGISTRIES.set(differencer, ImmutableList.of(registry.getUrl()));

EvaluationResult<GetRepoSpecByNameValue> result =
driver.evaluate(ImmutableList.of(getRepoSpecByNameKey("C")), evaluationContext);
driver.evaluate(ImmutableList.of(getRepoSpecByNameKey("C.3.0")), evaluationContext);
if (result.hasError()) {
fail(result.getError().toString());
}

Optional<RepoSpec> repoSpec = result.get(getRepoSpecByNameKey("C")).rule();
Optional<RepoSpec> repoSpec = result.get(getRepoSpecByNameKey("C.3.0")).rule();
assertThat(repoSpec)
.hasValue(
RepoSpec.builder()
.setRuleClassName("fake_http_archive_rule")
// This obviously wouldn't work in the real world since local_repository doesn't
// support patches -- but in the real world, registries also don't use
// local_repository.
.setRuleClassName("local_repository")
.setAttributes(
ImmutableMap.of(
"repo_name",
"C",
"name",
"C.3.0",
"path",
"/usr/local/modules/C.3.0",
"patches",
ImmutableList.of("//:foo.patch"),
"patch_args",
ImmutableList.of("-p1")))
.build());
}

@Test
public void getRepoSpec_multipleVersionOverride() throws Exception {
scratch.file(
workspaceRoot.getRelative("MODULE.bazel").getPathString(),
"module(name='A',version='0.1')",
"bazel_dep(name='B',version='1.0')",
"bazel_dep(name='C',version='2.0')",
"multiple_version_override(module_name='D',versions=['1.0','2.0'])");
FakeRegistry registry =
registryFactory
.newFakeRegistry("/usr/local/modules")
.addModule(
createModuleKey("B", "1.0"),
"module(name='B', version='1.0');bazel_dep(name='D',version='1.0')")
.addModule(
createModuleKey("C", "2.0"),
"module(name='C', version='2.0');bazel_dep(name='D',version='2.0')")
.addModule(createModuleKey("D", "1.0"), "module(name='D', version='1.0')")
.addModule(createModuleKey("D", "2.0"), "module(name='D', version='2.0')");
ModuleFileFunction.REGISTRIES.set(differencer, ImmutableList.of(registry.getUrl()));

EvaluationResult<GetRepoSpecByNameValue> result =
driver.evaluate(ImmutableList.of(getRepoSpecByNameKey("D.2.0")), evaluationContext);
if (result.hasError()) {
fail(result.getError().toString());
}

Optional<RepoSpec> repoSpec = result.get(getRepoSpecByNameKey("D.2.0")).rule();
assertThat(repoSpec)
.hasValue(
RepoSpec.builder()
.setRuleClassName("local_repository")
.setAttributes(ImmutableMap.of("name", "D.2.0", "path", "/usr/local/modules/D.2.0"))
.build());
}

@Test
public void getRepoSpec_notFound() throws Exception {
scratch.file(
Expand All @@ -238,7 +279,7 @@ public void getRepoSpec_notFound() throws Exception {
"bazel_dep(name='B',version='1.0')");
FakeRegistry registry =
registryFactory
.newFakeRegistry()
.newFakeRegistry("/usr/local/modules")
.addModule(createModuleKey("B", "1.0"), "module(name='B', version='1.0')");
ModuleFileFunction.REGISTRIES.set(differencer, ImmutableList.of(registry.getUrl()));

Expand Down

0 comments on commit 2b7d95f

Please sign in to comment.