Skip to content

Commit 466ac33

Browse files
authored
tests(pypi): add tests for namespace shims generation (#3066)
This adds functional tests for the generated pkgutil namespace files. The test works by creating two wheels with the necessary structure: * An `__init__.py` file isn't in the wheel for the namespace package * They are both part of the namespace package. * The test verifies both are importable. These are the tests for #3059
1 parent 3d93274 commit 466ac33

File tree

17 files changed

+236
-111
lines changed

17 files changed

+236
-111
lines changed

.bazelrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
# (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it)
55
# To update these lines, execute
66
# `bazel run @rules_bazel_integration_test//tools:update_deleted_packages`
7-
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered,tests/modules/another_module,tests/modules/other,tests/modules/other/nspkg_delta,tests/modules/other/nspkg_gamma,tests/modules/other/nspkg_single,tests/modules/other/simple_v1,tests/modules/other/simple_v2,tests/modules/other/with_external_data,tests/whl_with_build_files/testdata,tests/whl_with_build_files/testdata/somepkg,tests/whl_with_build_files/testdata/somepkg-1.0.dist-info,tests/whl_with_build_files/testdata/somepkg/subpkg
8-
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered,tests/modules/another_module,tests/modules/other,tests/modules/other/nspkg_delta,tests/modules/other/nspkg_gamma,tests/modules/other/nspkg_single,tests/modules/other/simple_v1,tests/modules/other/simple_v2,tests/modules/other/with_external_data,tests/whl_with_build_files/testdata,tests/whl_with_build_files/testdata/somepkg,tests/whl_with_build_files/testdata/somepkg-1.0.dist-info,tests/whl_with_build_files/testdata/somepkg/subpkg
7+
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,rules_python-repro,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered,tests/modules/another_module,tests/modules/other,tests/modules/other/nspkg_delta,tests/modules/other/nspkg_gamma,tests/modules/other/nspkg_single,tests/modules/other/simple_v1,tests/modules/other/simple_v2,tests/modules/other/with_external_data,tests/whl_with_build_files/testdata,tests/whl_with_build_files/testdata/somepkg,tests/whl_with_build_files/testdata/somepkg-1.0.dist-info,tests/whl_with_build_files/testdata/somepkg/subpkg
8+
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,rules_python-repro,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered,tests/modules/another_module,tests/modules/other,tests/modules/other/nspkg_delta,tests/modules/other/nspkg_gamma,tests/modules/other/nspkg_single,tests/modules/other/simple_v1,tests/modules/other/simple_v2,tests/modules/other/with_external_data,tests/whl_with_build_files/testdata,tests/whl_with_build_files/testdata/somepkg,tests/whl_with_build_files/testdata/somepkg-1.0.dist-info,tests/whl_with_build_files/testdata/somepkg/subpkg
99

1010
test --test_output=errors
1111

MODULE.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ internal_dev_deps = use_extension(
158158
use_repo(
159159
internal_dev_deps,
160160
"buildkite_config",
161+
"implicit_namespace_ns_sub1",
162+
"implicit_namespace_ns_sub2",
161163
"rules_python_runtime_env_tc_info",
162164
"somepkg_with_build_files",
163165
"whl_with_build_files",

python/private/internal_dev_deps.bzl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def _internal_dev_deps_impl(mctx):
3030
)
3131
runtime_env_repo(name = "rules_python_runtime_env_tc_info")
3232

33+
# Setup for //tests/whl_with_build_files
3334
whl_from_dir_repo(
3435
name = "whl_with_build_files",
3536
root = "//tests/whl_with_build_files:testdata/BUILD.bazel",
@@ -41,6 +42,31 @@ def _internal_dev_deps_impl(mctx):
4142
requirement = "somepkg",
4243
)
4344

45+
# Setup for //tests/implicit_namespace_packages
46+
whl_from_dir_repo(
47+
name = "implicit_namespace_ns_sub1_whl",
48+
root = "//tests/implicit_namespace_packages:testdata/ns-sub1/BUILD.bazel",
49+
output = "ns_sub1-1.0-any-none-any.whl",
50+
)
51+
whl_library(
52+
name = "implicit_namespace_ns_sub1",
53+
whl_file = "@implicit_namespace_ns_sub1_whl//:ns_sub1-1.0-any-none-any.whl",
54+
requirement = "ns-sub1",
55+
enable_implicit_namespace_pkgs = False,
56+
)
57+
58+
whl_from_dir_repo(
59+
name = "implicit_namespace_ns_sub2_whl",
60+
root = "//tests/implicit_namespace_packages:testdata/ns-sub2/BUILD.bazel",
61+
output = "ns_sub2-1.0-any-none-any.whl",
62+
)
63+
whl_library(
64+
name = "implicit_namespace_ns_sub2",
65+
whl_file = "@implicit_namespace_ns_sub2_whl//:ns_sub2-1.0-any-none-any.whl",
66+
requirement = "ns-sub2",
67+
enable_implicit_namespace_pkgs = False,
68+
)
69+
4470
internal_dev_deps = module_extension(
4571
implementation = _internal_dev_deps_impl,
4672
doc = "This extension creates internal rules_python dev dependencies.",

python/private/pypi/whl_library_targets.bzl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ load(
3030
"WHEEL_FILE_IMPL_LABEL",
3131
"WHEEL_FILE_PUBLIC_LABEL",
3232
)
33-
load(":namespace_pkgs.bzl", "create_inits")
33+
load(":namespace_pkgs.bzl", _create_inits = "create_inits")
3434
load(":pep508_deps.bzl", "deps")
3535

3636
def whl_library_targets_from_requires(
@@ -120,6 +120,7 @@ def whl_library_targets(
120120
py_binary = py_binary,
121121
py_library = py_library,
122122
env_marker_setting = env_marker_setting,
123+
create_inits = _create_inits,
123124
)):
124125
"""Create all of the whl_library targets.
125126
@@ -334,7 +335,7 @@ def whl_library_targets(
334335
if not enable_implicit_namespace_pkgs:
335336
srcs = srcs + getattr(native, "select", select)({
336337
Label("//python/config_settings:is_venvs_site_packages"): [],
337-
"//conditions:default": create_inits(
338+
"//conditions:default": rules.create_inits(
338339
srcs = srcs + data + pyi_srcs,
339340
ignored_dirnames = [], # If you need to ignore certain folders, you can patch rules_python here to do so.
340341
root = "site-packages",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
load("//python:py_test.bzl", "py_test")
2+
load("//tests/support:support.bzl", "SUPPORTS_BZLMOD_UNIXY")
3+
4+
py_test(
5+
name = "namespace_packages_test",
6+
srcs = ["namespace_packages_test.py"],
7+
target_compatible_with = SUPPORTS_BZLMOD_UNIXY,
8+
deps = [
9+
"@implicit_namespace_ns_sub1//:pkg",
10+
"@implicit_namespace_ns_sub2//:pkg",
11+
],
12+
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import unittest
2+
3+
4+
class NamespacePackagesTest(unittest.TestCase):
5+
6+
def test_both_importable(self):
7+
import nspkg
8+
import nspkg.subpkg1
9+
import nspkg.subpkg1.subpkgmod
10+
import nspkg.subpkg2.subpkgmod
11+
12+
self.assertEqual("nspkg.subpkg1", nspkg.subpkg1.expected_name)
13+
self.assertEqual(
14+
"nspkg.subpkg1.subpkgmod", nspkg.subpkg1.subpkgmod.expected_name
15+
)
16+
17+
self.assertEqual("nspkg.subpkg2", nspkg.subpkg2.expected_name)
18+
self.assertEqual(
19+
"nspkg.subpkg2.subpkgmod", nspkg.subpkg2.subpkgmod.expected_name
20+
)
21+
22+
23+
if __name__ == "__main__":
24+
unittest.main()

tests/implicit_namespace_packages/testdata/ns-sub1/ns-sub1-1.0.dist-info/METADATA

Whitespace-only changes.

tests/implicit_namespace_packages/testdata/ns-sub1/ns-sub1-1.0.dist-info/RECORD

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Wheel-Version: 1.0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
expected_name = "nspkg.subpkg1"

0 commit comments

Comments
 (0)