(
self, tmp_path: Path, monkeypatch: MonkeyPatch | None, pytester: Pytester
)
| 1380 | monkeypatch.setattr(sys, "pytest_namespace_packages_test", [], raising=False) |
| 1381 | |
| 1382 | def setup_directories( |
| 1383 | self, tmp_path: Path, monkeypatch: MonkeyPatch | None, pytester: Pytester |
| 1384 | ) -> tuple[Path, Path]: |
| 1385 | # Use a code to guard against modules being imported more than once. |
| 1386 | # This is a safeguard in case future changes break this invariant. |
| 1387 | code = dedent( |
| 1388 | """ |
| 1389 | import sys |
| 1390 | imported = getattr(sys, "pytest_namespace_packages_test", []) |
| 1391 | assert __name__ not in imported, f"{__name__} already imported" |
| 1392 | imported.append(__name__) |
| 1393 | sys.pytest_namespace_packages_test = imported |
| 1394 | """ |
| 1395 | ) |
| 1396 | |
| 1397 | # Set up a namespace package "com.company", containing |
| 1398 | # two subpackages, "app" and "calc". |
| 1399 | (tmp_path / "src/dist1/com/company/app/core").mkdir(parents=True) |
| 1400 | (tmp_path / "src/dist1/com/company/app/__init__.py").write_text( |
| 1401 | code, encoding="UTF-8" |
| 1402 | ) |
| 1403 | (tmp_path / "src/dist1/com/company/app/core/__init__.py").write_text( |
| 1404 | code, encoding="UTF-8" |
| 1405 | ) |
| 1406 | models_py = tmp_path / "src/dist1/com/company/app/core/models.py" |
| 1407 | models_py.touch() |
| 1408 | |
| 1409 | (tmp_path / "src/dist2/com/company/calc/algo").mkdir(parents=True) |
| 1410 | (tmp_path / "src/dist2/com/company/calc/__init__.py").write_text( |
| 1411 | code, encoding="UTF-8" |
| 1412 | ) |
| 1413 | (tmp_path / "src/dist2/com/company/calc/algo/__init__.py").write_text( |
| 1414 | code, encoding="UTF-8" |
| 1415 | ) |
| 1416 | algorithms_py = tmp_path / "src/dist2/com/company/calc/algo/algorithms.py" |
| 1417 | algorithms_py.write_text(code, encoding="UTF-8") |
| 1418 | |
| 1419 | r = validate_namespace_package( |
| 1420 | pytester, |
| 1421 | [tmp_path / "src/dist1", tmp_path / "src/dist2"], |
| 1422 | ["com.company.app.core.models", "com.company.calc.algo.algorithms"], |
| 1423 | ) |
| 1424 | assert r.ret == 0 |
| 1425 | if monkeypatch is not None: |
| 1426 | monkeypatch.syspath_prepend(tmp_path / "src/dist1") |
| 1427 | monkeypatch.syspath_prepend(tmp_path / "src/dist2") |
| 1428 | return models_py, algorithms_py |
| 1429 | |
| 1430 | @pytest.mark.parametrize("import_mode", ["prepend", "append", "importlib"]) |
| 1431 | def test_resolve_pkg_root_and_module_name_ns_multiple_levels( |
no test coverage detected