forked from orbit-oss/flask
Fix #2937: Ensure the consistency in load_dotenv's return type
This commit is contained in:
parent
d9fa28ba68
commit
db8cb31f2b
3 changed files with 19 additions and 3 deletions
|
|
@ -44,6 +44,9 @@ Unreleased
|
||||||
:pr:`3069`
|
:pr:`3069`
|
||||||
- The development server port can be set to 0, which tells the OS to
|
- The development server port can be set to 0, which tells the OS to
|
||||||
pick an available port. :issue:`2926`
|
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
|
.. _#2935: https://github.com/pallets/flask/issues/2935
|
||||||
.. _#2957: https://github.com/pallets/flask/issues/2957
|
.. _#2957: https://github.com/pallets/flask/issues/2957
|
||||||
|
|
|
||||||
14
flask/cli.py
14
flask/cli.py
|
|
@ -598,6 +598,10 @@ def load_dotenv(path=None):
|
||||||
:param path: Load the file at this location instead of searching.
|
:param path: Load the file at this location instead of searching.
|
||||||
:return: ``True`` if a file was loaded.
|
: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
|
.. versionadded:: 1.0
|
||||||
"""
|
"""
|
||||||
if dotenv is None:
|
if dotenv is None:
|
||||||
|
|
@ -607,10 +611,16 @@ def load_dotenv(path=None):
|
||||||
' Do "pip install python-dotenv" to use them.',
|
' Do "pip install python-dotenv" to use them.',
|
||||||
fg="yellow",
|
fg="yellow",
|
||||||
)
|
)
|
||||||
return
|
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
# if the given path specifies the actual file then return True,
|
||||||
|
# else False
|
||||||
if path is not None:
|
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
|
new_dir = None
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -513,7 +513,7 @@ def test_load_dotenv(monkeypatch):
|
||||||
|
|
||||||
monkeypatch.setenv("EGGS", "3")
|
monkeypatch.setenv("EGGS", "3")
|
||||||
monkeypatch.chdir(os.path.join(test_path, "cliapp", "inner1"))
|
monkeypatch.chdir(os.path.join(test_path, "cliapp", "inner1"))
|
||||||
load_dotenv()
|
assert load_dotenv()
|
||||||
assert os.getcwd() == test_path
|
assert os.getcwd() == test_path
|
||||||
# .flaskenv doesn't overwrite .env
|
# .flaskenv doesn't overwrite .env
|
||||||
assert os.environ["FOO"] == "env"
|
assert os.environ["FOO"] == "env"
|
||||||
|
|
@ -524,6 +524,9 @@ def test_load_dotenv(monkeypatch):
|
||||||
# set manually, files don't overwrite
|
# set manually, files don't overwrite
|
||||||
assert os.environ["EGGS"] == "3"
|
assert os.environ["EGGS"] == "3"
|
||||||
|
|
||||||
|
# Non existent file should not load
|
||||||
|
assert not load_dotenv('non-existent-file')
|
||||||
|
|
||||||
|
|
||||||
@need_dotenv
|
@need_dotenv
|
||||||
def test_dotenv_path(monkeypatch):
|
def test_dotenv_path(monkeypatch):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue