Spaces:
Sleeping
Sleeping
| """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 | |