Skip to content
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
1 change: 1 addition & 0 deletions examples/pip_install/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ py_test(
"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,
Expand Down
2 changes: 1 addition & 1 deletion examples/pip_install/WORKSPACE
Original file line number Diff line number Diff line change
@@ -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")

Expand Down
20 changes: 15 additions & 5 deletions examples/pip_install/pip_install_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import unittest
from pathlib import Path

from rules_python.python.runfiles import runfiles


class PipInstallTest(unittest.TestCase):
maxDiff = None
Expand All @@ -13,11 +15,16 @@ def test_entry_point_void_return(self):
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,
Expand All @@ -36,13 +43,16 @@ def test_entry_point_void_return(self):

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,
Expand Down
1 change: 1 addition & 0 deletions examples/pip_parse/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,5 @@ py_test(
"WHEEL_DIST_INFO_CONTENTS": "$(rootpaths {})".format(dist_info_requirement("requests")),
"YAMLLINT_ENTRY_POINT": "$(rootpath :yamllint)",
},
deps = ["@rules_python//python/runfiles"],
)
2 changes: 1 addition & 1 deletion examples/pip_parse/WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
workspace(name = "example_repo")
workspace(name = "rules_python_pip_parse_example")

local_repository(
name = "rules_python",
Expand Down
14 changes: 11 additions & 3 deletions examples/pip_parse/pip_parse_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import unittest
from pathlib import Path

from rules_python.python.runfiles import runfiles


class PipInstallTest(unittest.TestCase):
maxDiff = None
Expand All @@ -13,11 +15,14 @@ def test_entry_point_void_return(self):
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,
Expand All @@ -38,7 +43,10 @@ def test_entry_point_int_return(self):
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(
Expand Down
10 changes: 8 additions & 2 deletions examples/pip_repository_annotations/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ py_test(
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(
name = "pip_install_annotations_test",
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",
],
)
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,43 +19,77 @@ def wheel_pkg_dir(self) -> str:
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__":
Expand Down