Skip to content

Commit

Permalink
Disable WORKSPACE in java integration tests
Browse files Browse the repository at this point in the history
Working towards: #23023

Closes #23086.

PiperOrigin-RevId: 656036460
Change-Id: I7475d10964bfa9cefd9b919d300b5dc352d027b0
  • Loading branch information
meteorcloudy authored and copybara-github committed Jul 25, 2024
1 parent d69c453 commit 4af6578
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public BlackBoxTestContext prepareEnvironment(
new BlackBoxTestContext(
testName, "bazel", binaryPath, Collections.emptyMap(), executorService);
// Any Bazel command requires that workspace is already set up.
testContext.write("WORKSPACE", "workspace(name = 'main')", getWorkspaceWithDefaultRepos());
testContext.write("MODULE.bazel");
Path defaultLockfile = RunfilesUtil.find("io_bazel/src/test/tools/bzlmod/MODULE.bazel.lock");
Files.copy(defaultLockfile, testContext.getWorkDir().resolve("MODULE.bazel.lock"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,61 +14,22 @@

package com.google.devtools.build.lib.blackbox.bazel;

import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.blackbox.framework.BlackBoxTestContext;
import com.google.devtools.build.lib.blackbox.framework.ToolsSetup;
import com.google.devtools.build.lib.util.OS;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;

/** Setup for Bazel default tools */
public class DefaultToolsSetup implements ToolsSetup {

private static ImmutableList<String> repos =
ImmutableList.<String>builder()
.add("bazel_skylib")
.add("rules_cc")
.add("rules_proto")
.add("rules_java")
.add("rules_java_builtin_for_testing")
.add("rules_python")
.build();

private ImmutableList<String> getRepositoryOverrides() {
String sharedRepoHome = System.getenv("TEST_REPOSITORY_HOME");
if (sharedRepoHome == null) {
return ImmutableList.of();
}

Path sharedRepoHomePath = Paths.get(sharedRepoHome);
if (!Files.exists(sharedRepoHomePath)) {
return ImmutableList.of();
}

ImmutableList.Builder<String> lines = ImmutableList.builder();
for (String repo : repos) {
Path sharedRepoPath = sharedRepoHomePath.resolve(repo);
String suffix = "_for_testing";
repo = repo.endsWith(suffix) ? repo.substring(0, repo.length() - suffix.length()) : repo;
lines.add(
"common --override_repository="
+ repo
+ "="
+ sharedRepoPath.toString().replace('\\', '/'));
}

return lines.build();
}

@Override
public void setup(BlackBoxTestContext context) throws IOException {
Path outputRoot = Files.createTempDirectory(context.getTmpDir(), "root").toAbsolutePath();
ArrayList<String> lines = new ArrayList<>();
lines.add("startup --output_user_root=" + outputRoot.toString().replace('\\', '/'));
lines.addAll(getRepositoryOverrides());

String sharedInstallBase = System.getenv("TEST_INSTALL_BASE");
if (sharedInstallBase != null) {
Expand All @@ -93,6 +54,8 @@ public void setup(BlackBoxTestContext context) throws IOException {
lines.add("build --jvmopt=-Djava.net.preferIPv6Addresses");
}

lines.add("common --noenable_workspace");

context.write(".bazelrc", lines);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.devtools.build.lib.util.ResourceFileLoader;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
Expand Down Expand Up @@ -75,8 +73,4 @@ public final void dispose() {
MoreExecutors.shutdownAndAwaitTermination(executorService, 1, TimeUnit.SECONDS);
executorService = null;
}

public static String getWorkspaceWithDefaultRepos() throws IOException {
return ResourceFileLoader.loadResource(BlackBoxTestEnvironment.class, "blackbox.WORKSPACE");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public abstract class AbstractBlackBoxTest {
new JavaToolsSetup(),
new CxxToolsSetup(),
new CrossToolsSetup());
protected static final String WORKSPACE = "WORKSPACE";
protected static final String MODULE_DOT_BAZEL = "MODULE.bazel";

@Rule public TestName testName = new TestName();

Expand Down Expand Up @@ -81,6 +81,7 @@ public void setUp() throws Exception {

public void disableBzlmod() throws Exception {
PathUtils.append(context.getWorkDir().resolve(".bazelrc"), "common --noenable_bzlmod");
PathUtils.append(context.getWorkDir().resolve(".bazelrc"), "common --enable_workspace");
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.blackbox.bazel.PythonToolsSetup;
import com.google.devtools.build.lib.blackbox.framework.BlackBoxTestEnvironment;
import com.google.devtools.build.lib.blackbox.framework.BuilderRunner;
import com.google.devtools.build.lib.blackbox.framework.ProcessResult;
import com.google.devtools.build.lib.blackbox.framework.ToolsSetup;
Expand Down Expand Up @@ -52,7 +51,6 @@ public void testCompileAndRunHelloWorldStub() throws Exception {
}

private void writeHelloWorldFiles() throws IOException {
context().write("WORKSPACE", BlackBoxTestEnvironment.getWorkspaceWithDefaultRepos());
context().write("python/hello/BUILD", "py_binary(name = 'hello', srcs = ['hello.py'])");
context().write("python/hello/hello.py", String.format("print ('%s')", HELLO));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.google.devtools.build.lib.bazel.repository.DecompressorDescriptor;
import com.google.devtools.build.lib.bazel.repository.TarFunction;
import com.google.devtools.build.lib.blackbox.framework.BlackBoxTestEnvironment;
import com.google.devtools.build.lib.blackbox.framework.BuilderRunner;
import com.google.devtools.build.lib.blackbox.framework.PathUtils;
import com.google.devtools.build.lib.blackbox.junit.AbstractBlackBoxTest;
Expand All @@ -42,7 +41,6 @@ public class BazelEmbeddedStarlarkBlackBoxTest extends AbstractBlackBoxTest {

@Test
public void testPkgTar() throws Exception {
context().write("main/WORKSPACE", BlackBoxTestEnvironment.getWorkspaceWithDefaultRepos());
context().write("main/foo.txt", "Hello World");
context().write("main/bar.txt", "Hello World, again");
context()
Expand Down Expand Up @@ -89,11 +87,14 @@ public void testHttpArchive() throws Exception {

context()
.write(
"WORKSPACE",
BlackBoxTestEnvironment.getWorkspaceWithDefaultRepos(),
"MODULE.bazel",
"local_repository = use_repo_rule('@bazel_tools//tools/build_defs/repo:local.bzl',"
+ " 'local_repository')",
String.format(
"local_repository(name=\"ext_local\", path=\"%s\",)",
PathUtils.pathForStarlarkFile(repo)),
"http_archive = use_repo_rule('@bazel_tools//tools/build_defs/repo:http.bzl',"
+ " 'http_archive')",
String.format(
"http_archive(name=\"ext\", urls=[\"%s\"],)", PathUtils.pathToFileURI(zipFile)));

Expand All @@ -109,13 +110,15 @@ public void testHttpArchive() throws Exception {
String tarTarget = generator.getPkgTarTarget();
bazel.build("@ext_local//:" + tarTarget);
Path packedFile =
context().resolveBinPath(bazel, String.format("external/ext_local/%s.tar", tarTarget));
context()
.resolveBinPath(
bazel, String.format("external/_main~_repo_rules~ext_local/%s.tar", tarTarget));
Files.copy(packedFile, zipFile);

// now build the target from http_archive
bazel.build("@ext//:" + RepoWithRuleWritingTextGenerator.TARGET);

Path xPath = context().resolveBinPath(bazel, "external/ext/out");
Path xPath = context().resolveBinPath(bazel, "external/_main~_repo_rules~ext/out");
WorkspaceTestUtils.assertLinesExactly(xPath, HELLO_FROM_EXTERNAL_REPOSITORY);

// and use the rule from http_archive in the main repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ public void testCloneAtTag() throws Exception {
loadRule(""), callRule("call_write_text", "out.txt", HELLO_FROM_EXTERNAL_REPOSITORY));
context()
.write(
"WORKSPACE",
"load(\"@bazel_tools//tools/build_defs/repo:git.bzl\", \"new_git_repository\")",
"MODULE.bazel",
"new_git_repository = use_repo_rule(\"@bazel_tools//tools/build_defs/repo:git.bzl\","
+ " \"new_git_repository\")",
"new_git_repository(",
" name='ext',",
String.format(" remote='%s',", PathUtils.pathToFileURI(repo.resolve(".git"))),
Expand All @@ -83,7 +84,7 @@ public void testCloneAtTag() throws Exception {
// This creates Bazel without MSYS, see implementation for details.
BuilderRunner bazel = WorkspaceTestUtils.bazel(context());
bazel.build("@ext//:call_write_text");
Path outPath = context().resolveBinPath(bazel, "external/ext/out.txt");
Path outPath = context().resolveBinPath(bazel, "external/_main~_repo_rules~ext/out.txt");
WorkspaceTestUtils.assertLinesExactly(outPath, HELLO_FROM_EXTERNAL_REPOSITORY);
}

Expand All @@ -102,8 +103,9 @@ public void testCloneAtCommit() throws Exception {
loadRule(""), callRule("call_write_text", "out.txt", HELLO_FROM_EXTERNAL_REPOSITORY));
context()
.write(
"WORKSPACE",
"load(\"@bazel_tools//tools/build_defs/repo:git.bzl\", \"new_git_repository\")",
"MODULE.bazel",
"new_git_repository = use_repo_rule(\"@bazel_tools//tools/build_defs/repo:git.bzl\","
+ " \"new_git_repository\")",
"new_git_repository(",
" name='ext',",
String.format(" remote='%s',", PathUtils.pathToFileURI(repo.resolve(".git"))),
Expand All @@ -114,7 +116,7 @@ public void testCloneAtCommit() throws Exception {
// This creates Bazel without MSYS, see implementation for details.
BuilderRunner bazel = WorkspaceTestUtils.bazel(context());
bazel.build("@ext//:call_write_text");
Path outPath = context().resolveBinPath(bazel, "external/ext/out.txt");
Path outPath = context().resolveBinPath(bazel, "external/_main~_repo_rules~ext/out.txt");
WorkspaceTestUtils.assertLinesExactly(outPath, HELLO_FROM_EXTERNAL_REPOSITORY);
}

Expand All @@ -133,8 +135,9 @@ public void testCloneAtMain() throws Exception {
loadRule(""), callRule("call_write_text", "out.txt", HELLO_FROM_EXTERNAL_REPOSITORY));
context()
.write(
"WORKSPACE",
"load(\"@bazel_tools//tools/build_defs/repo:git.bzl\", \"new_git_repository\")",
"MODULE.bazel",
"new_git_repository = use_repo_rule(\"@bazel_tools//tools/build_defs/repo:git.bzl\","
+ " \"new_git_repository\")",
"new_git_repository(",
" name='ext',",
String.format(" remote='%s',", PathUtils.pathToFileURI(repo.resolve(".git"))),
Expand All @@ -145,7 +148,7 @@ public void testCloneAtMain() throws Exception {
// This creates Bazel without MSYS, see implementation for details.
BuilderRunner bazel = WorkspaceTestUtils.bazel(context());
bazel.build("@ext//:call_write_text");
Path outPath = context().resolveBinPath(bazel, "external/ext/out.txt");
Path outPath = context().resolveBinPath(bazel, "external/_main~_repo_rules~ext/out.txt");
WorkspaceTestUtils.assertLinesExactly(outPath, HELLO_FROM_EXTERNAL_REPOSITORY);
}

Expand Down Expand Up @@ -178,8 +181,9 @@ public void testCheckoutOfCommitFromBranch() throws Exception {

context()
.write(
"WORKSPACE",
"load(\"@bazel_tools//tools/build_defs/repo:git.bzl\", \"git_repository\")",
"MODULE.bazel",
"git_repository = use_repo_rule(\"@bazel_tools//tools/build_defs/repo:git.bzl\","
+ " \"git_repository\")",
"git_repository(",
" name='ext',",
String.format(" remote='%s',", PathUtils.pathToFileURI(repo.resolve(".git"))),
Expand All @@ -189,7 +193,7 @@ public void testCheckoutOfCommitFromBranch() throws Exception {
// This creates Bazel without MSYS, see implementation for details.
BuilderRunner bazel = WorkspaceTestUtils.bazel(context());
bazel.build("@ext//:write_text");
Path outPath = context().resolveBinPath(bazel, "external/ext/out");
Path outPath = context().resolveBinPath(bazel, "external/_main~_repo_rules~ext/out");
WorkspaceTestUtils.assertLinesExactly(outPath, HELLO_FROM_BRANCH);
}

Expand Down Expand Up @@ -228,8 +232,9 @@ public void testCheckoutOfCommitFromTag() throws Exception {

context()
.write(
"WORKSPACE",
"load(\"@bazel_tools//tools/build_defs/repo:git.bzl\", \"git_repository\")",
"MODULE.bazel",
"git_repository = use_repo_rule(\"@bazel_tools//tools/build_defs/repo:git.bzl\","
+ " \"git_repository\")",
"git_repository(",
" name='ext',",
String.format(" remote='%s',", PathUtils.pathToFileURI(repo.resolve(".git"))),
Expand All @@ -239,7 +244,7 @@ public void testCheckoutOfCommitFromTag() throws Exception {
// This creates Bazel without MSYS, see implementation for details.
BuilderRunner bazel = WorkspaceTestUtils.bazel(context());
bazel.build("@ext//:write_text");
Path outPath = context().resolveBinPath(bazel, "external/ext/out");
Path outPath = context().resolveBinPath(bazel, "external/_main~_repo_rules~ext/out");
WorkspaceTestUtils.assertLinesExactly(outPath, HELLO_FROM_BRANCH);
}

Expand All @@ -248,8 +253,9 @@ public void testCheckoutOfCommitFromTag() throws Exception {
public void testGitRepositoryErrorMessage() throws Exception {
context()
.write(
"WORKSPACE",
"load(\"@bazel_tools//tools/build_defs/repo:git.bzl\", \"git_repository\")",
"MODULE.bazel",
"git_repository = use_repo_rule(\"@bazel_tools//tools/build_defs/repo:git.bzl\","
+ " \"git_repository\")",
"git_repository(",
" name='ext',",
" remote='file:///some_path',",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ def _patched_repo_implementation(ctx):
""");
context()
.write(
WORKSPACE,
"load(\":patched_repo.bzl\", \"patched_repo\")",
MODULE_DOT_BAZEL,
"patched_repo = use_repo_rule(\"//:patched_repo.bzl\", \"patched_repo\")",
"",
"patched_repo(",
" name = \"test\",",
Expand Down Expand Up @@ -157,7 +157,8 @@ public void testFallBackToPatchToolDueToPatchArgs() throws Exception {
} else {
assertFooIsPatched(bazel);
// foo.sh.orig should be generated due to "-b" argument.
Path fooOrig = context().resolveExecRootPath(bazel, "external/test/foo.sh.orig");
Path fooOrig =
context().resolveExecRootPath(bazel, "external/_main~_repo_rules~test/foo.sh.orig");
assertThat(fooOrig.toFile().exists()).isTrue();
}
}
Expand Down Expand Up @@ -200,7 +201,7 @@ public void testFallBackToPatchCmdsWhenPatchCmdsWinNotSpecified() throws Excepti
}

private void assertFooIsPatched(BuilderRunner bazel) throws Exception {
Path foo = context().resolveExecRootPath(bazel, "external/test/foo.sh");
Path foo = context().resolveExecRootPath(bazel, "external/_main~_repo_rules~test/foo.sh");
assertThat(foo.toFile().exists()).isTrue();
ImmutableList<String> patchedFoo =
ImmutableList.of(
Expand Down
Loading

0 comments on commit 4af6578

Please sign in to comment.