Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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