1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
"""Tests for the database."""
from pathlib import Path
from textwrap import dedent
from autodoc2.analysis import analyse_module
from autodoc2.db import InMemoryDb
from autodoc2.resolve_all import AllResolver
from autodoc2.utils import yield_modules
def test_all_resolution(tmp_path: Path, data_regression):
"""Test __all__ resolution"""
package = tmp_path / "package"
package.mkdir()
package.joinpath("__init__.py").write_text(
dedent(
"""\
from package.a import a1
from package.a.c import ac1 as alias
from unknown import something
from .b import *
from .d import *
from other import *
__all__ = ['p', 'a1', 'alias', 'something', 'unknown']
p = 1
"""
),
"utf-8",
)
package.joinpath("a").mkdir()
package.joinpath("a", "__init__.py").write_text(
dedent(
"""\
from .c import *
from .d import *
__all__ = ['a1', 'ac1', 'ad1', 'ade1', 'adf1']
a1 = 1
"""
),
"utf-8",
)
package.joinpath("a", "c.py").write_text(
dedent(
"""\
__all__ = ['ac1']
ac1 = 1
"""
),
"utf-8",
)
package.joinpath("b.py").touch()
package.joinpath("d.py").write_text(
# circular import
dedent(
"""\
from package import *
__all__ = ['p']
"""
),
"utf-8",
)
db = InMemoryDb()
for path, modname in yield_modules(package):
for item in analyse_module(path, modname):
db.add(item)
warnings = []
resolver = AllResolver(db, warnings.append)
result = resolver.get_resolved_all("package")
data_regression.check({"result": result, "warnings": warnings})
|