Merge pull request #2939 from sharmaadarsh563/2937-correct-load_dotenv-return-value

Fix #2937: Ensure the consistency in load_dotenv's return type
This commit is contained in:
David Lord 2019-05-19 12:03:45 -07:00 committed by GitHub
commit d3e1fed777
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 3 deletions

View file

@ -44,6 +44,9 @@ Unreleased
:pr:`3069`
- The development server port can be set to 0, which tells the OS to
pick an available port. :issue:`2926`
- The return value from :meth:`cli.load_dotenv` is more consistent
with the documentation. It will return ``False`` if python-dotenv is
not installed, or if the given path isn't a file. :issue:`2937`
.. _#2935: https://github.com/pallets/flask/issues/2935
.. _#2957: https://github.com/pallets/flask/issues/2957

View file

@ -598,6 +598,10 @@ def load_dotenv(path=None):
:param path: Load the file at this location instead of searching.
:return: ``True`` if a file was loaded.
.. versionchanged:: 1.1.0
Returns ``False`` when python-dotenv is not installed, or when
the given path isn't a file.
.. versionadded:: 1.0
"""
if dotenv is None:
@ -607,10 +611,16 @@ def load_dotenv(path=None):
' Do "pip install python-dotenv" to use them.',
fg="yellow",
)
return
return False
# if the given path specifies the actual file then return True,
# else False
if path is not None:
return dotenv.load_dotenv(path)
if os.path.isfile(path):
return dotenv.load_dotenv(path)
return False
new_dir = None

View file

@ -513,7 +513,7 @@ def test_load_dotenv(monkeypatch):
monkeypatch.setenv("EGGS", "3")
monkeypatch.chdir(os.path.join(test_path, "cliapp", "inner1"))
load_dotenv()
assert load_dotenv()
assert os.getcwd() == test_path
# .flaskenv doesn't overwrite .env
assert os.environ["FOO"] == "env"
@ -524,6 +524,9 @@ def test_load_dotenv(monkeypatch):
# set manually, files don't overwrite
assert os.environ["EGGS"] == "3"
# Non existent file should not load
assert not load_dotenv('non-existent-file')
@need_dotenv
def test_dotenv_path(monkeypatch):