-
Notifications
You must be signed in to change notification settings - Fork 522
Expand file tree
/
Copy pathtest_fifo_dotenv.py
More file actions
64 lines (43 loc) · 1.68 KB
/
test_fifo_dotenv.py
File metadata and controls
64 lines (43 loc) · 1.68 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
import pathlib
import sys
import threading
import pytest
from dotenv import load_dotenv
from dotenv.main import _wait_for_file_content
pytestmark = pytest.mark.skipif(sys.platform == "win32", reason="FIFOs are Unix-only")
def test_load_dotenv_from_fifo(tmp_path: pathlib.Path, monkeypatch):
fifo = tmp_path / ".env"
os.mkfifo(fifo) # create named pipe
def writer():
with open(fifo, "w", encoding="utf-8") as w:
w.write("MY_PASSWORD=pipe-secret\n")
t = threading.Thread(target=writer)
t.start()
# Ensure env is clean
monkeypatch.delenv("MY_PASSWORD", raising=False)
ok = load_dotenv(dotenv_path=str(fifo), override=True)
t.join(timeout=2)
assert ok is True
assert os.getenv("MY_PASSWORD") == "pipe-secret"
def test_wait_for_file_content_with_regular_file(tmp_path: pathlib.Path):
"""Test that _wait_for_file_content handles regular files correctly."""
regular_file = tmp_path / ".env"
regular_file.write_text("KEY=value\n", encoding="utf-8")
stream = _wait_for_file_content(str(regular_file), encoding="utf-8")
content = stream.read()
assert content == "KEY=value\n"
stream.close()
def test_wait_for_file_content_with_fifo(tmp_path: pathlib.Path):
"""Test that _wait_for_file_content handles FIFOs correctly."""
fifo = tmp_path / ".env"
os.mkfifo(fifo)
def writer():
with open(fifo, "w", encoding="utf-8") as w:
w.write("FIFO_KEY=fifo-value\n")
t = threading.Thread(target=writer)
t.start()
stream = _wait_for_file_content(str(fifo), encoding="utf-8")
content = stream.read()
t.join(timeout=2)
assert content == "FIFO_KEY=fifo-value\n"