test(bzlmod): refactor tests to not depend on implementation details (#1628)

This refactors the `whl_mods` tests to not rely on the layout of the
repositories, which I found to be needed whilst prototyping on #1625.
Whilst doing this I realized that in general it would be great to
support `Path` instances in the `runfiles` library, but that should be
done next time.
diff --git a/examples/bzlmod/whl_mods/BUILD.bazel b/examples/bzlmod/whl_mods/BUILD.bazel
index 6ca07dd..241d9c1 100644
--- a/examples/bzlmod/whl_mods/BUILD.bazel
+++ b/examples/bzlmod/whl_mods/BUILD.bazel
@@ -9,8 +9,8 @@
     name = "pip_whl_mods_test",
     srcs = ["pip_whl_mods_test.py"],
     env = {
-        "REQUESTS_PKG_DIR": "pip_39_requests",
-        "WHEEL_PKG_DIR": "pip_39_wheel",
+        "REQUESTS_PKG": "$(rlocationpaths @pip//requests:pkg)",
+        "WHEEL_PKG": "$(rlocationpaths @pip//wheel:pkg)",
     },
     main = "pip_whl_mods_test.py",
     deps = [
diff --git a/examples/bzlmod/whl_mods/pip_whl_mods_test.py b/examples/bzlmod/whl_mods/pip_whl_mods_test.py
index c739b80..a88134b 100644
--- a/examples/bzlmod/whl_mods/pip_whl_mods_test.py
+++ b/examples/bzlmod/whl_mods/pip_whl_mods_test.py
@@ -27,22 +27,30 @@
 class PipWhlModsTest(unittest.TestCase):
     maxDiff = None
 
-    def package_path(self) -> str:
-        return "rules_python~override~pip~"
+    @staticmethod
+    def _get_bazel_pkg_dir_name(env_var: str) -> str:
+        a_file = Path(os.environ.get(env_var).split(" ")[0])
+        head = a_file
+        while head.parent.name:
+            head = head.parent
 
-    def wheel_pkg_dir(self) -> str:
-        env = os.environ.get("WHEEL_PKG_DIR")
-        self.assertIsNotNone(env)
-        return env
+        return head.name
+
+    @classmethod
+    def setUpClass(cls):
+        cls._wheel_pkg_dir = cls._get_bazel_pkg_dir_name("WHEEL_PKG")
+        cls._requests_pkg_dir = cls._get_bazel_pkg_dir_name("REQUESTS_PKG")
+
+    def wheel_pkg_dir(self) -> Path:
+        return self._wheel_pkg
 
     def test_build_content_and_data(self):
         r = runfiles.Create()
         rpath = r.Rlocation(
-                "{}{}/generated_file.txt".format(
-                    self.package_path(), 
-                    self.wheel_pkg_dir(),
-                    ),
-                )
+            "{}/generated_file.txt".format(
+                self._wheel_pkg_dir,
+            ),
+        )
         generated_file = Path(rpath)
         self.assertTrue(generated_file.exists())
 
@@ -52,11 +60,10 @@
     def test_copy_files(self):
         r = runfiles.Create()
         rpath = r.Rlocation(
-                "{}{}/copied_content/file.txt".format(
-                    self.package_path(), 
-                    self.wheel_pkg_dir(),
-                    )
-                )
+            "{}/copied_content/file.txt".format(
+                self._wheel_pkg_dir,
+            )
+        )
         copied_file = Path(rpath)
         self.assertTrue(copied_file.exists())
 
@@ -64,14 +71,17 @@
         self.assertEqual(content, "Hello world from copied file")
 
     def test_copy_executables(self):
+        executable_name = (
+            "executable.exe" if platform.system() == "windows" else "executable.py"
+        )
+
         r = runfiles.Create()
         rpath = r.Rlocation(
-                "{}{}/copied_content/executable{}".format(
-                    self.package_path(), 
-                    self.wheel_pkg_dir(),
-                    ".exe" if platform.system() == "windows" else ".py",
-                    )
-                )
+            "{}/copied_content/{}".format(
+                self._wheel_pkg_dir,
+                executable_name,
+            )
+        )
         executable = Path(rpath)
         self.assertTrue(executable.exists())
 
@@ -88,11 +98,10 @@
         current_wheel_version = "0.40.0"
 
         r = runfiles.Create()
-        dist_info_dir = "{}{}/site-packages/wheel-{}.dist-info".format(
-                self.package_path(), 
-                self.wheel_pkg_dir(),
-                current_wheel_version,
-                )
+        dist_info_dir = "{}/site-packages/wheel-{}.dist-info".format(
+            self._wheel_pkg_dir,
+            current_wheel_version,
+        )
 
         # Note: `METADATA` is important as it's consumed by https://docs.python.org/3/library/importlib.metadata.html
         # `METADATA` is expected to be there to show dist-info files are included in the runfiles.
@@ -101,24 +110,20 @@
         # However, `WHEEL` was explicitly excluded, so it should be missing
         wheel_path = r.Rlocation("{}/WHEEL".format(dist_info_dir))
 
-        self.assertTrue(Path(metadata_path).exists())
-        self.assertFalse(Path(wheel_path).exists())
-
-    def requests_pkg_dir(self) -> str:
-        env = os.environ.get("REQUESTS_PKG_DIR")
-        self.assertIsNotNone(env)
-        return env
+        self.assertTrue(Path(metadata_path).exists(), f"Could not find {metadata_path}")
+        self.assertFalse(
+            Path(wheel_path).exists(), f"Expected to not find {wheel_path}"
+        )
 
     def test_extra(self):
         # This test verifies that annotations work correctly for pip packages with extras
         # specified, in this case requests[security].
         r = runfiles.Create()
         rpath = r.Rlocation(
-                "{}{}/generated_file.txt".format(
-                    self.package_path(), 
-                    self.requests_pkg_dir(),
-                    ),
-                )
+            "{}/generated_file.txt".format(
+                self._requests_pkg_dir,
+            ),
+        )
         generated_file = Path(rpath)
         self.assertTrue(generated_file.exists())