Spaces:
Sleeping
Sleeping
File size: 1,447 Bytes
7e1ccef e5283e9 7e1ccef | 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 | """env_loader: load the nearest .env without clobbering real env vars."""
import os
from proteus.env_loader import find_dotenv, load_dotenv
def test_loads_keys_without_overriding_existing(tmp_path, monkeypatch):
(tmp_path / ".env").write_text(
"# provider keys\n"
"OLLAMA_API_KEY=from_dotenv\n"
'OPENAI_API_KEY="quoted_value"\n'
"export ANTHROPIC_API_KEY=exported_style # inline note\n"
"ALREADY_SET=should_not_win\n"
)
# Isolate os.environ to a throwaway dict so load_dotenv (which mutates the
# global environment) can't leak provider keys into later tests.
monkeypatch.setattr(os, "environ", {"ALREADY_SET": "real_env"})
loaded = load_dotenv(start=tmp_path)
assert loaded == str(tmp_path / ".env")
assert os.environ["OLLAMA_API_KEY"] == "from_dotenv"
assert os.environ["OPENAI_API_KEY"] == "quoted_value" # quotes stripped
assert os.environ["ANTHROPIC_API_KEY"] == "exported_style" # export + inline comment handled
assert os.environ["ALREADY_SET"] == "real_env" # not overridden
def test_finds_dotenv_walking_up(tmp_path):
(tmp_path / ".env").write_text("K=v\n")
deep = tmp_path / "a" / "b" / "c"
deep.mkdir(parents=True)
assert find_dotenv(start=deep) == tmp_path / ".env" # walks up to the root .env
def test_returns_none_when_absent(tmp_path):
assert load_dotenv(start=tmp_path) is None
|