Coverage for tests/test_requirements.py: 100%

27 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-11 09:18 +0000

1from pathlib import Path 1abcdefghijklmn

2 

3import pytest 1abcdefghijklmn

4from typer.testing import CliRunner 1abcdefghijklmn

5 

6from fastapi_cli.discover import get_import_data 1abcdefghijklmn

7from fastapi_cli.exceptions import FastAPICLIException 1abcdefghijklmn

8 

9from .utils import changing_dir 1abcdefghijklmn

10 

11runner = CliRunner() 1abcdefghijklmn

12 

13assets_path = Path(__file__).parent / "assets" 1abcdefghijklmn

14 

15 

16def test_no_uvicorn() -> None: 1abcdefghijklmn

17 import uvicorn 1opqrstuvwxyzAB

18 

19 import fastapi_cli.cli 1opqrstuvwxyzAB

20 

21 fastapi_cli.cli.uvicorn = None # type: ignore[attr-defined, assignment] 1opqrstuvwxyzAB

22 

23 with changing_dir(assets_path): 1opqrstuvwxyzAB

24 result = runner.invoke(fastapi_cli.cli.app, ["dev", "single_file_app.py"]) 1opqrstuvwxyzAB

25 assert result.exit_code == 1 1opqrstuvwxyzAB

26 assert result.exception is not None 1opqrstuvwxyzAB

27 assert ( 1opqrstuvwxyzAB

28 "Could not import Uvicorn, try running 'pip install uvicorn'" 

29 in result.exception.args[0] 

30 ) 

31 

32 fastapi_cli.cli.uvicorn = uvicorn # type: ignore[attr-defined] # ty: ignore[invalid-assignment] 1opqrstuvwxyzAB

33 

34 

35def test_no_fastapi() -> None: 1abcdefghijklmn

36 from fastapi import FastAPI 1CDEFGHIJKLMNOP

37 

38 import fastapi_cli.discover 1CDEFGHIJKLMNOP

39 

40 fastapi_cli.discover.FastAPI = None # type: ignore[attr-defined, assignment] 1CDEFGHIJKLMNOP

41 with changing_dir(assets_path): 1CDEFGHIJKLMNOP

42 with pytest.raises(FastAPICLIException) as exc_info: 1CDEFGHIJKLMNOP

43 get_import_data(path=Path("single_file_app.py")) 1CDEFGHIJKLMNOP

44 assert "Could not import FastAPI, try running 'pip install fastapi'" in str( 1CDEFGHIJKLMNOP

45 exc_info.value 

46 ) 

47 

48 fastapi_cli.discover.FastAPI = FastAPI # type: ignore[attr-defined] 1CDEFGHIJKLMNOP