Coverage for tests / test_tutorial / test_options / test_callback / test_tutorial004.py: 100%

32 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-02-09 12:36 +0000

1import importlib 1abcdefgh

2import os 1abcdefgh

3import subprocess 1abcdefgh

4import sys 1abcdefgh

5from pathlib import Path 1abcdefgh

6from types import ModuleType 1abcdefgh

7 

8import pytest 1abcdefgh

9from typer.testing import CliRunner 1abcdefgh

10 

11from ....utils import needs_py310 1abcdefgh

12 

13runner = CliRunner() 1abcdefgh

14 

15 

16@pytest.fixture( 1abcdefgh

17 name="mod", 

18 params=[ 

19 pytest.param("tutorial004_py39"), 

20 pytest.param("tutorial004_py310", marks=needs_py310), 

21 pytest.param("tutorial004_an_py39"), 

22 pytest.param("tutorial004_an_py310", marks=needs_py310), 

23 ], 

24) 

25def get_mod(request: pytest.FixtureRequest) -> ModuleType: 1abcdefgh

26 module_name = f"docs_src.options.callback.{request.param}" 1abcdefgh

27 mod = importlib.import_module(module_name) 1abcdefgh

28 return mod 1abcdefgh

29 

30 

31def test_1(mod: ModuleType): 1abcdefgh

32 result = runner.invoke(mod.app, ["--name", "Camila"]) 1abcdefgh

33 assert result.exit_code == 0 1abcdefgh

34 assert "Validating param: name" in result.output 1abcdefgh

35 assert "Hello Camila" in result.output 1abcdefgh

36 

37 

38def test_2(mod: ModuleType): 1abcdefgh

39 result = runner.invoke(mod.app, ["--name", "rick"]) 1abcdefgh

40 assert result.exit_code != 0 1abcdefgh

41 assert "Invalid value for '--name'" in result.output 1abcdefgh

42 assert "Only Camila is allowed" in result.output 1abcdefgh

43 

44 

45def test_script(mod: ModuleType): 1abcdefgh

46 result = subprocess.run( 1abcdefgh

47 [sys.executable, "-m", "coverage", "run", mod.__file__, "--help"], 

48 capture_output=True, 

49 encoding="utf-8", 

50 ) 

51 assert "Usage" in result.stdout 1abcdefgh

52 

53 

54def test_completion(mod: ModuleType): 1abcdefgh

55 file_name = Path(mod.__file__).name 1abcdefgh

56 result = subprocess.run( 1abcdefgh

57 [sys.executable, "-m", "coverage", "run", mod.__file__, " "], 

58 capture_output=True, 

59 encoding="utf-8", 

60 env={ 

61 **os.environ, 

62 f"_{file_name.upper()}_COMPLETE": "complete_bash", 

63 "COMP_WORDS": f"{file_name} --", 

64 "COMP_CWORD": "1", 

65 }, 

66 ) 

67 assert "--name" in result.stdout 1abcdefgh