From e5b8f5a8f2a588c13093d5a154b98be0bea314a6 Mon Sep 17 00:00:00 2001 From: Googler Date: Thu, 8 Aug 2024 04:55:40 -0700 Subject: [PATCH] Make sure bazel_rules_* tests work with --enable_workspace=false A part of https://github.com/bazelbuild/bazel/pull/23087 Working towards: https://github.com/bazelbuild/bazel/issues/23023 PiperOrigin-RevId: 660787082 Change-Id: Iad10e9795db06421c104e149bd2e695262ff225b --- src/test/shell/bazel/bazel_rules_cc_test.sh | 3 +- src/test/shell/bazel/bazel_rules_java_test.sh | 4 +- src/test/shell/bazel/bazel_rules_test.sh | 170 +++++++++--------- 3 files changed, 89 insertions(+), 88 deletions(-) diff --git a/src/test/shell/bazel/bazel_rules_cc_test.sh b/src/test/shell/bazel/bazel_rules_cc_test.sh index 777dbe4282164c..55b7aa7478f11c 100755 --- a/src/test/shell/bazel/bazel_rules_cc_test.sh +++ b/src/test/shell/bazel/bazel_rules_cc_test.sh @@ -78,11 +78,12 @@ filegroup(name = 'yolo') EOF cd rules_cc_can_be_overridden || fail "couldn't cd into workspace" - bazel build --noenable_bzlmod @rules_cc//:yolo &> $TEST_log || \ + bazel build --noenable_bzlmod --enable_workspace @rules_cc//:yolo &> $TEST_log || \ fail "Bazel failed to build @rules_cc" } function test_rules_cc_repository_builds_itself() { + add_rules_cc "MODULE.bazel" write_default_bazelrc # We test that a built-in @rules_cc repository is buildable. diff --git a/src/test/shell/bazel/bazel_rules_java_test.sh b/src/test/shell/bazel/bazel_rules_java_test.sh index 93f11b96b8235a..f1ad5aec1c00bc 100755 --- a/src/test/shell/bazel/bazel_rules_java_test.sh +++ b/src/test/shell/bazel/bazel_rules_java_test.sh @@ -85,13 +85,13 @@ def rules_java_toolchains(): EOF cd rules_java_can_be_overridden || fail "couldn't cd into workspace" - bazel build --noenable_bzlmod @rules_java//:yolo &> $TEST_log || \ + bazel build --noenable_bzlmod --enable_workspace @rules_java//:yolo &> $TEST_log || \ fail "Bazel failed to build @rules_java" } function test_rules_java_repository_builds_itself() { + add_rules_java "MODULE.bazel" write_default_bazelrc - setup_skylib_support # We test that a built-in @rules_java repository is buildable. bazel build -- @rules_java//java/... &> $TEST_log \ diff --git a/src/test/shell/bazel/bazel_rules_test.sh b/src/test/shell/bazel/bazel_rules_test.sh index 9134d20e40d704..c0919cec117052 100755 --- a/src/test/shell/bazel/bazel_rules_test.sh +++ b/src/test/shell/bazel/bazel_rules_test.sh @@ -106,10 +106,7 @@ function test_extra_action() { # Make a program to run on each action that just prints the path to the extra # action file. This file is a proto, but I don't want to bother implementing # a program that parses the proto here. - # The workspace name is initialized in testenv.sh; use that var rather than - # hardcoding it here. The extra sed pass is so we can selectively expand that - # one var while keeping the rest of the heredoc literal. - sed "s/{{WORKSPACE_NAME}}/$WORKSPACE_NAME/" > mypkg/echoer.sh << 'EOF' + cat > mypkg/echoer.sh << 'EOF' #!/bin/bash set -euo pipefail # --- begin runfiles.bash initialization --- @@ -133,7 +130,7 @@ else fi # --- end runfiles.bash initialization --- -if [[ ! -e "$(rlocation {{WORKSPACE_NAME}}/mypkg/runfile)" ]]; then +if [[ ! -e "$(rlocation _main/mypkg/runfile)" ]]; then echo "ERROR: Runfile not found" >&2 exit 1 fi @@ -326,12 +323,15 @@ EOF } function test_genrule_remote() { - cat >> $(create_workspace_with_default_repos WORKSPACE) < MODULE.bazel < package/BUILD <$TEST_log 2>&1 || fail "Should build" + bazel build @other_repo//package:abs_dep >$TEST_log 2>&1 || fail "Should build" } function test_genrule_remote_d() { - cat >> $(create_workspace_with_default_repos WORKSPACE) < MODULE.bazel < package/BUILD <<'EOF' genrule( @@ -374,10 +378,11 @@ genrule( cmd = "echo 'hi' | tee $(@D)/a/b $(@D)/c/d", ) EOF + cd .. - bazel build @r//package:hi >$TEST_log 2>&1 || fail "Should build" - expect_log "bazel-.*bin/external/r/package/a/b" - expect_log "bazel-.*bin/external/r/package/c/d" + bazel build @other_repo//package:hi >$TEST_log 2>&1 || fail "Should build" + expect_log "bazel-.*bin/external/+_repo_rules+other_repo/package/a/b" + expect_log "bazel-.*bin/external/+_repo_rules+other_repo/package/c/d" } function test_genrule_toolchain_dependency { @@ -396,56 +401,54 @@ EOF } function test_python_with_workspace_name() { + create_new_workspace + cd ${new_workspace_dir} + mkdir -p {module_a,module_b} + local remote_path="${new_workspace_dir}" - create_new_workspace - cd ${new_workspace_dir} - mkdir -p {module_a,module_b} - local remote_path="${new_workspace_dir}" - - cat > module_a/BUILD < module_a/BUILD < module_b/BUILD < module_b/BUILD < module_a/foo.py < module_a/foo.py < module_b/bar.py < module_b/bar.py < module_b/bar2.py < module_b/bar2.py <> $(create_workspace_with_default_repos WORKSPACE) < WORKSPACE < module1/BUILD < module1/BUILD < module2/BUILD < module2/BUILD < module1/fib.py < module1/fib.py < module2/bez.py < module2/bez.py <$TEST_log - expect_log "The number is 42" - expect_log "Print the number 42" - expect_log "Fib(10) is 89" - bazel run @remote//module_b:bar2 >$TEST_log - expect_log "The number is 42" + bazel run --enable_workspace //module2:bez >$TEST_log + expect_log "The number is 42" + expect_log "Print the number 42" + expect_log "Fib(10) is 89" + bazel run --enable_workspace @remote//module_b:bar2 >$TEST_log + expect_log "The number is 42" } function test_build_python_zip_with_middleman() { @@ -746,9 +749,8 @@ EOF } function setup_bash_runfiles_current_repository() { - touch MODULE.bazel - - cat >> WORKSPACE <<'EOF' + cat > MODULE.bazel <<'EOF' +local_repository = use_repo_rule("@bazel_tools//tools/build_defs/repo:local.bzl", "local_repository") local_repository( name = "other_repo", path = "other_repo", @@ -846,7 +848,7 @@ EOF chmod +x pkg/test.sh mkdir -p other_repo - touch other_repo/WORKSPACE + touch other_repo/REPO.bazel mkdir -p other_repo/pkg cat > other_repo/pkg/BUILD.bazel <<'EOF' @@ -953,13 +955,13 @@ function test_bash_runfiles_current_repository_binary_enable_runfiles() { &>"$TEST_log" || fail "Run should succeed" expect_log "in pkg/binary.sh: ''" expect_log "in pkg/library.sh: ''" - expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/library2.sh: '+_repo_rules+other_repo'" RUNFILES_LIB_DEBUG=1 bazel run --enable_bzlmod --enable_runfiles @other_repo//pkg:binary \ &>"$TEST_log" || fail "Run should succeed" - expect_log "in external/other_repo/pkg/binary.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/binary.sh: '+_repo_rules+other_repo'" expect_log "in pkg/library.sh: ''" - expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/library2.sh: '+_repo_rules+other_repo'" } function test_bash_runfiles_current_repository_binary_enable_runfiles_direct_run() { @@ -971,15 +973,15 @@ function test_bash_runfiles_current_repository_binary_enable_runfiles_direct_run || fail "Direct run should succeed" expect_log "in pkg/binary.sh: ''" expect_log "in pkg/library.sh: ''" - expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/library2.sh: '+_repo_rules+other_repo'" bazel run --enable_bzlmod --enable_runfiles @other_repo//pkg:binary \ &>"$TEST_log" || fail "Run should succeed" - clean_runfiles_run bazel-bin/external/other_repo/pkg/binary$exe_suffix \ + clean_runfiles_run bazel-bin/external/+_repo_rules+other_repo/pkg/binary$exe_suffix \ &>"$TEST_log" || fail "Direct run should succeed" - expect_log "in external/other_repo/pkg/binary.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/binary.sh: '+_repo_rules+other_repo'" expect_log "in pkg/library.sh: ''" - expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/library2.sh: '+_repo_rules+other_repo'" } function test_bash_runfiles_current_repository_test_enable_runfiles() { @@ -989,13 +991,13 @@ function test_bash_runfiles_current_repository_test_enable_runfiles() { --test_output=all //pkg:test &>"$TEST_log" || fail "Test should succeed" expect_log "in pkg/test.sh: ''" expect_log "in pkg/library.sh: ''" - expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/library2.sh: '+_repo_rules+other_repo'" bazel test --enable_bzlmod --enable_runfiles --test_env=RUNFILES_LIB_DEBUG=1 \ --test_output=all @other_repo//pkg:test &>"$TEST_log" || fail "Test should succeed" - expect_log "in external/other_repo/pkg/test.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/test.sh: '+_repo_rules+other_repo'" expect_log "in pkg/library.sh: ''" - expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/library2.sh: '+_repo_rules+other_repo'" } function test_bash_runfiles_current_repository_binary_noenable_runfiles() { @@ -1005,13 +1007,13 @@ function test_bash_runfiles_current_repository_binary_noenable_runfiles() { &>"$TEST_log" || fail "Run should succeed" expect_log "in pkg/binary.sh: ''" expect_log "in pkg/library.sh: ''" - expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/library2.sh: '+_repo_rules+other_repo'" RUNFILES_LIB_DEBUG=1 bazel run --enable_bzlmod --noenable_runfiles @other_repo//pkg:binary \ &>"$TEST_log" || fail "Run should succeed" - expect_log "in external/other_repo/pkg/binary.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/binary.sh: '+_repo_rules+other_repo'" expect_log "in pkg/library.sh: ''" - expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/library2.sh: '+_repo_rules+other_repo'" } function test_bash_runfiles_current_repository_binary_noenable_runfiles_direct_run() { @@ -1023,15 +1025,15 @@ function test_bash_runfiles_current_repository_binary_noenable_runfiles_direct_r || fail "Direct run should succeed" expect_log "in pkg/binary.sh: ''" expect_log "in pkg/library.sh: ''" - expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/library2.sh: '+_repo_rules+other_repo'" bazel run --enable_bzlmod --noenable_runfiles @other_repo//pkg:binary \ &>"$TEST_log" || fail "Run should succeed" - clean_runfiles_run bazel-bin/external/other_repo/pkg/binary$exe_suffix \ + clean_runfiles_run bazel-bin/external/+_repo_rules+other_repo/pkg/binary$exe_suffix \ &>"$TEST_log" || fail "Direct run should succeed" - expect_log "in external/other_repo/pkg/binary.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/binary.sh: '+_repo_rules+other_repo'" expect_log "in pkg/library.sh: ''" - expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/library2.sh: '+_repo_rules+other_repo'" } function test_bash_runfiles_current_repository_test_noenable_runfiles() { @@ -1041,13 +1043,13 @@ function test_bash_runfiles_current_repository_test_noenable_runfiles() { --test_output=all //pkg:test &>"$TEST_log" || fail "Test should succeed" expect_log "in pkg/test.sh: ''" expect_log "in pkg/library.sh: ''" - expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/library2.sh: '+_repo_rules+other_repo'" bazel test --enable_bzlmod --noenable_runfiles --test_env=RUNFILES_LIB_DEBUG=1 \ --test_output=all @other_repo//pkg:test &>"$TEST_log" || fail "Test should succeed" - expect_log "in external/other_repo/pkg/test.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/test.sh: '+_repo_rules+other_repo'" expect_log "in pkg/library.sh: ''" - expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/library2.sh: '+_repo_rules+other_repo'" } function test_bash_runfiles_current_repository_binary_nobuild_runfile_links() { @@ -1057,13 +1059,13 @@ function test_bash_runfiles_current_repository_binary_nobuild_runfile_links() { &>"$TEST_log" || fail "Run should succeed" expect_log "in pkg/binary.sh: ''" expect_log "in pkg/library.sh: ''" - expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/library2.sh: '+_repo_rules+other_repo'" RUNFILES_LIB_DEBUG=1 bazel run --enable_bzlmod --nobuild_runfile_links @other_repo//pkg:binary \ &>"$TEST_log" || fail "Run should succeed" - expect_log "in external/other_repo/pkg/binary.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/binary.sh: '+_repo_rules+other_repo'" expect_log "in pkg/library.sh: ''" - expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/library2.sh: '+_repo_rules+other_repo'" } function test_bash_runfiles_current_repository_binary_nobuild_runfile_links_direct_run() { @@ -1075,15 +1077,15 @@ function test_bash_runfiles_current_repository_binary_nobuild_runfile_links_dire || fail "Direct run should succeed" expect_log "in pkg/binary.sh: ''" expect_log "in pkg/library.sh: ''" - expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/library2.sh: '+_repo_rules+other_repo'" bazel run --enable_bzlmod --nobuild_runfile_links @other_repo//pkg:binary \ &>"$TEST_log" || fail "Run should succeed" - clean_runfiles_run bazel-bin/external/other_repo/pkg/binary$exe_suffix \ + clean_runfiles_run bazel-bin/external/+_repo_rules+other_repo/pkg/binary$exe_suffix \ &>"$TEST_log" || fail "Direct run should succeed" - expect_log "in external/other_repo/pkg/binary.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/binary.sh: '+_repo_rules+other_repo'" expect_log "in pkg/library.sh: ''" - expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/library2.sh: '+_repo_rules+other_repo'" } function test_bash_runfiles_current_repository_test_nobuild_runfile_links() { @@ -1094,18 +1096,18 @@ function test_bash_runfiles_current_repository_test_nobuild_runfile_links() { &>"$TEST_log" || fail "Test should succeed" expect_log "in pkg/test.sh: ''" expect_log "in pkg/library.sh: ''" - expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/library2.sh: '+_repo_rules+other_repo'" bazel test --enable_bzlmod --noenable_runfiles --nobuild_runfile_links \ --test_env=RUNFILES_LIB_DEBUG=1 --test_output=all @other_repo//pkg:test \ &>"$TEST_log" || fail "Test should succeed" - expect_log "in external/other_repo/pkg/test.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/test.sh: '+_repo_rules+other_repo'" expect_log "in pkg/library.sh: ''" - expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/library2.sh: '+_repo_rules+other_repo'" } function test_bash_runfiles_current_repository_action_binary_main_repo() { - touch MODULE.bazel + setup_module_dot_bazel "MODULE.bazel" mkdir -p pkg cat > pkg/BUILD.bazel <<'EOF' @@ -1148,7 +1150,7 @@ EOF } function test_bash_runfiles_current_repository_action_generated_binary_main_repo() { - touch MODULE.bazel + setup_module_dot_bazel "MODULE.bazel" mkdir -p pkg cat > pkg/BUILD.bazel <<'EOF' @@ -1198,9 +1200,8 @@ EOF } function test_bash_runfiles_current_repository_action_binary_external_repo() { - touch MODULE.bazel - - cat >> WORKSPACE <<'EOF' + cat > MODULE.bazel <<'EOF' +local_repository = use_repo_rule("@bazel_tools//tools/build_defs/repo:local.bzl", "local_repository") local_repository( name = "other_repo", path = "other_repo", @@ -1218,7 +1219,7 @@ genrule( EOF mkdir -p other_repo - touch other_repo/WORKSPACE + touch other_repo/REPO.bazel mkdir -p other_repo/pkg cat > other_repo/pkg/BUILD.bazel <<'EOF' @@ -1250,13 +1251,12 @@ EOF chmod +x other_repo/pkg/binary.sh bazel build --enable_bzlmod //pkg:gen &>"$TEST_log" || fail "Build should succeed" - expect_log "in external/other_repo/pkg/binary.sh: 'other_repo'" + expect_log "in external/other_repo/pkg/binary.sh: '+_repo_rules+other_repo'" } function test_bash_runfiles_current_repository_action_generated_binary_external_repo() { - touch MODULE.bazel - - cat >> WORKSPACE <<'EOF' + cat > MODULE.bazel <<'EOF' +local_repository = use_repo_rule("@bazel_tools//tools/build_defs/repo:local.bzl", "local_repository") local_repository( name = "other_repo", path = "other_repo", @@ -1274,7 +1274,7 @@ genrule( EOF mkdir -p other_repo - touch other_repo/WORKSPACE + touch other_repo/REPO.bazel mkdir -p other_repo/pkg cat > other_repo/pkg/BUILD.bazel <<'EOF' @@ -1313,7 +1313,7 @@ EOF chmod +x other_repo/pkg/binary.sh bazel build --enable_bzlmod //pkg:gen &>"$TEST_log" || fail "Build should succeed" - expect_log "in copy of external/other_repo/pkg/binary.sh: 'other_repo'" + expect_log "in copy of external/other_repo/pkg/binary.sh: '+_repo_rules+other_repo'" } run_suite "rules test"