Skip to content

Commit 66f191a

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 3314bec commit 66f191a

File tree

2 files changed

+63
-14
lines changed

2 files changed

+63
-14
lines changed

eval_protocol/auth.py

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,72 @@
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 find_dotenv, load_dotenv
6+
from dotenv import dotenv_values, find_dotenv, load_dotenv
77

88
logger = logging.getLogger(__name__)
99

10+
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+
1057
# --- Load .env files ---
1158
# Attempt to load .env.dev first, then .env as a fallback.
1259
# This happens when the module is imported.
1360
# We use override=False (default) so that existing environment variables
1461
# (e.g., set in the shell) are NOT overridden by .env files.
15-
_ENV_DEV_PATH = find_dotenv(filename=".env.dev", raise_error_if_not_found=False, usecwd=True)
16-
if _ENV_DEV_PATH:
17-
load_dotenv(dotenv_path=_ENV_DEV_PATH, override=False)
18-
logger.debug(f"eval_protocol.auth: Loaded environment variables from: {_ENV_DEV_PATH}")
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}")
1966
else:
20-
_ENV_PATH = find_dotenv(filename=".env", raise_error_if_not_found=False, usecwd=True)
21-
if _ENV_PATH:
22-
load_dotenv(dotenv_path=_ENV_PATH, override=False)
23-
logger.debug(f"eval_protocol.auth: Loaded environment variables from: {_ENV_PATH}")
24-
else:
25-
logger.debug(
26-
"eval_protocol.auth: No .env.dev or .env file found. Relying on shell/existing environment variables."
27-
)
67+
logger.debug(
68+
"eval_protocol.auth: No .env.dev or .env file found. Relying on shell/existing environment variables."
69+
)
2870
# --- End .env loading ---
2971

3072

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)