Skip to content

Commit 4978027

Browse files
aignasrickeylev
andauthored
fix(pypi): namespace_pkgs should pass correct arguments (#3026)
It seems that the only function that did not have unit tests have bugs and the integration tests did not catch it because we weren't creating namespacepkg `__init__.py` files. This change fixes the bug, adds a unit test for the remaining untested function. Fixes #3023 Co-authored-by: Richard Levasseur <[email protected]>
1 parent f6feca1 commit 4978027

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

python/private/pypi/namespace_pkgs.bzl

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,32 @@ def get_files(*, srcs, ignored_dirnames = [], root = None):
5959

6060
return sorted([d for d in dirs if d not in ignored])
6161

62-
def create_inits(**kwargs):
62+
def create_inits(*, srcs, ignored_dirnames = [], root = None, copy_file = copy_file, **kwargs):
6363
"""Create init files and return the list to be included `py_library` srcs.
6464
6565
Args:
66-
**kwargs: passed to {obj}`get_files`.
66+
srcs: {type}`src` a list of files to be passed to {bzl:obj}`py_library`
67+
as `srcs` and `data`. This is usually a result of a {obj}`glob`.
68+
ignored_dirnames: {type}`str` a list of patterns to ignore.
69+
root: {type}`str` the prefix to use as the root.
70+
copy_file: the `copy_file` rule to copy files in build context.
71+
**kwargs: passed to {obj}`copy_file`.
6772
6873
Returns:
6974
{type}`list[str]` to be included as part of `py_library`.
7075
"""
71-
srcs = []
72-
for out in get_files(**kwargs):
76+
ret = []
77+
for i, out in enumerate(get_files(srcs = srcs, ignored_dirnames = ignored_dirnames, root = root)):
7378
src = "{}/__init__.py".format(out)
74-
srcs.append(srcs)
79+
ret.append(src)
7580

7681
copy_file(
77-
name = "_cp_{}_namespace".format(out),
82+
# For the target name, use a number instead of trying to convert an output
83+
# path into a valid label.
84+
name = "_cp_{}_namespace".format(i),
7885
src = _TEMPLATE,
7986
out = src,
8087
**kwargs
8188
)
8289

83-
return srcs
90+
return ret

tests/pypi/namespace_pkgs/namespace_pkgs_tests.bzl

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
""
22

33
load("@rules_testing//lib:analysis_test.bzl", "test_suite")
4-
load("//python/private/pypi:namespace_pkgs.bzl", "get_files") # buildifier: disable=bzl-visibility
4+
load("//python/private/pypi:namespace_pkgs.bzl", "create_inits", "get_files") # buildifier: disable=bzl-visibility
55

66
_tests = []
77

@@ -160,6 +160,45 @@ def test_skips_ignored_directories(env):
160160

161161
_tests.append(test_skips_ignored_directories)
162162

163+
def _test_create_inits(env):
164+
srcs = [
165+
"nested/root/foo/bar/biz.py",
166+
"nested/root/foo/bee/boo.py",
167+
"nested/root/foo/buu/__init__.py",
168+
"nested/root/foo/buu/bii.py",
169+
]
170+
copy_file_calls = []
171+
template = Label("//python/private/pypi:namespace_pkg_tmpl.py")
172+
173+
got = create_inits(
174+
srcs = srcs,
175+
root = "nested/root",
176+
copy_file = lambda **kwargs: copy_file_calls.append(kwargs),
177+
)
178+
env.expect.that_collection(got).contains_exactly([
179+
call["out"]
180+
for call in copy_file_calls
181+
])
182+
env.expect.that_collection(copy_file_calls).contains_exactly([
183+
{
184+
"name": "_cp_0_namespace",
185+
"out": "nested/root/foo/__init__.py",
186+
"src": template,
187+
},
188+
{
189+
"name": "_cp_1_namespace",
190+
"out": "nested/root/foo/bar/__init__.py",
191+
"src": template,
192+
},
193+
{
194+
"name": "_cp_2_namespace",
195+
"out": "nested/root/foo/bee/__init__.py",
196+
"src": template,
197+
},
198+
])
199+
200+
_tests.append(_test_create_inits)
201+
163202
def namespace_pkgs_test_suite(name):
164203
test_suite(
165204
name = name,

0 commit comments

Comments
 (0)