-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_env_files.py
More file actions
56 lines (40 loc) · 1.76 KB
/
test_env_files.py
File metadata and controls
56 lines (40 loc) · 1.76 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
import os
from agentkit.toolkit.config import CommonConfig
from agentkit.toolkit.config.utils import load_dotenv_file, merge_runtime_envs
def test_load_dotenv_file_returns_values(tmp_path, monkeypatch):
env_file = tmp_path / ".env"
env_file.write_text("FOO=bar\nHELLO=world\n", encoding="utf-8")
monkeypatch.delenv("FOO", raising=False)
monkeypatch.delenv("HELLO", raising=False)
values = load_dotenv_file(tmp_path)
assert values["FOO"] == "bar"
assert values["HELLO"] == "world"
def test_load_dotenv_file_does_not_mutate_process_environment(tmp_path, monkeypatch):
env_file = tmp_path / ".env"
env_file.write_text(
"FOO=bar\nVOLCENGINE_ACCESS_KEY=ak_from_dotenv\n",
encoding="utf-8",
)
monkeypatch.delenv("FOO", raising=False)
monkeypatch.delenv("VOLCENGINE_ACCESS_KEY", raising=False)
values = load_dotenv_file(tmp_path)
assert values["FOO"] == "bar"
assert values["VOLCENGINE_ACCESS_KEY"] == "ak_from_dotenv"
assert "FOO" not in os.environ
assert "VOLCENGINE_ACCESS_KEY" not in os.environ
def test_merge_runtime_envs_precedence_includes_dotenv(tmp_path):
(tmp_path / "config.yaml").write_text(
"model:\n api_key: from_config\n",
encoding="utf-8",
)
(tmp_path / ".env").write_text(
"A=dotenv\nB=dotenv\nMODEL_API_KEY=from_env\n",
encoding="utf-8",
)
common_config = CommonConfig(runtime_envs={"A": "common", "B": "common"})
strategy_config = {"runtime_envs": {"B": "strategy", "C": "strategy"}}
merged = merge_runtime_envs(common_config, strategy_config, project_dir=tmp_path)
assert merged["A"] == "common"
assert merged["B"] == "strategy"
assert merged["C"] == "strategy"
assert merged["MODEL_API_KEY"] == "from_env"