Coverage for tests / test_tutorial / test_app_dir / test_tutorial001.py: 100%

28 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-03-26 21:46 +0000

1import subprocess 1abcdefg

2import sys 1abcdefg

3from pathlib import Path 1abcdefg

4 

5import pytest 1abcdefg

6import typer 1abcdefg

7from typer.testing import CliRunner 1abcdefg

8 

9from docs_src.app_dir import tutorial001_py310 as mod 1abcdefg

10 

11runner = CliRunner() 1abcdefg

12 

13 

14@pytest.fixture(name="config_file") 1abcdefg

15def create_config_file(): 1abcdefg

16 app_dir = Path(typer.get_app_dir("my-super-cli-app")) 1abcdefg

17 app_dir.mkdir(parents=True, exist_ok=True) 1abcdefg

18 config_path = app_dir / "config.json" 1abcdefg

19 config_path.touch(exist_ok=True) 1abcdefg

20 

21 yield config_path 1abcdefg

22 

23 config_path.unlink() 1abcdefg

24 app_dir.rmdir() 1abcdefg

25 

26 

27def test_cli_config_doesnt_exist(): 1abcdefg

28 result = runner.invoke(mod.app) 1abcdefg

29 assert result.exit_code == 0 1abcdefg

30 assert "Config file doesn't exist yet" in result.output 1abcdefg

31 

32 

33def test_cli_config_exists(config_file: Path): 1abcdefg

34 result = runner.invoke(mod.app) 1abcdefg

35 assert result.exit_code == 0 1abcdefg

36 assert "Config file doesn't exist yet" not in result.output 1abcdefg

37 

38 

39def test_script(): 1abcdefg

40 result = subprocess.run( 1abcdefg

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

42 capture_output=True, 

43 encoding="utf-8", 

44 ) 

45 assert "Usage" in result.stdout 1abcdefg