| 1 | from cyclopts import App |
| 2 | |
| 3 | |
| 4 | def test_interactive_shell(app, mocker, console): |
| 5 | mocker.patch( |
| 6 | "cyclopts.core.input", |
| 7 | side_effect=[ |
| 8 | "foo 1 2 3", |
| 9 | "bad-command 123", |
| 10 | "", |
| 11 | "bar bloop", |
| 12 | "quit", |
| 13 | ], |
| 14 | ) |
| 15 | |
| 16 | foo_called, bar_called = 0, 0 |
| 17 | |
| 18 | @app.command |
| 19 | def foo(a: int, b: int, c: int): |
| 20 | nonlocal foo_called |
| 21 | foo_called += 1 |
| 22 | |
| 23 | @app.command |
| 24 | def bar(token): |
| 25 | nonlocal bar_called |
| 26 | bar_called += 1 |
| 27 | |
| 28 | with console.capture() as capture: |
| 29 | app.interactive_shell(error_console=console) |
| 30 | |
| 31 | actual = capture.get() |
| 32 | |
| 33 | assert foo_called == 1 |
| 34 | assert bar_called == 1 |
| 35 | |
| 36 | assert actual == ( |
| 37 | "╭─ Error ────────────────────────────────────────────────────────────╮\n" |
| 38 | '│ Unknown command "bad-command". Available commands: foo, bar. │\n' |
| 39 | "╰────────────────────────────────────────────────────────────────────╯\n" |
| 40 | ) |
| 41 | |
| 42 | |
| 43 | def test_interactive_shell_result_action_default_string(mocker, console): |
| 44 | """Test that string returns are printed in interactive shell (default behavior).""" |
| 45 | app = App() |
| 46 | |
| 47 | mocker.patch( |
| 48 | "cyclopts.core.input", |
| 49 | side_effect=[ |
| 50 | "greet Alice", |
| 51 | "quit", |
| 52 | ], |
| 53 | ) |
| 54 | |
| 55 | @app.command |
| 56 | def greet(name: str) -> str: |
| 57 | return f"Hello {name}!" |
| 58 | |
| 59 | with console.capture() as capture: |
| 60 | app.interactive_shell(console=console) |
| 61 | |
| 62 | actual = capture.get() |
| 63 | assert "Hello Alice!" in actual |
| 64 | |
| 65 | |
| 66 | def test_interactive_shell_result_action_default_int(app, mocker, console): |
| 67 | """Test that int returns are not printed in interactive shell (default behavior).""" |
| 68 | mocker.patch( |
| 69 | "cyclopts.core.input", |
| 70 | side_effect=[ |
| 71 | "get-code 42", |
| 72 | "quit", |
| 73 | ], |
| 74 | ) |
| 75 | |
| 76 | @app.command |
| 77 | def get_code(code: int) -> int: |
| 78 | return code |
| 79 | |
| 80 | with console.capture() as capture: |
| 81 | app.interactive_shell(console=console) |
| 82 | |
| 83 | actual = capture.get() |
| 84 | assert "42" not in actual |
| 85 | |
| 86 | |
| 87 | def test_interactive_shell_result_action_default_bool_true(app, mocker, console): |
| 88 | """Test that True returns are not printed in interactive shell.""" |
| 89 | mocker.patch( |
| 90 | "cyclopts.core.input", |
| 91 | side_effect=[ |
| 92 | "check", |
| 93 | "quit", |
| 94 | ], |
| 95 | ) |
| 96 | |
| 97 | @app.command |
| 98 | def check() -> bool: |
| 99 | return True |
| 100 | |
| 101 | with console.capture() as capture: |
| 102 | app.interactive_shell(console=console) |
| 103 | |
| 104 | actual = capture.get() |
| 105 | assert "True" not in actual |
| 106 | |
| 107 | |
| 108 | def test_interactive_shell_result_action_default_bool_false(app, mocker, console): |
| 109 | """Test that False returns are not printed in interactive shell.""" |
| 110 | mocker.patch( |
| 111 | "cyclopts.core.input", |
| 112 | side_effect=[ |
| 113 | "check", |
| 114 | "quit", |
| 115 | ], |
| 116 | ) |
| 117 | |
| 118 | @app.command |
| 119 | def check() -> bool: |
| 120 | return False |
| 121 | |
| 122 | with console.capture() as capture: |
| 123 | app.interactive_shell(console=console) |
| 124 | |
| 125 | actual = capture.get() |
| 126 | assert "False" not in actual |
| 127 | |
| 128 | |
| 129 | def test_interactive_shell_result_action_default_none(app, mocker, console): |
| 130 | """Test that None returns are not printed in interactive shell.""" |
| 131 | mocker.patch( |
| 132 | "cyclopts.core.input", |
| 133 | side_effect=[ |
| 134 | "do-nothing", |
| 135 | "quit", |
| 136 | ], |
| 137 | ) |
| 138 | |
| 139 | @app.command |
| 140 | def do_nothing() -> None: |
| 141 | pass |
| 142 | |
| 143 | with console.capture() as capture: |
| 144 | app.interactive_shell(console=console) |
| 145 | |
| 146 | actual = capture.get() |
| 147 | assert "None" not in actual |
| 148 | |
| 149 | |
| 150 | def test_interactive_shell_result_action_default_list(mocker, console): |
| 151 | """Test that list returns are printed in interactive shell.""" |
| 152 | app = App() |
| 153 | |
| 154 | mocker.patch( |
| 155 | "cyclopts.core.input", |
| 156 | side_effect=[ |
| 157 | "get-list", |
| 158 | "quit", |
| 159 | ], |
| 160 | ) |
| 161 | |
| 162 | @app.command |
| 163 | def get_list() -> list: |
| 164 | return [1, 2, 3] |
| 165 | |
| 166 | with console.capture() as capture: |
| 167 | app.interactive_shell(console=console) |
| 168 | |
| 169 | actual = capture.get() |
| 170 | assert "[1, 2, 3]" in actual |
| 171 | |
| 172 | |
| 173 | def test_interactive_shell_result_action_custom_app(app, mocker, console): |
| 174 | """Test that custom result_action on App is respected in interactive shell.""" |
| 175 | custom_app = App(result_action="print_non_none_return_int_as_exit_code") |
| 176 | |
| 177 | mocker.patch( |
| 178 | "cyclopts.core.input", |
| 179 | side_effect=[ |
| 180 | "get-number", |
| 181 | "quit", |
| 182 | ], |
| 183 | ) |
| 184 | |
| 185 | @custom_app.command |
| 186 | def get_number() -> int: |
| 187 | return 42 |
| 188 | |
| 189 | with console.capture() as capture: |
| 190 | custom_app.interactive_shell(console=console) |
| 191 | |
| 192 | actual = capture.get() |
| 193 | assert "42" in actual |
| 194 | |
| 195 | |
| 196 | def test_interactive_shell_result_action_override_parameter(app, mocker, console): |
| 197 | """Test that result_action parameter overrides App setting.""" |
| 198 | mocker.patch( |
| 199 | "cyclopts.core.input", |
| 200 | side_effect=[ |
| 201 | "greet Bob", |
| 202 | "quit", |
| 203 | ], |
| 204 | ) |
| 205 | |
| 206 | @app.command |
| 207 | def greet(name: str) -> str: |
| 208 | return f"Hello {name}!" |
| 209 | |
| 210 | with console.capture() as capture: |
| 211 | app.interactive_shell(console=console, result_action="return_int_as_exit_code_else_zero") |
| 212 | |
| 213 | actual = capture.get() |
| 214 | assert "Hello Bob!" not in actual |
| 215 | |
| 216 | |
| 217 | def test_interactive_shell_result_action_callable(app, mocker, console): |
| 218 | """Test that callable result_action works in interactive shell.""" |
| 219 | mocker.patch( |
| 220 | "cyclopts.core.input", |
| 221 | side_effect=[ |
| 222 | "greet Alice", |
| 223 | "quit", |
| 224 | ], |
| 225 | ) |
| 226 | |
| 227 | results = [] |
| 228 | |
| 229 | def custom_handler(result): |
| 230 | results.append(f"CUSTOM: {result}") |
| 231 | return result |
| 232 | |
| 233 | @app.command |
| 234 | def greet(name: str) -> str: |
| 235 | return f"Hello {name}!" |
| 236 | |
| 237 | app.interactive_shell(console=console, result_action=custom_handler) |
| 238 | |
| 239 | assert results == ["CUSTOM: Hello Alice!"] |
| 240 | |
| 241 | |
| 242 | def test_interactive_shell_async_command(mocker, console): |
| 243 | """Async commands should be run, not returned as un-awaited coroutines. |
| 244 | |
| 245 | See https://github.com/BrianPugh/cyclopts/issues/826 |
| 246 | """ |
| 247 | app = App(backend="asyncio") |
| 248 | |
| 249 | mocker.patch( |
| 250 | "cyclopts.core.input", |
| 251 | side_effect=[ |
| 252 | "start", |
| 253 | "quit", |
| 254 | ], |
| 255 | ) |
| 256 | |
| 257 | start_called = 0 |
| 258 | |
| 259 | @app.command |
| 260 | async def start(): |
| 261 | nonlocal start_called |
| 262 | start_called += 1 |
| 263 | return "Started!" |
| 264 | |
| 265 | with console.capture() as capture: |
| 266 | app.interactive_shell(console=console) |
| 267 | |
| 268 | actual = capture.get() |
| 269 | |
| 270 | assert start_called == 1 |
| 271 | assert "Started!" in actual |
| 272 | assert "coroutine object" not in actual |
| 273 | |
| 274 | |
| 275 | def test_interactive_shell_no_sys_exit_on_command(app, mocker, console): |
| 276 | """Test that commands continue to execute (no sys.exit called) in interactive shell.""" |
| 277 | mocker.patch( |
| 278 | "cyclopts.core.input", |
| 279 | side_effect=[ |
| 280 | "cmd1", |
| 281 | "cmd2", |
| 282 | "cmd3", |
| 283 | "quit", |
| 284 | ], |
| 285 | ) |
| 286 | |
| 287 | cmd1_called, cmd2_called, cmd3_called = 0, 0, 0 |
| 288 | |
| 289 | @app.command |
| 290 | def cmd1(): |
| 291 | nonlocal cmd1_called |
| 292 | cmd1_called += 1 |
| 293 | return "result1" |
| 294 | |
| 295 | @app.command |
| 296 | def cmd2(): |
| 297 | nonlocal cmd2_called |
| 298 | cmd2_called += 1 |
| 299 | return 0 |
| 300 | |
| 301 | @app.command |
| 302 | def cmd3(): |
| 303 | nonlocal cmd3_called |
| 304 | cmd3_called += 1 |
| 305 | return True |
| 306 | |
| 307 | app.interactive_shell(console=console) |
| 308 | |
| 309 | assert cmd1_called == 1 |
| 310 | assert cmd2_called == 1 |
| 311 | assert cmd3_called == 1 |