From db8cb31f2b2c39c1b0800926969c16d44b28ea38 Mon Sep 17 00:00:00 2001 From: Adarsh Sharma Date: Thu, 11 Oct 2018 16:52:15 +0530 Subject: [PATCH] Fix #2937: Ensure the consistency in load_dotenv's return type --- CHANGES.rst | 3 +++ flask/cli.py | 14 ++++++++++++-- tests/test_cli.py | 5 ++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 3a9597a3..9d95317f 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 diff --git a/flask/cli.py b/flask/cli.py index 600379e9..7d882902 100644 --- a/flask/cli.py +++ b/flask/cli.py @@ -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 diff --git a/tests/test_cli.py b/tests/test_cli.py index 5079cfcc..25e7ab1a 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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):