| 1 |
"""End-to-end tests for documentation generation. |
| 2 |
|
| 3 |
These tests actually build documentation using mkdocs and sphinx, |
| 4 |
and verify that the generated output contains expected content. |
| 5 |
""" |
| 6 |
|
| 7 |
import os |
| 8 |
import shutil |
| 9 |
import subprocess |
| 10 |
import sys |
| 11 |
from pathlib import Path |
| 12 |
|
| 13 |
import pytest |
| 14 |
|
| 15 |
|
| 16 |
COMPLEX_DEMO_DIR = Path(__file__).parent / "apps" / "complex-demo" |
| 17 |
|
| 18 |
|
| 19 |
def _run_command(cmd: list[str], cwd: Path, env: dict | None = None) -> subprocess.CompletedProcess: |
| 20 |
"""Run a command and return the result.""" |
| 21 |
full_env = os.environ.copy() |
| 22 |
if env: |
| 23 |
full_env.update(env) |
| 24 |
|
| 25 |
return subprocess.run( |
| 26 |
cmd, |
| 27 |
cwd=cwd, |
| 28 |
capture_output=True, |
| 29 |
text=True, |
| 30 |
env=full_env, |
| 31 |
) |
| 32 |
|
| 33 |
|
| 34 |
@pytest.mark.slow |
| 35 |
class TestMkDocsBuild: |
| 36 |
"""Test MkDocs documentation builds.""" |
| 37 |
|
| 38 |
@pytest.fixture |
| 39 |
def mkdocs_build_dir(self, tmp_path): |
| 40 |
"""Create a temporary build directory for MkDocs.""" |
| 41 |
build_dir = tmp_path / "site" |
| 42 |
return build_dir |
| 43 |
|
| 44 |
@pytest.fixture |
| 45 |
def ensure_complex_demo_importable(self): |
| 46 |
"""Ensure the complex_app module can be imported.""" |
| 47 |
sys.path.insert(0, str(COMPLEX_DEMO_DIR)) |
| 48 |
yield |
| 49 |
sys.path.remove(str(COMPLEX_DEMO_DIR)) |
| 50 |
|
| 51 |
@pytest.mark.skipif( |
| 52 |
shutil.which("mkdocs") is None, |
| 53 |
reason="mkdocs not installed or not in PATH", |
| 54 |
) |
| 55 |
def test_mkdocs_build_succeeds(self, mkdocs_build_dir, ensure_complex_demo_importable): |
| 56 |
"""Test that mkdocs build completes successfully.""" |
| 57 |
pytest.importorskip("mkdocs") |
| 58 |
|
| 59 |
result = _run_command( |
| 60 |
["mkdocs", "build", "--site-dir", str(mkdocs_build_dir)], |
| 61 |
cwd=COMPLEX_DEMO_DIR, |
| 62 |
env={"PYTHONPATH": str(COMPLEX_DEMO_DIR)}, |
| 63 |
) |
| 64 |
|
| 65 |
assert result.returncode == 0, f"mkdocs build failed:\nSTDOUT:\n{result.stdout}\nSTDERR:\n{result.stderr}" |
| 66 |
assert mkdocs_build_dir.exists(), "Build directory was not created" |
| 67 |
|
| 68 |
@pytest.mark.skipif( |
| 69 |
shutil.which("mkdocs") is None, |
| 70 |
reason="mkdocs not installed or not in PATH", |
| 71 |
) |
| 72 |
def test_mkdocs_output_contains_commands(self, mkdocs_build_dir, ensure_complex_demo_importable): |
| 73 |
"""Test that built documentation contains expected command content.""" |
| 74 |
pytest.importorskip("mkdocs") |
| 75 |
|
| 76 |
result = _run_command( |
| 77 |
["mkdocs", "build", "--site-dir", str(mkdocs_build_dir)], |
| 78 |
cwd=COMPLEX_DEMO_DIR, |
| 79 |
env={"PYTHONPATH": str(COMPLEX_DEMO_DIR)}, |
| 80 |
) |
| 81 |
|
| 82 |
if result.returncode != 0: |
| 83 |
pytest.skip(f"mkdocs build failed: {result.stderr}") |
| 84 |
|
| 85 |
|
| 86 |
cli_index = mkdocs_build_dir / "cli" / "index" / "index.html" |
| 87 |
if not cli_index.exists(): |
| 88 |
|
| 89 |
cli_index = mkdocs_build_dir / "cli" / "index.html" |
| 90 |
|
| 91 |
assert cli_index.exists(), f"CLI index page not found. Contents: {list(mkdocs_build_dir.rglob('*.html'))}" |
| 92 |
|
| 93 |
content = cli_index.read_text() |
| 94 |
|
| 95 |
|
| 96 |
assert "admin" in content.lower(), "admin command not found in CLI index" |
| 97 |
assert "data" in content.lower(), "data command not found in CLI index" |
| 98 |
assert "server" in content.lower(), "server command not found in CLI index" |
| 99 |
|
| 100 |
@pytest.mark.skipif( |
| 101 |
shutil.which("mkdocs") is None, |
| 102 |
reason="mkdocs not installed or not in PATH", |
| 103 |
) |
| 104 |
def test_mkdocs_output_contains_nested_commands(self, mkdocs_build_dir, ensure_complex_demo_importable): |
| 105 |
"""Test that nested commands (4 levels deep) are documented.""" |
| 106 |
pytest.importorskip("mkdocs") |
| 107 |
|
| 108 |
result = _run_command( |
| 109 |
["mkdocs", "build", "--site-dir", str(mkdocs_build_dir)], |
| 110 |
cwd=COMPLEX_DEMO_DIR, |
| 111 |
env={"PYTHONPATH": str(COMPLEX_DEMO_DIR)}, |
| 112 |
) |
| 113 |
|
| 114 |
if result.returncode != 0: |
| 115 |
pytest.skip(f"mkdocs build failed: {result.stderr}") |
| 116 |
|
| 117 |
|
| 118 |
admin_page = mkdocs_build_dir / "cli" / "admin" / "index.html" |
| 119 |
if not admin_page.exists(): |
| 120 |
admin_page = mkdocs_build_dir / "cli" / "admin.html" |
| 121 |
|
| 122 |
if not admin_page.exists(): |
| 123 |
pytest.skip(f"Admin page not found. Contents: {list(mkdocs_build_dir.rglob('*.html'))}") |
| 124 |
|
| 125 |
content = admin_page.read_text() |
| 126 |
|
| 127 |
|
| 128 |
assert "users" in content.lower(), "users subcommand not found" |
| 129 |
assert "permissions" in content.lower(), "permissions subcommand not found" |
| 130 |
|
| 131 |
assert "roles" in content.lower() or "role" in content.lower(), "roles subcommand not found" |
| 132 |
|
| 133 |
@pytest.mark.skipif( |
| 134 |
shutil.which("mkdocs") is None, |
| 135 |
reason="mkdocs not installed or not in PATH", |
| 136 |
) |
| 137 |
def test_mkdocs_output_contains_dataclass_params(self, mkdocs_build_dir, ensure_complex_demo_importable): |
| 138 |
"""Test that dataclass-flattened parameters appear correctly.""" |
| 139 |
pytest.importorskip("mkdocs") |
| 140 |
|
| 141 |
result = _run_command( |
| 142 |
["mkdocs", "build", "--site-dir", str(mkdocs_build_dir)], |
| 143 |
cwd=COMPLEX_DEMO_DIR, |
| 144 |
env={"PYTHONPATH": str(COMPLEX_DEMO_DIR)}, |
| 145 |
) |
| 146 |
|
| 147 |
if result.returncode != 0: |
| 148 |
pytest.skip(f"mkdocs build failed: {result.stderr}") |
| 149 |
|
| 150 |
|
| 151 |
data_page = mkdocs_build_dir / "cli" / "data" / "index.html" |
| 152 |
if not data_page.exists(): |
| 153 |
data_page = mkdocs_build_dir / "cli" / "data.html" |
| 154 |
|
| 155 |
if not data_page.exists(): |
| 156 |
pytest.skip(f"Data page not found. Contents: {list(mkdocs_build_dir.rglob('*.html'))}") |
| 157 |
|
| 158 |
content = data_page.read_text() |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
assert "batch" in content.lower(), "batch-size parameter not found" |
| 163 |
assert "worker" in content.lower(), "workers parameter not found" |
| 164 |
assert "output" in content.lower(), "output-dir parameter not found" |
| 165 |
|
| 166 |
@pytest.mark.skipif( |
| 167 |
shutil.which("mkdocs") is None, |
| 168 |
reason="mkdocs not installed or not in PATH", |
| 169 |
) |
| 170 |
def test_mkdocs_output_contains_complex_types(self, mkdocs_build_dir, ensure_complex_demo_importable): |
| 171 |
"""Test that complex union types are documented correctly.""" |
| 172 |
pytest.importorskip("mkdocs") |
| 173 |
|
| 174 |
result = _run_command( |
| 175 |
["mkdocs", "build", "--site-dir", str(mkdocs_build_dir)], |
| 176 |
cwd=COMPLEX_DEMO_DIR, |
| 177 |
env={"PYTHONPATH": str(COMPLEX_DEMO_DIR)}, |
| 178 |
) |
| 179 |
|
| 180 |
if result.returncode != 0: |
| 181 |
pytest.skip(f"mkdocs build failed: {result.stderr}") |
| 182 |
|
| 183 |
|
| 184 |
utils_page = mkdocs_build_dir / "cli" / "utilities" / "index.html" |
| 185 |
if not utils_page.exists(): |
| 186 |
utils_page = mkdocs_build_dir / "cli" / "utilities.html" |
| 187 |
|
| 188 |
if not utils_page.exists(): |
| 189 |
pytest.skip(f"Utilities page not found. Contents: {list(mkdocs_build_dir.rglob('*.html'))}") |
| 190 |
|
| 191 |
content = utils_page.read_text() |
| 192 |
|
| 193 |
|
| 194 |
assert "auto" in content.lower(), "'auto' literal option not found" |
| 195 |
assert "worker" in content.lower(), "worker-count parameter not found" |
| 196 |
|
| 197 |
@pytest.mark.skipif( |
| 198 |
shutil.which("mkdocs") is None, |
| 199 |
reason="mkdocs not installed or not in PATH", |
| 200 |
) |
| 201 |
def test_mkdocs_hidden_commands_excluded_by_default(self, mkdocs_build_dir, ensure_complex_demo_importable): |
| 202 |
"""Test that hidden commands are not included by default.""" |
| 203 |
pytest.importorskip("mkdocs") |
| 204 |
|
| 205 |
result = _run_command( |
| 206 |
["mkdocs", "build", "--site-dir", str(mkdocs_build_dir)], |
| 207 |
cwd=COMPLEX_DEMO_DIR, |
| 208 |
env={"PYTHONPATH": str(COMPLEX_DEMO_DIR)}, |
| 209 |
) |
| 210 |
|
| 211 |
if result.returncode != 0: |
| 212 |
pytest.skip(f"mkdocs build failed: {result.stderr}") |
| 213 |
|
| 214 |
|
| 215 |
cli_index = mkdocs_build_dir / "cli" / "index" / "index.html" |
| 216 |
if not cli_index.exists(): |
| 217 |
cli_index = mkdocs_build_dir / "cli" / "index.html" |
| 218 |
|
| 219 |
if not cli_index.exists(): |
| 220 |
pytest.skip(f"CLI index not found. Contents: {list(mkdocs_build_dir.rglob('*.html'))}") |
| 221 |
|
| 222 |
content = cli_index.read_text() |
| 223 |
|
| 224 |
|
| 225 |
assert "internal_maintenance" not in content and "internal-maintenance" not in content, ( |
| 226 |
"Hidden command internal_maintenance should not appear" |
| 227 |
) |
| 228 |
|
| 229 |
@pytest.mark.skipif( |
| 230 |
shutil.which("mkdocs") is None, |
| 231 |
reason="mkdocs not installed or not in PATH", |
| 232 |
) |
| 233 |
def test_mkdocs_hidden_commands_included_when_requested(self, mkdocs_build_dir, ensure_complex_demo_importable): |
| 234 |
"""Test that hidden commands appear when include_hidden is True.""" |
| 235 |
pytest.importorskip("mkdocs") |
| 236 |
|
| 237 |
result = _run_command( |
| 238 |
["mkdocs", "build", "--site-dir", str(mkdocs_build_dir)], |
| 239 |
cwd=COMPLEX_DEMO_DIR, |
| 240 |
env={"PYTHONPATH": str(COMPLEX_DEMO_DIR)}, |
| 241 |
) |
| 242 |
|
| 243 |
if result.returncode != 0: |
| 244 |
pytest.skip(f"mkdocs build failed: {result.stderr}") |
| 245 |
|
| 246 |
|
| 247 |
full_page = mkdocs_build_dir / "cli" / "full" / "index.html" |
| 248 |
if not full_page.exists(): |
| 249 |
full_page = mkdocs_build_dir / "cli" / "full.html" |
| 250 |
|
| 251 |
if not full_page.exists(): |
| 252 |
pytest.skip(f"Full page not found. Contents: {list(mkdocs_build_dir.rglob('*.html'))}") |
| 253 |
|
| 254 |
content = full_page.read_text() |
| 255 |
|
| 256 |
|
| 257 |
assert "internal" in content.lower(), "Hidden command should appear with include_hidden=true" |
| 258 |
|
| 259 |
|
| 260 |
@pytest.mark.slow |
| 261 |
class TestSphinxBuild: |
| 262 |
"""Test Sphinx documentation builds.""" |
| 263 |
|
| 264 |
@pytest.fixture |
| 265 |
def sphinx_build_dir(self, tmp_path): |
| 266 |
"""Create a temporary build directory for Sphinx.""" |
| 267 |
build_dir = tmp_path / "_build" / "html" |
| 268 |
return build_dir |
| 269 |
|
| 270 |
@pytest.fixture |
| 271 |
def ensure_complex_demo_importable(self): |
| 272 |
"""Ensure the complex_app module can be imported.""" |
| 273 |
sys.path.insert(0, str(COMPLEX_DEMO_DIR)) |
| 274 |
yield |
| 275 |
sys.path.remove(str(COMPLEX_DEMO_DIR)) |
| 276 |
|
| 277 |
@pytest.mark.skipif( |
| 278 |
shutil.which("sphinx-build") is None, |
| 279 |
reason="sphinx-build not installed or not in PATH", |
| 280 |
) |
| 281 |
def test_sphinx_build_succeeds(self, sphinx_build_dir, ensure_complex_demo_importable): |
| 282 |
"""Test that sphinx-build completes successfully.""" |
| 283 |
pytest.importorskip("sphinx") |
| 284 |
|
| 285 |
source_dir = COMPLEX_DEMO_DIR / "docs" / "source" |
| 286 |
result = _run_command( |
| 287 |
["sphinx-build", "-b", "html", str(source_dir), str(sphinx_build_dir)], |
| 288 |
cwd=COMPLEX_DEMO_DIR, |
| 289 |
env={"PYTHONPATH": str(COMPLEX_DEMO_DIR)}, |
| 290 |
) |
| 291 |
|
| 292 |
assert result.returncode == 0, f"sphinx-build failed:\nSTDOUT:\n{result.stdout}\nSTDERR:\n{result.stderr}" |
| 293 |
assert sphinx_build_dir.exists(), "Build directory was not created" |
| 294 |
|
| 295 |
@pytest.mark.skipif( |
| 296 |
shutil.which("sphinx-build") is None, |
| 297 |
reason="sphinx-build not installed or not in PATH", |
| 298 |
) |
| 299 |
def test_sphinx_output_contains_commands(self, sphinx_build_dir, ensure_complex_demo_importable): |
| 300 |
"""Test that built documentation contains expected command content.""" |
| 301 |
pytest.importorskip("sphinx") |
| 302 |
|
| 303 |
source_dir = COMPLEX_DEMO_DIR / "docs" / "source" |
| 304 |
result = _run_command( |
| 305 |
["sphinx-build", "-b", "html", str(source_dir), str(sphinx_build_dir)], |
| 306 |
cwd=COMPLEX_DEMO_DIR, |
| 307 |
env={"PYTHONPATH": str(COMPLEX_DEMO_DIR)}, |
| 308 |
) |
| 309 |
|
| 310 |
if result.returncode != 0: |
| 311 |
pytest.skip(f"sphinx-build failed: {result.stderr}") |
| 312 |
|
| 313 |
|
| 314 |
cli_index = sphinx_build_dir / "cli" / "index.html" |
| 315 |
if not cli_index.exists(): |
| 316 |
pytest.skip(f"CLI index page not found. Contents: {list(sphinx_build_dir.rglob('*.html'))}") |
| 317 |
|
| 318 |
content = cli_index.read_text() |
| 319 |
|
| 320 |
|
| 321 |
assert "admin" in content.lower(), "admin command not found in CLI index" |
| 322 |
assert "data" in content.lower(), "data command not found in CLI index" |
| 323 |
assert "server" in content.lower(), "server command not found in CLI index" |
| 324 |
|
| 325 |
@pytest.mark.skipif( |
| 326 |
shutil.which("sphinx-build") is None, |
| 327 |
reason="sphinx-build not installed or not in PATH", |
| 328 |
) |
| 329 |
def test_sphinx_output_contains_nested_commands(self, sphinx_build_dir, ensure_complex_demo_importable): |
| 330 |
"""Test that nested commands (4 levels deep) are documented.""" |
| 331 |
pytest.importorskip("sphinx") |
| 332 |
|
| 333 |
source_dir = COMPLEX_DEMO_DIR / "docs" / "source" |
| 334 |
result = _run_command( |
| 335 |
["sphinx-build", "-b", "html", str(source_dir), str(sphinx_build_dir)], |
| 336 |
cwd=COMPLEX_DEMO_DIR, |
| 337 |
env={"PYTHONPATH": str(COMPLEX_DEMO_DIR)}, |
| 338 |
) |
| 339 |
|
| 340 |
if result.returncode != 0: |
| 341 |
pytest.skip(f"sphinx-build failed: {result.stderr}") |
| 342 |
|
| 343 |
|
| 344 |
admin_page = sphinx_build_dir / "cli" / "admin.html" |
| 345 |
if not admin_page.exists(): |
| 346 |
pytest.skip(f"Admin page not found. Contents: {list(sphinx_build_dir.rglob('*.html'))}") |
| 347 |
|
| 348 |
content = admin_page.read_text() |
| 349 |
|
| 350 |
|
| 351 |
assert "users" in content.lower(), "users subcommand not found" |
| 352 |
assert "permissions" in content.lower(), "permissions subcommand not found" |
| 353 |
|
| 354 |
@pytest.mark.skipif( |
| 355 |
shutil.which("sphinx-build") is None, |
| 356 |
reason="sphinx-build not installed or not in PATH", |
| 357 |
) |
| 358 |
def test_sphinx_output_contains_dataclass_params(self, sphinx_build_dir, ensure_complex_demo_importable): |
| 359 |
"""Test that dataclass-flattened parameters appear correctly.""" |
| 360 |
pytest.importorskip("sphinx") |
| 361 |
|
| 362 |
source_dir = COMPLEX_DEMO_DIR / "docs" / "source" |
| 363 |
result = _run_command( |
| 364 |
["sphinx-build", "-b", "html", str(source_dir), str(sphinx_build_dir)], |
| 365 |
cwd=COMPLEX_DEMO_DIR, |
| 366 |
env={"PYTHONPATH": str(COMPLEX_DEMO_DIR)}, |
| 367 |
) |
| 368 |
|
| 369 |
if result.returncode != 0: |
| 370 |
pytest.skip(f"sphinx-build failed: {result.stderr}") |
| 371 |
|
| 372 |
|
| 373 |
data_page = sphinx_build_dir / "cli" / "data.html" |
| 374 |
if not data_page.exists(): |
| 375 |
pytest.skip(f"Data page not found. Contents: {list(sphinx_build_dir.rglob('*.html'))}") |
| 376 |
|
| 377 |
content = data_page.read_text() |
| 378 |
|
| 379 |
|
| 380 |
assert "batch" in content.lower(), "batch-size parameter not found" |
| 381 |
assert "worker" in content.lower(), "workers parameter not found" |
| 382 |
|
| 383 |
@pytest.mark.skipif( |
| 384 |
shutil.which("sphinx-build") is None, |
| 385 |
reason="sphinx-build not installed or not in PATH", |
| 386 |
) |
| 387 |
def test_sphinx_rst_anchors_generated(self, sphinx_build_dir, ensure_complex_demo_importable): |
| 388 |
"""Test that RST reference anchors are generated.""" |
| 389 |
pytest.importorskip("sphinx") |
| 390 |
|
| 391 |
source_dir = COMPLEX_DEMO_DIR / "docs" / "source" |
| 392 |
result = _run_command( |
| 393 |
["sphinx-build", "-b", "html", str(source_dir), str(sphinx_build_dir)], |
| 394 |
cwd=COMPLEX_DEMO_DIR, |
| 395 |
env={"PYTHONPATH": str(COMPLEX_DEMO_DIR)}, |
| 396 |
) |
| 397 |
|
| 398 |
if result.returncode != 0: |
| 399 |
pytest.skip(f"sphinx-build failed: {result.stderr}") |
| 400 |
|
| 401 |
|
| 402 |
cli_index = sphinx_build_dir / "cli" / "index.html" |
| 403 |
if not cli_index.exists(): |
| 404 |
pytest.skip(f"CLI index not found. Contents: {list(sphinx_build_dir.rglob('*.html'))}") |
| 405 |
|
| 406 |
content = cli_index.read_text() |
| 407 |
|
| 408 |
|
| 409 |
assert 'id="' in content, "No anchor IDs found in output" |
| 410 |
|
| 411 |
|
| 412 |
class TestDocstringFormats: |
| 413 |
"""Test that different docstring formats are handled correctly.""" |
| 414 |
|
| 415 |
@pytest.fixture |
| 416 |
def ensure_complex_demo_importable(self): |
| 417 |
"""Ensure the complex_app module can be imported.""" |
| 418 |
sys.path.insert(0, str(COMPLEX_DEMO_DIR)) |
| 419 |
yield |
| 420 |
sys.path.remove(str(COMPLEX_DEMO_DIR)) |
| 421 |
|
| 422 |
def test_numpy_docstring_parsing(self, ensure_complex_demo_importable): |
| 423 |
"""Test that NumPy-style docstrings are parsed correctly.""" |
| 424 |
from complex_app import numpy_style |
| 425 |
|
| 426 |
|
| 427 |
assert numpy_style.__doc__ is not None |
| 428 |
assert "name" in numpy_style.__doc__ |
| 429 |
assert "count" in numpy_style.__doc__ |
| 430 |
|
| 431 |
def test_google_docstring_parsing(self, ensure_complex_demo_importable): |
| 432 |
"""Test that Google-style docstrings are parsed correctly.""" |
| 433 |
from complex_app import google_style |
| 434 |
|
| 435 |
|
| 436 |
assert google_style.__doc__ is not None |
| 437 |
assert "name" in google_style.__doc__ |
| 438 |
assert "count" in google_style.__doc__ |
| 439 |
|
| 440 |
def test_sphinx_docstring_parsing(self, ensure_complex_demo_importable): |
| 441 |
"""Test that Sphinx-style docstrings are parsed correctly.""" |
| 442 |
from complex_app import sphinx_style |
| 443 |
|
| 444 |
|
| 445 |
assert sphinx_style.__doc__ is not None |
| 446 |
assert "name" in sphinx_style.__doc__ |
| 447 |
assert "count" in sphinx_style.__doc__ |
| 448 |
|
| 449 |
|
| 450 |
class TestDataclassFlattening: |
| 451 |
"""Test that dataclass parameter flattening works correctly.""" |
| 452 |
|
| 453 |
@pytest.fixture |
| 454 |
def ensure_complex_demo_importable(self): |
| 455 |
"""Ensure the complex_app module can be imported.""" |
| 456 |
sys.path.insert(0, str(COMPLEX_DEMO_DIR)) |
| 457 |
yield |
| 458 |
sys.path.remove(str(COMPLEX_DEMO_DIR)) |
| 459 |
|
| 460 |
def test_dataclass_config_imported(self, ensure_complex_demo_importable): |
| 461 |
"""Test that dataclass configs can be imported.""" |
| 462 |
from complex_app import DatabaseConfig, PathConfig, PipelineConfig, ProcessingConfig |
| 463 |
|
| 464 |
|
| 465 |
assert DatabaseConfig is not None |
| 466 |
assert ProcessingConfig is not None |
| 467 |
assert PathConfig is not None |
| 468 |
assert PipelineConfig is not None |
| 469 |
|
| 470 |
def test_dataclass_defaults_accessible(self, ensure_complex_demo_importable): |
| 471 |
"""Test that dataclass defaults are accessible.""" |
| 472 |
from complex_app import DatabaseConfig |
| 473 |
|
| 474 |
config = DatabaseConfig() |
| 475 |
assert config.host == "localhost" |
| 476 |
assert config.port == 5432 |
| 477 |
assert config.ssl_mode == "prefer" |
| 478 |
|
| 479 |
def test_nested_dataclass_works(self, ensure_complex_demo_importable): |
| 480 |
"""Test that nested dataclasses work.""" |
| 481 |
from complex_app import PathConfig, PipelineConfig, ProcessingConfig |
| 482 |
|
| 483 |
config = PipelineConfig() |
| 484 |
assert isinstance(config.paths, PathConfig) |
| 485 |
assert isinstance(config.processing, ProcessingConfig) |
| 486 |
|
| 487 |
|
| 488 |
class TestEnumsAndFlags: |
| 489 |
"""Test enum and flag handling.""" |
| 490 |
|
| 491 |
@pytest.fixture |
| 492 |
def ensure_complex_demo_importable(self): |
| 493 |
"""Ensure the complex_app module can be imported.""" |
| 494 |
sys.path.insert(0, str(COMPLEX_DEMO_DIR)) |
| 495 |
yield |
| 496 |
sys.path.remove(str(COMPLEX_DEMO_DIR)) |
| 497 |
|
| 498 |
def test_enum_members(self, ensure_complex_demo_importable): |
| 499 |
"""Test that enums have expected members.""" |
| 500 |
from complex_app import LogLevel, OutputFormat |
| 501 |
|
| 502 |
assert LogLevel.DEBUG.value == "debug" |
| 503 |
assert LogLevel.INFO.value == "info" |
| 504 |
assert OutputFormat.JSON.value == "json" |
| 505 |
assert OutputFormat.YAML.value == "yaml" |
| 506 |
|
| 507 |
def test_flag_combinations(self, ensure_complex_demo_importable): |
| 508 |
"""Test that flags can be combined.""" |
| 509 |
from complex_app import Permission |
| 510 |
|
| 511 |
combined = Permission.READ | Permission.WRITE |
| 512 |
assert Permission.READ in combined |
| 513 |
assert Permission.WRITE in combined |
| 514 |
assert Permission.EXECUTE not in combined |
| 515 |
|
| 516 |
|
| 517 |
class TestAppStructure: |
| 518 |
"""Test the application structure.""" |
| 519 |
|
| 520 |
@pytest.fixture |
| 521 |
def ensure_complex_demo_importable(self): |
| 522 |
"""Ensure the complex_app module can be imported.""" |
| 523 |
sys.path.insert(0, str(COMPLEX_DEMO_DIR)) |
| 524 |
yield |
| 525 |
sys.path.remove(str(COMPLEX_DEMO_DIR)) |
| 526 |
|
| 527 |
def test_app_has_commands(self, ensure_complex_demo_importable): |
| 528 |
"""Test that the app has the expected commands registered.""" |
| 529 |
from complex_app import app |
| 530 |
|
| 531 |
|
| 532 |
command_names = set(app._commands.keys()) |
| 533 |
|
| 534 |
|
| 535 |
assert "admin" in command_names, "admin app not registered" |
| 536 |
assert "data" in command_names, "data app not registered" |
| 537 |
assert "server" in command_names, "server app not registered" |
| 538 |
assert "cache" in command_names, "cache app not registered" |
| 539 |
|
| 540 |
def test_nested_app_structure(self, ensure_complex_demo_importable): |
| 541 |
"""Test that nested apps have the expected structure.""" |
| 542 |
from complex_app import admin_app, permissions_app, roles_app, users_app |
| 543 |
|
| 544 |
|
| 545 |
assert "users" in admin_app._commands |
| 546 |
assert "permissions" in users_app._commands |
| 547 |
assert "roles" in permissions_app._commands |
| 548 |
|
| 549 |
|
| 550 |
role_commands = set(roles_app._commands.keys()) |
| 551 |
assert "list-roles" in role_commands or "list_roles" in role_commands |
| 552 |
assert "create-role" in role_commands or "create_role" in role_commands |
| 553 |
|
| 554 |
def test_groups_configured(self, ensure_complex_demo_importable): |
| 555 |
"""Test that groups are properly configured.""" |
| 556 |
from complex_app import global_group, subcommands_group, utilities_group |
| 557 |
|
| 558 |
assert global_group is not None |
| 559 |
assert subcommands_group is not None |
| 560 |
assert utilities_group is not None |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
assert global_group.sort_key is not None |
| 565 |
assert subcommands_group.sort_key is not None |
| 566 |
assert utilities_group.sort_key is not None |
| 567 |
assert global_group.sort_key < subcommands_group.sort_key |
| 568 |
assert subcommands_group.sort_key < utilities_group.sort_key |
| 569 |
|
| 570 |
|
| 571 |
class TestMarkdownGeneration: |
| 572 |
"""Test direct markdown generation without building.""" |
| 573 |
|
| 574 |
@pytest.fixture |
| 575 |
def ensure_complex_demo_importable(self): |
| 576 |
"""Ensure the complex_app module can be imported.""" |
| 577 |
sys.path.insert(0, str(COMPLEX_DEMO_DIR)) |
| 578 |
yield |
| 579 |
sys.path.remove(str(COMPLEX_DEMO_DIR)) |
| 580 |
|
| 581 |
def test_generate_markdown_basic(self, ensure_complex_demo_importable): |
| 582 |
"""Test basic markdown generation.""" |
| 583 |
from complex_app import app |
| 584 |
|
| 585 |
from cyclopts.docs.markdown import generate_markdown_docs |
| 586 |
|
| 587 |
markdown = generate_markdown_docs(app, recursive=True) |
| 588 |
|
| 589 |
assert "complex-cli" in markdown |
| 590 |
assert "admin" in markdown.lower() |
| 591 |
assert "data" in markdown.lower() |
| 592 |
assert "server" in markdown.lower() |
| 593 |
|
| 594 |
def test_generate_markdown_with_filter(self, ensure_complex_demo_importable): |
| 595 |
"""Test markdown generation with command filtering.""" |
| 596 |
from complex_app import app |
| 597 |
|
| 598 |
from cyclopts.docs.markdown import generate_markdown_docs |
| 599 |
|
| 600 |
markdown = generate_markdown_docs( |
| 601 |
app, |
| 602 |
recursive=True, |
| 603 |
commands_filter=["admin"], |
| 604 |
) |
| 605 |
|
| 606 |
assert "admin" in markdown.lower() |
| 607 |
|
| 608 |
assert "data process" not in markdown.lower() |
| 609 |
assert "server start" not in markdown.lower() |
| 610 |
|
| 611 |
def test_generate_markdown_nested_filter(self, ensure_complex_demo_importable): |
| 612 |
"""Test markdown generation with nested command filtering.""" |
| 613 |
from complex_app import app |
| 614 |
|
| 615 |
from cyclopts.docs.markdown import generate_markdown_docs |
| 616 |
|
| 617 |
markdown = generate_markdown_docs( |
| 618 |
app, |
| 619 |
recursive=True, |
| 620 |
commands_filter=["admin.users.permissions"], |
| 621 |
) |
| 622 |
|
| 623 |
assert "permissions" in markdown.lower() |
| 624 |
assert "grant" in markdown.lower() or "revoke" in markdown.lower() |
| 625 |
|
| 626 |
def test_generate_markdown_flattened(self, ensure_complex_demo_importable): |
| 627 |
"""Test markdown generation with flattened commands.""" |
| 628 |
from complex_app import app |
| 629 |
|
| 630 |
from cyclopts.docs.markdown import generate_markdown_docs |
| 631 |
|
| 632 |
markdown = generate_markdown_docs( |
| 633 |
app, |
| 634 |
recursive=True, |
| 635 |
flatten_commands=True, |
| 636 |
) |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
lines = markdown.split("\n") |
| 641 |
heading_levels = [] |
| 642 |
for line in lines: |
| 643 |
if line.startswith("#") and not line.startswith("```"): |
| 644 |
level = len(line) - len(line.lstrip("#")) |
| 645 |
heading_levels.append(level) |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
if len(heading_levels) > 2: |
| 650 |
|
| 651 |
from collections import Counter |
| 652 |
|
| 653 |
counts = Counter(heading_levels) |
| 654 |
most_common_level, _ = counts.most_common(1)[0] |
| 655 |
same_level_count = sum(1 for h in heading_levels if h == most_common_level) |
| 656 |
|
| 657 |
assert same_level_count >= len(heading_levels) // 2, "Commands not properly flattened" |
| 658 |
|
| 659 |
def test_generate_markdown_include_hidden(self, ensure_complex_demo_importable): |
| 660 |
"""Test markdown generation with hidden commands included.""" |
| 661 |
from complex_app import app |
| 662 |
|
| 663 |
from cyclopts.docs.markdown import generate_markdown_docs |
| 664 |
|
| 665 |
|
| 666 |
markdown_normal = generate_markdown_docs(app, recursive=True, include_hidden=False) |
| 667 |
|
| 668 |
|
| 669 |
markdown_hidden = generate_markdown_docs(app, recursive=True, include_hidden=True) |
| 670 |
|
| 671 |
|
| 672 |
has_internal_normal = "internal-maintenance" in markdown_normal or "internal_maintenance" in markdown_normal |
| 673 |
has_internal_hidden = "internal-maintenance" in markdown_hidden or "internal_maintenance" in markdown_hidden |
| 674 |
|
| 675 |
assert not has_internal_normal, "Hidden command should not appear without include_hidden" |
| 676 |
assert has_internal_hidden, "Hidden command should appear with include_hidden=True" |