Skip to content

Commit 13c66c9

Browse files
mathauseKludex
andauthored
Raise FileNotFoundError when the env_file parameter on Config is not valid (#2422)
* raise error on missing env file * format --------- Co-authored-by: Marcelo Trylesinski <[email protected]>
1 parent d2d9192 commit 13c66c9

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

starlette/config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ def __init__(
5858
self.environ = environ
5959
self.env_prefix = env_prefix
6060
self.file_values: typing.Dict[str, str] = {}
61-
if env_file is not None and os.path.isfile(env_file):
61+
if env_file is not None:
62+
if not os.path.isfile(env_file):
63+
raise FileNotFoundError(f"Config file '{env_file}' not found.")
6264
self.file_values = self._read_file(env_file)
6365

6466
@typing.overload

tests/test_config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ def cast_to_int(v) -> int:
104104
config.get("BOOL_AS_INT", cast=bool)
105105

106106

107+
def test_missing_env_file_raises(tmpdir):
108+
path = os.path.join(tmpdir, ".env")
109+
110+
with pytest.raises(FileNotFoundError, match=f"Config file '{path}' not found."):
111+
Config(path)
112+
113+
107114
def test_environ():
108115
environ = Environ()
109116

0 commit comments

Comments
 (0)