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

34 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-09 18:26 +0000

1import subprocess 1abcdefgh

2import sys 1abcdefgh

3from pathlib import Path 1abcdefgh

4 

5import typer 1abcdefgh

6from typer.testing import CliRunner 1abcdefgh

7 

8from docs_src.parameter_types.path import tutorial001 as mod 1abcdefgh

9 

10runner = CliRunner() 1abcdefgh

11 

12app = typer.Typer() 1abcdefgh

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

14 

15 

16def test_no_path(tmpdir): 1abcdefgh

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

18 result = runner.invoke(app) 1abcdefgh

19 assert result.exit_code == 1 1abcdefgh

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

21 assert "Aborted" in result.output 1abcdefgh

22 

23 

24def test_not_exists(tmpdir): 1abcdefgh

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

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

27 config_file.unlink() 

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

29 assert result.exit_code == 0 1abcdefgh

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

31 

32 

33def test_exists(tmpdir): 1abcdefgh

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

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

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

37 config_file.unlink() 1abcdefgh

38 assert result.exit_code == 0 1abcdefgh

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

40 

41 

42def test_dir(): 1abcdefgh

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

44 assert result.exit_code == 0 1abcdefgh

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

46 

47 

48def test_script(): 1abcdefgh

49 result = subprocess.run( 1abcdefgh

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

51 capture_output=True, 

52 encoding="utf-8", 

53 ) 

54 assert "Usage" in result.stdout 1abcdefgh