2025-12-02 09:32:38 +05:30
|
|
|
import os
|
2024-08-02 01:03:05 -05:00
|
|
|
import subprocess
|
|
|
|
|
import sys
|
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
|
|
import fastapi.cli
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_fastapi_cli():
|
|
|
|
|
result = subprocess.run(
|
|
|
|
|
[
|
|
|
|
|
sys.executable,
|
|
|
|
|
"-m",
|
|
|
|
|
"coverage",
|
|
|
|
|
"run",
|
|
|
|
|
"-m",
|
|
|
|
|
"fastapi",
|
|
|
|
|
"dev",
|
|
|
|
|
"non_existent_file.py",
|
|
|
|
|
],
|
|
|
|
|
capture_output=True,
|
|
|
|
|
encoding="utf-8",
|
2025-12-02 09:32:38 +05:30
|
|
|
env={**os.environ, "PYTHONIOENCODING": "utf-8"},
|
2024-08-02 01:03:05 -05:00
|
|
|
)
|
|
|
|
|
assert result.returncode == 1, result.stdout
|
2024-12-04 15:38:39 +01:00
|
|
|
assert "Path does not exist non_existent_file.py" in result.stdout
|
2024-08-02 01:03:05 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_fastapi_cli_not_installed():
|
|
|
|
|
with patch.object(fastapi.cli, "cli_main", None):
|
|
|
|
|
with pytest.raises(RuntimeError) as exc_info:
|
|
|
|
|
fastapi.cli.main()
|
|
|
|
|
assert "To use the fastapi command, please install" in str(exc_info.value)
|