| 1 |
"""Tests for the Cyclopts MkDocs plugin.""" |
| 2 |
|
| 3 |
import sys |
| 4 |
import textwrap |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from cyclopts import App |
| 9 |
from cyclopts.utils import import_app |
| 10 |
|
| 11 |
|
| 12 |
@pytest.fixture |
| 13 |
def importable_tmp_path(tmp_path, monkeypatch): |
| 14 |
"""Fixture that makes tmp_path importable and cleans up sys.modules after test. |
| 15 |
|
| 16 |
Usage: |
| 17 |
def test_foo(importable_tmp_path): |
| 18 |
module_file = importable_tmp_path / "my_module.py" |
| 19 |
module_file.write_text("...") |
| 20 |
import my_module # Works! |
| 21 |
# Cleanup is automatic |
| 22 |
""" |
| 23 |
monkeypatch.syspath_prepend(str(tmp_path)) |
| 24 |
modules_before = set(sys.modules.keys()) |
| 25 |
yield tmp_path |
| 26 |
|
| 27 |
for mod_name in list(sys.modules.keys()): |
| 28 |
if mod_name not in modules_before: |
| 29 |
del sys.modules[mod_name] |
| 30 |
|
| 31 |
|
| 32 |
class TestImportApp: |
| 33 |
"""Test the import_app function.""" |
| 34 |
|
| 35 |
def test_import_with_colon_notation(self, importable_tmp_path): |
| 36 |
"""Test importing an app using module:app notation.""" |
| 37 |
module_file = importable_tmp_path / "test_module.py" |
| 38 |
module_file.write_text( |
| 39 |
textwrap.dedent( |
| 40 |
"""\ |
| 41 |
from cyclopts import App |
| 42 |
|
| 43 |
my_app = App(name="test-app", help="Test application") |
| 44 |
|
| 45 |
@my_app.command |
| 46 |
def hello(): |
| 47 |
'''Say hello.''' |
| 48 |
pass |
| 49 |
""" |
| 50 |
) |
| 51 |
) |
| 52 |
|
| 53 |
app = import_app("test_module:my_app") |
| 54 |
assert isinstance(app, App) |
| 55 |
assert app.name == ("test-app",) |
| 56 |
|
| 57 |
def test_import_without_colon_finds_app(self, importable_tmp_path): |
| 58 |
"""Test importing when app name is not specified (auto-discovery).""" |
| 59 |
module_file = importable_tmp_path / "test_cli.py" |
| 60 |
module_file.write_text( |
| 61 |
textwrap.dedent( |
| 62 |
"""\ |
| 63 |
from cyclopts import App |
| 64 |
|
| 65 |
app = App(name="auto-found", help="Automatically found app") |
| 66 |
""" |
| 67 |
) |
| 68 |
) |
| 69 |
|
| 70 |
found_app = import_app("test_cli") |
| 71 |
assert isinstance(found_app, App) |
| 72 |
assert found_app.name == ("auto-found",) |
| 73 |
|
| 74 |
def test_import_module_not_found(self): |
| 75 |
"""Test error when module doesn't exist.""" |
| 76 |
with pytest.raises(ImportError, match="Cannot import module"): |
| 77 |
import_app("nonexistent_module:app") |
| 78 |
|
| 79 |
def test_import_app_not_found(self, importable_tmp_path): |
| 80 |
"""Test error when specified app doesn't exist in module.""" |
| 81 |
module_file = importable_tmp_path / "test_no_app.py" |
| 82 |
module_file.write_text( |
| 83 |
textwrap.dedent( |
| 84 |
"""\ |
| 85 |
from cyclopts import App |
| 86 |
|
| 87 |
some_app = App(name="test", help="Test") |
| 88 |
""" |
| 89 |
) |
| 90 |
) |
| 91 |
|
| 92 |
with pytest.raises(AttributeError, match="has no attribute 'missing_app'"): |
| 93 |
import_app("test_no_app:missing_app") |
| 94 |
|
| 95 |
def test_import_not_an_app(self, importable_tmp_path): |
| 96 |
"""Test error when object is not a Cyclopts App.""" |
| 97 |
module_name = "test_not_app_module" |
| 98 |
module_file = importable_tmp_path / f"{module_name}.py" |
| 99 |
module_file.write_text("not_an_app = 'This is just a string'") |
| 100 |
|
| 101 |
with pytest.raises(TypeError, match="is not a Cyclopts App instance"): |
| 102 |
import_app(f"{module_name}:not_an_app") |
| 103 |
|
| 104 |
|
| 105 |
class TestDirectiveOptions: |
| 106 |
"""Test the DirectiveOptions class.""" |
| 107 |
|
| 108 |
def test_parse_basic_directive(self): |
| 109 |
"""Test parsing a basic directive with minimal options.""" |
| 110 |
from cyclopts.ext.mkdocs import DirectiveOptions |
| 111 |
|
| 112 |
directive_text = textwrap.dedent( |
| 113 |
"""\ |
| 114 |
::: cyclopts |
| 115 |
module: myapp.cli:app |
| 116 |
""" |
| 117 |
) |
| 118 |
|
| 119 |
options = DirectiveOptions.from_directive_block(directive_text) |
| 120 |
assert options.module == "myapp.cli:app" |
| 121 |
assert options.heading_level == 2 |
| 122 |
assert options.recursive is True |
| 123 |
assert options.include_hidden is False |
| 124 |
|
| 125 |
def test_parse_full_directive(self): |
| 126 |
"""Test parsing a directive with all options.""" |
| 127 |
from cyclopts.ext.mkdocs import DirectiveOptions |
| 128 |
|
| 129 |
directive_text = textwrap.dedent( |
| 130 |
"""\ |
| 131 |
::: cyclopts |
| 132 |
module: myapp.cli:app |
| 133 |
heading_level: 3 |
| 134 |
recursive: false |
| 135 |
include_hidden: true |
| 136 |
flatten_commands: true |
| 137 |
generate_toc: false |
| 138 |
usage_name: uv run cli |
| 139 |
commands: |
| 140 |
- init |
| 141 |
- build |
| 142 |
- deploy |
| 143 |
exclude_commands: |
| 144 |
- debug |
| 145 |
- internal |
| 146 |
""" |
| 147 |
) |
| 148 |
|
| 149 |
options = DirectiveOptions.from_directive_block(directive_text) |
| 150 |
assert options.module == "myapp.cli:app" |
| 151 |
assert options.heading_level == 3 |
| 152 |
assert options.recursive is False |
| 153 |
assert options.include_hidden is True |
| 154 |
assert options.flatten_commands is True |
| 155 |
assert options.generate_toc is False |
| 156 |
assert options.usage_name == "uv run cli" |
| 157 |
assert options.commands == ["init", "build", "deploy"] |
| 158 |
assert options.exclude_commands == ["debug", "internal"] |
| 159 |
|
| 160 |
def test_parse_comma_separated_commands(self): |
| 161 |
"""Test parsing YAML list format for commands.""" |
| 162 |
from cyclopts.ext.mkdocs import DirectiveOptions |
| 163 |
|
| 164 |
directive_text = textwrap.dedent( |
| 165 |
"""\ |
| 166 |
::: cyclopts |
| 167 |
module: myapp.cli |
| 168 |
commands: [cmd1, cmd2, cmd3] |
| 169 |
""" |
| 170 |
) |
| 171 |
|
| 172 |
options = DirectiveOptions.from_directive_block(directive_text) |
| 173 |
assert options.commands == ["cmd1", "cmd2", "cmd3"] |
| 174 |
|
| 175 |
def test_parse_single_command_string_raises_error(self): |
| 176 |
"""Test that a single command string (not a list) raises ValueError. |
| 177 |
|
| 178 |
YAML parses 'commands: files.cp' as a string, not a list. |
| 179 |
We enforce that commands must be a list for clarity. |
| 180 |
""" |
| 181 |
from cyclopts.ext.mkdocs import DirectiveOptions |
| 182 |
|
| 183 |
directive_text = textwrap.dedent( |
| 184 |
"""\ |
| 185 |
::: cyclopts |
| 186 |
module: myapp.cli |
| 187 |
commands: files.cp |
| 188 |
""" |
| 189 |
) |
| 190 |
|
| 191 |
with pytest.raises(ValueError, match="'commands'.*must be.*list"): |
| 192 |
DirectiveOptions.from_directive_block(directive_text) |
| 193 |
|
| 194 |
def test_parse_single_exclude_command_string_raises_error(self): |
| 195 |
"""Test that a single exclude command string raises ValueError.""" |
| 196 |
from cyclopts.ext.mkdocs import DirectiveOptions |
| 197 |
|
| 198 |
directive_text = textwrap.dedent( |
| 199 |
"""\ |
| 200 |
::: cyclopts |
| 201 |
module: myapp.cli |
| 202 |
exclude_commands: debug |
| 203 |
""" |
| 204 |
) |
| 205 |
|
| 206 |
with pytest.raises(ValueError, match="'exclude_commands'.*must be.*list"): |
| 207 |
DirectiveOptions.from_directive_block(directive_text) |
| 208 |
|
| 209 |
def test_parse_missing_module_raises_error(self): |
| 210 |
"""Test that missing module option raises an error.""" |
| 211 |
from cyclopts.ext.mkdocs import DirectiveOptions |
| 212 |
|
| 213 |
directive_text = textwrap.dedent( |
| 214 |
"""\ |
| 215 |
::: cyclopts |
| 216 |
heading_level: 2 |
| 217 |
""" |
| 218 |
) |
| 219 |
|
| 220 |
with pytest.raises(ValueError, match="module.*required"): |
| 221 |
DirectiveOptions.from_directive_block(directive_text) |
| 222 |
|
| 223 |
def test_parse_invalid_heading_level_type_raises_error(self): |
| 224 |
"""Test that non-integer heading_level raises TypeError.""" |
| 225 |
from cyclopts.ext.mkdocs import DirectiveOptions |
| 226 |
|
| 227 |
directive_text = textwrap.dedent( |
| 228 |
"""\ |
| 229 |
::: cyclopts |
| 230 |
module: myapp.cli |
| 231 |
heading_level: "3" |
| 232 |
""" |
| 233 |
) |
| 234 |
|
| 235 |
with pytest.raises(ValueError, match="'heading_level'.*must be.*int"): |
| 236 |
DirectiveOptions.from_directive_block(directive_text) |
| 237 |
|
| 238 |
def test_parse_invalid_recursive_type_raises_error(self): |
| 239 |
"""Test that non-boolean recursive raises TypeError.""" |
| 240 |
from cyclopts.ext.mkdocs import DirectiveOptions |
| 241 |
|
| 242 |
directive_text = textwrap.dedent( |
| 243 |
"""\ |
| 244 |
::: cyclopts |
| 245 |
module: myapp.cli |
| 246 |
recursive: "yes" |
| 247 |
""" |
| 248 |
) |
| 249 |
|
| 250 |
with pytest.raises(ValueError, match="'recursive'.*must be.*bool"): |
| 251 |
DirectiveOptions.from_directive_block(directive_text) |
| 252 |
|
| 253 |
def test_parse_boolean_variations(self): |
| 254 |
"""Test parsing various boolean value formats.""" |
| 255 |
from cyclopts.ext.mkdocs import DirectiveOptions |
| 256 |
|
| 257 |
|
| 258 |
directive_text = textwrap.dedent( |
| 259 |
"""\ |
| 260 |
::: cyclopts |
| 261 |
module: myapp.cli |
| 262 |
recursive: true |
| 263 |
""" |
| 264 |
) |
| 265 |
options = DirectiveOptions.from_directive_block(directive_text) |
| 266 |
assert options.recursive is True |
| 267 |
|
| 268 |
|
| 269 |
directive_text = textwrap.dedent( |
| 270 |
"""\ |
| 271 |
::: cyclopts |
| 272 |
module: myapp.cli |
| 273 |
recursive: yes |
| 274 |
""" |
| 275 |
) |
| 276 |
options = DirectiveOptions.from_directive_block(directive_text) |
| 277 |
assert options.recursive is True |
| 278 |
|
| 279 |
|
| 280 |
directive_text = textwrap.dedent( |
| 281 |
"""\ |
| 282 |
::: cyclopts |
| 283 |
module: myapp.cli |
| 284 |
recursive: false |
| 285 |
""" |
| 286 |
) |
| 287 |
options = DirectiveOptions.from_directive_block(directive_text) |
| 288 |
assert options.recursive is False |
| 289 |
|
| 290 |
|
| 291 |
class TestProcessDirectives: |
| 292 |
"""Test the process_cyclopts_directives function.""" |
| 293 |
|
| 294 |
def test_process_simple_directive(self, importable_tmp_path): |
| 295 |
"""Test processing a simple directive in markdown.""" |
| 296 |
module_file = importable_tmp_path / "simple_app.py" |
| 297 |
module_file.write_text( |
| 298 |
textwrap.dedent( |
| 299 |
"""\ |
| 300 |
from cyclopts import App |
| 301 |
|
| 302 |
app = App(name="simple-cli", help="A simple CLI app") |
| 303 |
|
| 304 |
@app.default |
| 305 |
def main(name: str): |
| 306 |
'''Greet someone by name.''' |
| 307 |
print(f"Hello, {name}!") |
| 308 |
""" |
| 309 |
) |
| 310 |
) |
| 311 |
|
| 312 |
from cyclopts.ext.mkdocs import process_cyclopts_directives |
| 313 |
|
| 314 |
markdown = textwrap.dedent( |
| 315 |
"""\ |
| 316 |
# My CLI Tool |
| 317 |
|
| 318 |
Here's the documentation: |
| 319 |
|
| 320 |
::: cyclopts |
| 321 |
module: simple_app:app |
| 322 |
heading_level: 2 |
| 323 |
|
| 324 |
More content here. |
| 325 |
""" |
| 326 |
) |
| 327 |
|
| 328 |
result = process_cyclopts_directives(markdown, None) |
| 329 |
|
| 330 |
|
| 331 |
assert "::: cyclopts" not in result |
| 332 |
assert "simple-cli" in result or "simple_cli" in result |
| 333 |
assert "More content here." in result |
| 334 |
assert "# My CLI Tool" in result |
| 335 |
|
| 336 |
def test_process_multiple_directives(self, importable_tmp_path): |
| 337 |
"""Test processing multiple directives in one page.""" |
| 338 |
module_file = importable_tmp_path / "multi_app.py" |
| 339 |
module_file.write_text( |
| 340 |
textwrap.dedent( |
| 341 |
"""\ |
| 342 |
from cyclopts import App |
| 343 |
|
| 344 |
app = App(name="cli", help="Main CLI") |
| 345 |
|
| 346 |
@app.default |
| 347 |
def main(): |
| 348 |
pass |
| 349 |
""" |
| 350 |
) |
| 351 |
) |
| 352 |
|
| 353 |
from cyclopts.ext.mkdocs import process_cyclopts_directives |
| 354 |
|
| 355 |
markdown = textwrap.dedent( |
| 356 |
"""\ |
| 357 |
# First Section |
| 358 |
|
| 359 |
::: cyclopts |
| 360 |
module: multi_app:app |
| 361 |
heading_level: 2 |
| 362 |
generate_toc: false |
| 363 |
|
| 364 |
# Second Section |
| 365 |
|
| 366 |
::: cyclopts |
| 367 |
module: multi_app:app |
| 368 |
heading_level: 3 |
| 369 |
generate_toc: false |
| 370 |
""" |
| 371 |
) |
| 372 |
|
| 373 |
result = process_cyclopts_directives(markdown, None) |
| 374 |
|
| 375 |
|
| 376 |
assert result.count("::: cyclopts") == 0 |
| 377 |
assert "# First Section" in result |
| 378 |
assert "# Second Section" in result |
| 379 |
|
| 380 |
def test_process_directive_with_usage_name(self, importable_tmp_path): |
| 381 |
"""usage_name in the directive overrides the app name in Usage: lines.""" |
| 382 |
module_file = importable_tmp_path / "usage_name_app.py" |
| 383 |
module_file.write_text( |
| 384 |
textwrap.dedent( |
| 385 |
"""\ |
| 386 |
from cyclopts import App |
| 387 |
|
| 388 |
app = App(name="cli", help="Test CLI") |
| 389 |
|
| 390 |
@app.command |
| 391 |
def serve(port: int = 8000): |
| 392 |
'''Start the server.''' |
| 393 |
pass |
| 394 |
""" |
| 395 |
) |
| 396 |
) |
| 397 |
|
| 398 |
from cyclopts.ext.mkdocs import process_cyclopts_directives |
| 399 |
|
| 400 |
markdown = textwrap.dedent( |
| 401 |
"""\ |
| 402 |
::: cyclopts |
| 403 |
module: usage_name_app:app |
| 404 |
usage_name: uv run cli |
| 405 |
""" |
| 406 |
) |
| 407 |
|
| 408 |
result = process_cyclopts_directives(markdown, None) |
| 409 |
assert "uv run cli" in result |
| 410 |
|
| 411 |
def test_process_directive_with_error(self): |
| 412 |
"""Test that errors in directive processing raise PluginError.""" |
| 413 |
pytest.importorskip("mkdocs") |
| 414 |
from cyclopts.ext.mkdocs import PluginError, process_cyclopts_directives |
| 415 |
|
| 416 |
markdown = textwrap.dedent( |
| 417 |
"""\ |
| 418 |
# My Documentation |
| 419 |
|
| 420 |
::: cyclopts |
| 421 |
module: nonexistent_module:app |
| 422 |
|
| 423 |
More content. |
| 424 |
""" |
| 425 |
) |
| 426 |
|
| 427 |
|
| 428 |
with pytest.raises(PluginError, match="Error processing ::: cyclopts directive"): |
| 429 |
process_cyclopts_directives(markdown, None) |
| 430 |
|
| 431 |
def test_no_directives_unchanged(self): |
| 432 |
"""Test that markdown without directives is unchanged.""" |
| 433 |
from cyclopts.ext.mkdocs import process_cyclopts_directives |
| 434 |
|
| 435 |
markdown = textwrap.dedent( |
| 436 |
"""\ |
| 437 |
# My Documentation |
| 438 |
|
| 439 |
This is just regular markdown content. |
| 440 |
|
| 441 |
No directives here! |
| 442 |
""" |
| 443 |
) |
| 444 |
|
| 445 |
result = process_cyclopts_directives(markdown, None) |
| 446 |
assert result == markdown |
| 447 |
|
| 448 |
def test_default_heading_level_from_config(self): |
| 449 |
"""Test that default_heading_level from config is used when not specified in directive.""" |
| 450 |
from cyclopts.ext.mkdocs import DirectiveOptions |
| 451 |
|
| 452 |
|
| 453 |
directive_text = textwrap.dedent( |
| 454 |
"""\ |
| 455 |
::: cyclopts |
| 456 |
module: config_test_app:app |
| 457 |
""" |
| 458 |
) |
| 459 |
|
| 460 |
options = DirectiveOptions.from_directive_block(directive_text, default_heading_level=3) |
| 461 |
assert options.heading_level == 3 |
| 462 |
|
| 463 |
|
| 464 |
directive_text_with_level = textwrap.dedent( |
| 465 |
"""\ |
| 466 |
::: cyclopts |
| 467 |
module: config_test_app:app |
| 468 |
heading_level: 4 |
| 469 |
""" |
| 470 |
) |
| 471 |
|
| 472 |
options_override = DirectiveOptions.from_directive_block(directive_text_with_level, default_heading_level=3) |
| 473 |
assert options_override.heading_level == 4 |
| 474 |
|
| 475 |
def test_no_root_title_in_output(self, importable_tmp_path): |
| 476 |
"""Test that the root app title is not included in generated docs.""" |
| 477 |
module_file = importable_tmp_path / "no_title_app.py" |
| 478 |
module_file.write_text( |
| 479 |
textwrap.dedent( |
| 480 |
"""\ |
| 481 |
from cyclopts import App |
| 482 |
|
| 483 |
app = App(name="no-title-test", help="This app should not show its title") |
| 484 |
|
| 485 |
@app.default |
| 486 |
def main(arg: str): |
| 487 |
'''Do something.''' |
| 488 |
pass |
| 489 |
""" |
| 490 |
) |
| 491 |
) |
| 492 |
|
| 493 |
from cyclopts.ext.mkdocs import process_cyclopts_directives |
| 494 |
|
| 495 |
markdown = textwrap.dedent( |
| 496 |
"""\ |
| 497 |
# Documentation |
| 498 |
|
| 499 |
::: cyclopts |
| 500 |
module: no_title_app:app |
| 501 |
generate_toc: false |
| 502 |
|
| 503 |
More content. |
| 504 |
""" |
| 505 |
) |
| 506 |
|
| 507 |
result = process_cyclopts_directives(markdown, None) |
| 508 |
|
| 509 |
|
| 510 |
assert "# no-title-test" not in result |
| 511 |
assert "## no-title-test" not in result |
| 512 |
|
| 513 |
assert "This app should not show its title" in result |
| 514 |
assert "More content." in result |
| 515 |
|
| 516 |
def test_skip_preamble_skips_description_and_usage(self, importable_tmp_path): |
| 517 |
"""Test that skip_preamble skips description and usage for filtered command.""" |
| 518 |
module_file = importable_tmp_path / "skip_preamble_app.py" |
| 519 |
module_file.write_text( |
| 520 |
textwrap.dedent( |
| 521 |
"""\ |
| 522 |
from cyclopts import App |
| 523 |
|
| 524 |
app = App(name="main-app", help="Main app description") |
| 525 |
|
| 526 |
sub = App(name="sub", help="This description should be skipped") |
| 527 |
|
| 528 |
@sub.default |
| 529 |
def sub_cmd(arg: str): |
| 530 |
'''Sub command.''' |
| 531 |
pass |
| 532 |
|
| 533 |
app.command(sub) |
| 534 |
""" |
| 535 |
) |
| 536 |
) |
| 537 |
|
| 538 |
from cyclopts.ext.mkdocs import process_cyclopts_directives |
| 539 |
|
| 540 |
|
| 541 |
markdown_without_skip = textwrap.dedent( |
| 542 |
"""\ |
| 543 |
# Sub Commands |
| 544 |
|
| 545 |
::: cyclopts |
| 546 |
module: skip_preamble_app:app |
| 547 |
commands: [sub] |
| 548 |
generate_toc: false |
| 549 |
""" |
| 550 |
) |
| 551 |
|
| 552 |
result_without_skip = process_cyclopts_directives(markdown_without_skip, None) |
| 553 |
assert "This description should be skipped" in result_without_skip |
| 554 |
|
| 555 |
|
| 556 |
del sys.modules["skip_preamble_app"] |
| 557 |
|
| 558 |
|
| 559 |
markdown_with_skip = textwrap.dedent( |
| 560 |
"""\ |
| 561 |
# Sub Commands |
| 562 |
|
| 563 |
::: cyclopts |
| 564 |
module: skip_preamble_app:app |
| 565 |
commands: [sub] |
| 566 |
generate_toc: false |
| 567 |
skip_preamble: true |
| 568 |
""" |
| 569 |
) |
| 570 |
|
| 571 |
result_with_skip = process_cyclopts_directives(markdown_with_skip, None) |
| 572 |
assert "This description should be skipped" not in result_with_skip |
| 573 |
|
| 574 |
assert "arg" in result_with_skip.lower() |
| 575 |
|
| 576 |
def test_skip_preamble_with_nested_path(self, importable_tmp_path): |
| 577 |
"""Test that skip_preamble also skips intermediate commands for nested paths.""" |
| 578 |
module_file = importable_tmp_path / "nested_skip_app.py" |
| 579 |
module_file.write_text( |
| 580 |
textwrap.dedent( |
| 581 |
"""\ |
| 582 |
from cyclopts import App |
| 583 |
|
| 584 |
app = App(name="main-app", help="Main app description") |
| 585 |
|
| 586 |
parent = App(name="parent", help="Parent description - should be skipped") |
| 587 |
|
| 588 |
child = App(name="child", help="Child description - should be skipped") |
| 589 |
|
| 590 |
@child.default |
| 591 |
def child_cmd(arg: str): |
| 592 |
'''Child command.''' |
| 593 |
pass |
| 594 |
|
| 595 |
parent.command(child) |
| 596 |
app.command(parent) |
| 597 |
""" |
| 598 |
) |
| 599 |
) |
| 600 |
|
| 601 |
from cyclopts.ext.mkdocs import process_cyclopts_directives |
| 602 |
|
| 603 |
|
| 604 |
markdown = textwrap.dedent( |
| 605 |
"""\ |
| 606 |
# Child Commands |
| 607 |
|
| 608 |
::: cyclopts |
| 609 |
module: nested_skip_app:app |
| 610 |
commands: [parent.child] |
| 611 |
generate_toc: false |
| 612 |
skip_preamble: true |
| 613 |
""" |
| 614 |
) |
| 615 |
|
| 616 |
result = process_cyclopts_directives(markdown, None) |
| 617 |
|
| 618 |
|
| 619 |
assert "Parent description - should be skipped" not in result |
| 620 |
|
| 621 |
assert "Child description - should be skipped" not in result |
| 622 |
|
| 623 |
assert "arg" in result.lower() |
| 624 |
|
| 625 |
|
| 626 |
class TestDirectivePattern: |
| 627 |
"""Test the directive pattern regex.""" |
| 628 |
|
| 629 |
def test_pattern_matches_simple_directive(self): |
| 630 |
"""Test that the pattern matches a simple directive.""" |
| 631 |
from cyclopts.ext.mkdocs import DIRECTIVE_PATTERN |
| 632 |
|
| 633 |
text = textwrap.dedent( |
| 634 |
"""\ |
| 635 |
::: cyclopts |
| 636 |
module: myapp.cli |
| 637 |
""" |
| 638 |
) |
| 639 |
|
| 640 |
match = DIRECTIVE_PATTERN.search(text) |
| 641 |
assert match is not None |
| 642 |
assert match.group(0).startswith("::: cyclopts") |
| 643 |
|
| 644 |
def test_pattern_matches_multiline_directive(self): |
| 645 |
"""Test that the pattern matches directives with multiple options.""" |
| 646 |
from cyclopts.ext.mkdocs import DIRECTIVE_PATTERN |
| 647 |
|
| 648 |
text = textwrap.dedent( |
| 649 |
"""\ |
| 650 |
Some text before. |
| 651 |
|
| 652 |
::: cyclopts |
| 653 |
module: myapp.cli:app |
| 654 |
heading_level: 2 |
| 655 |
recursive: true |
| 656 |
commands: cmd1, cmd2 |
| 657 |
|
| 658 |
Some text after. |
| 659 |
""" |
| 660 |
) |
| 661 |
|
| 662 |
match = DIRECTIVE_PATTERN.search(text) |
| 663 |
assert match is not None |
| 664 |
matched_text = match.group(0) |
| 665 |
assert "module:" in matched_text |
| 666 |
assert "heading_level:" in matched_text |
| 667 |
assert "recursive:" in matched_text |
| 668 |
assert "commands:" in matched_text |
| 669 |
|
| 670 |
def test_pattern_finds_all_directives(self): |
| 671 |
"""Test that the pattern finds all directives in text.""" |
| 672 |
from cyclopts.ext.mkdocs import DIRECTIVE_PATTERN |
| 673 |
|
| 674 |
text = textwrap.dedent( |
| 675 |
"""\ |
| 676 |
# First |
| 677 |
|
| 678 |
::: cyclopts |
| 679 |
module: app1 |
| 680 |
|
| 681 |
# Second |
| 682 |
|
| 683 |
::: cyclopts |
| 684 |
module: app2 |
| 685 |
""" |
| 686 |
) |
| 687 |
|
| 688 |
matches = list(DIRECTIVE_PATTERN.finditer(text)) |
| 689 |
assert len(matches) == 2 |
| 690 |
|
| 691 |
|
| 692 |
class TestPluginIntegration: |
| 693 |
"""Test the MkDocs plugin integration.""" |
| 694 |
|
| 695 |
@pytest.fixture |
| 696 |
def plugin(self): |
| 697 |
"""Create a plugin instance.""" |
| 698 |
pytest.importorskip("mkdocs") |
| 699 |
from cyclopts.ext.mkdocs import CycloptsPlugin |
| 700 |
|
| 701 |
return CycloptsPlugin() |
| 702 |
|
| 703 |
def test_plugin_has_config(self, plugin): |
| 704 |
"""Test that the plugin has configuration.""" |
| 705 |
assert hasattr(plugin, "config") |
| 706 |
|
| 707 |
def test_plugin_on_page_markdown_with_directive(self, plugin, importable_tmp_path): |
| 708 |
"""Test the on_page_markdown event with a cyclopts directive.""" |
| 709 |
module_file = importable_tmp_path / "plugin_test_app.py" |
| 710 |
module_file.write_text( |
| 711 |
textwrap.dedent( |
| 712 |
"""\ |
| 713 |
from cyclopts import App |
| 714 |
|
| 715 |
app = App(name="test", help="Test app") |
| 716 |
|
| 717 |
@app.default |
| 718 |
def main(arg: str): |
| 719 |
'''Main function.''' |
| 720 |
pass |
| 721 |
""" |
| 722 |
) |
| 723 |
) |
| 724 |
|
| 725 |
markdown = textwrap.dedent( |
| 726 |
"""\ |
| 727 |
# Documentation |
| 728 |
|
| 729 |
::: cyclopts |
| 730 |
module: plugin_test_app:app |
| 731 |
heading_level: 2 |
| 732 |
generate_toc: false |
| 733 |
""" |
| 734 |
) |
| 735 |
|
| 736 |
|
| 737 |
result = plugin.on_page_markdown(markdown, page=None, config=None, files=None) |
| 738 |
|
| 739 |
assert "::: cyclopts" not in result |
| 740 |
assert "test" in result.lower() |
| 741 |
|
| 742 |
def test_plugin_on_page_markdown_without_directive(self, plugin): |
| 743 |
"""Test that pages without directives are unchanged.""" |
| 744 |
markdown = "# Just a regular page\n\nNo directives here." |
| 745 |
result = plugin.on_page_markdown(markdown, page=None, config=None, files=None) |
| 746 |
assert result == markdown |
| 747 |
|
| 748 |
|
| 749 |
@pytest.mark.skipif(not pytest.importorskip("mkdocs", reason="mkdocs not installed"), reason="mkdocs not installed") |
| 750 |
class TestMkDocsAvailable: |
| 751 |
"""Tests that require mkdocs to be installed.""" |
| 752 |
|
| 753 |
def test_plugin_class_exists(self): |
| 754 |
"""Test that the plugin class can be imported.""" |
| 755 |
from cyclopts.ext.mkdocs import CycloptsPlugin |
| 756 |
|
| 757 |
assert CycloptsPlugin is not None |
| 758 |
|
| 759 |
def test_plugin_config_class_exists(self): |
| 760 |
"""Test that the config class can be imported.""" |
| 761 |
from cyclopts.ext.mkdocs import CycloptsPluginConfig |
| 762 |
|
| 763 |
assert CycloptsPluginConfig is not None |
| 764 |
|
| 765 |
|
| 766 |
class TestCommandFiltering: |
| 767 |
"""Test command filtering functionality.""" |
| 768 |
|
| 769 |
def test_filter_specific_commands(self, importable_tmp_path): |
| 770 |
"""Test filtering to include only specific commands.""" |
| 771 |
module_file = importable_tmp_path / "multi_cmd_app.py" |
| 772 |
module_file.write_text( |
| 773 |
textwrap.dedent( |
| 774 |
"""\ |
| 775 |
from cyclopts import App |
| 776 |
|
| 777 |
app = App(name="multi-app", help="Multi-command app") |
| 778 |
|
| 779 |
@app.command |
| 780 |
def init(): |
| 781 |
'''Initialize project.''' |
| 782 |
pass |
| 783 |
|
| 784 |
@app.command |
| 785 |
def build(): |
| 786 |
'''Build project.''' |
| 787 |
pass |
| 788 |
|
| 789 |
@app.command |
| 790 |
def deploy(): |
| 791 |
'''Deploy project.''' |
| 792 |
pass |
| 793 |
""" |
| 794 |
) |
| 795 |
) |
| 796 |
|
| 797 |
from cyclopts.ext.mkdocs import process_cyclopts_directives |
| 798 |
|
| 799 |
markdown = textwrap.dedent( |
| 800 |
"""\ |
| 801 |
# CLI Reference |
| 802 |
|
| 803 |
::: cyclopts |
| 804 |
module: multi_cmd_app:app |
| 805 |
commands: [init, build] |
| 806 |
""" |
| 807 |
) |
| 808 |
|
| 809 |
result = process_cyclopts_directives(markdown, None) |
| 810 |
|
| 811 |
|
| 812 |
assert "init" in result.lower() |
| 813 |
assert "build" in result.lower() |
| 814 |
|
| 815 |
assert "deploy" not in result.lower() |
| 816 |
|
| 817 |
def test_exclude_specific_commands(self, importable_tmp_path): |
| 818 |
"""Test excluding specific commands.""" |
| 819 |
module_file = importable_tmp_path / "exclude_cmd_app.py" |
| 820 |
module_file.write_text( |
| 821 |
textwrap.dedent( |
| 822 |
"""\ |
| 823 |
from cyclopts import App |
| 824 |
|
| 825 |
app = App(name="exclude-app", help="App with exclusions") |
| 826 |
|
| 827 |
@app.command |
| 828 |
def public_cmd(): |
| 829 |
'''Public command.''' |
| 830 |
pass |
| 831 |
|
| 832 |
@app.command |
| 833 |
def debug(): |
| 834 |
'''Debug command.''' |
| 835 |
pass |
| 836 |
|
| 837 |
@app.command |
| 838 |
def internal(): |
| 839 |
'''Internal command.''' |
| 840 |
pass |
| 841 |
""" |
| 842 |
) |
| 843 |
) |
| 844 |
|
| 845 |
from cyclopts.ext.mkdocs import process_cyclopts_directives |
| 846 |
|
| 847 |
markdown = textwrap.dedent( |
| 848 |
"""\ |
| 849 |
# CLI Reference |
| 850 |
|
| 851 |
::: cyclopts |
| 852 |
module: exclude_cmd_app:app |
| 853 |
exclude_commands: [debug, internal] |
| 854 |
""" |
| 855 |
) |
| 856 |
|
| 857 |
result = process_cyclopts_directives(markdown, None) |
| 858 |
|
| 859 |
|
| 860 |
assert "public" in result.lower() |
| 861 |
|
| 862 |
assert "debug" not in result.lower() |
| 863 |
assert "internal" not in result.lower() |
| 864 |
|
| 865 |
|
| 866 |
class TestCodeBlockDetection: |
| 867 |
"""Test code block detection to avoid processing directives in code.""" |
| 868 |
|
| 869 |
def test_directive_in_fenced_code_block_ignored(self, importable_tmp_path): |
| 870 |
"""Test that directives in fenced code blocks are not processed.""" |
| 871 |
module_file = importable_tmp_path / "fence_test_app.py" |
| 872 |
module_file.write_text( |
| 873 |
textwrap.dedent( |
| 874 |
"""\ |
| 875 |
from cyclopts import App |
| 876 |
|
| 877 |
app = App(name="fence-test", help="Test app") |
| 878 |
|
| 879 |
@app.default |
| 880 |
def main(): |
| 881 |
pass |
| 882 |
""" |
| 883 |
) |
| 884 |
) |
| 885 |
|
| 886 |
from cyclopts.ext.mkdocs import process_cyclopts_directives |
| 887 |
|
| 888 |
markdown = textwrap.dedent( |
| 889 |
"""\ |
| 890 |
# Documentation |
| 891 |
|
| 892 |
Here's an example directive: |
| 893 |
|
| 894 |
```markdown |
| 895 |
::: cyclopts |
| 896 |
module: fence_test_app:app |
| 897 |
``` |
| 898 |
|
| 899 |
The above should not be processed. |
| 900 |
""" |
| 901 |
) |
| 902 |
|
| 903 |
result = process_cyclopts_directives(markdown, None) |
| 904 |
|
| 905 |
|
| 906 |
assert "::: cyclopts" in result |
| 907 |
assert "fence-test" not in result.lower() |
| 908 |
|
| 909 |
def test_directive_in_indented_code_block_ignored(self, importable_tmp_path): |
| 910 |
"""Test that directives in indented code blocks are not processed.""" |
| 911 |
module_file = importable_tmp_path / "indent_test_app.py" |
| 912 |
module_file.write_text( |
| 913 |
textwrap.dedent( |
| 914 |
"""\ |
| 915 |
from cyclopts import App |
| 916 |
|
| 917 |
app = App(name="indent-test", help="Test app") |
| 918 |
|
| 919 |
@app.default |
| 920 |
def main(): |
| 921 |
pass |
| 922 |
""" |
| 923 |
) |
| 924 |
) |
| 925 |
|
| 926 |
from cyclopts.ext.mkdocs import process_cyclopts_directives |
| 927 |
|
| 928 |
markdown = textwrap.dedent( |
| 929 |
"""\ |
| 930 |
# Documentation |
| 931 |
|
| 932 |
Here's an example directive: |
| 933 |
|
| 934 |
::: cyclopts |
| 935 |
module: indent_test_app:app |
| 936 |
|
| 937 |
The above should not be processed. |
| 938 |
""" |
| 939 |
) |
| 940 |
|
| 941 |
result = process_cyclopts_directives(markdown, None) |
| 942 |
|
| 943 |
|
| 944 |
assert "::: cyclopts" in result |
| 945 |
assert "indent-test" not in result.lower() |
| 946 |
|
| 947 |
|
| 948 |
class TestDirectiveEOFEdgeCases: |
| 949 |
"""Test directive pattern edge cases at end of file.""" |
| 950 |
|
| 951 |
def test_directive_at_eof_without_trailing_newline(self): |
| 952 |
"""Test directive at EOF without trailing newline.""" |
| 953 |
from cyclopts.ext.mkdocs import DIRECTIVE_PATTERN |
| 954 |
|
| 955 |
|
| 956 |
markdown = "::: cyclopts\n module: eof_test_app:app" |
| 957 |
|
| 958 |
matches = list(DIRECTIVE_PATTERN.finditer(markdown)) |
| 959 |
assert len(matches) == 1 |
| 960 |
assert matches[0].group(0) == markdown |
| 961 |
|
| 962 |
def test_directive_with_trailing_whitespace_at_eof(self): |
| 963 |
"""Test directive with trailing whitespace at EOF.""" |
| 964 |
from cyclopts.ext.mkdocs import DIRECTIVE_PATTERN |
| 965 |
|
| 966 |
|
| 967 |
markdown = "::: cyclopts\n module: eof_ws_test_app:app " |
| 968 |
|
| 969 |
matches = list(DIRECTIVE_PATTERN.finditer(markdown)) |
| 970 |
assert len(matches) == 1 |