Make rules_testing available for Bazel tests

Includes an update of bazel_skylib to 1.3.0 as the (very old) version 1.0.3 wasn't compatible with rules_testing.

Work towards #1920

Closes #18182.

PiperOrigin-RevId: 527021012
Change-Id: I80ebff1a1aa656974ad6a1bb1809d8f919a2fa22
diff --git a/MODULE.bazel b/MODULE.bazel
index 19f75c7..39fa31c 100644
--- a/MODULE.bazel
+++ b/MODULE.bazel
@@ -20,6 +20,7 @@
 bazel_dep(name = "rules_proto", version = "5.3.0-21.7")
 bazel_dep(name = "rules_jvm_external", version = "4.5")
 bazel_dep(name = "rules_python", version = "0.19.0")
+bazel_dep(name = "rules_testing", version = "0.0.4")
 
 single_version_override(
     module_name = "rules_jvm_external",
diff --git a/WORKSPACE b/WORKSPACE
index 8eccaae..1488fd3 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -23,6 +23,12 @@
     "echo 'exports_files([\"WORKSPACE\"], visibility = [\"//visibility:public\"])' >> BUILD.bazel",
 ]
 
+EXPORT_WORKSPACE_BAZEL_IN_BUILD_FILE = [
+    "test -f BUILD && chmod u+w BUILD || true",
+    "echo >> BUILD",
+    "echo 'exports_files([\"WORKSPACE.bazel\"], visibility = [\"//visibility:public\"])' >> BUILD",
+]
+
 EXPORT_WORKSPACE_IN_BUILD_FILE_WIN = [
     "Add-Content -Path BUILD -Value \"`nexports_files([`\"WORKSPACE`\"], visibility = [`\"//visibility:public`\"])`n\" -Force",
 ]
@@ -31,6 +37,10 @@
     "Add-Content -Path BUILD.bazel -Value \"`nexports_files([`\"WORKSPACE`\"], visibility = [`\"//visibility:public`\"])`n\" -Force",
 ]
 
+EXPORT_WORKSPACE_BAZEL_IN_BUILD_FILE_WIN = [
+    "Add-Content -Path BUILD -Value \"`nexports_files([`\"WORKSPACE.bazel`\"], visibility = [`\"//visibility:public`\"])`n\" -Force",
+]
+
 # Protobuf expects an //external:python_headers label which would contain the
 # Python headers if fast Python protos is enabled. Since we are not using fast
 # Python protos, bind python_headers to a dummy target.
@@ -566,6 +576,12 @@
     name = "rules_jvm_external",
 )
 
+dist_http_archive(
+    name = "rules_testing",
+    patch_cmds = EXPORT_WORKSPACE_BAZEL_IN_BUILD_FILE,
+    patch_cmds_win = EXPORT_WORKSPACE_BAZEL_IN_BUILD_FILE_WIN,
+)
+
 # Projects using gRPC as an external dependency must call both grpc_deps() and
 # grpc_extra_deps().
 load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps")
diff --git a/distdir_deps.bzl b/distdir_deps.bzl
index a0f4698..1133dc3 100644
--- a/distdir_deps.bzl
+++ b/distdir_deps.bzl
@@ -289,17 +289,17 @@
     #
     ###################################################
     "bazel_skylib": {
-        "archive": "bazel-skylib-1.0.3.tar.gz",
-        "sha256": "1c531376ac7e5a180e0237938a2536de0c54d93f5c278634818e0efc952dd56c",
+        "archive": "bazel-skylib-1.3.0.tar.gz",
+        "sha256": "74d544d96f4a5bb630d465ca8bbcfe231e3594e5aae57e1edbf17a6eb3ca2506",
         "urls": [
-            "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.0.3/bazel-skylib-1.0.3.tar.gz",
-            "https://github.com/bazelbuild/bazel-skylib/releases/download/1.0.3/bazel-skylib-1.0.3.tar.gz",
+            "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.3.0/bazel-skylib-1.3.0.tar.gz",
+            "https://github.com/bazelbuild/bazel-skylib/releases/download/1.3.0/bazel-skylib-1.3.0.tar.gz",
         ],
         "used_in": [
             "additional_distfiles",
             "test_WORKSPACE_files",
         ],
-        "package_version": "1.0.3",
+        "package_version": "1.3.0",
     },
     "io_bazel_skydoc": {
         "archive": "1ef781ced3b1443dca3ed05dec1989eca1a4e1cd.tar.gz",
@@ -362,6 +362,14 @@
         "archive": "rules_python-0.19.0.tar.gz",
         "used_in": ["additional_distfiles", "test_WORKSPACE_files"],
     },
+    "rules_testing": {
+        "sha256": "4e21f9aa7996944ce91431f27bca374bff56e680acfe497276074d56bc5d9af2",
+        "strip_prefix": "rules_testing-0.0.4",
+        "urls": ["https://github.com/bazelbuild/rules_testing/releases/download/v0.0.4/rules_testing-v0.0.4.tar.gz"],
+        "archive": "rules_testing-v0.0.4.tar.gz",
+        "used_in": ["additional_distfiles", "test_WORKSPACE_files"],
+        "package_version": "0.0.4",
+    },
     "desugar_jdk_libs": {
         # Commit 5847d6a06302136d95a14b4cbd4b55a9c9f1436e of 2021-03-10
         "archive": "5847d6a06302136d95a14b4cbd4b55a9c9f1436e.zip",
diff --git a/src/BUILD b/src/BUILD
index 64cf6e7..0913b95 100644
--- a/src/BUILD
+++ b/src/BUILD
@@ -587,6 +587,7 @@
         "@rules_pkg//:WORKSPACE",
         "@rules_proto//:WORKSPACE",
         "@rules_python//:WORKSPACE",
+        "@rules_testing//:WORKSPACE.bazel",
     ] + [
         "@openjdk%s_%s_archive//:WORKSPACE" % (version, os)
         for version in ("17", "20")
diff --git a/src/test/shell/BUILD b/src/test/shell/BUILD
index 113a81b..85de0e1 100644
--- a/src/test/shell/BUILD
+++ b/src/test/shell/BUILD
@@ -26,12 +26,14 @@
     name = "testenv_sh",
     out = "testenv.sh",
     repos = [
+        "bazel_skylib",
         "rules_cc",
         "rules_java",
         "rules_license",
         "rules_pkg",
         "rules_proto",
         "rules_python",
+        "rules_testing",
     ],
     template = "testenv.sh.tmpl",
     visibility = ["//visibility:public"],
diff --git a/src/test/shell/testenv.sh.tmpl b/src/test/shell/testenv.sh.tmpl
index a50b8bf..3cb2072 100755
--- a/src/test/shell/testenv.sh.tmpl
+++ b/src/test/shell/testenv.sh.tmpl
@@ -512,18 +512,10 @@
 }
 
 function setup_skylib_support() {
-  # Get skylib path portably by using rlocation to locate a top-level file in
-  # the repo. Use BUILD because it's in the //:test_deps target (unlike
-  # WORKSPACE).
-  local -r skylib_workspace="$(rlocation bazel_skylib/BUILD)"
-  [[ -n "$skylib_workspace" && -e "$skylib_workspace" ]] || fail "could not find Skylib"
-  local -r skylib_root="$(dirname "$skylib_workspace")"
   cat >> WORKSPACE << EOF
-new_local_repository(
-    name = 'bazel_skylib',
-    build_file_content = '',
-    path='$skylib_root',
-)
+load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
+
+{bazel_skylib}
 
 load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
 bazel_skylib_workspace()
@@ -578,6 +570,14 @@
 EOF
 }
 
+function add_rules_testing_to_workspace() {
+  cat >> "$1"<<EOF
+load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
+
+{rules_testing}
+EOF
+}
+
 function create_workspace_with_default_repos() {
   write_workspace_file "${1:-WORKSPACE}" "${2:-main}"
   echo "$1"