| 1 |
import os |
| 2 |
import subprocess |
| 3 |
import sys |
| 4 |
from unittest.mock import patch |
| 5 |
|
| 6 |
import fastapi.cli |
| 7 |
import pytest |
| 8 |
|
| 9 |
|
| 10 |
def test_fastapi_cli(): |
| 11 |
result = subprocess.run( |
| 12 |
[ |
| 13 |
sys.executable, |
| 14 |
"-m", |
| 15 |
"coverage", |
| 16 |
"run", |
| 17 |
"-m", |
| 18 |
"fastapi", |
| 19 |
"dev", |
| 20 |
"non_existent_file.py", |
| 21 |
], |
| 22 |
capture_output=True, |
| 23 |
encoding="utf-8", |
| 24 |
env={**os.environ, "PYTHONIOENCODING": "utf-8"}, |
| 25 |
) |
| 26 |
assert result.returncode == 1, result.stdout |
| 27 |
assert "Path does not exist non_existent_file.py" in result.stdout |
| 28 |
|
| 29 |
|
| 30 |
def test_fastapi_cli_not_installed(): |
| 31 |
with patch.object(fastapi.cli, "cli_main", None): |
| 32 |
with pytest.raises(RuntimeError) as exc_info: |
| 33 |
fastapi.cli.main() |
| 34 |
assert "To use the fastapi command, please install" in str(exc_info.value) |