Reuse code to test the namespace packages
This commit is contained in:
parent
6e5d2e13bc
commit
8fd7333554
|
@ -13,6 +13,7 @@ from typing import Any
|
||||||
from typing import Generator
|
from typing import Generator
|
||||||
from typing import Iterator
|
from typing import Iterator
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
from typing import Sequence
|
||||||
from typing import Tuple
|
from typing import Tuple
|
||||||
import unittest.mock
|
import unittest.mock
|
||||||
|
|
||||||
|
@ -37,6 +38,7 @@ from _pytest.pathlib import safe_exists
|
||||||
from _pytest.pathlib import symlink_or_skip
|
from _pytest.pathlib import symlink_or_skip
|
||||||
from _pytest.pathlib import visit
|
from _pytest.pathlib import visit
|
||||||
from _pytest.pytester import Pytester
|
from _pytest.pytester import Pytester
|
||||||
|
from _pytest.pytester import RunResult
|
||||||
from _pytest.tmpdir import TempPathFactory
|
from _pytest.tmpdir import TempPathFactory
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
@ -1141,16 +1143,10 @@ class TestNamespacePackages:
|
||||||
algorithms_py.touch()
|
algorithms_py.touch()
|
||||||
|
|
||||||
# Validate the namespace package by importing it in a Python subprocess.
|
# Validate the namespace package by importing it in a Python subprocess.
|
||||||
r = pytester.runpython_c(
|
r = self.run_ns_imports(
|
||||||
dedent(
|
pytester,
|
||||||
f"""
|
[tmp_path / "src/dist1", tmp_path / "src/dist2"],
|
||||||
import sys
|
["com.company.app.core.models", "com.company.calc.algo.algorithms"],
|
||||||
sys.path.append(r{str(tmp_path / "src/dist1")!r})
|
|
||||||
sys.path.append(r{str(tmp_path / "src/dist2")!r})
|
|
||||||
import com.company.app.core.models
|
|
||||||
import com.company.calc.algo.algorithms
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
assert r.ret == 0
|
assert r.ret == 0
|
||||||
if monkeypatch is not None:
|
if monkeypatch is not None:
|
||||||
|
@ -1158,6 +1154,19 @@ class TestNamespacePackages:
|
||||||
monkeypatch.syspath_prepend(tmp_path / "src/dist2")
|
monkeypatch.syspath_prepend(tmp_path / "src/dist2")
|
||||||
return models_py, algorithms_py
|
return models_py, algorithms_py
|
||||||
|
|
||||||
|
def run_ns_imports(
|
||||||
|
self, pytester: Pytester, paths: Sequence[Path], imports: Sequence[str]
|
||||||
|
) -> RunResult:
|
||||||
|
"""Validate a Python namespace package by importing modules from it in a Python subprocess"""
|
||||||
|
lines = [
|
||||||
|
"import sys",
|
||||||
|
# Configure sys.path.
|
||||||
|
*[f"sys.path.append(r{str(x)!r})" for x in paths],
|
||||||
|
# Imports.
|
||||||
|
*[f"import {x}" for x in imports],
|
||||||
|
]
|
||||||
|
return pytester.runpython_c("\n".join(lines))
|
||||||
|
|
||||||
@pytest.mark.parametrize("import_mode", ["prepend", "append", "importlib"])
|
@pytest.mark.parametrize("import_mode", ["prepend", "append", "importlib"])
|
||||||
def test_resolve_pkg_root_and_module_name_ns_multiple_levels(
|
def test_resolve_pkg_root_and_module_name_ns_multiple_levels(
|
||||||
self,
|
self,
|
||||||
|
@ -1233,16 +1242,10 @@ class TestNamespacePackages:
|
||||||
(tmp_path / "src/dist1/com/__init__.py").touch()
|
(tmp_path / "src/dist1/com/__init__.py").touch()
|
||||||
|
|
||||||
# Ensure Python no longer considers dist1/com a namespace package.
|
# Ensure Python no longer considers dist1/com a namespace package.
|
||||||
r = pytester.runpython_c(
|
r = self.run_ns_imports(
|
||||||
dedent(
|
pytester,
|
||||||
f"""
|
[tmp_path / "src/dist1", tmp_path / "src/dist2"],
|
||||||
import sys
|
["com.company.app.core.models", "com.company.calc.algo.algorithms"],
|
||||||
sys.path.append(r{str(tmp_path / "src/dist1")!r})
|
|
||||||
sys.path.append(r{str(tmp_path / "src/dist2")!r})
|
|
||||||
import com.company.app.core.models
|
|
||||||
import com.company.calc.algo.algorithms
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
assert r.ret == 1
|
assert r.ret == 1
|
||||||
r.stderr.fnmatch_lines("*No module named 'com.company.calc*")
|
r.stderr.fnmatch_lines("*No module named 'com.company.calc*")
|
||||||
|
@ -1340,16 +1343,10 @@ class TestNamespacePackages:
|
||||||
(tmp_path / "src/dist2/ns/a/core/foo/m.py").touch()
|
(tmp_path / "src/dist2/ns/a/core/foo/m.py").touch()
|
||||||
|
|
||||||
# Validate the namespace package by importing it in a Python subprocess.
|
# Validate the namespace package by importing it in a Python subprocess.
|
||||||
r = pytester.runpython_c(
|
r = self.run_ns_imports(
|
||||||
dedent(
|
pytester,
|
||||||
f"""
|
[tmp_path / "src/dist1", tmp_path / "src/dist2"],
|
||||||
import sys
|
["ns.b.app.bar.m", "ns.a.core.foo.m"],
|
||||||
sys.path.append(r{str(tmp_path / "src/dist1")!r})
|
|
||||||
sys.path.append(r{str(tmp_path / "src/dist2")!r})
|
|
||||||
import ns.b.app.bar.m
|
|
||||||
import ns.a.core.foo.m
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
assert r.ret == 0
|
assert r.ret == 0
|
||||||
monkeypatch.syspath_prepend(tmp_path / "src/dist1")
|
monkeypatch.syspath_prepend(tmp_path / "src/dist1")
|
||||||
|
|
Loading…
Reference in New Issue