Coverage for tests / test_tutorial / test_parameter_types / test_path / test_tutorial001.py: 100%

39 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 pathlib import Path 1abcdefgh

5from types import ModuleType 1abcdefgh

6 

7import pytest 1abcdefgh

8from typer.testing import CliRunner 1abcdefgh

9 

10from ....utils import needs_py310 1abcdefgh

11 

12runner = CliRunner() 1abcdefgh

13 

14 

15@pytest.fixture( 1abcdefgh

16 name="mod", 

17 params=[ 

18 pytest.param("tutorial001_py39"), 

19 pytest.param("tutorial001_py310", marks=needs_py310), 

20 pytest.param("tutorial001_an_py39"), 

21 pytest.param("tutorial001_an_py310", marks=needs_py310), 

22 ], 

23) 

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

25 module_name = f"docs_src.parameter_types.path.{request.param}" 1abcdefgh

26 mod = importlib.import_module(module_name) 1abcdefgh

27 return mod 1abcdefgh

28 

29 

30def test_no_path(tmpdir, mod: ModuleType): 1abcdefgh

31 Path(tmpdir) / "config.txt" 1abcdefgh

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

33 assert result.exit_code == 1 1abcdefgh

34 assert "No config file" in result.output 1abcdefgh

35 assert "Aborted" in result.output 1abcdefgh

36 

37 

38def test_not_exists(tmpdir, mod: ModuleType): 1abcdefgh

39 config_file = Path(tmpdir) / "config.txt" 1abcdefgh

40 if config_file.exists(): # pragma: no cover 1abcdefgh

41 config_file.unlink() 

42 result = runner.invoke(mod.app, ["--config", f"{config_file}"]) 1abcdefgh

43 assert result.exit_code == 0 1abcdefgh

44 assert "The config doesn't exist" in result.output 1abcdefgh

45 

46 

47def test_exists(tmpdir, mod: ModuleType): 1abcdefgh

48 config_file = Path(tmpdir) / "config.txt" 1abcdefgh

49 config_file.write_text("some settings") 1abcdefgh

50 result = runner.invoke(mod.app, ["--config", f"{config_file}"]) 1abcdefgh

51 config_file.unlink() 1abcdefgh

52 assert result.exit_code == 0 1abcdefgh

53 assert "Config file contents: some settings" in result.output 1abcdefgh

54 

55 

56def test_dir(mod: ModuleType): 1abcdefgh

57 result = runner.invoke(mod.app, ["--config", "./"]) 1abcdefgh

58 assert result.exit_code == 0 1abcdefgh

59 assert "Config is a directory, will use all its config files" in result.output 1abcdefgh

60 

61 

62def test_script(mod: ModuleType): 1abcdefgh

63 result = subprocess.run( 1abcdefgh

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

65 capture_output=True, 

66 encoding="utf-8", 

67 ) 

68 assert "Usage" in result.stdout 1abcdefgh