"""Tests for App.generate_docs() method."""
import tempfile
from pathlib import Path
from textwrap import dedent
from typing import Annotated
import pytest
from cyclopts import App, Parameter
def test_generate_docs_simple_app():
"""Test basic documentation generation for a simple app."""
app = App(name="myapp", help="A simple CLI application")
@app.default
def main(name: str, verbose: bool = False):
"""Main command.
Parameters
----------
name : str
Your name.
verbose : bool
Enable verbose output.
"""
pass
actual = app.generate_docs()
expected = dedent(
"""\
# myapp
```console
myapp NAME [ARGS]
```
A simple CLI application
**Parameters**:
* `NAME, --name`: Your name. **[required]**
* `VERBOSE, --verbose, --no-verbose`: Enable verbose output. *[default: False]*
"""
)
assert actual == expected
def test_generate_docs_with_commands():
"""Test documentation generation with subcommands."""
app = App(name="myapp", help="CLI with commands")
@app.command
def serve(port: int = 8000):
"""Start the server.
Parameters
----------
port : int
Port number.
"""
pass
@app.command
def build(output: str = "./dist"):
"""Build the project.
Parameters
----------
output : str
Output directory.
"""
pass
actual = app.generate_docs()
# Check structure without being too specific about Usage lines that vary by context
assert "# myapp" in actual
assert "CLI with commands" in actual
# Main usage section
assert "myapp COMMAND" in actual
# Commands list with hyperlinks
assert "**Commands**:" in actual
assert "* [`build`](#myapp-build): Build the project." in actual
assert "* [`serve`](#myapp-serve): Start the server." in actual
# Serve command details (now shows full path)
assert "## myapp serve" in actual
assert "Start the server." in actual
assert "**Parameters**:" in actual
assert "* `PORT, --port`: Port number. *[default: 8000]*" in actual
# Build command details (now shows full path)
assert "## myapp build" in actual
assert "Build the project." in actual
assert "* `OUTPUT, --output`: Output directory. *[default: ./dist]*" in actual
def test_generate_docs_recursive():
"""Test recursive documentation generation."""
app = App(name="myapp", help="Main app")
subapp = App(name="db", help="Database commands")
@subapp.command
def migrate():
"""Run database migrations."""
pass
@subapp.command
def backup(output: str):
"""Backup the database.
Parameters
----------
output : str
Backup file path.
"""
pass
app.command(subapp)
actual = app.generate_docs(recursive=True)
# Verify structure of recursive documentation
assert "# myapp" in actual
assert "Main app" in actual
assert "## myapp db" in actual
assert "Database commands" in actual
assert "migrate" in actual
assert "backup" in actual
assert "Run database migrations" in actual
assert "Backup the database" in actual
def test_generate_docs_non_recursive():
"""Test non-recursive documentation generation."""
app = App(name="myapp", help="Main app")
subapp = App(name="db", help="Database commands")
@subapp.command
def migrate():
"""Run database migrations."""
pass
app.command(subapp)
actual = app.generate_docs(recursive=False)
# Note: In non-recursive mode, subcommands like 'migrate' don't get headings,
# so they appear in the ToC but the link is broken. This is a known limitation.
# The current implementation always collects ToC entries recursively.
# Note: The 'db' subcommand doesn't show usage because it has no default_command.
expected = dedent(
"""\
# myapp
```console
myapp COMMAND
```
Main app
## Table of Contents
- [`db`](#myapp-db)
- [`migrate`](#myapp-db-migrate)
**Commands**:
* [`db`](#myapp-db): Database commands
## myapp db
Database commands
"""
)
assert actual == expected
def test_generate_docs_with_hidden_commands(mocker):
"""Test documentation with hidden commands."""
# Mock sys.argv[0] for consistent output
mocker.patch("sys.argv", ["myapp"])
app = App(name="myapp", help="App with hidden commands")
@app.command
def visible():
"""Visible command."""
pass
@app.command(show=False)
def hidden():
"""Hidden command."""
pass
# Test WITHOUT include_hidden
actual_without_hidden = app.generate_docs(include_hidden=False)
expected_without_hidden = dedent(
"""\
# myapp
```console
myapp COMMAND
```
App with hidden commands
## Table of Contents
- [`visible`](#myapp-visible)
**Commands**:
* [`visible`](#myapp-visible): Visible command.
## myapp visible
```console
myapp visible
```
Visible command.
"""
)
assert actual_without_hidden == expected_without_hidden
# Test WITH include_hidden
actual_with_hidden = app.generate_docs(include_hidden=True)
# Verify the hidden command is present when include_hidden=True
assert "## myapp hidden" in actual_with_hidden
assert "Hidden command." in actual_with_hidden
# Note: --help and --version are builtin flags, not commands, so they
# should not appear in the Commands section even with include_hidden=True
def test_generate_docs_with_required_parameters():
"""Test documentation with required parameters."""
app = App(name="myapp")
@app.default
def main(
required: Annotated[str, Parameter(help="Required parameter")],
optional: str = "default",
):
"""Main command."""
pass
actual = app.generate_docs()
expected = dedent(
"""\
# myapp
```console
myapp REQUIRED [ARGS]
```
Main command.
**Parameters**:
* `REQUIRED, --required`: Required parameter **[required]**
* `OPTIONAL, --optional`: *[default: default]*
"""
)
assert actual == expected
def test_generate_docs_with_choices():
"""Test documentation with parameter choices."""
from enum import Enum
app = App(name="myapp")
class Color(Enum):
RED = "red"
GREEN = "green"
BLUE = "blue"
@app.default
def main(color: Color = Color.RED):
"""Choose a color."""
pass
actual = app.generate_docs()
# Should show available choices in the documentation
assert "red" in actual
assert "green" in actual
assert "blue" in actual
assert "choices:" in actual
def test_generate_docs_with_custom_usage():
"""Test documentation with custom usage string."""
app = App(name="myapp", usage="myapp [OPTIONS]