Coverage for tests / test_tutorial / test_options / test_password / test_tutorial002.py: 100%

35 statements  

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

1import importlib 1abcdefghij

2import subprocess 1abcdefghij

3import sys 1abcdefghij

4from types import ModuleType 1abcdefghij

5 

6import pytest 1abcdefghij

7from typer.testing import CliRunner 1abcdefghij

8 

9from tests.utils import strip_double_spaces 1abcdefghij

10 

11runner = CliRunner() 1abcdefghij

12 

13 

14@pytest.fixture( 1abcdefghij

15 name="mod", 

16 params=[ 

17 pytest.param("tutorial002_non_an"), 

18 pytest.param("tutorial002"), 

19 ], 

20) 

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

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

23 mod = importlib.import_module(module_name) 1abcdefghij

24 return mod 1abcdefghij

25 

26 

27def test_option_password(mod: ModuleType): 1abcdefghij

28 result = runner.invoke(mod.app, ["Camila", "--password", "secretpassword"]) 1abcdefghij

29 assert result.exit_code == 0 1abcdefghij

30 assert "Hello Camila. Doing something very secure with password." in result.output 1abcdefghij

31 assert "...just kidding, here it is, very insecure: secretpassword" in result.output 1abcdefghij

32 

33 

34def test_option_password_prompt(mod: ModuleType): 1abcdefghij

35 result = runner.invoke( 1abcdefghij

36 mod.app, ["Camila"], input="secretpassword\nsecretpassword\n" 

37 ) 

38 assert result.exit_code == 0 1abcdefghij

39 assert "Password: " in result.output 1abcdefghij

40 assert "Password: secretpassword" not in result.output 1abcdefghij

41 assert "Repeat for confirmation: " in result.output 1abcdefghij

42 assert "Repeat for confirmation: secretpassword" not in result.output 1abcdefghij

43 assert "Hello Camila. Doing something very secure with password." in result.output 1abcdefghij

44 assert "...just kidding, here it is, very insecure: secretpassword" in result.output 1abcdefghij

45 

46 

47def test_help(mod: ModuleType): 1abcdefghij

48 result = runner.invoke(mod.app, ["--help"]) 1abcdefghij

49 assert result.exit_code == 0 1abcdefghij

50 output_without_double_spaces = strip_double_spaces(result.output) 1abcdefghij

51 assert "--password <str> [required]" in output_without_double_spaces 1abcdefghij

52 

53 

54def test_script(mod: ModuleType): 1abcdefghij

55 result = subprocess.run( 1abcdefghij

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

57 capture_output=True, 

58 encoding="utf-8", 

59 ) 

60 assert "Usage" in result.stdout 1abcdefghij