Skip to content

Commit 838c7a5

Browse files
author
Dylan Huang
committed
Refactor dotenv handling in auth module and integrate environment variable loading into local test command. Introduced functions to find and retrieve values from .env files, enhancing configuration management for Docker tests.
1 parent 7f8056c commit 838c7a5

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

eval_protocol/auth.py

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,75 @@
11
import logging
22
import os
3-
from typing import Optional
3+
from typing import Dict, Optional
44

55
import requests
6+
from dotenv import dotenv_values, find_dotenv, load_dotenv
67

78
logger = logging.getLogger(__name__)
89

910

11+
def find_dotenv_path(search_path: Optional[str] = None) -> Optional[str]:
12+
"""
13+
Find the .env file path, searching .env.dev first, then .env.
14+
15+
Args:
16+
search_path: Directory to search from. If None, uses current working directory.
17+
18+
Returns:
19+
Path to the .env file if found, otherwise None.
20+
"""
21+
# If a specific search path is provided, look there first
22+
if search_path:
23+
env_dev_path = os.path.join(search_path, ".env.dev")
24+
if os.path.isfile(env_dev_path):
25+
return env_dev_path
26+
env_path = os.path.join(search_path, ".env")
27+
if os.path.isfile(env_path):
28+
return env_path
29+
return None
30+
31+
# Otherwise use find_dotenv to search up the directory tree
32+
env_dev_path = find_dotenv(filename=".env.dev", raise_error_if_not_found=False, usecwd=True)
33+
if env_dev_path:
34+
return env_dev_path
35+
env_path = find_dotenv(filename=".env", raise_error_if_not_found=False, usecwd=True)
36+
if env_path:
37+
return env_path
38+
return None
39+
40+
41+
def get_dotenv_values(search_path: Optional[str] = None) -> Dict[str, Optional[str]]:
42+
"""
43+
Get all key-value pairs from the .env file.
44+
45+
Args:
46+
search_path: Directory to search from. If None, uses current working directory.
47+
48+
Returns:
49+
Dictionary of environment variable names to values.
50+
"""
51+
dotenv_path = find_dotenv_path(search_path)
52+
if dotenv_path:
53+
return dotenv_values(dotenv_path)
54+
return {}
55+
56+
57+
# --- Load .env files ---
58+
# Attempt to load .env.dev first, then .env as a fallback.
59+
# This happens when the module is imported.
60+
# We use override=False (default) so that existing environment variables
61+
# (e.g., set in the shell) are NOT overridden by .env files.
62+
_DOTENV_PATH = find_dotenv_path()
63+
if _DOTENV_PATH:
64+
load_dotenv(dotenv_path=_DOTENV_PATH, override=False)
65+
logger.debug(f"eval_protocol.auth: Loaded environment variables from: {_DOTENV_PATH}")
66+
else:
67+
logger.debug(
68+
"eval_protocol.auth: No .env.dev or .env file found. Relying on shell/existing environment variables."
69+
)
70+
# --- End .env loading ---
71+
72+
1073
def get_fireworks_api_key() -> Optional[str]:
1174
"""
1275
Retrieves the Fireworks API key.

eval_protocol/cli_commands/local_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sys
66
from typing import List
77

8+
from ..auth import get_dotenv_values
89
from .utils import _build_entry_point, _discover_and_select_tests
910

1011

@@ -71,6 +72,12 @@ def _run_pytest_in_docker(
7172
workdir,
7273
]
7374

75+
# Forward environment variables from .env file to the container
76+
dotenv_vars = get_dotenv_values(project_root)
77+
for key, value in dotenv_vars.items():
78+
if value is not None:
79+
cmd += ["-e", f"{key}={value}"]
80+
7481
# If EP_SUMMARY_JSON is set on the host, mirror it into the container so that
7582
# pytest evaluation tests can write summary artifacts that are visible to the
7683
# host. We map paths under the host logs directory (~/.eval_protocol) into the

0 commit comments

Comments
 (0)