diff options
author | Ignas Anikevicius <240938+aignas@users.noreply.github.com> | 2023-12-20 09:08:33 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-20 00:08:33 +0000 |
commit | 87a3a54cd937b037f531fedac243350933dd1eb7 (patch) | |
tree | 097f6f25d968b005633972ae27161da1eeb8d450 | |
parent | 6246b8e6e1d5936e09172f87aab8117609a9892a (diff) | |
download | bazelbuild-rules_python-87a3a54cd937b037f531fedac243350933dd1eb7.tar.gz |
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.
-rw-r--r-- | examples/bzlmod/whl_mods/BUILD.bazel | 4 | ||||
-rw-r--r-- | examples/bzlmod/whl_mods/pip_whl_mods_test.py | 83 |
2 files changed, 46 insertions, 41 deletions
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 @@ py_test( 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 @@ from python.runfiles import runfiles 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 @@ class PipWhlModsTest(unittest.TestCase): 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 @@ class PipWhlModsTest(unittest.TestCase): 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 @@ class PipWhlModsTest(unittest.TestCase): 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 @@ class PipWhlModsTest(unittest.TestCase): # 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()) |