Use `@rules_python//python/runfiles` for examples and tests (#614)
Co-authored-by: Alex Eagle <eagle@post.harvard.edu>
diff --git a/examples/pip_install/BUILD b/examples/pip_install/BUILD
index 10015c9..d112dc3 100644
--- a/examples/pip_install/BUILD
+++ b/examples/pip_install/BUILD
@@ -88,6 +88,7 @@
"WHEEL_DIST_INFO_CONTENTS": "$(rootpaths {})".format(dist_info_requirement("boto3")),
"YAMLLINT_ENTRY_POINT": "$(rootpath :yamllint)",
},
+ deps = ["@rules_python//python/runfiles"],
)
# Assert that tags are present on resulting py_library,
diff --git a/examples/pip_install/WORKSPACE b/examples/pip_install/WORKSPACE
index bde77c4..db5d378 100644
--- a/examples/pip_install/WORKSPACE
+++ b/examples/pip_install/WORKSPACE
@@ -1,4 +1,4 @@
-workspace(name = "example_repo")
+workspace(name = "rules_python_pip_install_example")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
diff --git a/examples/pip_install/pip_install_test.py b/examples/pip_install/pip_install_test.py
index e865dea..6c78f2d 100644
--- a/examples/pip_install/pip_install_test.py
+++ b/examples/pip_install/pip_install_test.py
@@ -5,6 +5,8 @@
import unittest
from pathlib import Path
+from rules_python.python.runfiles import runfiles
+
class PipInstallTest(unittest.TestCase):
maxDiff = None
@@ -13,11 +15,16 @@
env = os.environ.get("YAMLLINT_ENTRY_POINT")
self.assertIsNotNone(env)
- entry_point = Path(env)
+ r = runfiles.Create()
+
+ # To find an external target, this must use `{workspace_name}/$(rootpath @external_repo//:target)`
+ entry_point = Path(
+ r.Rlocation("rules_python_pip_install_example/{}".format(env))
+ )
self.assertTrue(entry_point.exists())
proc = subprocess.run(
- [entry_point, "--version"],
+ [str(entry_point), "--version"],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
@@ -36,13 +43,16 @@
def test_entry_point_int_return(self):
env = os.environ.get("SPHINX_BUILD_ENTRY_POINT")
- self.assertIsNotNone(env)
+ r = runfiles.Create()
- entry_point = Path(env)
+ # To find an external target, this must use `{workspace_name}/$(rootpath @external_repo//:target)`
+ entry_point = Path(
+ r.Rlocation("rules_python_pip_install_example/{}".format(env))
+ )
self.assertTrue(entry_point.exists())
proc = subprocess.run(
- [entry_point, "--version"],
+ [str(entry_point), "--version"],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
diff --git a/examples/pip_parse/BUILD b/examples/pip_parse/BUILD
index d0f0529..43bd2f8 100644
--- a/examples/pip_parse/BUILD
+++ b/examples/pip_parse/BUILD
@@ -87,4 +87,5 @@
"WHEEL_DIST_INFO_CONTENTS": "$(rootpaths {})".format(dist_info_requirement("requests")),
"YAMLLINT_ENTRY_POINT": "$(rootpath :yamllint)",
},
+ deps = ["@rules_python//python/runfiles"],
)
diff --git a/examples/pip_parse/WORKSPACE b/examples/pip_parse/WORKSPACE
index be1c8c7..986e4fb 100644
--- a/examples/pip_parse/WORKSPACE
+++ b/examples/pip_parse/WORKSPACE
@@ -1,4 +1,4 @@
-workspace(name = "example_repo")
+workspace(name = "rules_python_pip_parse_example")
local_repository(
name = "rules_python",
diff --git a/examples/pip_parse/pip_parse_test.py b/examples/pip_parse/pip_parse_test.py
index 8d8846a..6465fd6 100644
--- a/examples/pip_parse/pip_parse_test.py
+++ b/examples/pip_parse/pip_parse_test.py
@@ -5,6 +5,8 @@
import unittest
from pathlib import Path
+from rules_python.python.runfiles import runfiles
+
class PipInstallTest(unittest.TestCase):
maxDiff = None
@@ -13,11 +15,14 @@
env = os.environ.get("YAMLLINT_ENTRY_POINT")
self.assertIsNotNone(env)
- entry_point = Path(env)
+ r = runfiles.Create()
+
+ # To find an external target, this must use `{workspace_name}/$(rootpath @external_repo//:target)`
+ entry_point = Path(r.Rlocation("rules_python_pip_parse_example/{}".format(env)))
self.assertTrue(entry_point.exists())
proc = subprocess.run(
- [entry_point, "--version"],
+ [str(entry_point), "--version"],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
@@ -38,7 +43,10 @@
env = os.environ.get("SPHINX_BUILD_ENTRY_POINT")
self.assertIsNotNone(env)
- entry_point = Path(env)
+ r = runfiles.Create()
+
+ # To find an external target, this must use `{workspace_name}/$(rootpath @external_repo//:target)`
+ entry_point = Path(r.Rlocation("rules_python_pip_parse_example/{}".format(env)))
self.assertTrue(entry_point.exists())
proc = subprocess.run(
diff --git a/examples/pip_repository_annotations/BUILD b/examples/pip_repository_annotations/BUILD
index a5a0561..8c69c40 100644
--- a/examples/pip_repository_annotations/BUILD
+++ b/examples/pip_repository_annotations/BUILD
@@ -18,7 +18,10 @@
srcs = ["pip_repository_annotations_test.py"],
env = {"WHEEL_PKG_DIR": "pip_parsed_wheel"},
main = "pip_repository_annotations_test.py",
- deps = ["@pip_parsed_wheel//:pkg"],
+ deps = [
+ "@pip_parsed_wheel//:pkg",
+ "@rules_python//python/runfiles",
+ ],
)
py_test(
@@ -26,5 +29,8 @@
srcs = ["pip_repository_annotations_test.py"],
env = {"WHEEL_PKG_DIR": "pip_installed/pypi__wheel"},
main = "pip_repository_annotations_test.py",
- deps = [requirement("wheel")],
+ deps = [
+ requirement("wheel"),
+ "@rules_python//python/runfiles",
+ ],
)
diff --git a/examples/pip_repository_annotations/pip_repository_annotations_test.py b/examples/pip_repository_annotations/pip_repository_annotations_test.py
index a8f0863..f904015 100644
--- a/examples/pip_repository_annotations/pip_repository_annotations_test.py
+++ b/examples/pip_repository_annotations/pip_repository_annotations_test.py
@@ -1,11 +1,14 @@
#!/usr/bin/env python3
import os
+import platform
import subprocess
+import sys
import unittest
-from glob import glob
from pathlib import Path
+from rules_python.python.runfiles import runfiles
+
class PipRepositoryAnnotationsTest(unittest.TestCase):
maxDiff = None
@@ -16,43 +19,77 @@
return env
def test_build_content_and_data(self):
- generated_file = (
- Path.cwd() / "external" / self.wheel_pkg_dir() / "generated_file.txt"
+ r = runfiles.Create()
+ rpath = r.Rlocation(
+ "pip_repository_annotations_example/external/{}/generated_file.txt".format(
+ self.wheel_pkg_dir()
+ )
)
+ generated_file = Path(rpath)
self.assertTrue(generated_file.exists())
content = generated_file.read_text().rstrip()
self.assertEqual(content, "Hello world from build content file")
def test_copy_files(self):
- copied_file = (
- Path.cwd() / "external" / self.wheel_pkg_dir() / "copied_content/file.txt"
+ r = runfiles.Create()
+ rpath = r.Rlocation(
+ "pip_repository_annotations_example/external/{}/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 = (
- Path.cwd()
- / "external"
- / self.wheel_pkg_dir()
- / "copied_content/executable.py"
+ r = runfiles.Create()
+ rpath = r.Rlocation(
+ "pip_repository_annotations_example/external/{}/copied_content/executable{}".format(
+ self.wheel_pkg_dir(),
+ ".exe" if platform.system() == "windows" else ".py",
+ )
)
+ executable = Path(rpath)
self.assertTrue(executable.exists())
proc = subprocess.run(
- [executable], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
+ [sys.executable, str(executable)],
+ check=True,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
)
stdout = proc.stdout.decode("utf-8").strip()
self.assertEqual(stdout, "Hello world from copied executable")
def test_data_exclude_glob(self):
- files = glob("external/" + self.wheel_pkg_dir() + "/wheel-*.dist-info/*")
- basenames = [Path(path).name for path in files]
- self.assertIn("WHEEL", basenames)
- self.assertNotIn("RECORD", basenames)
+ current_wheel_version = "0.37.1"
+
+ r = runfiles.Create()
+ dist_info_dir = (
+ "pip_repository_annotations_example/external/{}/wheel-{}.dist-info".format(
+ self.wheel_pkg_dir(),
+ current_wheel_version,
+ )
+ )
+
+ # `WHEEL` is expected to be there to show dist-info files are included in the runfiles
+ wheel_path = r.Rlocation("{}/WHEEL".format(dist_info_dir))
+
+ # However, `RECORD` was explicitly excluded, so it should be missing
+ record_path = r.Rlocation("{}/RECORD".format(dist_info_dir))
+
+ # Because windows does not have `--enable_runfiles` on by default, the
+ # `runfiles.Rlocation` results will be different on this platform vs
+ # unix platforms. See `@rules_python//python/runfiles` for more details.
+ if platform.system() == "Windows":
+ self.assertIsNotNone(wheel_path)
+ self.assertIsNone(record_path)
+ else:
+ self.assertTrue(Path(wheel_path).exists())
+ self.assertFalse(Path(record_path).exists())
if __name__ == "__main__":