Debian Codegraph @

tests/test_trio.py

raw symbols SCIP
1 import sniffio
2 import trio
3
4 from cyclopts import App
5
6
7 def test_async_handler(app):
8 @app.command(name="command")
9 async def async_handler():
10 assert sniffio.current_async_library() == "trio"
11 await trio.lowlevel.checkpoint() # Ensure trio is functioning
12 return "Async handler works"
13
14 assert app("command", backend="trio") == "Async handler works"
15
16
17 def test_async_handler_with_subcommand_works(app):
18 sub_app = App(name="foo")
19 app.command(sub_app)
20
21 @sub_app.command(name="bar")
22 async def async_handler():
23 assert sniffio.current_async_library() == "trio"
24 await trio.lowlevel.checkpoint() # Ensure trio is functioning
25 return "Async handler works"
26
27 assert app("foo bar", backend="trio") == "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_interactive_shell_async_command(mocker, console):
39 """Async commands should be run with the trio backend in the interactive shell.
40
41 See https://github.com/BrianPugh/cyclopts/issues/826
42 """
43 app = App(backend="trio")
44
45 mocker.patch(
46 "cyclopts.core.input",
47 side_effect=[
48 "start",
49 "quit",
50 ],
51 )
52
53 start_called = 0
54
55 @app.command
56 async def start():
57 nonlocal start_called
58 assert sniffio.current_async_library() == "trio"
59 await trio.lowlevel.checkpoint() # Ensure trio is functioning
60 start_called += 1
61 return "Started!"
62
63 with console.capture() as capture:
64 app.interactive_shell(console=console)
65
66 actual = capture.get()
67
68 assert start_called == 1
69 assert "Started!" in actual
70 assert "coroutine object" not in actual