Skip to content

Commit

Permalink
chore: make testable config:
Browse files Browse the repository at this point in the history
  • Loading branch information
robcxyz committed Mar 5, 2024
1 parent c9f8e66 commit e372aa5
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions balanced_backend/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os

from dotenv import dotenv_values
from pydantic import BaseSettings


Expand Down Expand Up @@ -66,7 +68,29 @@ class Config:
case_sensitive = True


def load_env_to_variables(env_file_path):
"""
Load environment variables from a .env file and export them to the actual environment variables.
Args:
- env_file_path (str): Path to the .env file
Returns:
- None
"""
# Load environment variables from the .env file
env_vars = dotenv_values(env_file_path)

# Export variables to the actual environment
for key, value in env_vars.items():
os.environ[key] = value


# Ignored by default
test_env = os.path.join(os.path.dirname(__file__), "..", ".env.test")
if os.environ.get("ENV_FILE", False):
settings = Settings(_env_file=os.environ.get("ENV_FILE"))
else:
settings = Settings()
load_env_to_variables(os.environ.get("ENV_FILE"))
elif os.path.isfile(test_env):
load_env_to_variables(test_env)

config = Settings()

0 comments on commit e372aa5

Please sign in to comment.