]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
Raise `FileNotFoundError` when the `env_file` parameter on `Config` is not valid...
authorMathias Hauser <mathause@users.noreply.github.com>
Sat, 20 Jan 2024 12:06:50 +0000 (13:06 +0100)
committerGitHub <noreply@github.com>
Sat, 20 Jan 2024 12:06:50 +0000 (12:06 +0000)
* raise error on missing env file

* format

---------

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
starlette/config.py
tests/test_config.py

index 173955006e572c909be6ef82e3ccc029c2e6701f..1ac49ea85d0a35644495c3312c1bbf193a069766 100644 (file)
@@ -58,7 +58,9 @@ class Config:
         self.environ = environ
         self.env_prefix = env_prefix
         self.file_values: typing.Dict[str, str] = {}
-        if env_file is not None and os.path.isfile(env_file):
+        if env_file is not None:
+            if not os.path.isfile(env_file):
+                raise FileNotFoundError(f"Config file '{env_file}' not found.")
             self.file_values = self._read_file(env_file)
 
     @typing.overload
index 70b0a48120f64e623b533f77e74a2a198cbc2ae6..f7b294c7ae76b5c2d9f55ca00a62d16a60722218 100644 (file)
@@ -104,6 +104,13 @@ def test_config(tmpdir, monkeypatch):
         config.get("BOOL_AS_INT", cast=bool)
 
 
+def test_missing_env_file_raises(tmpdir):
+    path = os.path.join(tmpdir, ".env")
+
+    with pytest.raises(FileNotFoundError, match=f"Config file '{path}' not found."):
+        Config(path)
+
+
 def test_environ():
     environ = Environ()