From b8c577673336d65309c5b887139c169398845307 Mon Sep 17 00:00:00 2001 From: "Reid D. McKenzie" Date: Wed, 8 Oct 2025 21:26:48 -0600 Subject: [PATCH] Cherry pick tests from #610 --- MODULE.bazel | 8 +++++ WORKSPACE | 6 ++++ py/tests/py-internal-venv/BUILD.bazel | 13 -------- .../py-venv-standalone-interpreter/test.sh | 2 +- .../py_venv_conflict/test_import_roots.py | 6 ++-- py/tests/rpy610/BUILD.bazel | 16 ++++++++++ py/tests/rpy610/subrepo/BUILD.bazel | 10 ++++++ py/tests/rpy610/subrepo/MODULE.bazel | 0 py/tests/rpy610/subrepo/foo.py | 2 ++ py/tests/rpy610/test.py | 32 +++++++++++++++++++ 10 files changed, 78 insertions(+), 17 deletions(-) create mode 100644 py/tests/rpy610/BUILD.bazel create mode 100644 py/tests/rpy610/subrepo/BUILD.bazel create mode 100644 py/tests/rpy610/subrepo/MODULE.bazel create mode 100644 py/tests/rpy610/subrepo/foo.py create mode 100644 py/tests/rpy610/test.py diff --git a/MODULE.bazel b/MODULE.bazel index 920b210c..42965e9f 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -114,3 +114,11 @@ oci.pull( tag = "latest", ) use_repo(oci, "ubuntu", "ubuntu_linux_amd64", "ubuntu_linux_arm64_v8") + +# For tests +local_repository = use_repo_rule("@bazel_tools//tools/build_defs/repo:local.bzl", "local_repository") + +local_repository( + name = "rpy610_test", + path = "./py/tests/rpy610/subrepo", +) diff --git a/WORKSPACE b/WORKSPACE index 4fa5c8ec..2dad4023 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -317,3 +317,9 @@ oci_pull( load("@container_structure_test//:repositories.bzl", "container_structure_test_register_toolchain") container_structure_test_register_toolchain(name = "cst") + +# For tests +local_repository( + name = "rpy610_test", + path = "./py/tests/rpy610/subrepo", +) diff --git a/py/tests/py-internal-venv/BUILD.bazel b/py/tests/py-internal-venv/BUILD.bazel index 2834e4b4..f6716781 100644 --- a/py/tests/py-internal-venv/BUILD.bazel +++ b/py/tests/py-internal-venv/BUILD.bazel @@ -13,16 +13,3 @@ py_venv_test( "@pypi_cowsay//:pkg", ], ) - -py_venv_test( - name = "test_env_vars", - srcs = ["test_env_vars.py"], - env = { - "ONE": "un", - "TWO": "deux", - "RULEDIR": "$(RULEDIR)", - "LOCATION": "$(location :test_env_vars.py)", - "DEFINE": "$(SOME_VAR)", - }, - main = "test_env_vars.py", -) diff --git a/py/tests/py-venv-standalone-interpreter/test.sh b/py/tests/py-venv-standalone-interpreter/test.sh index d3775b73..fb0d923e 100755 --- a/py/tests/py-venv-standalone-interpreter/test.sh +++ b/py/tests/py-venv-standalone-interpreter/test.sh @@ -4,7 +4,7 @@ set -ex ROOT="$(dirname $0)" -"$ROOT"/.ex/bin/python --help +"$ROOT"/.ex/bin/python --help >/dev/null 2>&1 if [ "Hello, world!" != "$($ROOT/.ex/bin/python -c 'from ex import hello; print(hello())')" ]; then exit 1 diff --git a/py/tests/py_venv_conflict/test_import_roots.py b/py/tests/py_venv_conflict/test_import_roots.py index cf5304dd..f763239c 100644 --- a/py/tests/py_venv_conflict/test_import_roots.py +++ b/py/tests/py_venv_conflict/test_import_roots.py @@ -48,13 +48,13 @@ def tree(dir_path: Path, prefix: str=''): print(sys.prefix) import conflict -print("conflict.__file__", conflict.__file__) +print(conflict.__file__) assert conflict.__file__.startswith(sys.prefix) import noconflict -print("noconflict.__file__", noconflict.__file__) +print(noconflict.__file__) assert noconflict.__file__.startswith(sys.prefix) import py_venv_conflict.lib as srclib -print("srclib.__file__", srclib.__file__) +print(srclib.__file__) assert not srclib.__file__.startswith(sys.prefix) diff --git a/py/tests/rpy610/BUILD.bazel b/py/tests/rpy610/BUILD.bazel new file mode 100644 index 00000000..ff5fd0d3 --- /dev/null +++ b/py/tests/rpy610/BUILD.bazel @@ -0,0 +1,16 @@ +load("//py/private/py_venv:defs.bzl", "py_venv_test") + +py_venv_test( + name = "test", + srcs = [ + "test.py", + ], + imports = [ + ".", + ], + main = "test.py", + deps = [ + "@pypi_cowsay//:pkg", + "@rpy610_test//:foo", + ], +) diff --git a/py/tests/rpy610/subrepo/BUILD.bazel b/py/tests/rpy610/subrepo/BUILD.bazel new file mode 100644 index 00000000..f3d8cb46 --- /dev/null +++ b/py/tests/rpy610/subrepo/BUILD.bazel @@ -0,0 +1,10 @@ +load("@aspect_rules_py//py:defs.bzl", "py_library") + +py_library( + name = "foo", + srcs = [ + "foo.py", + ], + imports = ["."], + visibility = ["//visibility:public"], +) diff --git a/py/tests/rpy610/subrepo/MODULE.bazel b/py/tests/rpy610/subrepo/MODULE.bazel new file mode 100644 index 00000000..e69de29b diff --git a/py/tests/rpy610/subrepo/foo.py b/py/tests/rpy610/subrepo/foo.py new file mode 100644 index 00000000..d0aabc61 --- /dev/null +++ b/py/tests/rpy610/subrepo/foo.py @@ -0,0 +1,2 @@ +def foo(x): + return x ** 3.15 diff --git a/py/tests/rpy610/test.py b/py/tests/rpy610/test.py new file mode 100644 index 00000000..fd0befb2 --- /dev/null +++ b/py/tests/rpy610/test.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 + +import os +import sys +import site + +print("---") +print("__file__:", __file__) +print("sys.prefix:", sys.prefix) +print("sys.executable:", sys.executable) +print("site.PREFIXES:") +for p in site.PREFIXES: + print(" -", p) + +# The virtualenv module should have already been loaded at interpreter startup +assert "_virtualenv" in sys.modules + +# Note that we can't assume that a `.runfiles` tree has been created as CI may +# use a different layout. + +# The virtualenv changes the sys.prefix, which should be in our runfiles +assert sys.prefix.endswith("/py/tests/rpy610/.test") + +# That prefix should also be "the" prefix per site.PREFIXES +assert site.PREFIXES[0].endswith("/py/tests/rpy610/.test") + +# The virtualenv also changes the sys.executable (if we've done this right) +assert sys.executable.find("/py/tests/rpy610/.test/bin/python") != -1 + +# aspect-build/rules_py#610, these imports aren't quite right +import foo +print(foo.__file__)