|
28 | 28 | from _pytest.pathlib import safe_exists
|
29 | 29 | from _pytest.pathlib import symlink_or_skip
|
30 | 30 | from _pytest.pathlib import visit
|
| 31 | +from _pytest.pytester import Pytester |
31 | 32 | from _pytest.tmpdir import TempPathFactory
|
32 | 33 |
|
33 | 34 |
|
@@ -592,6 +593,10 @@ def test_module_name_from_path(self, tmp_path: Path) -> None:
|
592 | 593 | result = module_name_from_path(tmp_path / "src/app/__init__.py", tmp_path)
|
593 | 594 | assert result == "src.app"
|
594 | 595 |
|
| 596 | + # Unless __init__.py file is at the root, in which case we cannot have an empty module name. |
| 597 | + result = module_name_from_path(tmp_path / "__init__.py", tmp_path) |
| 598 | + assert result == "__init__" |
| 599 | + |
595 | 600 | def test_insert_missing_modules(
|
596 | 601 | self, monkeypatch: MonkeyPatch, tmp_path: Path
|
597 | 602 | ) -> None:
|
@@ -663,6 +668,22 @@ def __init__(self) -> None:
|
663 | 668 | mod = import_path(init, root=tmp_path, mode=ImportMode.importlib)
|
664 | 669 | assert len(mod.instance.INSTANCES) == 1
|
665 | 670 |
|
| 671 | + def test_importlib_root_is_package(self, pytester: Pytester) -> None: |
| 672 | + """ |
| 673 | + Regression for importing a `__init__`.py file that is at the root |
| 674 | + (#11417). |
| 675 | + """ |
| 676 | + pytester.makepyfile(__init__="") |
| 677 | + pytester.makepyfile( |
| 678 | + """ |
| 679 | + def test_my_test(): |
| 680 | + assert True |
| 681 | + """ |
| 682 | + ) |
| 683 | + |
| 684 | + result = pytester.runpytest("--import-mode=importlib") |
| 685 | + result.stdout.fnmatch_lines("* 1 passed *") |
| 686 | + |
666 | 687 |
|
667 | 688 | def test_safe_exists(tmp_path: Path) -> None:
|
668 | 689 | d = tmp_path.joinpath("some_dir")
|
|
0 commit comments