Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added integration test for testing nuances of pip_repository macros #615

Merged
merged 3 commits into from
Feb 8, 2022
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
2 changes: 2 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ platforms:
- "..."
# Gazelle is not fully Windows compatible: https://github.com/bazelbuild/bazel-gazelle/issues/1122
- "-//gazelle/..."
# The dependencies needed for this test are not cross-platform: https://github.com/bazelbuild/rules_python/issues/260
- "-//tests:pip_repository_entry_points_example"
4 changes: 2 additions & 2 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# This lets us glob() up all the files inside the examples to make them inputs to tests
# (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it)
# To update these lines, run tools/bazel_integration_test/update_deleted_packages.sh
build --deleted_packages=examples/build_file_generation,examples/pip_install,examples/pip_parse,examples/pip_repository_annotations,examples/py_import,examples/relative_requirements
query --deleted_packages=examples/build_file_generation,examples/pip_install,examples/pip_parse,examples/pip_repository_annotations,examples/py_import,examples/relative_requirements
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
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

test --test_output=errors

Expand Down
5 changes: 4 additions & 1 deletion BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ filegroup(
"//third_party/github.com/bazelbuild/bazel-skylib/rules/private:distribution",
"//tools:distribution",
],
visibility = ["//examples:__pkg__"],
visibility = [
"//examples:__pkg__",
"//tests:__pkg__",
],
)

# Reexport of all bzl files used to allow downstream rules to generate docs
Expand Down
10 changes: 10 additions & 0 deletions tests/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
load("//tools/bazel_integration_test:bazel_integration_test.bzl", "bazel_integration_test")

package(default_visibility = ["//visibility:public"])

licenses(["notice"]) # Apache 2.0

bazel_integration_test(
name = "pip_repository_entry_points_example",
timeout = "long",
)
4 changes: 4 additions & 0 deletions tests/pip_repository_entry_points/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Bazel configuration flags

# https://docs.bazel.build/versions/main/best-practices.html#using-the-bazelrc-file
try-import %workspace%/user.bazelrc
4 changes: 4 additions & 0 deletions tests/pip_repository_entry_points/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# git ignore patterns

/bazel-*
user.bazelrc
53 changes: 53 additions & 0 deletions tests/pip_repository_entry_points/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
load("@pip_installed//:requirements.bzl", installed_entry_point = "entry_point")
load("@pip_parsed//:requirements.bzl", parsed_entry_point = "entry_point")
load("@rules_python//python:defs.bzl", "py_test")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")

# This rule adds a convenient way to update the requirements file.
compile_pip_requirements(
name = "requirements",
extra_args = ["--allow-unsafe"],
)

pip_parsed_sphinx = parsed_entry_point(
pkg = "sphinx",
script = "sphinx-build",
)

pip_parsed_yamllint = parsed_entry_point("yamllint")

py_test(
name = "pip_parse_entry_points_test",
srcs = ["pip_repository_entry_points_test.py"],
data = [
pip_parsed_sphinx,
pip_parsed_yamllint,
],
env = {
"SPHINX_BUILD_ENTRY_POINT": "$(rootpath {})".format(pip_parsed_sphinx),
"YAMLLINT_ENTRY_POINT": "$(rootpath {})".format(pip_parsed_yamllint),
},
main = "pip_repository_entry_points_test.py",
deps = ["@rules_python//python/runfiles"],
)

pip_installed_sphinx = installed_entry_point(
pkg = "sphinx",
script = "sphinx-build",
)

pip_installed_yamllint = installed_entry_point("yamllint")

py_test(
name = "pip_install_annotations_test",
srcs = ["pip_repository_entry_points_test.py"],
data = [
pip_installed_sphinx,
pip_installed_yamllint,
],
env = {
"SPHINX_BUILD_ENTRY_POINT": "$(rootpath {})".format(pip_installed_sphinx),
"YAMLLINT_ENTRY_POINT": "$(rootpath {})".format(pip_installed_yamllint),
},
main = "pip_repository_entry_points_test.py",
)
24 changes: 24 additions & 0 deletions tests/pip_repository_entry_points/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
workspace(name = "pip_repository_annotations_example")

local_repository(
name = "rules_python",
path = "../..",
)

load("@rules_python//python:pip.bzl", "pip_install", "pip_parse")

# For a more thorough example of `pip_parse`. See `@rules_python//examples/pip_parse`
pip_parse(
name = "pip_parsed",
requirements_lock = "//:requirements.txt",
)

load("@pip_parsed//:requirements.bzl", "install_deps")

install_deps()

# For a more thorough example of `pip_install`. See `@rules_python//examples/pip_install`
pip_install(
name = "pip_installed",
requirements = "//:requirements.txt",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env python3

import os
import subprocess
import unittest
from pathlib import Path


class PipRepositoryEntryPointsTest(unittest.TestCase):
maxDiff = None

def test_entry_point_void_return(self):
env = os.environ.get("YAMLLINT_ENTRY_POINT")
self.assertIsNotNone(env)

entry_point = Path(env)
self.assertTrue(entry_point.exists())

proc = subprocess.run(
[str(entry_point), "--version"],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
self.assertEqual(proc.stdout.decode("utf-8").strip(), "yamllint 1.26.3")

# yamllint entry_point is of the form `def run(argv=None):`
with self.assertRaises(subprocess.CalledProcessError) as context:
subprocess.run(
[str(entry_point), "--option-does-not-exist"],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
self.assertIn("returned non-zero exit status 2", str(context.exception))

def test_entry_point_int_return(self):
env = os.environ.get("SPHINX_BUILD_ENTRY_POINT")
self.assertIsNotNone(env)

entry_point = Path(env)
self.assertTrue(entry_point.exists())

proc = subprocess.run(
[str(entry_point), "--version"],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
# sphinx-build uses args[0] for its name, only assert the version here
self.assertTrue(proc.stdout.decode("utf-8").strip().endswith("4.3.2"))

# sphinx-build entry_point is of the form `def main(argv: List[str] = sys.argv[1:]) -> int:`
with self.assertRaises(subprocess.CalledProcessError) as context:
subprocess.run(
[entry_point, "--option-does-not-exist"],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
self.assertIn("returned non-zero exit status 2", str(context.exception))


if __name__ == "__main__":
unittest.main()
5 changes: 5 additions & 0 deletions tests/pip_repository_entry_points/requirements.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sphinx==4.3.2
yamllint==1.26.3

# Last avialable for ubuntu python3.6
setuptools==59.6.0
Loading