Coverage for tests / test_tutorial / test_options_autocompletion / test_tutorial003.py: 100%

33 statements  

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

1import importlib 1abcdefghij

2import os 1abcdefghij

3import subprocess 1abcdefghij

4import sys 1abcdefghij

5from pathlib import Path 1abcdefghij

6from types import ModuleType 1abcdefghij

7 

8import pytest 1abcdefghij

9from typer.testing import CliRunner 1abcdefghij

10 

11runner = CliRunner() 1abcdefghij

12 

13 

14@pytest.fixture( 1abcdefghij

15 name="mod", 

16 params=[ 

17 pytest.param("tutorial003_non_an"), 

18 pytest.param("tutorial003"), 

19 ], 

20) 

21def get_mod(request: pytest.FixtureRequest) -> ModuleType: 1abcdefghij

22 module_name = f"docs_src.options_autocompletion.{request.param}" 1abcdefghij

23 mod = importlib.import_module(module_name) 1abcdefghij

24 return mod 1abcdefghij

25 

26 

27def test_completion_zsh(mod: ModuleType): 1abcdefghij

28 file_name = Path(mod.__file__).name 1abcdefghij

29 result = subprocess.run( 1abcdefghij

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

31 capture_output=True, 

32 encoding="utf-8", 

33 env={ 

34 **os.environ, 

35 f"_{file_name.upper()}_COMPLETE": "complete_zsh", 

36 "_TYPER_COMPLETE_ARGS": f"{file_name} --name Seb", 

37 }, 

38 ) 

39 assert "Camila" not in result.stdout 1abcdefghij

40 assert "Carlos" not in result.stdout 1abcdefghij

41 assert "Sebastian" in result.stdout 1abcdefghij

42 

43 

44def test_completion_powershell(mod: ModuleType): 1abcdefghij

45 file_name = Path(mod.__file__).name 1abcdefghij

46 result = subprocess.run( 1abcdefghij

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

48 capture_output=True, 

49 encoding="utf-8", 

50 env={ 

51 **os.environ, 

52 f"_{file_name.upper()}_COMPLETE": "complete_powershell", 

53 "_TYPER_COMPLETE_ARGS": f"{file_name} --name Seb", 

54 "_TYPER_COMPLETE_WORD_TO_COMPLETE": "Seb", 

55 }, 

56 ) 

57 assert "Camila" not in result.stdout 1abcdefghij

58 assert "Carlos" not in result.stdout 1abcdefghij

59 assert "Sebastian" in result.stdout 1abcdefghij

60 

61 

62def test_1(mod: ModuleType): 1abcdefghij

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

64 assert result.exit_code == 0 1abcdefghij

65 assert "Hello Camila" in result.output 1abcdefghij

66 

67 

68def test_script(mod: ModuleType): 1abcdefghij

69 result = subprocess.run( 1abcdefghij

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

71 capture_output=True, 

72 encoding="utf-8", 

73 ) 

74 assert "Usage" in result.stdout 1abcdefghij