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

1import subprocess 1abcdefgh

2import sys 1abcdefgh

3from pathlib import Path 1abcdefgh

4 

5import pytest 1abcdefgh

6import typer 1abcdefgh

7from typer.testing import CliRunner 1abcdefgh

8 

9from docs_src.app_dir import tutorial001_py39 as mod 1abcdefgh

10 

11runner = CliRunner() 1abcdefgh

12 

13 

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

15def create_config_file(): 1abcdefgh

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

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

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

19 config_path.touch(exist_ok=True) 1abcdefgh

20 

21 yield config_path 1abcdefgh

22 

23 config_path.unlink() 1abcdefgh

24 app_dir.rmdir() 1abcdefgh

25 

26 

27def test_cli_config_doesnt_exist(): 1abcdefgh

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

29 assert result.exit_code == 0 1abcdefgh

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

31 

32 

33def test_cli_config_exists(config_file: Path): 1abcdefgh

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

35 assert result.exit_code == 0 1abcdefgh

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

37 

38 

39def test_script(): 1abcdefgh

40 result = subprocess.run( 1abcdefgh

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

42 capture_output=True, 

43 encoding="utf-8", 

44 ) 

45 assert "Usage" in result.stdout 1abcdefgh