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-02-09 12:36 +0000

1import importlib 1abcdefgh

2import subprocess 1abcdefgh

3import sys 1abcdefgh

4from types import ModuleType 1abcdefgh

5 

6import pytest 1abcdefgh

7from typer.testing import CliRunner 1abcdefgh

8 

9from tests.utils import strip_double_spaces 1abcdefgh

10 

11runner = CliRunner() 1abcdefgh

12 

13 

14@pytest.fixture( 1abcdefgh

15 name="mod", 

16 params=[ 

17 pytest.param("tutorial002_py39"), 

18 pytest.param("tutorial002_an_py39"), 

19 ], 

20) 

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

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

23 mod = importlib.import_module(module_name) 1abcdefgh

24 return mod 1abcdefgh

25 

26 

27def test_option_password(mod: ModuleType): 1abcdefgh

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

29 assert result.exit_code == 0 1abcdefgh

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

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

32 

33 

34def test_option_password_prompt(mod: ModuleType): 1abcdefgh

35 result = runner.invoke( 1abcdefgh

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

37 ) 

38 assert result.exit_code == 0 1abcdefgh

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

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

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

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

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

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

45 

46 

47def test_help(mod: ModuleType): 1abcdefgh

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

49 assert result.exit_code == 0 1abcdefgh

50 output_without_double_spaces = strip_double_spaces(result.output) 1abcdefgh

51 assert "--password TEXT [required]" in output_without_double_spaces 1abcdefgh

52 

53 

54def test_script(mod: ModuleType): 1abcdefgh

55 result = subprocess.run( 1abcdefgh

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

57 capture_output=True, 

58 encoding="utf-8", 

59 ) 

60 assert "Usage" in result.stdout 1abcdefgh