Coverage for tests/test_cli.py: 100%
38 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-10-26 18:28 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-10-26 18:28 +0000
1import tempfile
2from pathlib import Path
3from subprocess import PIPE, run
4from typing import Iterator
6import pytest
8DOTENV_FILE = """
9# comment=foo
10TEST=foo
11TWOLINES='foo\nbar'
12TEST_COMMENT=foo # bar
13LINE_WITH_EQUAL='foo=bar'
14"""
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()
26def test_stdout(dotenvfile: Path) -> None:
27 proc = run(["dotenv", "echo", "test"], stdout=PIPE)
28 assert b"test" in proc.stdout
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
36def test_returncode(dotenvfile: Path) -> None:
37 proc = run(["dotenv", "false"])
38 assert proc.returncode == 1
40 proc = run(["dotenv", "true"])
41 assert proc.returncode == 0
44def test_alternative_dotenv() -> None:
45 with tempfile.NamedTemporaryFile("w", delete=False) as f:
46 f.write("foo=bar")
48 proc = run(["dotenv", "-e", f.name, "env"], stdout=PIPE)
49 assert b"foo=bar" in proc.stdout
51 proc = run(["dotenv", "--dotenv", f.name, "env"], stdout=PIPE)
52 assert b"foo=bar" in proc.stdout
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
61def test_no_command() -> None:
62 proc = run(["dotenv"])
63 assert proc.returncode == 0