Debian Codegraph @

tests/cli/test_complete.py

raw symbols SCIP
1 """Tests for the hidden '_complete' command for dynamic completion."""
2
3 from textwrap import dedent
4 from unittest.mock import patch
5
6 from cyclopts.cli import app as cyclopts_cli
7
8
9 def test_complete_run_subcommand(tmp_path, capsys):
10 """Test completion for 'run' subcommand."""
11 script = tmp_path / "app.py"
12 script.write_text(
13 dedent(
14 """\
15 from cyclopts import App
16
17 app = App(name="testapp")
18
19 @app.command
20 def build():
21 '''Build the project.'''
22 pass
23
24 @app.command
25 def deploy():
26 '''Deploy the app.'''
27 pass
28 """
29 )
30 )
31
32 with patch("sys.exit"):
33 cyclopts_cli(["_complete", "run", str(script)])
34
35 captured = capsys.readouterr()
36 assert "build" in captured.out
37 assert "deploy" in captured.out
38
39
40 def test_complete_run_with_options(tmp_path, capsys):
41 """Test completion includes options."""
42 script = tmp_path / "app.py"
43 script.write_text(
44 dedent(
45 """\
46 from cyclopts import App
47
48 app = App(name="testapp")
49
50 @app.default
51 def main(verbose: bool = False, count: int = 1):
52 '''Main command.'''
53 pass
54 """
55 )
56 )
57
58 with patch("sys.exit"):
59 cyclopts_cli(["_complete", "run", str(script)])
60
61 captured = capsys.readouterr()
62 assert "--verbose" in captured.out
63 assert "--count" in captured.out
64
65
66 def test_complete_run_invalid_script_silent(tmp_path, capsys):
67 """Test that completion silently fails for invalid scripts."""
68 nonexistent = tmp_path / "nonexistent.py"
69
70 with patch("sys.exit"):
71 cyclopts_cli(["_complete", "run", str(nonexistent)])
72
73 captured = capsys.readouterr()
74 assert captured.out == ""
75
76
77 def test_complete_run_nested_commands(tmp_path, capsys):
78 """Test completion for nested command structure."""
79 script = tmp_path / "nested.py"
80 script.write_text(
81 dedent(
82 """\
83 from cyclopts import App
84
85 app = App(name="testapp")
86 db = App(name="db", help="Database commands")
87
88 @db.command
89 def migrate():
90 '''Run migrations.'''
91 pass
92
93 app.command(db)
94 """
95 )
96 )
97
98 with patch("sys.exit"):
99 cyclopts_cli(["_complete", "run", str(script)])
100
101 captured = capsys.readouterr()
102 assert "db" in captured.out
103
104
105 def test_complete_run_with_words(tmp_path, capsys):
106 """Test completion with current command line words."""
107 script = tmp_path / "app.py"
108 script.write_text(
109 dedent(
110 """\
111 from cyclopts import App
112
113 app = App(name="testapp")
114 db = App(name="db")
115
116 @db.command
117 def migrate():
118 '''Run migrations.'''
119 pass
120
121 @db.command
122 def backup():
123 '''Backup database.'''
124 pass
125
126 app.command(db)
127 """
128 )
129 )
130
131 with patch("sys.exit"):
132 cyclopts_cli(["_complete", "run", str(script), "db"])
133
134 captured = capsys.readouterr()
135 assert "migrate" in captured.out
136 assert "backup" in captured.out
137
138
139 def test_complete_non_run_subcommand_silent(tmp_path, capsys):
140 """Test that completion is silent for non-run subcommands."""
141 script = tmp_path / "app.py"
142 script.write_text("from cyclopts import App\napp = App()")
143
144 with patch("sys.exit"):
145 cyclopts_cli(["_complete", "generate-docs", str(script)])
146
147 captured = capsys.readouterr()
148 assert captured.out == ""
149
150
151 def test_complete_run_script_with_syntax_error_silent(tmp_path, capsys):
152 """Test that completion silently handles scripts with syntax errors."""
153 script = tmp_path / "broken.py"
154 script.write_text("this is not valid python $$$ @@@")
155
156 with patch("sys.exit"):
157 cyclopts_cli(["_complete", "run", str(script)])
158
159 captured = capsys.readouterr()
160 assert captured.out == ""
161
162
163 def test_complete_shows_help_for_default_command(tmp_path, capsys):
164 """Test that options from default command are shown."""
165 script = tmp_path / "app.py"
166 script.write_text(
167 dedent(
168 """\
169 from cyclopts import App
170 from typing import Annotated
171 from cyclopts import Parameter
172
173 app = App(name="testapp")
174
175 @app.default
176 def main(
177 verbose: Annotated[bool, Parameter(help="Enable verbose mode")] = False,
178 debug: Annotated[bool, Parameter(help="Enable debug mode")] = False,
179 ):
180 pass
181 """
182 )
183 )
184
185 with patch("sys.exit"):
186 cyclopts_cli(["_complete", "run", str(script)])
187
188 captured = capsys.readouterr()
189 assert "--verbose" in captured.out
190 assert "--debug" in captured.out
191 assert "Enable verbose mode" in captured.out or "verbose mode" in captured.out.lower()