| 1 |
"""Tests for the Cyclopts Sphinx extension.""" |
| 2 |
|
| 3 |
import sys |
| 4 |
from unittest.mock import ANY, MagicMock |
| 5 |
|
| 6 |
import pytest |
| 7 |
from docutils.statemachine import StringList |
| 8 |
|
| 9 |
from cyclopts import App |
| 10 |
from cyclopts.utils import import_app |
| 11 |
|
| 12 |
|
| 13 |
class TestImportApp: |
| 14 |
"""Test the import_app function.""" |
| 15 |
|
| 16 |
def test_import_with_colon_notation(self, tmp_path): |
| 17 |
"""Test importing an app using module:app notation.""" |
| 18 |
|
| 19 |
module_file = tmp_path / "test_module.py" |
| 20 |
module_file.write_text(""" |
| 21 |
from cyclopts import App |
| 22 |
|
| 23 |
my_app = App(name="test-app", help="Test application") |
| 24 |
|
| 25 |
@my_app.command |
| 26 |
def hello(): |
| 27 |
'''Say hello.''' |
| 28 |
pass |
| 29 |
""") |
| 30 |
|
| 31 |
|
| 32 |
sys.path.insert(0, str(tmp_path)) |
| 33 |
try: |
| 34 |
app = import_app("test_module:my_app") |
| 35 |
assert isinstance(app, App) |
| 36 |
assert app.name == ("test-app",) |
| 37 |
finally: |
| 38 |
sys.path.remove(str(tmp_path)) |
| 39 |
|
| 40 |
def test_import_without_colon_finds_app(self, tmp_path): |
| 41 |
"""Test importing when app name is not specified.""" |
| 42 |
module_file = tmp_path / "test_cli.py" |
| 43 |
module_file.write_text(""" |
| 44 |
from cyclopts import App |
| 45 |
|
| 46 |
app = App(name="auto-found", help="Automatically found app") |
| 47 |
""") |
| 48 |
|
| 49 |
sys.path.insert(0, str(tmp_path)) |
| 50 |
try: |
| 51 |
found_app = import_app("test_cli") |
| 52 |
assert isinstance(found_app, App) |
| 53 |
assert found_app.name == ("auto-found",) |
| 54 |
finally: |
| 55 |
sys.path.remove(str(tmp_path)) |
| 56 |
|
| 57 |
def test_import_module_not_found(self): |
| 58 |
"""Test error when module doesn't exist.""" |
| 59 |
with pytest.raises(ImportError, match="Cannot import module"): |
| 60 |
import_app("nonexistent_module:app") |
| 61 |
|
| 62 |
def test_import_app_not_found(self, tmp_path): |
| 63 |
"""Test error when specified app doesn't exist in module.""" |
| 64 |
module_file = tmp_path / "test_module.py" |
| 65 |
module_file.write_text(""" |
| 66 |
from cyclopts import App |
| 67 |
|
| 68 |
some_app = App(name="test", help="Test") |
| 69 |
""") |
| 70 |
|
| 71 |
sys.path.insert(0, str(tmp_path)) |
| 72 |
try: |
| 73 |
with pytest.raises(AttributeError, match="has no attribute 'missing_app'"): |
| 74 |
import_app("test_module:missing_app") |
| 75 |
finally: |
| 76 |
sys.path.remove(str(tmp_path)) |
| 77 |
|
| 78 |
def test_import_not_an_app(self, tmp_path): |
| 79 |
"""Test error when object is not a Cyclopts App.""" |
| 80 |
|
| 81 |
module_name = "test_not_app_module" |
| 82 |
module_file = tmp_path / f"{module_name}.py" |
| 83 |
module_file.write_text("not_an_app = 'This is just a string'") |
| 84 |
|
| 85 |
sys.path.insert(0, str(tmp_path)) |
| 86 |
try: |
| 87 |
with pytest.raises(TypeError, match="is not a Cyclopts App instance"): |
| 88 |
import_app(f"{module_name}:not_an_app") |
| 89 |
finally: |
| 90 |
|
| 91 |
sys.path.remove(str(tmp_path)) |
| 92 |
|
| 93 |
if module_name in sys.modules: |
| 94 |
del sys.modules[module_name] |
| 95 |
|
| 96 |
|
| 97 |
class TestCycloptsDirective: |
| 98 |
"""Test the CycloptsDirective class.""" |
| 99 |
|
| 100 |
@pytest.fixture |
| 101 |
def mock_directive(self): |
| 102 |
"""Create a mock directive with necessary attributes.""" |
| 103 |
from cyclopts.ext.sphinx import CycloptsDirective |
| 104 |
|
| 105 |
|
| 106 |
directive = MagicMock(spec=CycloptsDirective) |
| 107 |
directive.arguments = ["test_module:app"] |
| 108 |
directive.options = {} |
| 109 |
directive.content_offset = 0 |
| 110 |
|
| 111 |
|
| 112 |
directive.state = MagicMock() |
| 113 |
directive.state.nested_parse = MagicMock() |
| 114 |
|
| 115 |
return directive |
| 116 |
|
| 117 |
def test_directive_run_success(self, mock_directive, tmp_path): |
| 118 |
"""Test successful directive execution.""" |
| 119 |
|
| 120 |
module_file = tmp_path / "test_directive_module.py" |
| 121 |
module_file.write_text("""from cyclopts import App |
| 122 |
|
| 123 |
app = App(name="test-cli", help="Test CLI") |
| 124 |
|
| 125 |
@app.command |
| 126 |
def cmd(): |
| 127 |
'''A command.''' |
| 128 |
pass |
| 129 |
""") |
| 130 |
|
| 131 |
sys.path.insert(0, str(tmp_path)) |
| 132 |
try: |
| 133 |
from cyclopts.ext.sphinx import CycloptsDirective |
| 134 |
|
| 135 |
|
| 136 |
directive = CycloptsDirective( |
| 137 |
name="cyclopts", |
| 138 |
arguments=["test_directive_module:app"], |
| 139 |
options={}, |
| 140 |
content=StringList(), |
| 141 |
lineno=1, |
| 142 |
content_offset=0, |
| 143 |
block_text="", |
| 144 |
state=mock_directive.state, |
| 145 |
state_machine=MagicMock(), |
| 146 |
) |
| 147 |
|
| 148 |
|
| 149 |
result = directive.run() |
| 150 |
|
| 151 |
|
| 152 |
assert len(result) >= 1 |
| 153 |
|
| 154 |
mock_directive.state.nested_parse.assert_called() |
| 155 |
|
| 156 |
finally: |
| 157 |
sys.path.remove(str(tmp_path)) |
| 158 |
|
| 159 |
def test_directive_with_options(self, mock_directive, tmp_path): |
| 160 |
"""Test directive with various options.""" |
| 161 |
module_file = tmp_path / "test_options_module.py" |
| 162 |
module_file.write_text("""from cyclopts import App |
| 163 |
|
| 164 |
app = App(name="test-cli", help="Test CLI") |
| 165 |
|
| 166 |
@app.command |
| 167 |
def visible(): |
| 168 |
'''Visible command.''' |
| 169 |
pass |
| 170 |
|
| 171 |
@app.command(show=False) |
| 172 |
def hidden(): |
| 173 |
'''Hidden command.''' |
| 174 |
pass |
| 175 |
""") |
| 176 |
|
| 177 |
sys.path.insert(0, str(tmp_path)) |
| 178 |
try: |
| 179 |
from cyclopts.ext.sphinx import CycloptsDirective |
| 180 |
|
| 181 |
|
| 182 |
directive = CycloptsDirective( |
| 183 |
name="cyclopts", |
| 184 |
arguments=["test_options_module:app"], |
| 185 |
options={"heading-level": 2, "recursive": True, "include-hidden": True}, |
| 186 |
content=StringList(), |
| 187 |
lineno=1, |
| 188 |
content_offset=0, |
| 189 |
block_text="", |
| 190 |
state=mock_directive.state, |
| 191 |
state_machine=MagicMock(), |
| 192 |
) |
| 193 |
|
| 194 |
result = directive.run() |
| 195 |
assert len(result) >= 1 |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
all_calls = mock_directive.state.nested_parse.call_args_list |
| 200 |
all_content = [] |
| 201 |
for call_args in all_calls: |
| 202 |
rst_lines = call_args[0][0] |
| 203 |
all_content.extend(rst_lines) |
| 204 |
|
| 205 |
|
| 206 |
rst_content = "\n".join(all_content) |
| 207 |
|
| 208 |
|
| 209 |
assert ( |
| 210 |
"hidden" in rst_content.lower() or len(result) >= 2 |
| 211 |
) |
| 212 |
|
| 213 |
finally: |
| 214 |
sys.path.remove(str(tmp_path)) |
| 215 |
|
| 216 |
def test_directive_import_error(self, mock_directive): |
| 217 |
"""Test directive handles import errors gracefully.""" |
| 218 |
from cyclopts.ext.sphinx import CycloptsDirective |
| 219 |
|
| 220 |
directive = CycloptsDirective( |
| 221 |
name="cyclopts", |
| 222 |
arguments=["nonexistent_module:app"], |
| 223 |
options={}, |
| 224 |
content=StringList(), |
| 225 |
lineno=1, |
| 226 |
content_offset=0, |
| 227 |
block_text="", |
| 228 |
state=MagicMock(), |
| 229 |
state_machine=MagicMock(), |
| 230 |
) |
| 231 |
|
| 232 |
result = directive.run() |
| 233 |
|
| 234 |
|
| 235 |
assert len(result) == 1 |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
class TestSphinxSetup: |
| 240 |
"""Test the setup function for Sphinx integration.""" |
| 241 |
|
| 242 |
def test_setup_function(self): |
| 243 |
"""Test the setup function registers the directive.""" |
| 244 |
from cyclopts import __version__ |
| 245 |
from cyclopts.ext.sphinx import setup |
| 246 |
|
| 247 |
|
| 248 |
mock_app = MagicMock() |
| 249 |
|
| 250 |
|
| 251 |
result = setup(mock_app) |
| 252 |
|
| 253 |
|
| 254 |
mock_app.add_directive.assert_called_once_with("cyclopts", ANY) |
| 255 |
|
| 256 |
|
| 257 |
assert result["version"] == __version__ |
| 258 |
assert result["parallel_read_safe"] is True |
| 259 |
assert result["parallel_write_safe"] is True |
| 260 |
|
| 261 |
|
| 262 |
class TestNewDirectiveOptions: |
| 263 |
"""Test the new directive options for distinct headings.""" |
| 264 |
|
| 265 |
def test_flatten_commands_option(self, tmp_path): |
| 266 |
"""Test flatten-commands option generates consistent heading levels.""" |
| 267 |
module_file = tmp_path / "test_flatten_module.py" |
| 268 |
module_file.write_text("""from cyclopts import App |
| 269 |
|
| 270 |
app = App(name="test-cli", help="Test CLI") |
| 271 |
|
| 272 |
sub1 = App(name="sub1", help="First subcommand") |
| 273 |
sub2 = App(name="sub2", help="Second subcommand") |
| 274 |
|
| 275 |
@sub1.command |
| 276 |
def action(): |
| 277 |
'''Sub1 action command.''' |
| 278 |
pass |
| 279 |
|
| 280 |
@sub2.command |
| 281 |
def process(): |
| 282 |
'''Sub2 process command.''' |
| 283 |
pass |
| 284 |
|
| 285 |
app.command(sub1) |
| 286 |
app.command(sub2) |
| 287 |
""") |
| 288 |
|
| 289 |
sys.path.insert(0, str(tmp_path)) |
| 290 |
try: |
| 291 |
from cyclopts.ext.sphinx import CycloptsDirective |
| 292 |
|
| 293 |
|
| 294 |
mock_state = MagicMock() |
| 295 |
mock_state.nested_parse = MagicMock() |
| 296 |
|
| 297 |
|
| 298 |
directive = CycloptsDirective( |
| 299 |
name="cyclopts", |
| 300 |
arguments=["test_flatten_module:app"], |
| 301 |
options={"flatten-commands": True, "recursive": True}, |
| 302 |
content=StringList(), |
| 303 |
lineno=1, |
| 304 |
content_offset=0, |
| 305 |
block_text="", |
| 306 |
state=mock_state, |
| 307 |
state_machine=MagicMock(), |
| 308 |
) |
| 309 |
|
| 310 |
result = directive.run() |
| 311 |
assert len(result) >= 1 |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
all_calls = mock_state.nested_parse.call_args_list |
| 316 |
all_content = [] |
| 317 |
for call_args in all_calls: |
| 318 |
if call_args: |
| 319 |
rst_lines = call_args[0][0] |
| 320 |
all_content.extend(rst_lines) |
| 321 |
|
| 322 |
|
| 323 |
rst_content = "\n".join(all_content) |
| 324 |
|
| 325 |
|
| 326 |
assert "sub1" in rst_content |
| 327 |
assert "sub2" in rst_content |
| 328 |
assert "action" in rst_content |
| 329 |
assert "process" in rst_content |
| 330 |
|
| 331 |
finally: |
| 332 |
sys.path.remove(str(tmp_path)) |
| 333 |
|
| 334 |
def test_automatic_anchors(self, tmp_path): |
| 335 |
"""Test that RST reference labels are automatically generated.""" |
| 336 |
module_file = tmp_path / "test_anchors_module.py" |
| 337 |
module_file.write_text("""from cyclopts import App |
| 338 |
|
| 339 |
app = App(name="test-cli", help="Test CLI") |
| 340 |
|
| 341 |
sub_cmd = App(name="sub", help="Subcommand") |
| 342 |
app.command(sub_cmd) |
| 343 |
|
| 344 |
@sub_cmd.command |
| 345 |
def action(): |
| 346 |
'''Perform action.''' |
| 347 |
pass |
| 348 |
""") |
| 349 |
|
| 350 |
sys.path.insert(0, str(tmp_path)) |
| 351 |
try: |
| 352 |
from cyclopts.ext.sphinx import CycloptsDirective |
| 353 |
|
| 354 |
mock_state = MagicMock() |
| 355 |
mock_state.nested_parse = MagicMock() |
| 356 |
|
| 357 |
directive = CycloptsDirective( |
| 358 |
name="cyclopts", |
| 359 |
arguments=["test_anchors_module:app"], |
| 360 |
options={"recursive": True}, |
| 361 |
content=StringList(), |
| 362 |
lineno=1, |
| 363 |
content_offset=0, |
| 364 |
block_text="", |
| 365 |
state=mock_state, |
| 366 |
state_machine=MagicMock(), |
| 367 |
) |
| 368 |
|
| 369 |
result = directive.run() |
| 370 |
assert len(result) >= 1 |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
all_calls = mock_state.nested_parse.call_args_list |
| 375 |
all_content = [] |
| 376 |
for call_args in all_calls: |
| 377 |
if call_args: |
| 378 |
rst_lines = call_args[0][0] |
| 379 |
all_content.extend(rst_lines) |
| 380 |
|
| 381 |
rst_content = "\n".join(all_content) |
| 382 |
|
| 383 |
|
| 384 |
assert ".. _cyclopts-test-cli:" in rst_content |
| 385 |
|
| 386 |
assert ".. _cyclopts-test-cli-sub:" in rst_content |
| 387 |
assert ".. _cyclopts-test-cli-sub-action:" in rst_content |
| 388 |
|
| 389 |
finally: |
| 390 |
sys.path.remove(str(tmp_path)) |
| 391 |
|
| 392 |
def test_commands_filter_option(self, tmp_path): |
| 393 |
"""Test :commands: option to filter specific commands.""" |
| 394 |
module_file = tmp_path / "test_commands_filter.py" |
| 395 |
module_file.write_text("""from cyclopts import App |
| 396 |
|
| 397 |
app = App(name="test-cli", help="Test CLI") |
| 398 |
|
| 399 |
@app.command |
| 400 |
def include_me(): |
| 401 |
'''This command should be included.''' |
| 402 |
pass |
| 403 |
|
| 404 |
@app.command |
| 405 |
def exclude_me(): |
| 406 |
'''This command should be excluded.''' |
| 407 |
pass |
| 408 |
|
| 409 |
@app.command |
| 410 |
def also_include(): |
| 411 |
'''This command should also be included.''' |
| 412 |
pass |
| 413 |
""") |
| 414 |
|
| 415 |
sys.path.insert(0, str(tmp_path)) |
| 416 |
try: |
| 417 |
from cyclopts.ext.sphinx import CycloptsDirective |
| 418 |
|
| 419 |
mock_state = MagicMock() |
| 420 |
mock_state.nested_parse = MagicMock() |
| 421 |
|
| 422 |
|
| 423 |
directive = CycloptsDirective( |
| 424 |
name="cyclopts", |
| 425 |
arguments=["test_commands_filter:app"], |
| 426 |
options={"commands": "include_me, also_include"}, |
| 427 |
content=StringList(), |
| 428 |
lineno=1, |
| 429 |
content_offset=0, |
| 430 |
block_text="", |
| 431 |
state=mock_state, |
| 432 |
state_machine=MagicMock(), |
| 433 |
) |
| 434 |
|
| 435 |
result = directive.run() |
| 436 |
assert len(result) >= 1 |
| 437 |
|
| 438 |
|
| 439 |
all_calls = mock_state.nested_parse.call_args_list |
| 440 |
all_content = [] |
| 441 |
for call_args in all_calls: |
| 442 |
if call_args: |
| 443 |
rst_lines = call_args[0][0] |
| 444 |
all_content.extend(rst_lines) |
| 445 |
|
| 446 |
rst_content = "\n".join(all_content) |
| 447 |
|
| 448 |
|
| 449 |
assert "include_me" in rst_content or "include-me" in rst_content |
| 450 |
assert "also_include" in rst_content or "also-include" in rst_content |
| 451 |
|
| 452 |
assert "exclude_me" not in rst_content and "exclude-me" not in rst_content |
| 453 |
|
| 454 |
finally: |
| 455 |
sys.path.remove(str(tmp_path)) |
| 456 |
|
| 457 |
def test_exclude_commands_option(self, tmp_path): |
| 458 |
"""Test :exclude-commands: option to exclude specific commands.""" |
| 459 |
module_file = tmp_path / "test_exclude_commands.py" |
| 460 |
module_file.write_text("""from cyclopts import App |
| 461 |
|
| 462 |
app = App(name="test-cli", help="Test CLI") |
| 463 |
|
| 464 |
@app.command |
| 465 |
def keep_me(): |
| 466 |
'''This command should be kept.''' |
| 467 |
pass |
| 468 |
|
| 469 |
@app.command |
| 470 |
def exclude_me(): |
| 471 |
'''This command should be excluded.''' |
| 472 |
pass |
| 473 |
|
| 474 |
@app.command |
| 475 |
def also_keep(): |
| 476 |
'''This command should also be kept.''' |
| 477 |
pass |
| 478 |
""") |
| 479 |
|
| 480 |
sys.path.insert(0, str(tmp_path)) |
| 481 |
try: |
| 482 |
from cyclopts.ext.sphinx import CycloptsDirective |
| 483 |
|
| 484 |
mock_state = MagicMock() |
| 485 |
mock_state.nested_parse = MagicMock() |
| 486 |
|
| 487 |
|
| 488 |
directive = CycloptsDirective( |
| 489 |
name="cyclopts", |
| 490 |
arguments=["test_exclude_commands:app"], |
| 491 |
options={"exclude-commands": "exclude_me"}, |
| 492 |
content=StringList(), |
| 493 |
lineno=1, |
| 494 |
content_offset=0, |
| 495 |
block_text="", |
| 496 |
state=mock_state, |
| 497 |
state_machine=MagicMock(), |
| 498 |
) |
| 499 |
|
| 500 |
result = directive.run() |
| 501 |
assert len(result) >= 1 |
| 502 |
|
| 503 |
|
| 504 |
all_calls = mock_state.nested_parse.call_args_list |
| 505 |
all_content = [] |
| 506 |
for call_args in all_calls: |
| 507 |
if call_args: |
| 508 |
rst_lines = call_args[0][0] |
| 509 |
all_content.extend(rst_lines) |
| 510 |
|
| 511 |
rst_content = "\n".join(all_content) |
| 512 |
|
| 513 |
|
| 514 |
assert "keep_me" in rst_content or "keep-me" in rst_content |
| 515 |
assert "also_keep" in rst_content or "also-keep" in rst_content |
| 516 |
|
| 517 |
assert "exclude_me" not in rst_content and "exclude-me" not in rst_content |
| 518 |
|
| 519 |
finally: |
| 520 |
sys.path.remove(str(tmp_path)) |
| 521 |
|
| 522 |
def test_nested_command_filtering(self, tmp_path): |
| 523 |
"""Test filtering nested commands with dot notation.""" |
| 524 |
module_file = tmp_path / "test_nested_filter.py" |
| 525 |
module_file.write_text("""from cyclopts import App |
| 526 |
|
| 527 |
app = App(name="test-cli", help="Test CLI") |
| 528 |
|
| 529 |
# Database commands |
| 530 |
db_app = App(name="db", help="Database commands") |
| 531 |
|
| 532 |
@db_app.command |
| 533 |
def migrate(): |
| 534 |
'''Run migrations.''' |
| 535 |
pass |
| 536 |
|
| 537 |
@db_app.command |
| 538 |
def backup(): |
| 539 |
'''Backup database.''' |
| 540 |
pass |
| 541 |
|
| 542 |
# API commands |
| 543 |
api_app = App(name="api", help="API commands") |
| 544 |
|
| 545 |
@api_app.command |
| 546 |
def start(): |
| 547 |
'''Start API server.''' |
| 548 |
pass |
| 549 |
|
| 550 |
@api_app.command |
| 551 |
def stop(): |
| 552 |
'''Stop API server.''' |
| 553 |
pass |
| 554 |
|
| 555 |
app.command(db_app) |
| 556 |
app.command(api_app) |
| 557 |
""") |
| 558 |
|
| 559 |
sys.path.insert(0, str(tmp_path)) |
| 560 |
try: |
| 561 |
from cyclopts.ext.sphinx import CycloptsDirective |
| 562 |
|
| 563 |
mock_state = MagicMock() |
| 564 |
mock_state.nested_parse = MagicMock() |
| 565 |
|
| 566 |
|
| 567 |
directive = CycloptsDirective( |
| 568 |
name="cyclopts", |
| 569 |
arguments=["test_nested_filter:app"], |
| 570 |
options={"commands": "db.migrate, api", "recursive": True}, |
| 571 |
content=StringList(), |
| 572 |
lineno=1, |
| 573 |
content_offset=0, |
| 574 |
block_text="", |
| 575 |
state=mock_state, |
| 576 |
state_machine=MagicMock(), |
| 577 |
) |
| 578 |
|
| 579 |
result = directive.run() |
| 580 |
assert len(result) >= 1 |
| 581 |
|
| 582 |
|
| 583 |
all_calls = mock_state.nested_parse.call_args_list |
| 584 |
all_content = [] |
| 585 |
for call_args in all_calls: |
| 586 |
if call_args: |
| 587 |
rst_lines = call_args[0][0] |
| 588 |
all_content.extend(rst_lines) |
| 589 |
|
| 590 |
rst_content = "\n".join(all_content) |
| 591 |
|
| 592 |
|
| 593 |
assert "migrate" in rst_content |
| 594 |
|
| 595 |
assert "api" in rst_content |
| 596 |
assert "start" in rst_content |
| 597 |
assert "stop" in rst_content |
| 598 |
|
| 599 |
|
| 600 |
lines = rst_content.split("\n") |
| 601 |
backup_as_command = any("backup" in line and ("``" in line or "::" in line) for line in lines) |
| 602 |
assert not backup_as_command |
| 603 |
|
| 604 |
finally: |
| 605 |
sys.path.remove(str(tmp_path)) |
| 606 |
|
| 607 |
def test_mixed_underscore_dash_names(self, tmp_path): |
| 608 |
"""Test that underscore and dash command names are handled correctly.""" |
| 609 |
module_file = tmp_path / "test_mixed_names.py" |
| 610 |
module_file.write_text("""from cyclopts import App |
| 611 |
|
| 612 |
app = App(name="test-cli", help="Test CLI") |
| 613 |
|
| 614 |
@app.command |
| 615 |
def my_command(): |
| 616 |
'''Command with underscore in function name.''' |
| 617 |
pass |
| 618 |
|
| 619 |
@app.command(name="another-command") |
| 620 |
def another_func(): |
| 621 |
'''Command with explicit dash name.''' |
| 622 |
pass |
| 623 |
|
| 624 |
@app.command |
| 625 |
def third_command(): |
| 626 |
'''Another underscore command.''' |
| 627 |
pass |
| 628 |
""") |
| 629 |
|
| 630 |
sys.path.insert(0, str(tmp_path)) |
| 631 |
try: |
| 632 |
from cyclopts.ext.sphinx import CycloptsDirective |
| 633 |
|
| 634 |
mock_state = MagicMock() |
| 635 |
mock_state.nested_parse = MagicMock() |
| 636 |
|
| 637 |
|
| 638 |
directive = CycloptsDirective( |
| 639 |
name="cyclopts", |
| 640 |
arguments=["test_mixed_names:app"], |
| 641 |
options={"commands": "my_command, another-command"}, |
| 642 |
content=StringList(), |
| 643 |
lineno=1, |
| 644 |
content_offset=0, |
| 645 |
block_text="", |
| 646 |
state=mock_state, |
| 647 |
state_machine=MagicMock(), |
| 648 |
) |
| 649 |
|
| 650 |
result = directive.run() |
| 651 |
assert len(result) >= 1 |
| 652 |
|
| 653 |
|
| 654 |
all_calls = mock_state.nested_parse.call_args_list |
| 655 |
all_content = [] |
| 656 |
for call_args in all_calls: |
| 657 |
if call_args: |
| 658 |
rst_lines = call_args[0][0] |
| 659 |
all_content.extend(rst_lines) |
| 660 |
|
| 661 |
rst_content = "\n".join(all_content) |
| 662 |
|
| 663 |
|
| 664 |
assert "my-command" in rst_content or "my_command" in rst_content |
| 665 |
assert "another-command" in rst_content |
| 666 |
|
| 667 |
assert "third-command" not in rst_content and "third_command" not in rst_content |
| 668 |
|
| 669 |
finally: |
| 670 |
sys.path.remove(str(tmp_path)) |
| 671 |
|
| 672 |
def test_deeply_nested_commands(self, tmp_path): |
| 673 |
"""Test filtering with deeply nested command hierarchies (3+ levels).""" |
| 674 |
module_file = tmp_path / "test_deep_nested.py" |
| 675 |
module_file.write_text("""from cyclopts import App |
| 676 |
|
| 677 |
app = App(name="test-cli", help="Test CLI") |
| 678 |
|
| 679 |
# Level 1: admin |
| 680 |
admin_app = App(name="admin", help="Admin commands") |
| 681 |
|
| 682 |
# Level 2: admin.users |
| 683 |
users_app = App(name="users", help="User management") |
| 684 |
|
| 685 |
@users_app.command |
| 686 |
def list(): |
| 687 |
'''List all users.''' |
| 688 |
pass |
| 689 |
|
| 690 |
@users_app.command |
| 691 |
def create(): |
| 692 |
'''Create a new user.''' |
| 693 |
pass |
| 694 |
|
| 695 |
# Level 3: admin.users.permissions |
| 696 |
permissions_app = App(name="permissions", help="Permission management") |
| 697 |
|
| 698 |
@permissions_app.command |
| 699 |
def grant(): |
| 700 |
'''Grant permissions.''' |
| 701 |
pass |
| 702 |
|
| 703 |
@permissions_app.command |
| 704 |
def revoke(): |
| 705 |
'''Revoke permissions.''' |
| 706 |
pass |
| 707 |
|
| 708 |
users_app.command(permissions_app) |
| 709 |
admin_app.command(users_app) |
| 710 |
|
| 711 |
# Level 2: admin.logs |
| 712 |
logs_app = App(name="logs", help="Log management") |
| 713 |
|
| 714 |
@logs_app.command |
| 715 |
def view(): |
| 716 |
'''View logs.''' |
| 717 |
pass |
| 718 |
|
| 719 |
@logs_app.command |
| 720 |
def clear(): |
| 721 |
'''Clear logs.''' |
| 722 |
pass |
| 723 |
|
| 724 |
admin_app.command(logs_app) |
| 725 |
app.command(admin_app) |
| 726 |
|
| 727 |
# Another top-level command |
| 728 |
@app.command |
| 729 |
def status(): |
| 730 |
'''Show status.''' |
| 731 |
pass |
| 732 |
""") |
| 733 |
|
| 734 |
sys.path.insert(0, str(tmp_path)) |
| 735 |
try: |
| 736 |
from cyclopts.ext.sphinx import CycloptsDirective |
| 737 |
|
| 738 |
mock_state = MagicMock() |
| 739 |
mock_state.nested_parse = MagicMock() |
| 740 |
|
| 741 |
|
| 742 |
directive = CycloptsDirective( |
| 743 |
name="cyclopts", |
| 744 |
arguments=["test_deep_nested:app"], |
| 745 |
options={"commands": "admin.users.permissions.grant, status", "recursive": True}, |
| 746 |
content=StringList(), |
| 747 |
lineno=1, |
| 748 |
content_offset=0, |
| 749 |
block_text="", |
| 750 |
state=mock_state, |
| 751 |
state_machine=MagicMock(), |
| 752 |
) |
| 753 |
|
| 754 |
result = directive.run() |
| 755 |
assert len(result) >= 1 |
| 756 |
|
| 757 |
|
| 758 |
all_calls = mock_state.nested_parse.call_args_list |
| 759 |
all_content = [] |
| 760 |
for call_args in all_calls: |
| 761 |
if call_args: |
| 762 |
rst_lines = call_args[0][0] |
| 763 |
all_content.extend(rst_lines) |
| 764 |
|
| 765 |
rst_content = "\n".join(all_content) |
| 766 |
|
| 767 |
|
| 768 |
assert "admin" in rst_content |
| 769 |
|
| 770 |
|
| 771 |
assert "users" in rst_content or "User management" in rst_content |
| 772 |
|
| 773 |
assert "permissions" in rst_content or "Permission management" in rst_content |
| 774 |
assert "grant" in rst_content or "Grant permissions" in rst_content |
| 775 |
|
| 776 |
assert "status" in rst_content |
| 777 |
|
| 778 |
assert "logs" not in rst_content |
| 779 |
assert "clear" not in rst_content |
| 780 |
|
| 781 |
lines = rst_content.split("\n") |
| 782 |
revoke_as_command = any( |
| 783 |
"revoke" in line and ("``" in line or "::" in line or "revoke" == line.strip()) for line in lines |
| 784 |
) |
| 785 |
assert not revoke_as_command |
| 786 |
|
| 787 |
finally: |
| 788 |
sys.path.remove(str(tmp_path)) |
| 789 |
|
| 790 |
def test_conflicting_filters(self, tmp_path): |
| 791 |
"""Test behavior when include and exclude filters conflict.""" |
| 792 |
module_file = tmp_path / "test_conflicts.py" |
| 793 |
module_file.write_text("""from cyclopts import App |
| 794 |
|
| 795 |
app = App(name="test-cli", help="Test CLI") |
| 796 |
|
| 797 |
@app.command |
| 798 |
def cmd1(): |
| 799 |
'''Command 1.''' |
| 800 |
pass |
| 801 |
|
| 802 |
@app.command |
| 803 |
def cmd2(): |
| 804 |
'''Command 2.''' |
| 805 |
pass |
| 806 |
|
| 807 |
@app.command |
| 808 |
def cmd3(): |
| 809 |
'''Command 3.''' |
| 810 |
pass |
| 811 |
""") |
| 812 |
|
| 813 |
sys.path.insert(0, str(tmp_path)) |
| 814 |
try: |
| 815 |
from cyclopts.ext.sphinx import CycloptsDirective |
| 816 |
|
| 817 |
mock_state = MagicMock() |
| 818 |
mock_state.nested_parse = MagicMock() |
| 819 |
|
| 820 |
|
| 821 |
directive = CycloptsDirective( |
| 822 |
name="cyclopts", |
| 823 |
arguments=["test_conflicts:app"], |
| 824 |
options={"commands": "cmd1, cmd2", "exclude-commands": "cmd2, cmd3"}, |
| 825 |
content=StringList(), |
| 826 |
lineno=1, |
| 827 |
content_offset=0, |
| 828 |
block_text="", |
| 829 |
state=mock_state, |
| 830 |
state_machine=MagicMock(), |
| 831 |
) |
| 832 |
|
| 833 |
result = directive.run() |
| 834 |
assert len(result) >= 1 |
| 835 |
|
| 836 |
|
| 837 |
all_calls = mock_state.nested_parse.call_args_list |
| 838 |
all_content = [] |
| 839 |
for call_args in all_calls: |
| 840 |
if call_args: |
| 841 |
rst_lines = call_args[0][0] |
| 842 |
all_content.extend(rst_lines) |
| 843 |
|
| 844 |
rst_content = "\n".join(all_content) |
| 845 |
|
| 846 |
|
| 847 |
assert "cmd1" in rst_content |
| 848 |
assert "cmd2" not in rst_content |
| 849 |
assert "cmd3" not in rst_content |
| 850 |
|
| 851 |
finally: |
| 852 |
sys.path.remove(str(tmp_path)) |
| 853 |
|
| 854 |
def test_empty_filter_lists(self, tmp_path): |
| 855 |
"""Test behavior with empty filter lists.""" |
| 856 |
module_file = tmp_path / "test_empty_filters.py" |
| 857 |
module_file.write_text("""from cyclopts import App |
| 858 |
|
| 859 |
app = App(name="test-cli", help="Test CLI") |
| 860 |
|
| 861 |
@app.command |
| 862 |
def cmd1(): |
| 863 |
'''Command 1.''' |
| 864 |
pass |
| 865 |
|
| 866 |
@app.command |
| 867 |
def cmd2(): |
| 868 |
'''Command 2.''' |
| 869 |
pass |
| 870 |
""") |
| 871 |
|
| 872 |
sys.path.insert(0, str(tmp_path)) |
| 873 |
try: |
| 874 |
from cyclopts.ext.sphinx import CycloptsDirective |
| 875 |
|
| 876 |
mock_state = MagicMock() |
| 877 |
mock_state.nested_parse = MagicMock() |
| 878 |
|
| 879 |
|
| 880 |
directive = CycloptsDirective( |
| 881 |
name="cyclopts", |
| 882 |
arguments=["test_empty_filters:app"], |
| 883 |
options={"commands": ""}, |
| 884 |
content=StringList(), |
| 885 |
lineno=1, |
| 886 |
content_offset=0, |
| 887 |
block_text="", |
| 888 |
state=mock_state, |
| 889 |
state_machine=MagicMock(), |
| 890 |
) |
| 891 |
|
| 892 |
directive.run() |
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
all_calls = mock_state.nested_parse.call_args_list |
| 898 |
all_content = [] |
| 899 |
for call_args in all_calls: |
| 900 |
if call_args: |
| 901 |
rst_lines = call_args[0][0] |
| 902 |
all_content.extend(rst_lines) |
| 903 |
|
| 904 |
if all_content: |
| 905 |
rst_content = "\n".join(all_content) |
| 906 |
|
| 907 |
assert "cmd1" not in rst_content |
| 908 |
assert "cmd2" not in rst_content |
| 909 |
else: |
| 910 |
|
| 911 |
pass |
| 912 |
|
| 913 |
finally: |
| 914 |
sys.path.remove(str(tmp_path)) |
| 915 |
|
| 916 |
def test_skip_preamble_option(self, tmp_path): |
| 917 |
"""Test :skip-preamble: option to skip description and usage.""" |
| 918 |
module_file = tmp_path / "test_skip_preamble.py" |
| 919 |
module_file.write_text("""from cyclopts import App |
| 920 |
|
| 921 |
app = App(name="test-cli", help="Main app description") |
| 922 |
|
| 923 |
sub_cmd = App(name="sub", help="This description should be skipped") |
| 924 |
|
| 925 |
@sub_cmd.default |
| 926 |
def sub_handler(arg: str): |
| 927 |
'''Sub command handler.''' |
| 928 |
pass |
| 929 |
|
| 930 |
app.command(sub_cmd) |
| 931 |
""") |
| 932 |
|
| 933 |
sys.path.insert(0, str(tmp_path)) |
| 934 |
try: |
| 935 |
from cyclopts.ext.sphinx import CycloptsDirective |
| 936 |
|
| 937 |
|
| 938 |
mock_state1 = MagicMock() |
| 939 |
mock_state1.nested_parse = MagicMock() |
| 940 |
|
| 941 |
directive1 = CycloptsDirective( |
| 942 |
name="cyclopts", |
| 943 |
arguments=["test_skip_preamble:app"], |
| 944 |
options={"commands": "sub"}, |
| 945 |
content=StringList(), |
| 946 |
lineno=1, |
| 947 |
content_offset=0, |
| 948 |
block_text="", |
| 949 |
state=mock_state1, |
| 950 |
state_machine=MagicMock(), |
| 951 |
) |
| 952 |
|
| 953 |
directive1.run() |
| 954 |
|
| 955 |
|
| 956 |
all_calls1 = mock_state1.nested_parse.call_args_list |
| 957 |
all_content1 = [] |
| 958 |
for call_args in all_calls1: |
| 959 |
if call_args: |
| 960 |
rst_lines = call_args[0][0] |
| 961 |
all_content1.extend(rst_lines) |
| 962 |
rst_content1 = "\n".join(all_content1) |
| 963 |
|
| 964 |
|
| 965 |
assert "This description should be skipped" in rst_content1 |
| 966 |
|
| 967 |
|
| 968 |
mock_state2 = MagicMock() |
| 969 |
mock_state2.nested_parse = MagicMock() |
| 970 |
|
| 971 |
directive2 = CycloptsDirective( |
| 972 |
name="cyclopts", |
| 973 |
arguments=["test_skip_preamble:app"], |
| 974 |
options={"commands": "sub", "skip-preamble": True}, |
| 975 |
content=StringList(), |
| 976 |
lineno=1, |
| 977 |
content_offset=0, |
| 978 |
block_text="", |
| 979 |
state=mock_state2, |
| 980 |
state_machine=MagicMock(), |
| 981 |
) |
| 982 |
|
| 983 |
directive2.run() |
| 984 |
|
| 985 |
|
| 986 |
all_calls2 = mock_state2.nested_parse.call_args_list |
| 987 |
all_content2 = [] |
| 988 |
for call_args in all_calls2: |
| 989 |
if call_args: |
| 990 |
rst_lines = call_args[0][0] |
| 991 |
all_content2.extend(rst_lines) |
| 992 |
rst_content2 = "\n".join(all_content2) |
| 993 |
|
| 994 |
|
| 995 |
assert "This description should be skipped" not in rst_content2 |
| 996 |
|
| 997 |
assert "arg" in rst_content2.lower() |
| 998 |
|
| 999 |
finally: |
| 1000 |
sys.path.remove(str(tmp_path)) |
| 1001 |
|
| 1002 |
def test_usage_name_option(self, tmp_path): |
| 1003 |
""":usage-name: overrides the app name in Usage: lines.""" |
| 1004 |
module_file = tmp_path / "test_usage_name_module.py" |
| 1005 |
module_file.write_text("""from cyclopts import App |
| 1006 |
|
| 1007 |
app = App(name="cli", help="Test CLI") |
| 1008 |
|
| 1009 |
@app.command |
| 1010 |
def serve(port: int = 8000): |
| 1011 |
'''Start the server.''' |
| 1012 |
pass |
| 1013 |
""") |
| 1014 |
|
| 1015 |
sys.path.insert(0, str(tmp_path)) |
| 1016 |
try: |
| 1017 |
from cyclopts.ext.sphinx import CycloptsDirective |
| 1018 |
|
| 1019 |
mock_state = MagicMock() |
| 1020 |
mock_state.nested_parse = MagicMock() |
| 1021 |
|
| 1022 |
directive = CycloptsDirective( |
| 1023 |
name="cyclopts", |
| 1024 |
arguments=["test_usage_name_module:app"], |
| 1025 |
options={"usage-name": "uv run cli"}, |
| 1026 |
content=StringList(), |
| 1027 |
lineno=1, |
| 1028 |
content_offset=0, |
| 1029 |
block_text="", |
| 1030 |
state=mock_state, |
| 1031 |
state_machine=MagicMock(), |
| 1032 |
) |
| 1033 |
|
| 1034 |
directive.run() |
| 1035 |
|
| 1036 |
all_calls = mock_state.nested_parse.call_args_list |
| 1037 |
all_content = [] |
| 1038 |
for call_args in all_calls: |
| 1039 |
if call_args: |
| 1040 |
rst_lines = call_args[0][0] |
| 1041 |
all_content.extend(rst_lines) |
| 1042 |
|
| 1043 |
rst_content = "\n".join(all_content) |
| 1044 |
assert "uv run cli" in rst_content |
| 1045 |
finally: |
| 1046 |
sys.path.remove(str(tmp_path)) |
| 1047 |
if "test_usage_name_module" in sys.modules: |
| 1048 |
del sys.modules["test_usage_name_module"] |
| 1049 |
|
| 1050 |
|
| 1051 |
class TestRstContentParsing: |
| 1052 |
"""Test RST content parsing and formatting.""" |
| 1053 |
|
| 1054 |
def test_consecutive_lines_in_same_paragraph(self, tmp_path): |
| 1055 |
"""Test that consecutive non-empty lines are kept in the same paragraph.""" |
| 1056 |
module_file = tmp_path / "test_paragraph_module.py" |
| 1057 |
module_file.write_text("""from cyclopts import App |
| 1058 |
|
| 1059 |
app = App(name="test", help="Test") |
| 1060 |
|
| 1061 |
@app.command |
| 1062 |
def cmd(): |
| 1063 |
'''A command with multiline description. |
| 1064 |
|
| 1065 |
Shell completion is available. Run once to install (persistent): |
| 1066 |
``cyclopts --install-completion`` |
| 1067 |
''' |
| 1068 |
pass |
| 1069 |
""") |
| 1070 |
|
| 1071 |
sys.path.insert(0, str(tmp_path)) |
| 1072 |
try: |
| 1073 |
from cyclopts.ext.sphinx import CycloptsDirective |
| 1074 |
|
| 1075 |
mock_state = MagicMock() |
| 1076 |
|
| 1077 |
|
| 1078 |
parsed_content = [] |
| 1079 |
|
| 1080 |
def capture_nested_parse(string_list, offset, parent): |
| 1081 |
|
| 1082 |
content = "\n".join(string_list) |
| 1083 |
parsed_content.append(content) |
| 1084 |
|
| 1085 |
mock_state.nested_parse = capture_nested_parse |
| 1086 |
|
| 1087 |
directive = CycloptsDirective( |
| 1088 |
name="cyclopts", |
| 1089 |
arguments=["test_paragraph_module:app"], |
| 1090 |
options={}, |
| 1091 |
content=StringList(), |
| 1092 |
lineno=1, |
| 1093 |
content_offset=0, |
| 1094 |
block_text="", |
| 1095 |
state=mock_state, |
| 1096 |
state_machine=MagicMock(), |
| 1097 |
) |
| 1098 |
|
| 1099 |
directive.run() |
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
consecutive_line_found = False |
| 1105 |
for content in parsed_content: |
| 1106 |
if "Shell completion is available" in content and "cyclopts --install-completion" in content: |
| 1107 |
consecutive_line_found = True |
| 1108 |
break |
| 1109 |
|
| 1110 |
|
| 1111 |
assert consecutive_line_found, "Consecutive lines should be parsed together in the same paragraph" |
| 1112 |
|
| 1113 |
finally: |
| 1114 |
sys.path.remove(str(tmp_path)) |
| 1115 |
|
| 1116 |
|
| 1117 |
class TestBackwardCompatibility: |
| 1118 |
"""Test backward compatibility of cyclopts.sphinx_ext.""" |
| 1119 |
|
| 1120 |
def test_deprecation_warning(self): |
| 1121 |
"""Test that importing cyclopts.sphinx_ext shows a deprecation warning.""" |
| 1122 |
import sys |
| 1123 |
|
| 1124 |
|
| 1125 |
if "cyclopts.sphinx_ext" in sys.modules: |
| 1126 |
del sys.modules["cyclopts.sphinx_ext"] |
| 1127 |
|
| 1128 |
with pytest.warns(DeprecationWarning, match="Importing from 'cyclopts.sphinx_ext' is deprecated"): |
| 1129 |
import cyclopts.sphinx_ext |
| 1130 |
|
| 1131 |
def test_backward_compatible_imports(self): |
| 1132 |
"""Test that old import path still works.""" |
| 1133 |
import warnings |
| 1134 |
|
| 1135 |
|
| 1136 |
with warnings.catch_warnings(): |
| 1137 |
warnings.simplefilter("ignore", DeprecationWarning) |
| 1138 |
from cyclopts.sphinx_ext import CycloptsDirective, DirectiveOptions, setup |
| 1139 |
|
| 1140 |
assert CycloptsDirective is not None |
| 1141 |
assert DirectiveOptions is not None |
| 1142 |
assert setup is not None |