Option C Tools
Loading...
Searching...
No Matches
test_find_packages.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# tests/tests_docs/test_find_packages.py
4
5"""
6Purpose
7-------
8Validate the OI-426 recursive package discovery in
9``oct.docs.oct_docs._find_packages``.
10
11Responsibilities
12----------------
13- Confirm depth-1 and depth-2 cases continue to work (backward compat).
14- Confirm depth-3+ cases now resolve (new capability).
15- Confirm walker stops at the first ``__init__.py`` (autodoc handles descent).
16- Confirm ``LINTER_EXCLUDE_DIRS`` / ``WILDCARD_EXCLUDE_DIRS`` are respected.
17- Confirm the ``_MAX_PACKAGE_DEPTH`` safety cap prevents runaway walks.
18- Confirm the explicit ``autodoc_modules`` override path is unchanged.
19
20Diagnostics
21-----------
22Domain: DOCS-TESTS
23L2: test lifecycle
24L3: assertion details
25L4: deep tracing
26
27Contracts
28---------
29Inputs: tmp_path pytest fixture
30Outputs: pass/fail assertions
31"""
32
33from pathlib import Path
34
35import pytest
36
37from oct.docs.oct_docs import _find_packages, _MAX_PACKAGE_DEPTH
38
39
40def _mkpkg(path: Path) -> None:
41 path.mkdir(parents=True, exist_ok=True)
42 (path / "__init__.py").write_text("", encoding="utf-8")
43
44
45def test_find_packages_depth_1(tmp_path: Path) -> None:
46 """Backward compat: a top-level package is discovered."""
47 _mkpkg(tmp_path / "pkg")
48
49 result = _find_packages(tmp_path, {})
50
51 assert result == [("pkg", tmp_path / "pkg")]
52
53
55 """Backward compat: ``oct/oct/__init__.py`` resolves to the inner dir."""
56 inner = tmp_path / "oct" / "oct"
57 _mkpkg(inner)
58
59 result = _find_packages(tmp_path, {})
60
61 assert result == [("oct", inner)]
62
63
64def test_find_packages_depth_3_src_layout(tmp_path: Path) -> None:
65 """New capability: ``src/mypkg/__init__.py`` is now discovered."""
66 inner = tmp_path / "src" / "mypkg"
67 _mkpkg(inner)
68
69 result = _find_packages(tmp_path, {})
70
71 assert result == [("mypkg", inner)]
72
73
74def test_find_packages_stops_at_package(tmp_path: Path) -> None:
75 """Subpackages under a discovered package must not be listed."""
76 _mkpkg(tmp_path / "pkg")
77 _mkpkg(tmp_path / "pkg" / "sub")
78
79 result = _find_packages(tmp_path, {})
80
81 assert result == [("pkg", tmp_path / "pkg")]
82
83
84def test_find_packages_respects_exclusions(tmp_path: Path) -> None:
85 """Hidden / excluded directories are skipped."""
86 _mkpkg(tmp_path / ".git" / "fake")
87 _mkpkg(tmp_path / "real")
88
89 result = _find_packages(tmp_path, {})
90
91 assert result == [("real", tmp_path / "real")]
92
93
94def test_find_packages_max_depth_safety(tmp_path: Path) -> None:
95 """A 15-deep empty tree returns [] and does not raise."""
96 deep = tmp_path
97 for i in range(15):
98 deep = deep / f"level{i}"
99 deep.mkdir(parents=True)
100
101 result = _find_packages(tmp_path, {})
102
103 assert result == []
104 assert _MAX_PACKAGE_DEPTH == 10
105
106
108 """Explicit ``autodoc_modules`` config bypasses the walker."""
109 _mkpkg(tmp_path / "hidden")
110 _mkpkg(tmp_path / "chosen")
111
112 result = _find_packages(
113 tmp_path, {"autodoc_modules": ["chosen"]},
114 )
115
116 assert result == [("chosen", tmp_path / "chosen")]
117
118
119def test_find_packages_multiple_siblings(tmp_path: Path) -> None:
120 """Multiple top-level packages are all discovered."""
121 _mkpkg(tmp_path / "a")
122 _mkpkg(tmp_path / "b")
123 _mkpkg(tmp_path / "c")
124
125 result = _find_packages(tmp_path, {})
126 names = sorted(name for name, _ in result)
127
128 assert names == ["a", "b", "c"]
None test_find_packages_max_depth_safety(Path tmp_path)
None test_find_packages_respects_exclusions(Path tmp_path)
None test_find_packages_autodoc_modules_override(Path tmp_path)
None test_find_packages_stops_at_package(Path tmp_path)
None test_find_packages_depth_2_nested_project(Path tmp_path)
None test_find_packages_depth_1(Path tmp_path)
None test_find_packages_multiple_siblings(Path tmp_path)
None test_find_packages_depth_3_src_layout(Path tmp_path)