Fix shell tests not to assume MODULE.bazel is empty

Removing this assumption makes it possible to put something into the default MODULE.bazel, generated by setup_module_dot_bazel.

In my case, I'll use this to temporarily patch rules_java and protobuf so that they don't cause additional unnecessary downloads.

Some tests assumed that MODULE.bazel was empty and deleted its content. Other tests needed empty MODULE.bazel, and assumed it already was.

I believe this change is generally useful, when we need to do some patching.

PiperOrigin-RevId: 687256923
Change-Id: Id0a5f25676c0ad8992737a37dfe9acaf573f1d35
diff --git a/src/test/shell/bazel/bazel_java_tools_test.sh b/src/test/shell/bazel/bazel_java_tools_test.sh
index 76479ab..6d60825 100755
--- a/src/test/shell/bazel/bazel_java_tools_test.sh
+++ b/src/test/shell/bazel/bazel_java_tools_test.sh
@@ -67,7 +67,7 @@
   if "$is_windows"; then
         java_tools_prebuilt_zip_file_url="file:///${java_tools_prebuilt_rlocation}"
   fi
-  cat > $(setup_module_dot_bazel) <<EOF
+  cat >> $(setup_module_dot_bazel) <<EOF
 http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
 http_archive(
     name = "local_java_tools",
diff --git a/src/test/shell/bazel/execroot_test.sh b/src/test/shell/bazel/execroot_test.sh
index 1986214..66006de 100755
--- a/src/test/shell/bazel/execroot_test.sh
+++ b/src/test/shell/bazel/execroot_test.sh
@@ -71,8 +71,6 @@
 }
 
 function test_sibling_repository_layout() {
-    setup_module_dot_bazel
-
     mkdir -p external/foo
     cat > external/foo/BUILD <<'EOF'
 genrule(
@@ -96,8 +94,6 @@
 
 # Regression test for b/149771751
 function test_sibling_repository_layout_indirect_dependency() {
-    setup_module_dot_bazel
-
     mkdir external
     mkdir -p foo
     cat > BUILD <<'EOF'
@@ -121,8 +117,6 @@
 
 # Regression test for b/149771751
 function test_subdirectory_repository_layout_indirect_dependency() {
-    setup_module_dot_bazel
-
     mkdir external
     mkdir -p foo
     cat > BUILD <<'EOF'
@@ -145,8 +139,6 @@
 }
 
 function test_no_sibling_repository_layout() {
-    setup_module_dot_bazel
-
     mkdir -p external/foo
     cat > external/foo/BUILD <<'EOF'
 genrule(
@@ -171,8 +163,6 @@
 }
 
 function test_external_directory_globs() {
-  setup_module_dot_bazel
-
   mkdir -p external/a external/c
   echo file_ab > external/a/b
   echo file_cd > external/c/d
@@ -194,7 +184,6 @@
 }
 
 function test_cc_smoke_with_new_layouts() {
-  setup_module_dot_bazel
   mkdir -p external/a
   cat > external/a/BUILD <<EOF
 cc_binary(name='a', srcs=['a.cc'])
@@ -213,7 +202,6 @@
 }
 
 function test_java_smoke_with_new_layouts() {
-  setup_module_dot_bazel
   mkdir -p external/java/a
   cat > external/java/a/BUILD <<EOF
 java_binary(name='a', srcs=['A.java'])
diff --git a/src/test/shell/bazel/external_path_test.sh b/src/test/shell/bazel/external_path_test.sh
index 16765ac..fbfdd68 100755
--- a/src/test/shell/bazel/external_path_test.sh
+++ b/src/test/shell/bazel/external_path_test.sh
@@ -115,7 +115,7 @@
 
   mkdir main
   cd main
-  cat > $(setup_module_dot_bazel) <<EOF
+  cat >> $(setup_module_dot_bazel) <<EOF
 http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
 http_archive(
   name="remote",
@@ -179,7 +179,7 @@
 
   mkdir main
   cd main
-  cat > $(setup_module_dot_bazel) <<EOF
+  cat >> $(setup_module_dot_bazel) <<EOF
 http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
 http_archive(
   name="remote",
@@ -245,7 +245,7 @@
 
   mkdir main
   cd main
-  cat > $(setup_module_dot_bazel) <<EOF
+  cat >> $(setup_module_dot_bazel) <<EOF
 http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
 http_archive(
   name="remotelib",
@@ -313,7 +313,7 @@
 
   mkdir main
   cd main
-  cat > $(setup_module_dot_bazel) <<EOF
+  cat >> $(setup_module_dot_bazel) <<EOF
 http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
 http_archive(
   name="remote",
@@ -397,7 +397,7 @@
 
   mkdir main
   cd main
-  cat > $(setup_module_dot_bazel) <<EOF
+  cat >> $(setup_module_dot_bazel) <<EOF
 http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
 http_archive(
   name="r",
@@ -446,7 +446,7 @@
 
   mkdir main
   cd main
-  cat > $(setup_module_dot_bazel) <<EOF
+  cat >> $(setup_module_dot_bazel) <<EOF
 http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
 http_archive(
   name="a",
@@ -559,7 +559,7 @@
 
   mkdir main
   cd main
-  cat > $(setup_module_dot_bazel) <<EOF
+  cat >> $(setup_module_dot_bazel) <<EOF
 http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
 http_archive(
   name="r",
@@ -612,7 +612,7 @@
 
   mkdir main
   cd main
-  cat > $(setup_module_dot_bazel) <<EOF
+  cat >> $(setup_module_dot_bazel) <<EOF
 http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
 http_archive(
   name="r",
@@ -633,7 +633,6 @@
   # create, in the current working directory, a package called
   # rule that has an implicit dependency on @data//:file.txt, a target
   # of a different, external, repository.
-  setup_module_dot_bazel
   mkdir -p rule
   cat > rule/BUILD <<'EOF'
 genrule(
@@ -733,7 +732,7 @@
   (cd rule && repo_with_embedded_foreign_path)
   tar cvf rule.tar rule
   rm -rf rule
-  cat >> $(setup_module_dot_bazel "main/MODULE.bazel") <<EOF
+  cat >> main/MODULE.bazel <<EOF
 http_archive(
   name="rule",
   strip_prefix="rule",
diff --git a/src/test/shell/bazel/new_local_repo_test.sh b/src/test/shell/bazel/new_local_repo_test.sh
index 8aff660..705ce21 100755
--- a/src/test/shell/bazel/new_local_repo_test.sh
+++ b/src/test/shell/bazel/new_local_repo_test.sh
@@ -63,6 +63,7 @@
   mkdir $pkg/A || fail "mkdir $pkg/A"
   mkdir $pkg/B || fail "mkdir $pkg/B"
 
+  setup_module_dot_bazel "$pkg/A/MODULE.bazel"
   cat >$pkg/A/MODULE.bazel <<'eof'
 new_local_repository = use_repo_rule("@bazel_tools//tools/build_defs/repo:local.bzl", "new_local_repository")
 new_local_repository(
@@ -77,7 +78,7 @@
     path = "../B",
 )
 eof
-  setup_module_dot_bazel "$pkg/A/MODULE.bazel"
+
 
   cat >$pkg/A/BUILD <<'eof'
 genrule(
@@ -117,6 +118,7 @@
   touch "$pkg/B/root.txt"
   touch "$pkg/B/subdir/outer.txt"
   touch "$pkg/B/subdir/inner/inner.txt"
+  setup_module_dot_bazel "$pkg/A/MODULE.bazel"
   cat >"$pkg/A/MODULE.bazel" <<eof
 new_local_repository = use_repo_rule("@bazel_tools//tools/build_defs/repo:local.bzl", "new_local_repository")
 new_local_repository(
@@ -129,7 +131,6 @@
   cat >"$pkg/A/BUILD.myext" <<eof
 filegroup(name = "all_files", srcs = glob(["**"]))
 eof
-  setup_module_dot_bazel "$pkg/A/MODULE.bazel"
 
   # Shut down the server afterwards so the test cleanup can remove $pkg/A.
   ( cd "$pkg/A"
diff --git a/src/test/shell/bazel/python_version_test.sh b/src/test/shell/bazel/python_version_test.sh
index e1ce747..e4eeec8 100755
--- a/src/test/shell/bazel/python_version_test.sh
+++ b/src/test/shell/bazel/python_version_test.sh
@@ -537,8 +537,7 @@
     ],
 )
 EOF
-  cat > MODULE.bazel <<EOF
-module(name="python_version_test")
+  cat >> $(setup_module_dot_bazel MODULE.bazel) <<EOF
 bazel_dep(name = "external_repo", version="0.0.0")
 local_path_override(
     module_name = "external_repo",
diff --git a/src/test/shell/bazel/resource_compiler_toolchain_test.sh b/src/test/shell/bazel/resource_compiler_toolchain_test.sh
index 7cc6084..e9c5c32 100755
--- a/src/test/shell/bazel/resource_compiler_toolchain_test.sh
+++ b/src/test/shell/bazel/resource_compiler_toolchain_test.sh
@@ -190,7 +190,7 @@
 }
 
 function test_toolchain_selection() {
-  echo "module(name = 'io_bazel')" >> MODULE.bazel
+  echo "module(name = 'io_bazel')" > MODULE.bazel
   add_platforms "MODULE.bazel"
   _symlink_res_toolchain_files
   _create_pkg
diff --git a/src/test/shell/bazel/starlark_git_repository_test.sh b/src/test/shell/bazel/starlark_git_repository_test.sh
index ba4cc37..362d45b 100755
--- a/src/test/shell/bazel/starlark_git_repository_test.sh
+++ b/src/test/shell/bazel/starlark_git_repository_test.sh
@@ -440,6 +440,7 @@
 function test_git_repository_not_refetched_on_server_restart() {
   local repo_dir=$TEST_TMPDIR/repos/refetch
 
+  rm MODULE.bazel
   cat >> MODULE.bazel <<EOF
 git_repository = use_repo_rule('@bazel_tools//tools/build_defs/repo:git.bzl', 'git_repository')
 git_repository(name='g', remote='$repo_dir', commit='22095302abaf776886879efa5129aa4d44c53017', verbose=True)
@@ -483,6 +484,7 @@
 function test_git_repository_not_refetched_on_server_restart_strip_prefix() {
   local repo_dir=$TEST_TMPDIR/repos/refetch
   # Change the strip_prefix which should cause a new checkout
+  rm MODULE.bazel
   cat >> MODULE.bazel <<EOF
 git_repository = use_repo_rule('@bazel_tools//tools/build_defs/repo:git.bzl', 'git_repository')
 git_repository(name='g', remote='$repo_dir', commit='17ea13b242e4cbcc27a6ef745939ebb7dcccea10', verbose=True)
@@ -505,6 +507,7 @@
 function test_git_repository_refetched_when_commit_changes() {
   local repo_dir=$TEST_TMPDIR/repos/refetch
 
+  rm MODULE.bazel
   cat >> MODULE.bazel <<EOF
 git_repository = use_repo_rule('@bazel_tools//tools/build_defs/repo:git.bzl', 'git_repository')
 git_repository(name='g', remote='$repo_dir', commit='22095302abaf776886879efa5129aa4d44c53017', verbose=True)
@@ -529,6 +532,7 @@
 function test_git_repository_and_nofetch() {
   local repo_dir=$TEST_TMPDIR/repos/refetch
 
+  rm MODULE.bazel
   cat >> MODULE.bazel <<EOF
 git_repository = use_repo_rule('@bazel_tools//tools/build_defs/repo:git.bzl', 'git_repository')
 git_repository(name='g', remote='$repo_dir', commit='22095302abaf776886879efa5129aa4d44c53017')
diff --git a/src/test/shell/bazel/starlark_repository_test.sh b/src/test/shell/bazel/starlark_repository_test.sh
index 063a4a0..deb9a12 100755
--- a/src/test/shell/bazel/starlark_repository_test.sh
+++ b/src/test/shell/bazel/starlark_repository_test.sh
@@ -1342,6 +1342,7 @@
 
 # Test native.bazel_version
 function test_bazel_version() {
+  cat > MODULE.bazel
   create_new_workspace
   repo2=$new_workspace_dir
 
diff --git a/src/test/shell/integration/aquery_test.sh b/src/test/shell/integration/aquery_test.sh
index 65a1eab..40479b9 100755
--- a/src/test/shell/integration/aquery_test.sh
+++ b/src/test/shell/integration/aquery_test.sh
@@ -1457,6 +1457,7 @@
 }
 
 function test_aquery_skyframe_state_with_filter_with_previous_build() {
+  cat > MODULE.bazel
   local pkg="${FUNCNAME[0]}"
   mkdir -p "$pkg" || fail "mkdir -p $pkg"
   cat > "$pkg/BUILD" <<'EOF'
diff --git a/src/test/shell/integration/load_removed_symbols_test.sh b/src/test/shell/integration/load_removed_symbols_test.sh
index 737338b..629c030 100755
--- a/src/test/shell/integration/load_removed_symbols_test.sh
+++ b/src/test/shell/integration/load_removed_symbols_test.sh
@@ -68,7 +68,7 @@
   cat > "${rules_java_workspace}/MODULE.bazel" << EOF
 module(name = "rules_java")
 EOF
-  cat >> MODULE.bazel << EOF
+  cat > MODULE.bazel << EOF
 bazel_dep(
     name = "rules_java",
 )
@@ -461,7 +461,6 @@
 }
 
 function test_legacy_globals() {
-  setup_module_dot_bazel
   mock_rules_java
 
   rules_java_workspace="${TEST_TMPDIR}/rules_java_workspace"
diff --git a/src/test/shell/testenv.sh b/src/test/shell/testenv.sh
index cfcc9e0..7a69e22 100755
--- a/src/test/shell/testenv.sh
+++ b/src/test/shell/testenv.sh
@@ -631,7 +631,9 @@
 # Note: this function echos the MODULE.bazel file path.
 function setup_module_dot_bazel() {
   module_dot_bazel=${1:-MODULE.bazel}
-  touch $module_dot_bazel
+  cat > $module_dot_bazel <<EOF
+module(name = 'test')
+EOF
   cp -f $(rlocation io_bazel/src/test/tools/bzlmod/MODULE.bazel.lock) "$(dirname ${module_dot_bazel})/MODULE.bazel.lock"
   echo $module_dot_bazel
 }