Skip to content

Commit 88cc6c5

Browse files
committed
Allow tests to build pkgutil legacy namespaces
1 parent d651344 commit 88cc6c5

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

setuptools/tests/namespaces.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import ast
2+
import json
13
import textwrap
24

35

@@ -7,33 +9,44 @@ def iter_namespace_pkgs(namespace):
79
yield ".".join(parts[:i+1])
810

911

10-
def build_namespace_package(tmpdir, name, version="1.0"):
12+
def build_namespace_package(tmpdir, name, version="1.0", impl="pkg_resources"):
1113
src_dir = tmpdir / name
1214
src_dir.mkdir()
1315
setup_py = src_dir / 'setup.py'
1416
namespace, _, rest = name.rpartition('.')
1517
namespaces = list(iter_namespace_pkgs(namespace))
18+
setup_args = {
19+
"name": name,
20+
"version": version,
21+
"packages": namespaces,
22+
}
23+
24+
if impl == "pkg_resources":
25+
tmpl = '__import__("pkg_resources").declare_namespace(__name__)'
26+
setup_args["namespace_packages"] = namespaces
27+
elif impl == "pkgutil":
28+
tmpl = '__path__ = __import__("pkgutil").extend_path(__path__, __name__)'
29+
else:
30+
raise ValueError(f"Cannot recognise {impl=} when creating namespaces")
31+
32+
args = json.dumps(setup_args, indent=4)
33+
assert ast.literal_eval(args) # ensure it is valid Python
34+
1635
script = textwrap.dedent(
17-
"""
36+
"""\
1837
import setuptools
19-
setuptools.setup(
20-
name={name!r},
21-
version={version!r},
22-
namespace_packages={namespaces!r},
23-
packages={namespaces!r},
24-
)
38+
args = {args}
39+
setuptools.setup(**args)
2540
"""
26-
).format(**locals())
41+
).format(args=args)
2742
setup_py.write_text(script, encoding='utf-8')
2843

2944
ns_pkg_dir = src_dir / namespace.replace(".", "/")
3045
ns_pkg_dir.mkdir(parents=True)
3146

3247
for ns in namespaces:
3348
pkg_init = src_dir / ns.replace(".", "/") / '__init__.py'
34-
tmpl = '__import__("pkg_resources").declare_namespace(__name__)'
35-
decl = tmpl.format(**locals())
36-
pkg_init.write_text(decl, encoding='utf-8')
49+
pkg_init.write_text(tmpl, encoding='utf-8')
3750

3851
pkg_mod = ns_pkg_dir / (rest + '.py')
3952
some_functionality = 'name = {rest!r}'.format(**locals())

setuptools/tests/test_editable_install.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,17 @@ def test_nspkg_file_is_unique(self, tmp_path, monkeypatch):
261261
files = list(installation_dir.glob("*-nspkg.pth"))
262262
assert len(files) == len(examples)
263263

264+
@pytest.mark.parametrize(
265+
"impl",
266+
(
267+
"pkg_resources",
268+
# "pkgutil", => does not work
269+
)
270+
)
264271
@pytest.mark.parametrize("ns", ("myns.n",))
265-
def test_namespace_package_importable(self, venv, tmp_path, ns, editable_opts):
272+
def test_namespace_package_importable(
273+
self, venv, tmp_path, ns, impl, editable_opts
274+
):
266275
"""
267276
Installing two packages sharing the same namespace, one installed
268277
naturally using pip or `--single-version-externally-managed`
@@ -275,8 +284,8 @@ def test_namespace_package_importable(self, venv, tmp_path, ns, editable_opts):
275284
requires = ["setuptools"]
276285
build-backend = "setuptools.build_meta"
277286
"""
278-
pkg_A = namespaces.build_namespace_package(tmp_path, f"{ns}.pkgA")
279-
pkg_B = namespaces.build_namespace_package(tmp_path, f"{ns}.pkgB")
287+
pkg_A = namespaces.build_namespace_package(tmp_path, f"{ns}.pkgA", impl=impl)
288+
pkg_B = namespaces.build_namespace_package(tmp_path, f"{ns}.pkgB", impl=impl)
280289
(pkg_A / "pyproject.toml").write_text(build_system, encoding="utf-8")
281290
(pkg_B / "pyproject.toml").write_text(build_system, encoding="utf-8")
282291
# use pip to install to the target directory

0 commit comments

Comments
 (0)