Coverage for tests/test_cli_pyproject.py: 100%
62 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
1from pathlib import Path 1abcdefghijkl
2from unittest.mock import patch 1abcdefghijkl
4import uvicorn 1abcdefghijkl
5from typer.testing import CliRunner 1abcdefghijkl
7from fastapi_cli.cli import app 1abcdefghijkl
8from tests.utils import changing_dir 1abcdefghijkl
10runner = CliRunner() 1abcdefghijkl
12assets_path = Path(__file__).parent / "assets" 1abcdefghijkl
15def test_dev_with_pyproject_app_config_uses() -> None: 1abcdefghijkl
16 with ( 1KL
17 changing_dir(assets_path / "pyproject_config"),
18 patch.object(uvicorn, "run") as mock_run,
19 ):
20 result = runner.invoke(app, ["dev"]) 1KOPQRLSTUVWX
21 assert result.exit_code == 0, result.output 1KOPQRLSTUVWX
23 assert mock_run.call_args.kwargs["app"] == "my_module:app" 1KOPQRLSTUVWX
24 assert mock_run.call_args.kwargs["host"] == "127.0.0.1" 1KOPQRLSTUVWX
25 assert mock_run.call_args.kwargs["port"] == 8000 1KOPQRLSTUVWX
26 assert mock_run.call_args.kwargs["reload"] is True 1KOPQRLSTUVWX
28 assert "Using import string: my_module:app" in result.output 1KOPQRLSTUVWX
29 # a configured entrypoint is not auto-discovery
30 assert "(auto-discovered" not in result.output 1KOPQRLSTUVWX
31 assert "Configuration sources:" not in result.output 1KOPQRLSTUVWX
34def test_run_with_pyproject_app_config() -> None: 1abcdefghijkl
35 with ( 1MN
36 changing_dir(assets_path / "pyproject_config"),
37 patch.object(uvicorn, "run") as mock_run,
38 ):
39 result = runner.invoke(app, ["run"]) 1MYZ01N234567
40 assert result.exit_code == 0, result.output 1MYZ01N234567
42 assert mock_run.call_args.kwargs["app"] == "my_module:app" 1MYZ01N234567
43 assert mock_run.call_args.kwargs["host"] == "0.0.0.0" 1MYZ01N234567
44 assert mock_run.call_args.kwargs["port"] == 8000 1MYZ01N234567
45 assert mock_run.call_args.kwargs["reload"] is False 1MYZ01N234567
47 assert "Using import string: my_module:app" in result.output 1MYZ01N234567
48 assert "(auto-discovered" not in result.output 1MYZ01N234567
49 assert "Configuration sources:" not in result.output 1MYZ01N234567
52def test_cli_arg_overrides_pyproject_config() -> None: 1abcdefghijkl
53 with ( 189
54 changing_dir(assets_path / "pyproject_config"),
55 patch.object(uvicorn, "run") as mock_run,
56 ):
57 result = runner.invoke(app, ["dev", "another_module.py"]) 18!#$%9'()*+,
59 assert result.exit_code == 0, result.output 18!#$%9'()*+,
61 assert mock_run.call_args.kwargs["app"] == "another_module:app" 18!#$%9'()*+,
62 assert "Using import string: another_module:app" in result.output 18!#$%9'()*+,
63 assert "Configuration sources:" not in result.output 18!#$%9'()*+,
66def test_pyproject_app_config_invalid_format() -> None: 1abcdefghijkl
67 test_dir = assets_path / "pyproject_invalid_config" 1yzABCDEFGHIJ
68 test_dir.mkdir(exist_ok=True) 1yzABCDEFGHIJ
70 pyproject_file = test_dir / "pyproject.toml" 1yzABCDEFGHIJ
71 pyproject_file.write_text(""" 1yzABCDEFGHIJ
72[tool.fastapi]
73entrypoint = "invalid_format_without_colon"
74""")
76 try: 1yzABCDEFGHIJ
77 with changing_dir(test_dir): 1yzABCDEFGHIJ
78 result = runner.invoke(app, ["dev"]) 1yzABCDEFGHIJ
79 assert result.exit_code == 1 1yzABCDEFGHIJ
80 assert ( 1yzABCDEFGHIJ
81 "Import string must be in the format module.submodule:app_name"
82 in result.output
83 )
84 finally:
85 pyproject_file.unlink() 1yzABCDEFGHIJ
86 test_dir.rmdir() 1yzABCDEFGHIJ
89def test_pyproject_validation_error() -> None: 1abcdefghijkl
90 test_dir = assets_path / "pyproject_validation_error" 1mnopqrstuvwx
91 test_dir.mkdir(exist_ok=True) 1mnopqrstuvwx
93 pyproject_file = test_dir / "pyproject.toml" 1mnopqrstuvwx
94 pyproject_file.write_text(""" 1mnopqrstuvwx
95[tool.fastapi]
96entrypoint = 123
97""")
99 try: 1mnopqrstuvwx
100 with changing_dir(test_dir): 1mnopqrstuvwx
101 result = runner.invoke(app, ["dev"]) 1mnopqrstuvwx
102 assert result.exit_code == 1 1mnopqrstuvwx
103 assert "Invalid configuration in pyproject.toml:" in result.output 1mnopqrstuvwx
104 assert "entrypoint" in result.output.lower() 1mnopqrstuvwx
105 finally:
106 pyproject_file.unlink() 1mnopqrstuvwx
107 test_dir.rmdir() 1mnopqrstuvwx