Coverage for tests/test_requirements.py: 100%

27 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-12-05 11:33 +0000

1from pathlib import Path 1abcdefghijkl

2 

3import pytest 1abcdefghijkl

4from typer.testing import CliRunner 1abcdefghijkl

5 

6from fastapi_cli.discover import get_import_data 1abcdefghijkl

7from fastapi_cli.exceptions import FastAPICLIException 1abcdefghijkl

8 

9from .utils import changing_dir 1abcdefghijkl

10 

11runner = CliRunner() 1abcdefghijkl

12 

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

14 

15 

16def test_no_uvicorn() -> None: 1abcdefghijkl

17 import uvicorn 1mnopqrstuvwx

18 

19 import fastapi_cli.cli 1mnopqrstuvwx

20 

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

22 

23 with changing_dir(assets_path): 1mnopqrstuvwx

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

25 assert result.exit_code == 1 1mnopqrstuvwx

26 assert result.exception is not None 1mnopqrstuvwx

27 assert ( 1mnopqrstuvwx

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] 1mnopqrstuvwx

33 

34 

35def test_no_fastapi() -> None: 1abcdefghijkl

36 from fastapi import FastAPI 1yzABCDEFGHIJ

37 

38 import fastapi_cli.discover 1yzABCDEFGHIJ

39 

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

41 with changing_dir(assets_path): 1yzABCDEFGHIJ

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

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

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

45 exc_info.value 

46 ) 

47 

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