Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 53c7bdd

Browse filesBrowse files
Use @rules_python//python/runfiles for examples and tests (bazel-contrib#614)
Co-authored-by: Alex Eagle <eagle@post.harvard.edu>
1 parent f99ea2a commit 53c7bdd
Copy full SHA for 53c7bdd

File tree

8 files changed

+90
-27
lines changed
Filter options

8 files changed

+90
-27
lines changed

‎examples/pip_install/BUILD

Copy file name to clipboardExpand all lines: examples/pip_install/BUILD
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ py_test(
8888
"WHEEL_DIST_INFO_CONTENTS": "$(rootpaths {})".format(dist_info_requirement("boto3")),
8989
"YAMLLINT_ENTRY_POINT": "$(rootpath :yamllint)",
9090
},
91+
deps = ["@rules_python//python/runfiles"],
9192
)
9293

9394
# Assert that tags are present on resulting py_library,

‎examples/pip_install/WORKSPACE

Copy file name to clipboardExpand all lines: examples/pip_install/WORKSPACE
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
workspace(name = "example_repo")
1+
workspace(name = "rules_python_pip_install_example")
22

33
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
44

‎examples/pip_install/pip_install_test.py

Copy file name to clipboardExpand all lines: examples/pip_install/pip_install_test.py
+15-5Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import unittest
66
from pathlib import Path
77

8+
from rules_python.python.runfiles import runfiles
9+
810

911
class PipInstallTest(unittest.TestCase):
1012
maxDiff = None
@@ -13,11 +15,16 @@ def test_entry_point_void_return(self):
1315
env = os.environ.get("YAMLLINT_ENTRY_POINT")
1416
self.assertIsNotNone(env)
1517

16-
entry_point = Path(env)
18+
r = runfiles.Create()
19+
20+
# To find an external target, this must use `{workspace_name}/$(rootpath @external_repo//:target)`
21+
entry_point = Path(
22+
r.Rlocation("rules_python_pip_install_example/{}".format(env))
23+
)
1724
self.assertTrue(entry_point.exists())
1825

1926
proc = subprocess.run(
20-
[entry_point, "--version"],
27+
[str(entry_point), "--version"],
2128
check=True,
2229
stdout=subprocess.PIPE,
2330
stderr=subprocess.PIPE,
@@ -36,13 +43,16 @@ def test_entry_point_void_return(self):
3643

3744
def test_entry_point_int_return(self):
3845
env = os.environ.get("SPHINX_BUILD_ENTRY_POINT")
39-
self.assertIsNotNone(env)
46+
r = runfiles.Create()
4047

41-
entry_point = Path(env)
48+
# To find an external target, this must use `{workspace_name}/$(rootpath @external_repo//:target)`
49+
entry_point = Path(
50+
r.Rlocation("rules_python_pip_install_example/{}".format(env))
51+
)
4252
self.assertTrue(entry_point.exists())
4353

4454
proc = subprocess.run(
45-
[entry_point, "--version"],
55+
[str(entry_point), "--version"],
4656
check=True,
4757
stdout=subprocess.PIPE,
4858
stderr=subprocess.PIPE,

‎examples/pip_parse/BUILD

Copy file name to clipboardExpand all lines: examples/pip_parse/BUILD
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,5 @@ py_test(
8787
"WHEEL_DIST_INFO_CONTENTS": "$(rootpaths {})".format(dist_info_requirement("requests")),
8888
"YAMLLINT_ENTRY_POINT": "$(rootpath :yamllint)",
8989
},
90+
deps = ["@rules_python//python/runfiles"],
9091
)

‎examples/pip_parse/WORKSPACE

Copy file name to clipboardExpand all lines: examples/pip_parse/WORKSPACE
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
workspace(name = "example_repo")
1+
workspace(name = "rules_python_pip_parse_example")
22

33
local_repository(
44
name = "rules_python",

‎examples/pip_parse/pip_parse_test.py

Copy file name to clipboardExpand all lines: examples/pip_parse/pip_parse_test.py
+11-3Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import unittest
66
from pathlib import Path
77

8+
from rules_python.python.runfiles import runfiles
9+
810

911
class PipInstallTest(unittest.TestCase):
1012
maxDiff = None
@@ -13,11 +15,14 @@ def test_entry_point_void_return(self):
1315
env = os.environ.get("YAMLLINT_ENTRY_POINT")
1416
self.assertIsNotNone(env)
1517

16-
entry_point = Path(env)
18+
r = runfiles.Create()
19+
20+
# To find an external target, this must use `{workspace_name}/$(rootpath @external_repo//:target)`
21+
entry_point = Path(r.Rlocation("rules_python_pip_parse_example/{}".format(env)))
1722
self.assertTrue(entry_point.exists())
1823

1924
proc = subprocess.run(
20-
[entry_point, "--version"],
25+
[str(entry_point), "--version"],
2126
check=True,
2227
stdout=subprocess.PIPE,
2328
stderr=subprocess.PIPE,
@@ -38,7 +43,10 @@ def test_entry_point_int_return(self):
3843
env = os.environ.get("SPHINX_BUILD_ENTRY_POINT")
3944
self.assertIsNotNone(env)
4045

41-
entry_point = Path(env)
46+
r = runfiles.Create()
47+
48+
# To find an external target, this must use `{workspace_name}/$(rootpath @external_repo//:target)`
49+
entry_point = Path(r.Rlocation("rules_python_pip_parse_example/{}".format(env)))
4250
self.assertTrue(entry_point.exists())
4351

4452
proc = subprocess.run(

‎examples/pip_repository_annotations/BUILD

Copy file name to clipboardExpand all lines: examples/pip_repository_annotations/BUILD
+8-2Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,19 @@ py_test(
1818
srcs = ["pip_repository_annotations_test.py"],
1919
env = {"WHEEL_PKG_DIR": "pip_parsed_wheel"},
2020
main = "pip_repository_annotations_test.py",
21-
deps = ["@pip_parsed_wheel//:pkg"],
21+
deps = [
22+
"@pip_parsed_wheel//:pkg",
23+
"@rules_python//python/runfiles",
24+
],
2225
)
2326

2427
py_test(
2528
name = "pip_install_annotations_test",
2629
srcs = ["pip_repository_annotations_test.py"],
2730
env = {"WHEEL_PKG_DIR": "pip_installed/pypi__wheel"},
2831
main = "pip_repository_annotations_test.py",
29-
deps = [requirement("wheel")],
32+
deps = [
33+
requirement("wheel"),
34+
"@rules_python//python/runfiles",
35+
],
3036
)

‎examples/pip_repository_annotations/pip_repository_annotations_test.py

Copy file name to clipboardExpand all lines: examples/pip_repository_annotations/pip_repository_annotations_test.py
+52-15Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
#!/usr/bin/env python3
22

33
import os
4+
import platform
45
import subprocess
6+
import sys
57
import unittest
6-
from glob import glob
78
from pathlib import Path
89

10+
from rules_python.python.runfiles import runfiles
11+
912

1013
class PipRepositoryAnnotationsTest(unittest.TestCase):
1114
maxDiff = None
@@ -16,43 +19,77 @@ def wheel_pkg_dir(self) -> str:
1619
return env
1720

1821
def test_build_content_and_data(self):
19-
generated_file = (
20-
Path.cwd() / "external" / self.wheel_pkg_dir() / "generated_file.txt"
22+
r = runfiles.Create()
23+
rpath = r.Rlocation(
24+
"pip_repository_annotations_example/external/{}/generated_file.txt".format(
25+
self.wheel_pkg_dir()
26+
)
2127
)
28+
generated_file = Path(rpath)
2229
self.assertTrue(generated_file.exists())
2330

2431
content = generated_file.read_text().rstrip()
2532
self.assertEqual(content, "Hello world from build content file")
2633

2734
def test_copy_files(self):
28-
copied_file = (
29-
Path.cwd() / "external" / self.wheel_pkg_dir() / "copied_content/file.txt"
35+
r = runfiles.Create()
36+
rpath = r.Rlocation(
37+
"pip_repository_annotations_example/external/{}/copied_content/file.txt".format(
38+
self.wheel_pkg_dir()
39+
)
3040
)
41+
copied_file = Path(rpath)
3142
self.assertTrue(copied_file.exists())
3243

3344
content = copied_file.read_text().rstrip()
3445
self.assertEqual(content, "Hello world from copied file")
3546

3647
def test_copy_executables(self):
37-
executable = (
38-
Path.cwd()
39-
/ "external"
40-
/ self.wheel_pkg_dir()
41-
/ "copied_content/executable.py"
48+
r = runfiles.Create()
49+
rpath = r.Rlocation(
50+
"pip_repository_annotations_example/external/{}/copied_content/executable{}".format(
51+
self.wheel_pkg_dir(),
52+
".exe" if platform.system() == "windows" else ".py",
53+
)
4254
)
55+
executable = Path(rpath)
4356
self.assertTrue(executable.exists())
4457

4558
proc = subprocess.run(
46-
[executable], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
59+
[sys.executable, str(executable)],
60+
check=True,
61+
stdout=subprocess.PIPE,
62+
stderr=subprocess.PIPE,
4763
)
4864
stdout = proc.stdout.decode("utf-8").strip()
4965
self.assertEqual(stdout, "Hello world from copied executable")
5066

5167
def test_data_exclude_glob(self):
52-
files = glob("external/" + self.wheel_pkg_dir() + "/wheel-*.dist-info/*")
53-
basenames = [Path(path).name for path in files]
54-
self.assertIn("WHEEL", basenames)
55-
self.assertNotIn("RECORD", basenames)
68+
current_wheel_version = "0.37.1"
69+
70+
r = runfiles.Create()
71+
dist_info_dir = (
72+
"pip_repository_annotations_example/external/{}/wheel-{}.dist-info".format(
73+
self.wheel_pkg_dir(),
74+
current_wheel_version,
75+
)
76+
)
77+
78+
# `WHEEL` is expected to be there to show dist-info files are included in the runfiles
79+
wheel_path = r.Rlocation("{}/WHEEL".format(dist_info_dir))
80+
81+
# However, `RECORD` was explicitly excluded, so it should be missing
82+
record_path = r.Rlocation("{}/RECORD".format(dist_info_dir))
83+
84+
# Because windows does not have `--enable_runfiles` on by default, the
85+
# `runfiles.Rlocation` results will be different on this platform vs
86+
# unix platforms. See `@rules_python//python/runfiles` for more details.
87+
if platform.system() == "Windows":
88+
self.assertIsNotNone(wheel_path)
89+
self.assertIsNone(record_path)
90+
else:
91+
self.assertTrue(Path(wheel_path).exists())
92+
self.assertFalse(Path(record_path).exists())
5693

5794

5895
if __name__ == "__main__":

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.