Coverage for tests / test_tutorial / test_options / test_version / test_tutorial001.py: 100%

35 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("tutorial001_py39"), 

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

21 pytest.param("tutorial001_an_py39"), 

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

23 ], 

24) 

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

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

27 mod = importlib.import_module(module_name) 1abcdefgh

28 return mod 1abcdefgh

29 

30 

31def test_version(mod: ModuleType): 1abcdefgh

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

33 assert result.exit_code == 0 1abcdefgh

34 assert "Awesome CLI Version: 0.1.0" in result.output 1abcdefgh

35 

36 

37def test_version_with_name(mod: ModuleType): 1abcdefgh

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

39 assert result.exit_code == 0 1abcdefgh

40 assert "Awesome CLI Version: 0.1.0" in result.output 1abcdefgh

41 assert "Hello" not in result.output 1abcdefgh

42 

43 

44def test_no_version(mod: ModuleType): 1abcdefgh

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

46 assert result.exit_code == 0 1abcdefgh

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

48 

49 

50def test_script(mod: ModuleType): 1abcdefgh

51 result = subprocess.run( 1abcdefgh

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

53 capture_output=True, 

54 encoding="utf-8", 

55 ) 

56 assert "Usage" in result.stdout 1abcdefgh

57 

58 

59def test_completion(mod: ModuleType): 1abcdefgh

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

61 result = subprocess.run( 1abcdefgh

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

63 capture_output=True, 

64 encoding="utf-8", 

65 env={ 

66 **os.environ, 

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

68 "COMP_WORDS": f"{file_name} --name Rick --v", 

69 "COMP_CWORD": "3", 

70 }, 

71 ) 

72 assert "--version" in result.stdout 1abcdefgh