Coverage for tests/test_cli.py: 100%

38 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-07-01 17:15 +0000

1import tempfile 

2from pathlib import Path 

3from subprocess import PIPE, run 

4from typing import Iterator 

5 

6import pytest 

7 

8DOTENV_FILE = """ 

9# comment=foo 

10TEST=foo 

11TWOLINES='foo\nbar' 

12TEST_COMMENT=foo # bar 

13LINE_WITH_EQUAL='foo=bar' 

14""" 

15 

16 

17@pytest.fixture 

18def dotenvfile() -> Iterator[Path]: 

19 _file = Path.cwd() / ".env" 

20 with _file.open("w") as fh: 

21 fh.write(DOTENV_FILE) 

22 yield _file 

23 _file.unlink() 

24 

25 

26def test_stdout(dotenvfile: Path) -> None: 

27 proc = run(["dotenv", "echo", "test"], stdout=PIPE) 

28 assert b"test" in proc.stdout 

29 

30 

31def test_stderr(dotenvfile: Path) -> None: 

32 proc = run(["dotenv echo test 1>&2"], stderr=PIPE, shell=True) 

33 assert b"test" in proc.stderr 

34 

35 

36def test_returncode(dotenvfile: Path) -> None: 

37 proc = run(["dotenv", "false"]) 

38 assert proc.returncode == 1 

39 

40 proc = run(["dotenv", "true"]) 

41 assert proc.returncode == 0 

42 

43 

44def test_alternative_dotenv() -> None: 

45 with tempfile.NamedTemporaryFile("w", delete=False) as f: 

46 f.write("foo=bar") 

47 

48 proc = run(["dotenv", "-e", f.name, "env"], stdout=PIPE) 

49 assert b"foo=bar" in proc.stdout 

50 

51 proc = run(["dotenv", "--dotenv", f.name, "env"], stdout=PIPE) 

52 assert b"foo=bar" in proc.stdout 

53 

54 

55def test_nonexisting_dotenv() -> None: 

56 proc = run(["dotenv", "-e", "/tmp/i.dont.exist", "true"], stderr=PIPE) 

57 assert proc.returncode == 0 

58 assert b"does not exist" in proc.stderr 

59 

60 

61def test_no_command() -> None: 

62 proc = run(["dotenv"]) 

63 assert proc.returncode == 0