Debian Codegraph @

tests/cli/test_generate_docs.py

raw symbols SCIP
1 """Tests for the 'cyclopts generate-docs' command."""
2
3 from textwrap import dedent
4 from unittest.mock import patch
5
6 import pytest
7
8 from cyclopts.cli import app as cyclopts_cli
9
10
11 def test_generate_docs_to_stdout(tmp_path, capsys):
12 """Test generating docs to stdout with explicit format."""
13 script = tmp_path / "app.py"
14 script.write_text(
15 dedent(
16 """\
17 from cyclopts import App
18
19 app = App(name="myapp", help="Test application")
20
21 @app.default
22 def main(name: str = "World"):
23 '''Greet someone.
24
25 Parameters
26 ----------
27 name : str
28 Name to greet.
29 '''
30 pass
31 """
32 )
33 )
34
35 with patch("sys.exit"):
36 cyclopts_cli(["generate-docs", str(script), "--format", "markdown"])
37
38 captured = capsys.readouterr()
39 assert "# myapp" in captured.out
40 assert "Test application" in captured.out
41
42
43 def test_generate_docs_to_file(tmp_path, capsys):
44 """Test generating docs to a file."""
45 script = tmp_path / "app.py"
46 script.write_text(
47 dedent(
48 """\
49 from cyclopts import App
50
51 app = App(name="myapp", help="Test app")
52
53 @app.default
54 def main():
55 '''Main command.'''
56 pass
57 """
58 )
59 )
60
61 output_file = tmp_path / "docs.md"
62
63 with patch("sys.exit"):
64 cyclopts_cli(["generate-docs", str(script), "--output", str(output_file)])
65
66 assert output_file.exists()
67 content = output_file.read_text()
68 assert "# myapp" in content
69 assert "Test app" in content
70
71
72 def test_generate_docs_format_inference_md(tmp_path):
73 """Test format inference from .md extension."""
74 script = tmp_path / "app.py"
75 script.write_text(
76 dedent(
77 """\
78 from cyclopts import App
79 app = App(name="myapp", help="Test")
80 """
81 )
82 )
83
84 output_file = tmp_path / "docs.md"
85
86 with patch("sys.exit"):
87 cyclopts_cli(["generate-docs", str(script), "-o", str(output_file)])
88
89 assert output_file.exists()
90 content = output_file.read_text()
91 assert "# myapp" in content
92
93
94 def test_generate_docs_explicit_format_overrides_extension(tmp_path):
95 """Test that explicit format overrides file extension."""
96 script = tmp_path / "app.py"
97 script.write_text(
98 dedent(
99 """\
100 from cyclopts import App
101 app = App(name="myapp", help="Test")
102 """
103 )
104 )
105
106 output_file = tmp_path / "docs.txt"
107
108 with patch("sys.exit"):
109 cyclopts_cli(["generate-docs", str(script), "-o", str(output_file), "--format", "markdown"])
110
111 assert output_file.exists()
112 content = output_file.read_text()
113 assert "# myapp" in content
114
115
116 def test_generate_docs_no_format_no_output_error(capsys):
117 """Test error when neither format nor output is specified."""
118 with pytest.raises((SystemExit, ValueError)):
119 cyclopts_cli(["generate-docs", "dummy.py"])
120
121
122 def test_generate_docs_invalid_extension_error(tmp_path, capsys):
123 """Test error when output file has invalid extension."""
124 script = tmp_path / "app.py"
125 script.write_text(
126 dedent(
127 """\
128 from cyclopts import App
129 app = App(name="myapp")
130 """
131 )
132 )
133
134 output_file = tmp_path / "docs.pdf"
135
136 with pytest.raises((SystemExit, ValueError)):
137 cyclopts_cli(["generate-docs", str(script), "-o", str(output_file)])
138
139
140 def test_generate_docs_with_include_hidden(tmp_path, capsys):
141 """Test generating docs with hidden commands included."""
142 script = tmp_path / "app.py"
143 script.write_text(
144 dedent(
145 """\
146 from cyclopts import App
147
148 app = App(name="myapp")
149
150 @app.command(show=False)
151 def hidden():
152 '''Hidden command.'''
153 pass
154 """
155 )
156 )
157
158 with patch("sys.exit"):
159 cyclopts_cli(["generate-docs", str(script), "--format", "markdown", "--include-hidden"])
160
161 captured = capsys.readouterr()
162 assert "hidden" in captured.out.lower()
163
164
165 def test_generate_docs_with_heading_level(tmp_path, capsys):
166 """Test custom heading level."""
167 script = tmp_path / "app.py"
168 script.write_text(
169 dedent(
170 """\
171 from cyclopts import App
172
173 app = App(name="myapp", help="Test")
174
175 @app.command
176 def cmd():
177 '''Command.'''
178 pass
179 """
180 )
181 )
182
183 with patch("sys.exit"):
184 cyclopts_cli(["generate-docs", str(script), "--format", "markdown", "--heading-level", "2"])
185
186 captured = capsys.readouterr()
187 assert "## myapp" in captured.out
188
189
190 def test_generate_docs_with_app_notation(tmp_path, capsys):
191 """Test generating docs with :app notation."""
192 script = tmp_path / "multi.py"
193 script.write_text(
194 dedent(
195 """\
196 from cyclopts import App
197
198 app1 = App(name="app1", help="First app")
199 app2 = App(name="app2", help="Second app")
200 """
201 )
202 )
203
204 with patch("sys.exit"):
205 cyclopts_cli(["generate-docs", f"{script}:app2", "--format", "markdown"])
206
207 captured = capsys.readouterr()
208 assert "# app2" in captured.out
209 assert "Second app" in captured.out
210
211
212 def test_generate_docs_script_not_found(tmp_path, capsys):
213 """Test error when script not found."""
214 with pytest.raises(SystemExit) as exc_info:
215 cyclopts_cli(["generate-docs", str(tmp_path / "nonexistent.py"), "--format", "markdown"])
216
217 assert exc_info.value.code == 1
218 captured = capsys.readouterr()
219 assert "not found" in captured.err.lower()
220
221
222 def test_generate_docs_cli_usage_name_override(tmp_path, capsys):
223 """--usage-name replaces the app name in Usage: lines of stdout output."""
224 script = tmp_path / "app.py"
225 script.write_text(
226 dedent(
227 """\
228 from cyclopts import App
229
230 app = App(name="cli", help="Test application")
231
232 @app.default
233 def main(name: str = "World"):
234 '''Greet.
235
236 Parameters
237 ----------
238 name : str
239 Name to greet.
240 '''
241 pass
242 """
243 )
244 )
245
246 with patch("sys.exit"):
247 cyclopts_cli(
248 [
249 "generate-docs",
250 str(script),
251 "--format",
252 "markdown",
253 "--usage-name",
254 "uv run cli",
255 ]
256 )
257
258 captured = capsys.readouterr()
259 # Heading still uses plain app name
260 assert "# cli" in captured.out
261 # Usage block shows the override
262 assert "uv run cli" in captured.out