Debian Codegraph @

tests/cli/test_run.py

raw symbols SCIP
1 """Tests for the 'cyclopts run' command."""
2
3 import sys
4 from textwrap import dedent
5
6 import pytest
7
8 from cyclopts.cli import app as cyclopts_cli
9
10
11 def test_run_simple_script(tmp_path, capsys):
12 """Test running a simple script with default app."""
13 script = tmp_path / "simple.py"
14 script.write_text(
15 dedent(
16 """\
17 from cyclopts import App
18
19 app = App(name="simple")
20
21 @app.default
22 def main(name: str = "World"):
23 print(f"Hello, {name}!")
24
25 if __name__ == "__main__":
26 app()
27 """
28 )
29 )
30
31 with pytest.raises(SystemExit) as exc_info:
32 cyclopts_cli(["run", str(script)])
33
34 assert exc_info.value.code == 0
35 captured = capsys.readouterr()
36 assert "Hello, World!" in captured.out
37
38
39 def test_run_script_with_args(tmp_path, capsys):
40 """Test running a script with command-line arguments."""
41 script = tmp_path / "greet.py"
42 script.write_text(
43 dedent(
44 """\
45 from cyclopts import App
46
47 app = App(name="greet")
48
49 @app.default
50 def main(name: str, greeting: str = "Hello"):
51 print(f"{greeting}, {name}!")
52 """
53 )
54 )
55
56 with pytest.raises(SystemExit) as exc_info:
57 cyclopts_cli(["run", str(script), "--name", "Alice", "--greeting", "Hi"])
58
59 assert exc_info.value.code == 0
60 captured = capsys.readouterr()
61 assert "Hi, Alice!" in captured.out
62
63
64 def test_run_script_with_positional_args(tmp_path, capsys):
65 """Test running a script with positional arguments."""
66 script = tmp_path / "echo.py"
67 script.write_text(
68 dedent(
69 """\
70 from cyclopts import App
71
72 app = App(name="echo")
73
74 @app.default
75 def main(message: str):
76 print(message)
77 """
78 )
79 )
80
81 with pytest.raises(SystemExit) as exc_info:
82 cyclopts_cli(["run", str(script), "Hello World"])
83
84 assert exc_info.value.code == 0
85 captured = capsys.readouterr()
86 assert "Hello World" in captured.out
87
88
89 def test_run_script_with_app_notation(tmp_path, capsys):
90 """Test running a script with ':app_name' notation."""
91 script = tmp_path / "multi.py"
92 script.write_text(
93 dedent(
94 """\
95 from cyclopts import App
96
97 app1 = App(name="app1")
98 app2 = App(name="app2")
99
100 @app1.default
101 def main1():
102 print("App 1")
103
104 @app2.default
105 def main2():
106 print("App 2")
107 """
108 )
109 )
110
111 with pytest.raises(SystemExit) as exc_info:
112 cyclopts_cli(["run", f"{script}:app2"])
113
114 assert exc_info.value.code == 0
115 captured = capsys.readouterr()
116 assert "App 2" in captured.out
117
118
119 def test_run_script_with_subcommands(tmp_path, capsys):
120 """Test running a script with subcommands."""
121 script = tmp_path / "commands.py"
122 script.write_text(
123 dedent(
124 """\
125 from cyclopts import App
126
127 app = App(name="commands")
128
129 @app.command
130 def build():
131 print("Building...")
132
133 @app.command
134 def deploy():
135 print("Deploying...")
136 """
137 )
138 )
139
140 with pytest.raises(SystemExit) as exc_info:
141 cyclopts_cli(["run", str(script), "build"])
142
143 assert exc_info.value.code == 0
144 captured = capsys.readouterr()
145 assert "Building..." in captured.out
146
147
148 def test_run_script_not_found(tmp_path, capsys):
149 """Test error when script file not found."""
150 nonexistent = tmp_path / "nonexistent.py"
151
152 with pytest.raises(SystemExit) as exc_info:
153 cyclopts_cli(["run", str(nonexistent)])
154
155 assert exc_info.value.code == 1
156 captured = capsys.readouterr()
157 assert "not found" in captured.err.lower()
158
159
160 def test_run_not_python_file(tmp_path, capsys):
161 """Test error when file is not a Python file."""
162 textfile = tmp_path / "notpython.txt"
163 textfile.write_text("This is not Python")
164
165 with pytest.raises(SystemExit) as exc_info:
166 cyclopts_cli(["run", str(textfile)])
167
168 assert exc_info.value.code == 1
169 captured = capsys.readouterr()
170 assert "not a python file" in captured.err.lower()
171
172
173 def test_run_no_app_found(tmp_path, capsys):
174 """Test error when no App object found in script."""
175 script = tmp_path / "noapp.py"
176 script.write_text(
177 dedent(
178 """\
179 # No App object here
180 def main():
181 print("No app")
182 """
183 )
184 )
185
186 with pytest.raises(SystemExit) as exc_info:
187 cyclopts_cli(["run", str(script)])
188
189 assert exc_info.value.code == 1
190 captured = capsys.readouterr()
191 assert "no cyclopts app" in captured.err.lower()
192
193
194 def test_run_multiple_apps_error(tmp_path, capsys):
195 """Test error when multiple App objects found without specification."""
196 script = tmp_path / "multiapp.py"
197 script.write_text(
198 dedent(
199 """\
200 from cyclopts import App
201
202 app1 = App(name="app1")
203 app2 = App(name="app2")
204
205 @app1.default
206 def main1():
207 pass
208
209 @app2.default
210 def main2():
211 pass
212 """
213 )
214 )
215
216 with pytest.raises(SystemExit) as exc_info:
217 cyclopts_cli(["run", str(script)])
218
219 assert exc_info.value.code == 1
220 captured = capsys.readouterr()
221 assert "multiple app objects" in captured.err.lower()
222
223
224 def test_run_specified_app_not_found(tmp_path, capsys):
225 """Test error when specified app object not found."""
226 script = tmp_path / "test.py"
227 script.write_text(
228 dedent(
229 """\
230 from cyclopts import App
231 app = App()
232 """
233 )
234 )
235
236 with pytest.raises(SystemExit) as exc_info:
237 cyclopts_cli(["run", f"{script}:nonexistent"])
238
239 assert exc_info.value.code == 1
240 captured = capsys.readouterr()
241 assert "no object named" in captured.err.lower()
242
243
244 def test_run_specified_object_not_app(tmp_path, capsys):
245 """Test error when specified object is not an App."""
246 script = tmp_path / "notapp.py"
247 script.write_text(
248 dedent(
249 """\
250 from cyclopts import App
251
252 app = App()
253 some_string = "not an app"
254 """
255 )
256 )
257
258 with pytest.raises(SystemExit) as exc_info:
259 cyclopts_cli(["run", f"{script}:some_string"])
260
261 assert exc_info.value.code == 1
262 captured = capsys.readouterr()
263 assert "not a cyclopts app" in captured.err.lower()
264
265
266 def test_run_with_variadic_args(tmp_path, capsys):
267 """Test running script with variadic arguments."""
268 script = tmp_path / "variadic.py"
269 script.write_text(
270 dedent(
271 """\
272 from cyclopts import App
273
274 app = App(name="variadic")
275
276 @app.default
277 def main(*files: str):
278 for f in files:
279 print(f"Processing: {f}")
280 """
281 )
282 )
283
284 with pytest.raises(SystemExit) as exc_info:
285 cyclopts_cli(["run", str(script), "file1.txt", "file2.txt", "file3.txt"])
286
287 assert exc_info.value.code == 0
288 captured = capsys.readouterr()
289 assert "Processing: file1.txt" in captured.out
290 assert "Processing: file2.txt" in captured.out
291 assert "Processing: file3.txt" in captured.out
292
293
294 def test_run_with_leading_hyphen(tmp_path, capsys):
295 """Test that script path can start with hyphen."""
296 script = tmp_path / "-special.py"
297 script.write_text(
298 dedent(
299 """\
300 from cyclopts import App
301
302 app = App(name="special")
303
304 @app.default
305 def main():
306 print("Special script")
307 """
308 )
309 )
310
311 with pytest.raises(SystemExit) as exc_info:
312 cyclopts_cli(["run", str(script)])
313
314 assert exc_info.value.code == 0
315 captured = capsys.readouterr()
316 assert "Special script" in captured.out
317
318
319 def test_run_script_with_import_error(tmp_path, capsys):
320 """Test handling script with import errors."""
321 script = tmp_path / "broken.py"
322 script.write_text(
323 dedent(
324 """\
325 import nonexistent_module
326 from cyclopts import App
327 app = App()
328 """
329 )
330 )
331
332 with pytest.raises((SystemExit, ImportError)):
333 cyclopts_cli(["run", str(script)])
334
335
336 @pytest.mark.skipif(sys.platform == "win32", reason="Windows path handling differs")
337 def test_run_script_with_windows_path_colon(tmp_path, capsys):
338 """Test that Windows paths with drive letters are handled correctly."""
339 script = tmp_path / "winpath.py"
340 script.write_text(
341 dedent(
342 """\
343 from cyclopts import App
344
345 app = App(name="winpath")
346
347 @app.default
348 def main():
349 print("Windows path")
350 """
351 )
352 )
353
354 with pytest.raises(SystemExit) as exc_info:
355 cyclopts_cli(["run", str(script.resolve())])
356
357 assert exc_info.value.code == 0
358 captured = capsys.readouterr()
359 assert "Windows path" in captured.out