Debian Codegraph @

tests/test_async.py

raw symbols SCIP
1 import asyncio
2 from typing import Annotated
3
4 import sniffio
5
6 from cyclopts import App, Parameter
7
8
9 def test_async_handler(app):
10 @app.command(name="command")
11 async def async_handler():
12 assert sniffio.current_async_library() == "asyncio"
13 return "Async handler works"
14
15 assert app("command") == "Async handler works"
16
17
18 def test_async_handler_with_subcommand_works(app):
19 sub_app = App(name="foo")
20 app.command(sub_app)
21
22 @sub_app.command(name="bar")
23 async def async_handler():
24 assert sniffio.current_async_library() == "asyncio"
25 return "Async handler works"
26
27 assert app("foo bar") == "Async handler works"
28
29
30 def test_handler(app):
31 @app.command(name="command")
32 def sync_handler():
33 return "Sync handler works"
34
35 assert app("command") == "Sync handler works"
36
37
38 def test_async_meta_with_async_command(app):
39 results = []
40
41 @app.command
42 async def async_command(value: int):
43 await asyncio.sleep(0) # Simulate async work
44 result = f"Async command executed with {value}"
45 results.append(result)
46 return result
47
48 @app.meta.default
49 async def launcher(*tokens: Annotated[str, Parameter(show=False, allow_leading_hyphen=True)]):
50 await asyncio.sleep(0) # Simulate async initialization
51 results.append("Meta initialized")
52 result = await app.run_async(tokens) # Use run_async when inside an async context
53 results.append("Meta finished")
54 return result
55
56 result = app.meta(["async-command", "42"])
57 assert result == "Async command executed with 42"
58 assert results == ["Meta initialized", "Async command executed with 42", "Meta finished"]
59
60
61 def test_async_meta_with_sync_command(app):
62 results = []
63
64 @app.command
65 def sync_command(value: int):
66 result = f"Sync command executed with {value}"
67 results.append(result)
68 return result
69
70 @app.meta.default
71 async def launcher(*tokens: Annotated[str, Parameter(show=False, allow_leading_hyphen=True)]):
72 await asyncio.sleep(0) # Simulate async initialization
73 results.append("Meta initialized")
74 # Synchronous commands should also work from async meta
75 result = await app.run_async(tokens) # Use run_async when inside an async context
76 results.append("Meta finished")
77 return result
78
79 result = app.meta(["sync-command", "42"])
80 assert result == "Sync command executed with 42"
81 assert results == ["Meta initialized", "Sync command executed with 42", "Meta finished"]
82
83
84 def test_async_meta_with_nested_async(app):
85 results = []
86
87 @app.default
88 async def default_handler():
89 await asyncio.sleep(0)
90 results.append("Default handler")
91 return "Default result"
92
93 @app.meta.default
94 async def meta(*tokens: Annotated[str, Parameter(show=False, allow_leading_hyphen=True)]):
95 await asyncio.sleep(0)
96 results.append("Meta handler")
97 result = await app.run_async(tokens) # Use run_async when inside an async context
98 results.append("Meta complete")
99 return result
100
101 result = app.meta([])
102 assert result == "Default result"
103 assert results == ["Meta handler", "Default handler", "Meta complete"]
104
105
106 def test_run_async_inherits_app_parameters():
107 """Test that run_async inherits parameters from App instance when not specified."""
108 app = App(
109 print_error=False,
110 exit_on_error=False,
111 verbose=True,
112 backend="asyncio",
113 result_action="return_value",
114 )
115
116 @app.default
117 async def handler():
118 return "test"
119
120 # These should inherit from the app instance
121 async def run_test():
122 result = await app.run_async([])
123 return result
124
125 result = asyncio.run(run_test())
126 assert result == "test"
127
128
129 def test_run_async_overrides_app_parameters():
130 """Test that run_async can override App instance parameters."""
131 app = App(
132 print_error=True,
133 exit_on_error=True,
134 verbose=False,
135 result_action="return_value",
136 )
137
138 @app.default
139 async def handler():
140 return "test"
141
142 # Override the app-level settings
143 async def run_test():
144 result = await app.run_async(
145 [],
146 print_error=False,
147 exit_on_error=False,
148 verbose=True,
149 )
150 return result
151
152 result = asyncio.run(run_test())
153 assert result == "test"
154
155
156 def test_run_async_backend_parameter(app):
157 """Test that run_async accepts and respects backend parameter."""
158
159 @app.default
160 async def handler():
161 # Verify we're using asyncio
162 assert sniffio.current_async_library() == "asyncio"
163 return "test"
164
165 async def run_test():
166 # Explicitly specify asyncio backend
167 result = await app.run_async([], backend="asyncio")
168 return result
169
170 result = asyncio.run(run_test())
171 assert result == "test"
172
173
174 def test_run_async_with_none_parameters(app):
175 """Test that run_async handles None parameters correctly."""
176
177 @app.default
178 async def handler():
179 return "test"
180
181 async def run_test():
182 # All None parameters should inherit from app defaults
183 result = await app.run_async(
184 [],
185 print_error=None,
186 exit_on_error=None,
187 verbose=None,
188 backend=None,
189 )
190 return result
191
192 result = asyncio.run(run_test())
193 assert result == "test"
194
195
196 def test_run_async_with_result_action(app):
197 """Test that run_async properly handles result_action parameter."""
198
199 @app.default
200 async def handler():
201 return 42
202
203 async def run_test():
204 # With result_action="return_value", should return the value directly
205 result = await app.run_async([], result_action="return_value")
206 return result
207
208 result = asyncio.run(run_test())
209 assert result == 42