Coverage for tests/test_cli.py: 100%
343 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-12-05 11:33 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-12-05 11:33 +0000
1import logging 1abcdefghijkl
2import subprocess 1abcdefghijkl
3import sys 1abcdefghijkl
4from pathlib import Path 1abcdefghijkl
5from unittest.mock import patch 1abcdefghijkl
7import pytest 1abcdefghijkl
8import uvicorn 1abcdefghijkl
9from fastapi.testclient import TestClient 1abcdefghijkl
10from httpx import Response 1abcdefghijkl
11from typer.testing import CliRunner 1abcdefghijkl
12from uvicorn.importer import import_from_string 1abcdefghijkl
14from fastapi_cli.cli import app 1abcdefghijkl
15from fastapi_cli.utils.cli import get_uvicorn_log_config 1abcdefghijkl
16from tests.utils import changing_dir 1abcdefghijkl
18runner = CliRunner() 1abcdefghijkl
20assets_path = Path(__file__).parent / "assets" 1abcdefghijkl
23@pytest.fixture(autouse=True) 1abcdefghijkl
24def isolate_cli_environment(monkeypatch: pytest.MonkeyPatch) -> None: 1abcdefghijkl
25 monkeypatch.delenv("FASTAPI_ENV", raising=False) 1abcdefghijkl
26 monkeypatch.setattr("fastapi_cli.cli.should_use_rich_logs", lambda: True) 2a K hbL RbSbm ` !cM n Tb#c$ctbUbVbN WbXbYb%cb O ibP Zb0bo { 'cQ p 1b(c)cub2b3bR 4b5b6b*cc S jbT 7b8bq | +cU r 9b,c-cvb!b#bV $b%b'b.cd e W kbX (b)bs } /cY t *b:c;cwb+b,bZ -b.b/b=c0 lb1 :b;bu ~ ?c2 v =b@c[cxb?b@b3 [b]b^b]cf 4 mb5 _b`bw ab^c6 x {b_c`cyb|b}b7 ~bacbc{cg 8 nb9 ccdcy bb|c! z ec}c~czbfcgc# hcicjcadh i $ ob% kclcA cbbd' B mccdddAbncoc( pcqcrced) pb* sctcC dbfd+ D ucgdhdBbvcwc, xcyczcidj - qb. AcBcE ebjd/ F CckdldCbDcEc: FcGcHcmdk l ; rb= IcJcG fbnd? H KcodpdDbLcMc@ NcOcPcqd[ sb] QcRcI gbrd^ J ScsdtdEbTcUc_ VcWcXcud
29def test_dev() -> None: 1abcdefghijkl
30 with changing_dir(assets_path): 1KOSW048$)-;[
31 with patch.object(uvicorn, "run") as mock_run: 1KOSW048$)-;[
32 result = runner.invoke(app, ["dev", "single_file_app.py"]) 1KOSW048$)-;[
33 assert result.exit_code == 0, result.output 1KOSW048$)-;[
34 assert mock_run.called 1KOSW048$)-;[
35 assert mock_run.call_args 1KOSW048$)-;[
36 assert mock_run.call_args.kwargs == { 1KOSW048$)-;[
37 "app": "single_file_app:app",
38 "host": "127.0.0.1",
39 "port": 8000,
40 "reload": True,
41 "reload_dirs": None,
42 "workers": None,
43 "root_path": "",
44 "proxy_headers": True,
45 "forwarded_allow_ips": None,
46 "log_config": get_uvicorn_log_config(),
47 }
48 assert "Starting FastAPI in development mode" in result.output 1KOSW048$)-;[
49 assert "Using import string: single_file_app:app" in result.output 1KOSW048$)-;[
50 assert "Server started at http://127.0.0.1:8000" in result.output 1KOSW048$)-;[
51 assert "Documentation at http://127.0.0.1:8000/docs" in result.output 1KOSW048$)-;[
52 # a passed path is explicit, so no source hint and no pyproject nudge
53 assert "(auto-discovered" not in result.output 1KOSW048$)-;[
54 assert "You can configure an entrypoint in pyproject.toml" not in result.output 1KOSW048$)-;[
55 # the step-by-step narration is --verbose only
56 assert "Searching for package file structure" not in result.output 1KOSW048$)-;[
57 assert "Configuration sources:" not in result.output 1KOSW048$)-;[
60def _get_fastapi_env_response(*, command: str, fastapi_env: str | None) -> Response: 1abcdefghijkl
61 module_name = "environment_app" 1LMNPQRTUVXYZ1235679!#%'(*+,./:=?@]^_
62 sys.modules.pop(module_name, None) 1LMNPQRTUVXYZ1235679!#%'(*+,./:=?@]^_
64 try: 1LMNPQRTUVXYZ1235679!#%'(*+,./:=?@]^_
65 with changing_dir(assets_path): 1LMNPQRTUVXYZ1235679!#%'(*+,./:=?@]^_
66 with patch.object(uvicorn, "run") as mock_run: 1LMNPQRTUVXYZ1235679!#%'(*+,./:=?@]^_
67 result = runner.invoke( 1LMNPQRTUVXYZ1235679!#%'(*+,./:=?@]^_
68 app,
69 [command, f"{module_name}.py"],
70 env={"FASTAPI_ENV": fastapi_env},
71 )
73 assert result.exit_code == 0, result.output 1LMNPQRTUVXYZ1235679!#%'(*+,./:=?@]^_
74 assert mock_run.call_args 1LMNPQRTUVXYZ1235679!#%'(*+,./:=?@]^_
75 imported_app = import_from_string(mock_run.call_args.kwargs["app"]) 1LMNPQRTUVXYZ1235679!#%'(*+,./:=?@]^_
76 with TestClient(imported_app) as test_client: 1LMNPQRTUVXYZ1235679!#%'(*+,./:=?@]^_
77 return test_client.get("/fastapi-env") 1LMNPQRTUVXYZ1235679!#%'(*+,./:=?@]^_
78 finally:
79 sys.modules.pop(module_name, None) 1LMNPQRTUVXYZ1235679!#%'(*+,./:=?@]^_
82def test_dev_sets_fastapi_env_before_app_import() -> None: 1abcdefghijkl
83 response = _get_fastapi_env_response(command="dev", fastapi_env=None) 1MQUY26!'+/?^
85 assert response.status_code == 200 1MQUY26!'+/?^
86 assert response.json() == {"fastapi_env": "development"} 1MQUY26!'+/?^
89def test_dev_does_not_override_existing_fastapi_env() -> None: 1abcdefghijkl
90 response = _get_fastapi_env_response(command="dev", fastapi_env="chicken") 1LPTX159%*.=]
92 assert response.status_code == 200 1LPTX159%*.=]
93 assert response.json() == {"fastapi_env": "chicken"} 1LPTX159%*.=]
96def test_run_does_not_set_fastapi_env() -> None: 1abcdefghijkl
97 response = _get_fastapi_env_response(command="run", fastapi_env=None) 1NRVZ37#(,:@_
99 assert response.status_code == 200 1NRVZ37#(,:@_
100 assert response.json() == {"fastapi_env": None} 1NRVZ37#(,:@_
103def test_run_uses_uvicorn_default_log_config_without_rich_logs( 1abcdefghijkl
104 monkeypatch: pytest.MonkeyPatch,
105) -> None:
106 monkeypatch.setattr("fastapi_cli.cli.should_use_rich_logs", lambda: False) 2vdwdxdydzdAdBdCdDdEdFdGd
108 with changing_dir(assets_path): 2vdwdxdydzdAdBdCdDdEdFdGd
109 with patch.object(uvicorn, "run") as mock_run: 2vdwdxdydzdAdBdCdDdEdFdGd
110 result = runner.invoke(app, ["run", "single_file_app.py"]) 2vdwdxdydzdAdBdCdDdEdFdGd
111 assert result.exit_code == 0, result.output 2vdwdxdydzdAdBdCdDdEdFdGd
112 assert mock_run.called 2vdwdxdydzdAdBdCdDdEdFdGd
113 assert mock_run.call_args 2vdwdxdydzdAdBdCdDdEdFdGd
115 assert "log_config" not in mock_run.call_args.kwargs 2vdwdxdydzdAdBdCdDdEdFdGd
118def test_dev_no_args_auto_discovery() -> None: 1abcdefghijkl
119 """Test that auto-discovery works when no args and no pyproject.toml entrypoint"""
120 with changing_dir(assets_path / "default_files" / "default_main"): 1moqsuwyACEGI
121 with patch.object(uvicorn, "run") as mock_run: 1moqsuwyACEGI
122 result = runner.invoke(app, ["dev"]) # No path argument 1moqsuwyACEGI
123 assert result.exit_code == 0, result.output 1moqsuwyACEGI
124 assert mock_run.called 1moqsuwyACEGI
125 assert mock_run.call_args 1moqsuwyACEGI
126 assert mock_run.call_args.kwargs["app"] == "main:app" 1moqsuwyACEGI
127 assert mock_run.call_args.kwargs["host"] == "127.0.0.1" 1moqsuwyACEGI
128 assert mock_run.call_args.kwargs["port"] == 8000 1moqsuwyACEGI
129 assert mock_run.call_args.kwargs["reload"] is True 1moqsuwyACEGI
130 assert "Starting FastAPI in development mode" in result.output 1moqsuwyACEGI
131 assert "Using import string: main:app" in result.output 1moqsuwyACEGI
132 # Rich can wrap the rest of the hint based on terminal and emoji width
133 assert "(auto-discovered, use --verbose" in result.output 1moqsuwyACEGI
134 # auto-discovery nudges you to pin the entrypoint
135 assert "You can configure an entrypoint in pyproject.toml" in result.output 1moqsuwyACEGI
136 assert "[tool.fastapi]" in result.output 1moqsuwyACEGI
137 assert 'entrypoint = "main:app"' in result.output 1moqsuwyACEGI
138 # the full breakdown stays behind --verbose
139 assert "Configuration sources:" not in result.output 1moqsuwyACEGI
142def test_dev_package() -> None: 1abcdefghijkl
143 with changing_dir(assets_path): 2` { | } ~ abbbcbdbebfbgb
144 with patch.object(uvicorn, "run") as mock_run: 2` { | } ~ abbbcbdbebfbgb
145 result = runner.invoke(app, ["dev", "nested_package/package"]) 2` { | } ~ abbbcbdbebfbgb
146 assert result.exit_code == 0, result.output 2` { | } ~ abbbcbdbebfbgb
147 assert mock_run.called 2` { | } ~ abbbcbdbebfbgb
148 assert mock_run.call_args 2` { | } ~ abbbcbdbebfbgb
149 assert mock_run.call_args.kwargs == { 2` { | } ~ abbbcbdbebfbgb
150 "app": "nested_package.package:app",
151 "host": "127.0.0.1",
152 "port": 8000,
153 "reload": True,
154 "reload_dirs": None,
155 "workers": None,
156 "root_path": "",
157 "proxy_headers": True,
158 "forwarded_allow_ips": None,
159 "log_config": get_uvicorn_log_config(),
160 }
161 assert "Starting FastAPI in development mode" in result.output 2` { | } ~ abbbcbdbebfbgb
162 assert "Using import string: nested_package.package:app" in result.output 2` { | } ~ abbbcbdbebfbgb
163 assert "Server started at http://127.0.0.1:8000" in result.output 2` { | } ~ abbbcbdbebfbgb
164 assert "Documentation at http://127.0.0.1:8000/docs" in result.output 2` { | } ~ abbbcbdbebfbgb
165 assert "(auto-discovered" not in result.output 2` { | } ~ abbbcbdbebfbgb
166 assert "You can configure an entrypoint in pyproject.toml" not in result.output 2` { | } ~ abbbcbdbebfbgb
167 # the file tree is --verbose only
168 assert "📁 package" not in result.output 2` { | } ~ abbbcbdbebfbgb
171def test_dev_args() -> None: 1abcdefghijkl
172 with changing_dir(assets_path): 2hbibjbkblbmbnbobpbqbrbsb
173 with patch.object(uvicorn, "run") as mock_run: 2hbibjbkblbmbnbobpbqbrbsb
174 result = runner.invoke( 2hbibjbkblbmbnbobpbqbrbsb
175 app,
176 [
177 "dev",
178 "single_file_app.py",
179 "--host",
180 "192.168.0.2",
181 "--port",
182 "8080",
183 "--no-reload",
184 "--root-path",
185 "/api",
186 "--app",
187 "api",
188 "--no-proxy-headers",
189 ],
190 )
191 assert result.exit_code == 0, result.output 2hbibjbkblbmbnbobpbqbrbsb
192 assert mock_run.called 2hbibjbkblbmbnbobpbqbrbsb
193 assert mock_run.call_args 2hbibjbkblbmbnbobpbqbrbsb
194 assert mock_run.call_args.kwargs == { 2hbibjbkblbmbnbobpbqbrbsb
195 "app": "single_file_app:api",
196 "host": "192.168.0.2",
197 "port": 8080,
198 "reload": False,
199 "reload_dirs": None,
200 "workers": None,
201 "root_path": "/api",
202 "proxy_headers": False,
203 "forwarded_allow_ips": None,
204 "log_config": get_uvicorn_log_config(),
205 }
206 assert "Starting FastAPI in development mode" in result.output 2hbibjbkblbmbnbobpbqbrbsb
207 assert "Using import string: single_file_app:api" in result.output 2hbibjbkblbmbnbobpbqbrbsb
208 assert "Server started at http://192.168.0.2:8080" in result.output 2hbibjbkblbmbnbobpbqbrbsb
209 assert "Documentation at http://192.168.0.2:8080/docs" in result.output 2hbibjbkblbmbnbobpbqbrbsb
210 assert "(auto-discovered" not in result.output 2hbibjbkblbmbnbobpbqbrbsb
211 assert "You can configure an entrypoint in pyproject.toml" not in result.output 2hbibjbkblbmbnbobpbqbrbsb
214def test_dev_env_vars() -> None: 1abcdefghijkl
215 with changing_dir(assets_path): 2RbZb7b(b:b_bcckcscAcIcQc
216 with patch.object(uvicorn, "run") as mock_run: 2RbZb7b(b:b_bcckcscAcIcQc
217 result = runner.invoke( 2RbZb7b(b:b_bcckcscAcIcQc
218 app, ["dev", "single_file_app.py"], env={"PORT": "8111"}
219 )
220 assert result.exit_code == 0, result.output 2RbZb7b(b:b_bcckcscAcIcQc
221 assert mock_run.called 2RbZb7b(b:b_bcckcscAcIcQc
222 assert mock_run.call_args 2RbZb7b(b:b_bcckcscAcIcQc
223 assert mock_run.call_args.kwargs == { 2RbZb7b(b:b_bcckcscAcIcQc
224 "app": "single_file_app:app",
225 "host": "127.0.0.1",
226 "port": 8111,
227 "reload": True,
228 "reload_dirs": None,
229 "workers": None,
230 "root_path": "",
231 "proxy_headers": True,
232 "forwarded_allow_ips": None,
233 "log_config": get_uvicorn_log_config(),
234 }
235 assert "Starting FastAPI in development mode" in result.output 2RbZb7b(b:b_bcckcscAcIcQc
236 assert "Using import string: single_file_app:app" in result.output 2RbZb7b(b:b_bcckcscAcIcQc
237 assert "Server started at http://127.0.0.1:8111" in result.output 2RbZb7b(b:b_bcckcscAcIcQc
238 assert "Documentation at http://127.0.0.1:8111/docs" in result.output 2RbZb7b(b:b_bcckcscAcIcQc
241def test_dev_env_vars_and_args() -> None: 1abcdefghijkl
242 with changing_dir(assets_path): 2Sb0b8b)b;b`bdclctcBcJcRc
243 with patch.object(uvicorn, "run") as mock_run: 2Sb0b8b)b;b`bdclctcBcJcRc
244 result = runner.invoke( 2Sb0b8b)b;b`bdclctcBcJcRc
245 app,
246 [
247 "dev",
248 "single_file_app.py",
249 "--port",
250 "8080",
251 ],
252 env={"PORT": "8111"},
253 )
254 assert result.exit_code == 0, result.output 2Sb0b8b)b;b`bdclctcBcJcRc
255 assert mock_run.called 2Sb0b8b)b;b`bdclctcBcJcRc
256 assert mock_run.call_args 2Sb0b8b)b;b`bdclctcBcJcRc
257 assert mock_run.call_args.kwargs == { 2Sb0b8b)b;b`bdclctcBcJcRc
258 "app": "single_file_app:app",
259 "host": "127.0.0.1",
260 "port": 8080,
261 "reload": True,
262 "reload_dirs": None,
263 "workers": None,
264 "root_path": "",
265 "proxy_headers": True,
266 "forwarded_allow_ips": None,
267 "log_config": get_uvicorn_log_config(),
268 }
269 assert "Starting FastAPI in development mode" in result.output 2Sb0b8b)b;b`bdclctcBcJcRc
270 assert "Using import string: single_file_app:app" in result.output 2Sb0b8b)b;b`bdclctcBcJcRc
271 assert "Server started at http://127.0.0.1:8080" in result.output 2Sb0b8b)b;b`bdclctcBcJcRc
272 assert "Documentation at http://127.0.0.1:8080/docs" in result.output 2Sb0b8b)b;b`bdclctcBcJcRc
275def test_dev_verbose() -> None: 1abcdefghijkl
276 """--verbose restores the full narration and the module file tree."""
277 with changing_dir(assets_path): 1nprtvxzBDFHJ
278 with patch.object(uvicorn, "run") as mock_run: 1nprtvxzBDFHJ
279 result = runner.invoke(app, ["dev", "nested_package/package", "--verbose"]) 1nprtvxzBDFHJ
280 assert result.exit_code == 0, result.output 1nprtvxzBDFHJ
281 assert mock_run.called 1nprtvxzBDFHJ
282 assert "Starting FastAPI in development mode" in result.output 1nprtvxzBDFHJ
283 assert "Searching for package file structure" in result.output 1nprtvxzBDFHJ
284 assert "Importing from" in result.output 1nprtvxzBDFHJ
285 assert "Importing the FastAPI app object" in result.output 1nprtvxzBDFHJ
286 assert "from nested_package.package import app" in result.output 1nprtvxzBDFHJ
287 assert "Using import string: nested_package.package:app" in result.output 1nprtvxzBDFHJ
288 # module and app come from different sources here
289 assert "Configuration sources:" in result.output 1nprtvxzBDFHJ
290 assert "Module: path CLI argument" in result.output 1nprtvxzBDFHJ
291 assert "App name: auto-discovery" in result.output 1nprtvxzBDFHJ
292 # the file tree
293 assert "📁 package" in result.output 1nprtvxzBDFHJ
294 assert "🐍 __init__.py" in result.output 1nprtvxzBDFHJ
295 # the source hint is folded away in verbose mode
296 assert "(auto-discovered, use --verbose to learn more)" not in result.output 1nprtvxzBDFHJ
299def test_dev_verbose_auto_discovery() -> None: 1abcdefghijkl
300 """--verbose with auto-discovery shows the single 'import string' source."""
301 with changing_dir(assets_path / "default_files" / "default_main"): 2Tb1b9b*b=b{becmcucCcKcSc
302 with patch.object(uvicorn, "run") as mock_run: 2Tb1b9b*b=b{becmcucCcKcSc
303 result = runner.invoke(app, ["dev", "--verbose"]) 2Tb1b9b*b=b{becmcucCcKcSc
304 assert result.exit_code == 0, result.output 2Tb1b9b*b=b{becmcucCcKcSc
305 assert mock_run.called 2Tb1b9b*b=b{becmcucCcKcSc
306 assert "Searching for package file structure" in result.output 2Tb1b9b*b=b{becmcucCcKcSc
307 assert "Importing the FastAPI app object" in result.output 2Tb1b9b*b=b{becmcucCcKcSc
308 assert "Using import string: main:app" in result.output 2Tb1b9b*b=b{becmcucCcKcSc
309 assert "Configuration sources:" in result.output 2Tb1b9b*b=b{becmcucCcKcSc
310 # module and app share the same source (auto-discovery)
311 assert "Import string: auto-discovery" in result.output 2Tb1b9b*b=b{becmcucCcKcSc
312 # auto-discovery still nudges to pin the entrypoint
313 assert "You can configure an entrypoint in pyproject.toml" in result.output 2Tb1b9b*b=b{becmcucCcKcSc
316def test_global_verbose_enables_debug_logging() -> None: 1abcdefghijkl
317 with changing_dir(assets_path): 2$c)c-c;c[c`c~cddhdldpdtd
318 with patch.object(uvicorn, "run"): 2$c)c-c;c[c`c~cddhdldpdtd
319 result = runner.invoke(app, ["--verbose", "dev", "single_file_app.py"]) 2$c)c-c;c[c`c~cddhdldpdtd
320 assert result.exit_code == 0, result.output 2$c)c-c;c[c`c~cddhdldpdtd
322 assert logging.getLogger("fastapi_cli").level == logging.DEBUG 2$c)c-c;c[c`c~cddhdldpdtd
324 # a run without --verbose resets the level
325 with changing_dir(assets_path): 2$c)c-c;c[c`c~cddhdldpdtd
326 with patch.object(uvicorn, "run"): 2$c)c-c;c[c`c~cddhdldpdtd
327 result = runner.invoke(app, ["dev", "single_file_app.py"]) 2$c)c-c;c[c`c~cddhdldpdtd
328 assert result.exit_code == 0, result.output 2$c)c-c;c[c`c~cddhdldpdtd
330 assert logging.getLogger("fastapi_cli").level == logging.INFO 2$c)c-c;c[c`c~cddhdldpdtd
333def test_entrypoint_mutually_exclusive_with_path() -> None: 1abcdefghijkl
334 result = runner.invoke(app, ["dev", "mymodule.py", "--entrypoint", "other:app"]) 2*d+d,d-d.d/d:d;d=d?d@d[d
336 assert result.exit_code == 1 2*d+d,d-d.d/d:d;d=d?d@d[d
337 assert ( 2*d+d,d-d.d/d:d;d=d?d@d[d
338 "Cannot use --entrypoint together with path or --app arguments" in result.output
339 )
342def test_entrypoint_mutually_exclusive_with_app() -> None: 1abcdefghijkl
343 result = runner.invoke(app, ["dev", "--app", "myapp", "--entrypoint", "other:app"]) 2]d^d_d`d{d|d}d~daebecede
344 assert result.exit_code == 1 2]d^d_d`d{d|d}d~daebecede
345 assert ( 2]d^d_d`d{d|d}d~daebecede
346 "Cannot use --entrypoint together with path or --app arguments" in result.output
347 )
350def test_run() -> None: 1abcdefghijkl
351 with changing_dir(assets_path): 2Ub2b!b+b?b|bfcncvcDcLcTc
352 with patch.object(uvicorn, "run") as mock_run: 2Ub2b!b+b?b|bfcncvcDcLcTc
353 result = runner.invoke(app, ["run", "single_file_app.py"]) 2Ub2b!b+b?b|bfcncvcDcLcTc
354 assert result.exit_code == 0, result.output 2Ub2b!b+b?b|bfcncvcDcLcTc
355 assert mock_run.called 2Ub2b!b+b?b|bfcncvcDcLcTc
356 assert mock_run.call_args 2Ub2b!b+b?b|bfcncvcDcLcTc
357 assert mock_run.call_args.kwargs == { 2Ub2b!b+b?b|bfcncvcDcLcTc
358 "app": "single_file_app:app",
359 "host": "0.0.0.0",
360 "port": 8000,
361 "reload": False,
362 "reload_dirs": None,
363 "workers": None,
364 "root_path": "",
365 "proxy_headers": True,
366 "forwarded_allow_ips": None,
367 "log_config": get_uvicorn_log_config(),
368 }
369 assert "Starting FastAPI in production mode" in result.output 2Ub2b!b+b?b|bfcncvcDcLcTc
370 assert "Using import string: single_file_app:app" in result.output 2Ub2b!b+b?b|bfcncvcDcLcTc
371 assert "Server started at http://0.0.0.0:8000" in result.output 2Ub2b!b+b?b|bfcncvcDcLcTc
372 assert "Documentation at http://0.0.0.0:8000/docs" in result.output 2Ub2b!b+b?b|bfcncvcDcLcTc
375def test_run_trust_proxy() -> None: 1abcdefghijkl
376 with changing_dir(assets_path): 2Yb6b'b/b^bbcjcrczcHcPcXc
377 with patch.object(uvicorn, "run") as mock_run: 2Yb6b'b/b^bbcjcrczcHcPcXc
378 result = runner.invoke( 2Yb6b'b/b^bbcjcrczcHcPcXc
379 app, ["run", "single_file_app.py", "--forwarded-allow-ips", "*"]
380 )
381 assert result.exit_code == 0, result.output 2Yb6b'b/b^bbcjcrczcHcPcXc
382 assert mock_run.called 2Yb6b'b/b^bbcjcrczcHcPcXc
383 assert mock_run.call_args 2Yb6b'b/b^bbcjcrczcHcPcXc
384 assert mock_run.call_args.kwargs == { 2Yb6b'b/b^bbcjcrczcHcPcXc
385 "app": "single_file_app:app",
386 "host": "0.0.0.0",
387 "port": 8000,
388 "reload": False,
389 "reload_dirs": None,
390 "workers": None,
391 "root_path": "",
392 "proxy_headers": True,
393 "forwarded_allow_ips": "*",
394 "log_config": get_uvicorn_log_config(),
395 }
396 assert "Starting FastAPI in production mode" in result.output 2Yb6b'b/b^bbcjcrczcHcPcXc
397 assert "Using import string: single_file_app:app" in result.output 2Yb6b'b/b^bbcjcrczcHcPcXc
398 assert "Server started at http://0.0.0.0:8000" in result.output 2Yb6b'b/b^bbcjcrczcHcPcXc
399 assert "Documentation at http://0.0.0.0:8000/docs" in result.output 2Yb6b'b/b^bbcjcrczcHcPcXc
402def test_run_args() -> None: 1abcdefghijkl
403 with changing_dir(assets_path): 2Vb3b#b,b@b}bgcocwcEcMcUc
404 with patch.object(uvicorn, "run") as mock_run: 2Vb3b#b,b@b}bgcocwcEcMcUc
405 result = runner.invoke( 2Vb3b#b,b@b}bgcocwcEcMcUc
406 app,
407 [
408 "run",
409 "single_file_app.py",
410 "--host",
411 "192.168.0.2",
412 "--port",
413 "8080",
414 "--no-reload",
415 "--workers",
416 "2",
417 "--root-path",
418 "/api",
419 "--app",
420 "api",
421 "--no-proxy-headers",
422 ],
423 )
424 assert result.exit_code == 0, result.output 2Vb3b#b,b@b}bgcocwcEcMcUc
425 assert mock_run.called 2Vb3b#b,b@b}bgcocwcEcMcUc
426 assert mock_run.call_args 2Vb3b#b,b@b}bgcocwcEcMcUc
427 assert mock_run.call_args.kwargs == { 2Vb3b#b,b@b}bgcocwcEcMcUc
428 "app": "single_file_app:api",
429 "host": "192.168.0.2",
430 "port": 8080,
431 "reload": False,
432 "reload_dirs": None,
433 "workers": 2,
434 "root_path": "/api",
435 "proxy_headers": False,
436 "forwarded_allow_ips": None,
437 "log_config": get_uvicorn_log_config(),
438 }
440 assert "Starting FastAPI in production mode" in result.output 2Vb3b#b,b@b}bgcocwcEcMcUc
441 assert "Using import string: single_file_app:api" in result.output 2Vb3b#b,b@b}bgcocwcEcMcUc
442 assert "Server started at http://192.168.0.2:8080" in result.output 2Vb3b#b,b@b}bgcocwcEcMcUc
443 assert "Documentation at http://192.168.0.2:8080/docs" in result.output 2Vb3b#b,b@b}bgcocwcEcMcUc
446def test_run_env_vars() -> None: 1abcdefghijkl
447 with changing_dir(assets_path): 2Wb4b$b-b[b~bhcpcxcFcNcVc
448 with patch.object(uvicorn, "run") as mock_run: 2Wb4b$b-b[b~bhcpcxcFcNcVc
449 result = runner.invoke( 2Wb4b$b-b[b~bhcpcxcFcNcVc
450 app, ["run", "single_file_app.py"], env={"PORT": "8111"}
451 )
452 assert result.exit_code == 0, result.output 2Wb4b$b-b[b~bhcpcxcFcNcVc
453 assert mock_run.called 2Wb4b$b-b[b~bhcpcxcFcNcVc
454 assert mock_run.call_args 2Wb4b$b-b[b~bhcpcxcFcNcVc
455 assert mock_run.call_args.kwargs == { 2Wb4b$b-b[b~bhcpcxcFcNcVc
456 "app": "single_file_app:app",
457 "host": "0.0.0.0",
458 "port": 8111,
459 "reload": False,
460 "reload_dirs": None,
461 "workers": None,
462 "root_path": "",
463 "proxy_headers": True,
464 "forwarded_allow_ips": None,
465 "log_config": get_uvicorn_log_config(),
466 }
467 assert "Starting FastAPI in production mode" in result.output 2Wb4b$b-b[b~bhcpcxcFcNcVc
468 assert "Using import string: single_file_app:app" in result.output 2Wb4b$b-b[b~bhcpcxcFcNcVc
469 assert "Server started at http://0.0.0.0:8111" in result.output 2Wb4b$b-b[b~bhcpcxcFcNcVc
470 assert "Documentation at http://0.0.0.0:8111/docs" in result.output 2Wb4b$b-b[b~bhcpcxcFcNcVc
473def test_run_env_vars_and_args() -> None: 1abcdefghijkl
474 with changing_dir(assets_path): 2Xb5b%b.b]bacicqcycGcOcWc
475 with patch.object(uvicorn, "run") as mock_run: 2Xb5b%b.b]bacicqcycGcOcWc
476 result = runner.invoke( 2Xb5b%b.b]bacicqcycGcOcWc
477 app,
478 [
479 "run",
480 "single_file_app.py",
481 "--port",
482 "8080",
483 ],
484 env={"PORT": "8111"},
485 )
486 assert result.exit_code == 0, result.output 2Xb5b%b.b]bacicqcycGcOcWc
487 assert mock_run.called 2Xb5b%b.b]bacicqcycGcOcWc
488 assert mock_run.call_args 2Xb5b%b.b]bacicqcycGcOcWc
489 assert mock_run.call_args.kwargs == { 2Xb5b%b.b]bacicqcycGcOcWc
490 "app": "single_file_app:app",
491 "host": "0.0.0.0",
492 "port": 8080,
493 "reload": False,
494 "reload_dirs": None,
495 "workers": None,
496 "root_path": "",
497 "proxy_headers": True,
498 "forwarded_allow_ips": None,
499 "log_config": get_uvicorn_log_config(),
500 }
501 assert "Starting FastAPI in production mode" in result.output 2Xb5b%b.b]bacicqcycGcOcWc
502 assert "Using import string: single_file_app:app" in result.output 2Xb5b%b.b]bacicqcycGcOcWc
503 assert "Server started at http://0.0.0.0:8080" in result.output 2Xb5b%b.b]bacicqcycGcOcWc
504 assert "Documentation at http://0.0.0.0:8080/docs" in result.output 2Xb5b%b.b]bacicqcycGcOcWc
507@pytest.mark.parametrize("command", ["dev", "run"]) 1abcdefghijkl
508@pytest.mark.parametrize( 1abcdefghijkl
509 "public_url",
510 [
511 "https://myapp.example.com",
512 "https://myapp.example.com/",
513 "https://myapp.example.com/subpath/",
514 ],
515)
516def test_public_url_env_var(command: str, public_url: str) -> None: 1abcdefghijkl
517 with changing_dir(assets_path): 2tbubvbwbxbybzbAbBbCbDbEb
518 with patch.object(uvicorn, "run") as mock_run: 2tbubvbwbxbybzbAbBbCbDbEb
519 result = runner.invoke( 2tbubvbwbxbybzbAbBbCbDbEb
520 app,
521 [
522 command,
523 "single_file_app.py",
524 "--host",
525 "0.0.0.0",
526 ],
527 env={"FASTAPI_PUBLIC_URL": public_url},
528 )
529 assert result.exit_code == 0, result.output 2tbubvbwbxbybzbAbBbCbDbEb
530 assert mock_run.called 2tbubvbwbxbybzbAbBbCbDbEb
531 assert mock_run.call_args 2tbubvbwbxbybzbAbBbCbDbEb
532 assert mock_run.call_args.kwargs == { 2tbubvbwbxbybzbAbBbCbDbEb
533 "app": "single_file_app:app",
534 "host": "0.0.0.0",
535 "port": 8000,
536 "reload": True if command == "dev" else False,
537 "reload_dirs": None,
538 "workers": None,
539 "root_path": "",
540 "proxy_headers": True,
541 "forwarded_allow_ips": None,
542 "log_config": get_uvicorn_log_config(),
543 }
545 assert "Using import string: single_file_app:app" in result.output 2tbubvbwbxbybzbAbBbCbDbEb
546 assert ( 2tbubvbwbxbybzbAbBbCbDbEb
547 f"Starting FastAPI in {'development' if command == 'dev' else 'production'} mode"
548 in result.output
549 )
550 expected_url_base = public_url.rstrip("/") 2tbubvbwbxbybzbAbBbCbDbEb
551 assert f"Server started at {expected_url_base}" in result.output 2tbubvbwbxbybzbAbBbCbDbEb
552 assert f"Documentation at {expected_url_base}/docs" in result.output 2tbubvbwbxbybzbAbBbCbDbEb
555def test_run_error() -> None: 1abcdefghijkl
556 with changing_dir(assets_path): 2HdIdJdKdLdMdNdOdPdQdRdSd
557 result = runner.invoke(app, ["run", "non_existing_file.py"]) 2HdIdJdKdLdMdNdOdPdQdRdSd
558 assert result.exit_code == 1, result.output 2HdIdJdKdLdMdNdOdPdQdRdSd
559 assert "Path does not exist non_existing_file.py" in result.output 2HdIdJdKdLdMdNdOdPdQdRdSd
562def test_dev_help() -> None: 1abcdefghijkl
563 result = runner.invoke(app, ["dev", "--help"]) 2FbGbHbIbJbKbLbMbNbObPbQb
564 assert result.exit_code == 0, result.output 2FbGbHbIbJbKbLbMbNbObPbQb
565 assert "Run a FastAPI app in development mode." in result.output 2FbGbHbIbJbKbLbMbNbObPbQb
566 assert ( 2FbGbHbIbJbKbLbMbNbObPbQb
567 "This is equivalent to fastapi run but with reload enabled and listening on the 127.0.0.1 address."
568 in result.output
569 )
570 assert ( 2FbGbHbIbJbKbLbMbNbObPbQb
571 "Otherwise, it uses the first FastAPI app found in the imported module or package."
572 in result.output
573 )
574 assert "A path to a Python file or package directory" in result.output 2FbGbHbIbJbKbLbMbNbObPbQb
575 assert "The host to serve on." in result.output 2FbGbHbIbJbKbLbMbNbObPbQb
576 assert "The port to serve on." in result.output 2FbGbHbIbJbKbLbMbNbObPbQb
577 assert "Enable auto-reload of the server when (code) files change." in result.output 2FbGbHbIbJbKbLbMbNbObPbQb
578 assert "Set reload directories explicitly" in result.output 2FbGbHbIbJbKbLbMbNbObPbQb
579 assert "The root path is used to tell your app" in result.output 2FbGbHbIbJbKbLbMbNbObPbQb
580 assert "The name of the variable that contains the FastAPI app" in result.output 2FbGbHbIbJbKbLbMbNbObPbQb
581 assert "Use multiple worker processes." not in result.output 2FbGbHbIbJbKbLbMbNbObPbQb
584def test_run_help() -> None: 1abcdefghijkl
585 result = runner.invoke(app, ["run", "--help"]) 2YcZc0c1c2c3c4c5c6c7c8c9c
586 assert result.exit_code == 0, result.output 2YcZc0c1c2c3c4c5c6c7c8c9c
587 assert "Run a FastAPI app in production mode." in result.output 2YcZc0c1c2c3c4c5c6c7c8c9c
588 assert ( 2YcZc0c1c2c3c4c5c6c7c8c9c
589 "This is equivalent to fastapi dev but with reload disabled and listening on the 0.0.0.0 address."
590 in result.output
591 )
592 assert ( 2YcZc0c1c2c3c4c5c6c7c8c9c
593 "Otherwise, it uses the first FastAPI app found in the imported module or package."
594 in result.output
595 )
596 assert "A path to a Python file or package directory" in result.output 2YcZc0c1c2c3c4c5c6c7c8c9c
597 assert "The host to serve on." in result.output 2YcZc0c1c2c3c4c5c6c7c8c9c
598 assert "The port to serve on." in result.output 2YcZc0c1c2c3c4c5c6c7c8c9c
599 assert "Enable auto-reload of the server when (code) files change." in result.output 2YcZc0c1c2c3c4c5c6c7c8c9c
600 assert "The root path is used to tell your app" in result.output 2YcZc0c1c2c3c4c5c6c7c8c9c
601 assert "The name of the variable that contains the FastAPI app" in result.output 2YcZc0c1c2c3c4c5c6c7c8c9c
602 assert "Use multiple worker processes." in result.output 2YcZc0c1c2c3c4c5c6c7c8c9c
605def test_callback_help() -> None: 1abcdefghijkl
606 result = runner.invoke(app, ["--help"]) 2TdUdVdWdXdYdZd0d1d2d3d4d
607 assert result.exit_code == 0, result.output 2TdUdVdWdXdYdZd0d1d2d3d4d
608 assert "FastAPI CLI - The fastapi command line app." in result.output 2TdUdVdWdXdYdZd0d1d2d3d4d
609 assert "Show the version and exit." in result.output 2TdUdVdWdXdYdZd0d1d2d3d4d
612def test_version() -> None: 1abcdefghijkl
613 result = runner.invoke(app, ["--version"]) 25d6d7d8d9d!d#d$d%d'd(d)d
614 assert result.exit_code == 0, result.output 25d6d7d8d9d!d#d$d%d'd(d)d
615 assert "FastAPI CLI version:" in result.output 25d6d7d8d9d!d#d$d%d'd(d)d
616 assert "FastAPI Cloud CLI version:" in result.output 25d6d7d8d9d!d#d$d%d'd(d)d
619def test_dev_reload_dir() -> None: 1abcdefghijkl
620 with changing_dir(assets_path): 2!c'c+c/c?c^c|cbdfdjdndrd
621 with patch.object(uvicorn, "run") as mock_run: 2!c'c+c/c?c^c|cbdfdjdndrd
622 result = runner.invoke( 2!c'c+c/c?c^c|cbdfdjdndrd
623 app,
624 [
625 "dev",
626 "single_file_app.py",
627 "--reload-dir",
628 "src",
629 "--reload-dir",
630 "lib",
631 ],
632 )
633 assert result.exit_code == 0, result.output 2!c'c+c/c?c^c|cbdfdjdndrd
634 assert mock_run.called 2!c'c+c/c?c^c|cbdfdjdndrd
635 assert mock_run.call_args 2!c'c+c/c?c^c|cbdfdjdndrd
636 # Paths are resolved to absolute paths
637 reload_dirs = mock_run.call_args.kwargs["reload_dirs"] 2!c'c+c/c?c^c|cbdfdjdndrd
638 assert len(reload_dirs) == 2 2!c'c+c/c?c^c|cbdfdjdndrd
639 assert reload_dirs[0] == str((assets_path / "src").resolve()) 2!c'c+c/c?c^c|cbdfdjdndrd
640 assert reload_dirs[1] == str((assets_path / "lib").resolve()) 2!c'c+c/c?c^c|cbdfdjdndrd
643def test_dev_with_import_string() -> None: 1abcdefghijkl
644 with changing_dir(assets_path): 2#c(c,c:c@c_c}ccdgdkdodsd
645 with patch.object(uvicorn, "run") as mock_run: 2#c(c,c:c@c_c}ccdgdkdodsd
646 result = runner.invoke(app, ["dev", "--entrypoint", "single_file_app:api"]) 2#c(c,c:c@c_c}ccdgdkdodsd
647 assert result.exit_code == 0, result.output 2#c(c,c:c@c_c}ccdgdkdodsd
648 assert mock_run.called 2#c(c,c:c@c_c}ccdgdkdodsd
649 assert mock_run.call_args 2#c(c,c:c@c_c}ccdgdkdodsd
650 assert mock_run.call_args.kwargs == { 2#c(c,c:c@c_c}ccdgdkdodsd
651 "app": "single_file_app:api",
652 "forwarded_allow_ips": None,
653 "host": "127.0.0.1",
654 "port": 8000,
655 "reload": True,
656 "reload_dirs": None,
657 "workers": None,
658 "root_path": "",
659 "proxy_headers": True,
660 "log_config": get_uvicorn_log_config(),
661 }
662 assert "Using import string: single_file_app:api" in result.output 2#c(c,c:c@c_c}ccdgdkdodsd
663 # an explicit entrypoint is not auto-discovery
664 assert "(auto-discovered" not in result.output 2#c(c,c:c@c_c}ccdgdkdodsd
665 assert "You can configure an entrypoint in pyproject.toml" not in result.output 2#c(c,c:c@c_c}ccdgdkdodsd
668def test_run_with_import_string() -> None: 1abcdefghijkl
669 with changing_dir(assets_path): 2%c*c.c=c]c{cadedidmdqdud
670 with patch.object(uvicorn, "run") as mock_run: 2%c*c.c=c]c{cadedidmdqdud
671 result = runner.invoke(app, ["run", "--entrypoint", "single_file_app:app"]) 2%c*c.c=c]c{cadedidmdqdud
672 assert result.exit_code == 0, result.output 2%c*c.c=c]c{cadedidmdqdud
673 assert mock_run.called 2%c*c.c=c]c{cadedidmdqdud
674 assert mock_run.call_args 2%c*c.c=c]c{cadedidmdqdud
675 assert mock_run.call_args.kwargs == { 2%c*c.c=c]c{cadedidmdqdud
676 "app": "single_file_app:app",
677 "forwarded_allow_ips": None,
678 "host": "0.0.0.0",
679 "port": 8000,
680 "reload": False,
681 "reload_dirs": None,
682 "workers": None,
683 "root_path": "",
684 "proxy_headers": True,
685 "log_config": get_uvicorn_log_config(),
686 }
687 assert "Using import string: single_file_app:app" in result.output 2%c*c.c=c]c{cadedidmdqdud
688 assert "(auto-discovered" not in result.output 2%c*c.c=c]c{cadedidmdqdud
689 assert "You can configure an entrypoint in pyproject.toml" not in result.output 2%c*c.c=c]c{cadedidmdqdud
692def test_script() -> None: 1abcdefghijkl
693 result = subprocess.run( 2eefegeheiejekelemeneoepe
694 [sys.executable, "-m", "coverage", "run", "-m", "fastapi_cli", "--help"],
695 capture_output=True,
696 encoding="utf-8",
697 )
698 assert "Usage" in result.stdout 2eefegeheiejekelemeneoepe