Skip to content

Commit 821a323

Browse files
authored
tests: make pip_parse example work with bzlmod enabled (#1524)
Bazel is enabling bzlmod by default, which means the examples need to be updated to be bzlmod compatible. Work towards #1520
1 parent f848f11 commit 821a323

File tree

4 files changed

+62
-36
lines changed

4 files changed

+62
-36
lines changed

examples/pip_parse/BUILD.bazel

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
load(
2-
"@pypi//:requirements.bzl",
3-
"data_requirement",
4-
"dist_info_requirement",
5-
"entry_point",
6-
)
71
load("@rules_python//python:defs.bzl", "py_binary", "py_test")
82
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
3+
load("@rules_python//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary")
94

105
# Toolchain setup, this is optional.
116
# Demonstrate that we can use the same python interpreter for the toolchain and executing pip in pip install (see WORKSPACE).
@@ -37,7 +32,7 @@ py_binary(
3732
name = "main",
3833
srcs = ["main.py"],
3934
deps = [
40-
"@pypi_requests//:pkg",
35+
"@pypi//requests:pkg",
4136
],
4237
)
4338

@@ -50,9 +45,9 @@ py_test(
5045
# For pip dependencies which have entry points, the `entry_point` macro can be
5146
# used from the generated `pip_parse` repository to access a runnable binary.
5247

53-
alias(
48+
py_console_script_binary(
5449
name = "yamllint",
55-
actual = entry_point("yamllint"),
50+
pkg = "@pypi//yamllint",
5651
)
5752

5853
# This rule adds a convenient way to update the requirements file.
@@ -68,13 +63,13 @@ py_test(
6863
srcs = ["pip_parse_test.py"],
6964
data = [
7065
":yamllint",
71-
data_requirement("s3cmd"),
72-
dist_info_requirement("requests"),
66+
"@pypi//requests:dist_info",
67+
"@pypi//s3cmd:data",
7368
],
7469
env = {
75-
"WHEEL_DATA_CONTENTS": "$(rootpaths {})".format(data_requirement("s3cmd")),
76-
"WHEEL_DIST_INFO_CONTENTS": "$(rootpaths {})".format(dist_info_requirement("requests")),
77-
"YAMLLINT_ENTRY_POINT": "$(rootpath :yamllint)",
70+
"WHEEL_DATA_CONTENTS": "$(rootpaths @pypi//s3cmd:data)",
71+
"WHEEL_DIST_INFO_CONTENTS": "$(rootpaths @pypi//requests:dist_info)",
72+
"YAMLLINT_ENTRY_POINT": "$(rlocationpath :yamllint)",
7873
},
7974
deps = ["@rules_python//python/runfiles"],
8075
)

examples/pip_parse/MODULE.bazel

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module(name = "rules_python_pip_parse_example")
2+
3+
bazel_dep(name = "rules_python", version = "0.0.0")
4+
local_path_override(
5+
module_name = "rules_python",
6+
path = "../..",
7+
)
8+
9+
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
10+
python.toolchain(
11+
python_version = "3.9",
12+
)
13+
14+
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
15+
pip.parse(
16+
hub_name = "pypi",
17+
python_version = "3.9",
18+
requirements_lock = "//:requirements_lock.txt",
19+
)
20+
use_repo(pip, "pypi")

examples/pip_parse/WORKSPACE.bzlmod

Whitespace-only changes.

examples/pip_parse/pip_parse_test.py

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,28 @@
1919
import unittest
2020
from pathlib import Path
2121

22-
from rules_python.python.runfiles import runfiles
22+
from python.runfiles import runfiles
2323

2424

2525
class PipInstallTest(unittest.TestCase):
2626
maxDiff = None
2727

28+
def _remove_leading_dirs(self, paths):
29+
# Removes the first two directories (external/<reponame>)
30+
# to normalize what workspace and bzlmod produce.
31+
#return [str(Path(*Path(v).parts[2:])) for v in paths]
32+
return [
33+
'/'.join(v.split('/')[2:])
34+
for v in paths
35+
]
36+
2837
def test_entry_point(self):
29-
env = os.environ.get("YAMLLINT_ENTRY_POINT")
30-
self.assertIsNotNone(env)
38+
entry_point_path = os.environ.get("YAMLLINT_ENTRY_POINT")
39+
self.assertIsNotNone(entry_point_path)
3140

3241
r = runfiles.Create()
3342

34-
# To find an external target, this must use `{workspace_name}/$(rootpath @external_repo//:target)`
35-
entry_point = Path(r.Rlocation("rules_python_pip_parse_example/{}".format(env)))
43+
entry_point = Path(r.Rlocation(entry_point_path))
3644
self.assertTrue(entry_point.exists())
3745

3846
proc = subprocess.run(
@@ -44,31 +52,34 @@ def test_entry_point(self):
4452
self.assertEqual(proc.stdout.decode("utf-8").strip(), "yamllint 1.28.0")
4553

4654
def test_data(self):
47-
env = os.environ.get("WHEEL_DATA_CONTENTS")
48-
self.assertIsNotNone(env)
55+
actual = os.environ.get("WHEEL_DATA_CONTENTS")
56+
self.assertIsNotNone(actual)
57+
actual = self._remove_leading_dirs(actual.split(" "))
58+
4959
self.assertListEqual(
50-
env.split(" "),
60+
actual,
5161
[
52-
"external/pypi_s3cmd/data/share/doc/packages/s3cmd/INSTALL.md",
53-
"external/pypi_s3cmd/data/share/doc/packages/s3cmd/LICENSE",
54-
"external/pypi_s3cmd/data/share/doc/packages/s3cmd/NEWS",
55-
"external/pypi_s3cmd/data/share/doc/packages/s3cmd/README.md",
56-
"external/pypi_s3cmd/data/share/man/man1/s3cmd.1",
62+
"data/share/doc/packages/s3cmd/INSTALL.md",
63+
"data/share/doc/packages/s3cmd/LICENSE",
64+
"data/share/doc/packages/s3cmd/NEWS",
65+
"data/share/doc/packages/s3cmd/README.md",
66+
"data/share/man/man1/s3cmd.1",
5767
],
5868
)
5969

6070
def test_dist_info(self):
61-
env = os.environ.get("WHEEL_DIST_INFO_CONTENTS")
62-
self.assertIsNotNone(env)
71+
actual = os.environ.get("WHEEL_DIST_INFO_CONTENTS")
72+
self.assertIsNotNone(actual)
73+
actual = self._remove_leading_dirs(actual.split(" "))
6374
self.assertListEqual(
64-
env.split(" "),
75+
actual,
6576
[
66-
"external/pypi_requests/site-packages/requests-2.25.1.dist-info/INSTALLER",
67-
"external/pypi_requests/site-packages/requests-2.25.1.dist-info/LICENSE",
68-
"external/pypi_requests/site-packages/requests-2.25.1.dist-info/METADATA",
69-
"external/pypi_requests/site-packages/requests-2.25.1.dist-info/RECORD",
70-
"external/pypi_requests/site-packages/requests-2.25.1.dist-info/WHEEL",
71-
"external/pypi_requests/site-packages/requests-2.25.1.dist-info/top_level.txt",
77+
"site-packages/requests-2.25.1.dist-info/INSTALLER",
78+
"site-packages/requests-2.25.1.dist-info/LICENSE",
79+
"site-packages/requests-2.25.1.dist-info/METADATA",
80+
"site-packages/requests-2.25.1.dist-info/RECORD",
81+
"site-packages/requests-2.25.1.dist-info/WHEEL",
82+
"site-packages/requests-2.25.1.dist-info/top_level.txt",
7283
],
7384
)
7485

0 commit comments

Comments
 (0)