Skip to content

Commit 66b06b1

Browse files
authored
Added integration test for testing nuances of pip_repository macros (#615)
1 parent 8cf1220 commit 66b06b1

File tree

11 files changed

+389
-3
lines changed

11 files changed

+389
-3
lines changed

.bazelci/presubmit.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ platforms:
3434
- "..."
3535
# Gazelle is not fully Windows compatible: https://github.com/bazelbuild/bazel-gazelle/issues/1122
3636
- "-//gazelle/..."
37+
# The dependencies needed for this test are not cross-platform: https://github.com/bazelbuild/rules_python/issues/260
38+
- "-//tests:pip_repository_entry_points_example"

.bazelrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
# This lets us glob() up all the files inside the examples to make them inputs to tests
44
# (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it)
55
# To update these lines, run tools/bazel_integration_test/update_deleted_packages.sh
6-
build --deleted_packages=examples/build_file_generation,examples/pip_install,examples/pip_parse,examples/pip_repository_annotations,examples/py_import,examples/relative_requirements
7-
query --deleted_packages=examples/build_file_generation,examples/pip_install,examples/pip_parse,examples/pip_repository_annotations,examples/py_import,examples/relative_requirements
6+
build --deleted_packages=examples/build_file_generation,examples/pip_install,examples/pip_parse,examples/pip_repository_annotations,examples/py_import,examples/relative_requirements,tests/pip_repository_entry_points
7+
query --deleted_packages=examples/build_file_generation,examples/pip_install,examples/pip_parse,examples/pip_repository_annotations,examples/py_import,examples/relative_requirements,tests/pip_repository_entry_points
88

99
test --test_output=errors
1010

BUILD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ filegroup(
3636
"//third_party/github.com/bazelbuild/bazel-skylib/rules/private:distribution",
3737
"//tools:distribution",
3838
],
39-
visibility = ["//examples:__pkg__"],
39+
visibility = [
40+
"//examples:__pkg__",
41+
"//tests:__pkg__",
42+
],
4043
)
4144

4245
# Reexport of all bzl files used to allow downstream rules to generate docs

tests/BUILD

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
load("//tools/bazel_integration_test:bazel_integration_test.bzl", "bazel_integration_test")
2+
3+
package(default_visibility = ["//visibility:public"])
4+
5+
licenses(["notice"]) # Apache 2.0
6+
7+
bazel_integration_test(
8+
name = "pip_repository_entry_points_example",
9+
timeout = "long",
10+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Bazel configuration flags
2+
3+
# https://docs.bazel.build/versions/main/best-practices.html#using-the-bazelrc-file
4+
try-import %workspace%/user.bazelrc
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# git ignore patterns
2+
3+
/bazel-*
4+
user.bazelrc
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
load("@pip_installed//:requirements.bzl", installed_entry_point = "entry_point")
2+
load("@pip_parsed//:requirements.bzl", parsed_entry_point = "entry_point")
3+
load("@rules_python//python:defs.bzl", "py_test")
4+
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
5+
6+
# This rule adds a convenient way to update the requirements file.
7+
compile_pip_requirements(
8+
name = "requirements",
9+
extra_args = ["--allow-unsafe"],
10+
)
11+
12+
pip_parsed_sphinx = parsed_entry_point(
13+
pkg = "sphinx",
14+
script = "sphinx-build",
15+
)
16+
17+
pip_parsed_yamllint = parsed_entry_point("yamllint")
18+
19+
py_test(
20+
name = "pip_parse_entry_points_test",
21+
srcs = ["pip_repository_entry_points_test.py"],
22+
data = [
23+
pip_parsed_sphinx,
24+
pip_parsed_yamllint,
25+
],
26+
env = {
27+
"SPHINX_BUILD_ENTRY_POINT": "$(rootpath {})".format(pip_parsed_sphinx),
28+
"YAMLLINT_ENTRY_POINT": "$(rootpath {})".format(pip_parsed_yamllint),
29+
},
30+
main = "pip_repository_entry_points_test.py",
31+
deps = ["@rules_python//python/runfiles"],
32+
)
33+
34+
pip_installed_sphinx = installed_entry_point(
35+
pkg = "sphinx",
36+
script = "sphinx-build",
37+
)
38+
39+
pip_installed_yamllint = installed_entry_point("yamllint")
40+
41+
py_test(
42+
name = "pip_install_annotations_test",
43+
srcs = ["pip_repository_entry_points_test.py"],
44+
data = [
45+
pip_installed_sphinx,
46+
pip_installed_yamllint,
47+
],
48+
env = {
49+
"SPHINX_BUILD_ENTRY_POINT": "$(rootpath {})".format(pip_installed_sphinx),
50+
"YAMLLINT_ENTRY_POINT": "$(rootpath {})".format(pip_installed_yamllint),
51+
},
52+
main = "pip_repository_entry_points_test.py",
53+
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
workspace(name = "pip_repository_annotations_example")
2+
3+
local_repository(
4+
name = "rules_python",
5+
path = "../..",
6+
)
7+
8+
load("@rules_python//python:pip.bzl", "pip_install", "pip_parse")
9+
10+
# For a more thorough example of `pip_parse`. See `@rules_python//examples/pip_parse`
11+
pip_parse(
12+
name = "pip_parsed",
13+
requirements_lock = "//:requirements.txt",
14+
)
15+
16+
load("@pip_parsed//:requirements.bzl", "install_deps")
17+
18+
install_deps()
19+
20+
# For a more thorough example of `pip_install`. See `@rules_python//examples/pip_install`
21+
pip_install(
22+
name = "pip_installed",
23+
requirements = "//:requirements.txt",
24+
)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env python3
2+
3+
import os
4+
import subprocess
5+
import unittest
6+
from pathlib import Path
7+
8+
9+
class PipRepositoryEntryPointsTest(unittest.TestCase):
10+
maxDiff = None
11+
12+
def test_entry_point_void_return(self):
13+
env = os.environ.get("YAMLLINT_ENTRY_POINT")
14+
self.assertIsNotNone(env)
15+
16+
entry_point = Path(env)
17+
self.assertTrue(entry_point.exists())
18+
19+
proc = subprocess.run(
20+
[str(entry_point), "--version"],
21+
check=True,
22+
stdout=subprocess.PIPE,
23+
stderr=subprocess.PIPE,
24+
)
25+
self.assertEqual(proc.stdout.decode("utf-8").strip(), "yamllint 1.26.3")
26+
27+
# yamllint entry_point is of the form `def run(argv=None):`
28+
with self.assertRaises(subprocess.CalledProcessError) as context:
29+
subprocess.run(
30+
[str(entry_point), "--option-does-not-exist"],
31+
check=True,
32+
stdout=subprocess.PIPE,
33+
stderr=subprocess.PIPE,
34+
)
35+
self.assertIn("returned non-zero exit status 2", str(context.exception))
36+
37+
def test_entry_point_int_return(self):
38+
env = os.environ.get("SPHINX_BUILD_ENTRY_POINT")
39+
self.assertIsNotNone(env)
40+
41+
entry_point = Path(env)
42+
self.assertTrue(entry_point.exists())
43+
44+
proc = subprocess.run(
45+
[str(entry_point), "--version"],
46+
check=True,
47+
stdout=subprocess.PIPE,
48+
stderr=subprocess.PIPE,
49+
)
50+
# sphinx-build uses args[0] for its name, only assert the version here
51+
self.assertTrue(proc.stdout.decode("utf-8").strip().endswith("4.3.2"))
52+
53+
# sphinx-build entry_point is of the form `def main(argv: List[str] = sys.argv[1:]) -> int:`
54+
with self.assertRaises(subprocess.CalledProcessError) as context:
55+
subprocess.run(
56+
[entry_point, "--option-does-not-exist"],
57+
check=True,
58+
stdout=subprocess.PIPE,
59+
stderr=subprocess.PIPE,
60+
)
61+
self.assertIn("returned non-zero exit status 2", str(context.exception))
62+
63+
64+
if __name__ == "__main__":
65+
unittest.main()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
sphinx==4.3.2
2+
yamllint==1.26.3
3+
4+
# Last avialable for ubuntu python3.6
5+
setuptools==59.6.0

0 commit comments

Comments
 (0)