forked from theskumar/python-dotenv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_lib.py
More file actions
46 lines (37 loc) · 1.1 KB
/
test_lib.py
File metadata and controls
46 lines (37 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import subprocess
from pathlib import Path
from typing import Sequence
def run_dotenv(
args: Sequence[str],
cwd: str | Path | None = None,
env: dict | None = None,
) -> subprocess.CompletedProcess:
"""
Run the `dotenv` CLI in a subprocess with the given arguments.
"""
process = subprocess.run(
["dotenv", *args],
capture_output=True,
text=True,
cwd=cwd,
env=env,
)
return process
def check_process(
process: subprocess.CompletedProcess,
exit_code: int,
stdout: str | None = None,
):
"""
Check that the process completed with the expected exit code and output.
This provides better error messages than directly checking the attributes.
"""
assert process.returncode == exit_code, (
f"Unexpected exit code {process.returncode} (expected {exit_code})\n"
f"stdout:\n{process.stdout}\n"
f"stderr:\n{process.stderr}"
)
if stdout is not None:
assert process.stdout == stdout, (
f"Unexpected output: {process.stdout.strip()!r} (expected {stdout!r})"
)