feat(dotenv): support home directory relative paths (e.g. ~/.env) (#1804)

closes #1803

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
This commit is contained in:
PrettyWood
2020-10-09 11:23:28 +02:00
committed by GitHub
parent c5fc921620
commit 76fdbe92be
3 changed files with 26 additions and 1 deletions
+1
View File
@@ -0,0 +1 @@
Support home directory relative paths for `dotenv` files (e.g. `~/.env`).
+1 -1
View File
@@ -58,7 +58,7 @@ class BaseSettings(BaseModel):
env_file = _env_file if _env_file != env_file_sentinel else self.__config__.env_file
env_file_encoding = _env_file_encoding if _env_file_encoding is not None else self.__config__.env_file_encoding
if env_file is not None:
env_path = Path(env_file)
env_path = Path(env_file).expanduser()
if env_path.is_file():
env_vars = {
**read_env_file(
+24
View File
@@ -1,4 +1,6 @@
import os
import uuid
from pathlib import Path
from typing import Dict, List, Optional, Set
import pytest
@@ -547,6 +549,28 @@ def test_env_file_config_custom_encoding(tmp_path):
assert s.pika == 'p!±@'
@pytest.fixture
def home_tmp():
tmp_filename = f'{uuid.uuid4()}.env'
home_tmp_path = Path.home() / tmp_filename
yield home_tmp_path, tmp_filename
home_tmp_path.unlink()
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
def test_env_file_home_directory(home_tmp):
home_tmp_path, tmp_filename = home_tmp
home_tmp_path.write_text('pika=baz')
class Settings(BaseSettings):
pika: str
class Config:
env_file = f'~/{tmp_filename}'
assert Settings().pika == 'baz'
@pytest.mark.skipif(not dotenv, reason='python-dotenv not installed')
def test_env_file_none(tmp_path):
p = tmp_path / '.env'