Skip to content

Commit

Permalink
test(bzlmod): refactor tests to not depend on implementation details (#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
aignas authored Dec 20, 2023
1 parent 6246b8e commit 87a3a54
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 41 deletions.
4 changes: 2 additions & 2 deletions examples/bzlmod/whl_mods/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
83 changes: 44 additions & 39 deletions examples/bzlmod/whl_mods/pip_whl_mods_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand All @@ -52,26 +60,28 @@ def test_build_content_and_data(self):
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())

content = copied_file.read_text().rstrip()
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())

Expand All @@ -88,11 +98,10 @@ def test_data_exclude_glob(self):
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.
Expand All @@ -101,24 +110,20 @@ def test_data_exclude_glob(self):
# 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())

Expand Down

0 comments on commit 87a3a54

Please sign in to comment.