From 24ee36054cc21c5eaa98de84eda0c22ce0354c69 Mon Sep 17 00:00:00 2001 From: Matt Mackay Date: Fri, 10 Jun 2022 09:34:54 -0400 Subject: [PATCH] tests: adds first party dependencies to the container test, add note about usage --- py/tests/containers/BUILD.bazel | 11 +++++++++++ py/tests/containers/__main__.py | 5 ++++- py/tests/containers/branding/BUILD.bazel | 8 ++++++++ py/tests/containers/branding/__init__.py | 2 ++ py/tests/containers/py_image_test.yaml | 2 +- py/tests/internal-deps/BUILD.bazel | 14 ++------------ py/tests/internal-deps/__main__.py | 2 +- py/tests/internal-deps/adder/BUILD.bazel | 16 ++++++++++++++++ py/tests/internal-deps/adder/__init__.py | 0 py/tests/internal-deps/{ => adder}/add.py | 0 10 files changed, 45 insertions(+), 15 deletions(-) create mode 100644 py/tests/containers/branding/BUILD.bazel create mode 100644 py/tests/containers/branding/__init__.py create mode 100644 py/tests/internal-deps/adder/BUILD.bazel create mode 100644 py/tests/internal-deps/adder/__init__.py rename py/tests/internal-deps/{ => adder}/add.py (100%) diff --git a/py/tests/containers/BUILD.bazel b/py/tests/containers/BUILD.bazel index 5827fb8f..c63b6c01 100644 --- a/py/tests/containers/BUILD.bazel +++ b/py/tests/containers/BUILD.bazel @@ -6,10 +6,21 @@ py_binary( name = "main_bin", srcs = ["__main__.py"], deps = [ + "//py/tests/containers/branding", + "//py/tests/internal-deps/adder", "@pypi_colorama//:pkg", ], ) +# py3_image container support reverts us back to the rules_python way of invoking the binary, +# (as really there is a py_binary in the py3_image). +# This test ensures that rules_py can still run in that mode, but there are a few notable exceptions: +# * There is no longer a virtual env to run in +# * The rules_py launcher isn't invoked +# * There is no pip +# * Dependencies that were consumed via wheel targets are lost +# Perhaps rules_container can provide a suitable alternative here, or rules_py provides a thin wrapper around the +# rules_docker container_image that produces the expected output, but it would be limited in terms of customisation. py3_image( name = "py_image", srcs = ["__main__.py"], diff --git a/py/tests/containers/__main__.py b/py/tests/containers/__main__.py index c44d6251..ebfaf15f 100644 --- a/py/tests/containers/__main__.py +++ b/py/tests/containers/__main__.py @@ -1,4 +1,7 @@ from colorama import Fore, Style +from branding import get_branding +from adder.add import add + if __name__ == "__main__": - print(f"{Fore.GREEN}Hello rules_py{Style.RESET_ALL}") + print(f"{Fore.GREEN}Hello {get_branding()} - {add(3, .14)}{Style.RESET_ALL}") diff --git a/py/tests/containers/branding/BUILD.bazel b/py/tests/containers/branding/BUILD.bazel new file mode 100644 index 00000000..e357207d --- /dev/null +++ b/py/tests/containers/branding/BUILD.bazel @@ -0,0 +1,8 @@ +load("//py:defs.bzl", "py_library") + +py_library( + name = "branding", + srcs = ["__init__.py"], + imports = [".."], + visibility = ["//py/tests/containers:__pkg__"], +) diff --git a/py/tests/containers/branding/__init__.py b/py/tests/containers/branding/__init__.py new file mode 100644 index 00000000..39fe20c8 --- /dev/null +++ b/py/tests/containers/branding/__init__.py @@ -0,0 +1,2 @@ +def get_branding(): + return "rules_py" diff --git a/py/tests/containers/py_image_test.yaml b/py/tests/containers/py_image_test.yaml index 827db647..2efeda58 100644 --- a/py/tests/containers/py_image_test.yaml +++ b/py/tests/containers/py_image_test.yaml @@ -11,4 +11,4 @@ commandTests: command: /usr/bin/python args: - /app/py/tests/containers/py_image.binary - expectedOutput: ["Hello rules_py"] \ No newline at end of file + expectedOutput: ["Hello rules_py - 3.14"] \ No newline at end of file diff --git a/py/tests/internal-deps/BUILD.bazel b/py/tests/internal-deps/BUILD.bazel index f043600f..260db2ab 100644 --- a/py/tests/internal-deps/BUILD.bazel +++ b/py/tests/internal-deps/BUILD.bazel @@ -8,16 +8,6 @@ rules_py_py_library( ], ) -rules_py_py_library( - name = "add", - srcs = [ - "add.py", - ], - deps = [ - ":init", - ], -) - rules_py_py_library( name = "sub", srcs = [ @@ -44,9 +34,9 @@ py_binary( "__main__.py", ], deps = [ - ":add", ":pi", ":sub", + "//py/tests/internal-deps/adder", ], ) @@ -56,8 +46,8 @@ py_test( "__main__.py", ], deps = [ - ":add", ":pi", ":sub", + "//py/tests/internal-deps/adder", ], ) diff --git a/py/tests/internal-deps/__main__.py b/py/tests/internal-deps/__main__.py index ab77949d..132ded85 100644 --- a/py/tests/internal-deps/__main__.py +++ b/py/tests/internal-deps/__main__.py @@ -1,4 +1,4 @@ -from add import add +from adder.add import add from sub import sub from pi import format_pi diff --git a/py/tests/internal-deps/adder/BUILD.bazel b/py/tests/internal-deps/adder/BUILD.bazel new file mode 100644 index 00000000..c8fd66e0 --- /dev/null +++ b/py/tests/internal-deps/adder/BUILD.bazel @@ -0,0 +1,16 @@ +load("//py:defs.bzl", "py_library") + +py_library( + name = "adder", + srcs = [ + "__init__.py", + "add.py", + ], + imports = [".."], + # This library contributes to the container test, testing we can pull in and use a library from another + # package in the repo. + visibility = [ + "//py/tests/containers:__pkg__", + "//py/tests/internal-deps:__pkg__", + ], +) diff --git a/py/tests/internal-deps/adder/__init__.py b/py/tests/internal-deps/adder/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/py/tests/internal-deps/add.py b/py/tests/internal-deps/adder/add.py similarity index 100% rename from py/tests/internal-deps/add.py rename to py/tests/internal-deps/adder/add.py