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

29 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-04-14 00:18 +0000

1import subprocess 1abcdefghi

2import sys 1abcdefghi

3from pathlib import Path 1abcdefghi

4 

5import typer 1abcdefghi

6from typer.testing import CliRunner 1abcdefghi

7 

8from docs_src.parameter_types.path import tutorial002 as mod 1abcdefghi

9 

10runner = CliRunner() 1abcdefghi

11 

12app = typer.Typer() 1abcdefghi

13app.command()(mod.main) 1abcdefghi

14 

15 

16def test_not_exists(tmpdir): 1abcdefghi

17 config_file = Path(tmpdir) / "config.txt" 1abcdefghi

18 if config_file.exists(): # pragma: no cover 1abcdefghi

19 config_file.unlink() 

20 result = runner.invoke(app, ["--config", f"{config_file}"]) 1abcdefghi

21 assert result.exit_code != 0 1abcdefghi

22 assert "Invalid value for '--config': File" in result.output 1abcdefghi

23 assert "does not exist" in result.output 1abcdefghi

24 

25 

26def test_exists(tmpdir): 1abcdefghi

27 config_file = Path(tmpdir) / "config.txt" 1abcdefghi

28 config_file.write_text("some settings") 1abcdefghi

29 result = runner.invoke(app, ["--config", f"{config_file}"]) 1abcdefghi

30 config_file.unlink() 1abcdefghi

31 assert result.exit_code == 0 1abcdefghi

32 assert "Config file contents: some settings" in result.output 1abcdefghi

33 

34 

35def test_dir(): 1abcdefghi

36 result = runner.invoke(app, ["--config", "./"]) 1abcdefghi

37 assert result.exit_code != 0 1abcdefghi

38 assert "Invalid value for '--config': File './' is a directory." in result.output 1abcdefghi

39 

40 

41def test_script(): 1abcdefghi

42 result = subprocess.run( 1abcdefghi

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

44 capture_output=True, 

45 encoding="utf-8", 

46 ) 

47 assert "Usage" in result.stdout 1abcdefghi