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

27 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-09 18:26 +0000

1import os 1abcdefgh

2import subprocess 1abcdefgh

3import sys 1abcdefgh

4 

5import typer 1abcdefgh

6from typer.testing import CliRunner 1abcdefgh

7 

8from docs_src.options.version import tutorial003 as mod 1abcdefgh

9 

10runner = CliRunner() 1abcdefgh

11 

12app = typer.Typer() 1abcdefgh

13app.command()(mod.main) 1abcdefgh

14 

15 

16def test_1(): 1abcdefgh

17 result = runner.invoke(app, ["--name", "Rick", "--version"]) 1abcdefgh

18 assert result.exit_code == 0 1abcdefgh

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

20 

21 

22def test_2(): 1abcdefgh

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

24 assert result.exit_code != 0 1abcdefgh

25 assert "Invalid value for '--name': Only Camila is allowed" in result.output 1abcdefgh

26 

27 

28def test_3(): 1abcdefgh

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

30 assert result.exit_code == 0 1abcdefgh

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

32 

33 

34def test_script(): 1abcdefgh

35 result = subprocess.run( 1abcdefgh

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

37 capture_output=True, 

38 encoding="utf-8", 

39 ) 

40 assert "Usage" in result.stdout 1abcdefgh

41 

42 

43def test_completion(): 1abcdefgh

44 result = subprocess.run( 1abcdefgh

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

46 capture_output=True, 

47 encoding="utf-8", 

48 env={ 

49 **os.environ, 

50 "_TUTORIAL003.PY_COMPLETE": "complete_bash", 

51 "COMP_WORDS": "tutorial003.py --name Rick --v", 

52 "COMP_CWORD": "3", 

53 }, 

54 ) 

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