From c45575bc429db1a2315789e5f93264c34963e8a4 Mon Sep 17 00:00:00 2001 From: Christian Jauvin Date: Fri, 27 Apr 2018 12:28:48 -0400 Subject: [PATCH 1/4] Fix to enable loading from current directory --- dotenv/main.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/dotenv/main.py b/dotenv/main.py index cdb5d9d4..252a2dd3 100644 --- a/dotenv/main.py +++ b/dotenv/main.py @@ -232,7 +232,11 @@ def find_dotenv(filename='.env', raise_error_if_not_found=False, usecwd=False): path = os.getcwd() else: # will work for .py files - frame_filename = sys._getframe().f_back.f_code.co_filename + frame = sys._getframe() + # find first frame that is outside of this file + while frame.f_code.co_filename == __file__: + frame = frame.f_back + frame_filename = frame.f_code.co_filename path = os.path.dirname(os.path.abspath(frame_filename)) for dirname in _walk_to_root(path): From a3bbecbfe38774a2ee6e9ae9ed4c857a12f4b5fe Mon Sep 17 00:00:00 2001 From: Christian Jauvin Date: Fri, 27 Apr 2018 16:32:47 -0400 Subject: [PATCH 2/4] Add test to make sure loading from current dir works --- tests/test_core.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/test_core.py b/tests/test_core.py index a7fb347c..e7f9a19a 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1,4 +1,4 @@ -# -*- coding: utf8 -*- +# -*- coding: utf-8 -*- from __future__ import unicode_literals import os @@ -107,6 +107,17 @@ def test_load_dotenv_override(cli): sh.rm(dotenv_path) +def test_load_dotenv_in_current_dir(): + dotenv_path = '.env' + with open(dotenv_path, 'w') as f: + f.write("TOTO=bla\n") + assert 'TOTO' not in os.environ + success = load_dotenv(verbose=True) + assert success + assert os.environ['TOTO'] == 'bla' + sh.rm(dotenv_path) + + def test_ipython(): tmpdir = os.path.realpath(tempfile.mkdtemp()) os.chdir(tmpdir) From 15c0435415e233796432b1c0c7f1e4dfe6251b5d Mon Sep 17 00:00:00 2001 From: Christian Jauvin Date: Fri, 27 Apr 2018 16:44:03 -0400 Subject: [PATCH 3/4] Reorder test_core tests to avoid directory problem --- tests/test_core.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/test_core.py b/tests/test_core.py index e7f9a19a..3e591c41 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -38,6 +38,17 @@ def test_warns_if_file_does_not_exist(): assert str(w[0].message) == "File doesn't exist .does_not_exist" +def test_load_dotenv_in_current_dir(): + dotenv_path = '.env' + with open(dotenv_path, 'w') as f: + f.write("TOTO=bla\n") + assert 'TOTO' not in os.environ + success = load_dotenv(verbose=True) + assert success + assert os.environ['TOTO'] == 'bla' + sh.rm(dotenv_path) + + def test_find_dotenv(): """ Create a temporary folder structure like the following: @@ -107,17 +118,6 @@ def test_load_dotenv_override(cli): sh.rm(dotenv_path) -def test_load_dotenv_in_current_dir(): - dotenv_path = '.env' - with open(dotenv_path, 'w') as f: - f.write("TOTO=bla\n") - assert 'TOTO' not in os.environ - success = load_dotenv(verbose=True) - assert success - assert os.environ['TOTO'] == 'bla' - sh.rm(dotenv_path) - - def test_ipython(): tmpdir = os.path.realpath(tempfile.mkdtemp()) os.chdir(tmpdir) From 1df8b69c7744b2fcc6f3426b339dc979bef0a339 Mon Sep 17 00:00:00 2001 From: Christian Jauvin Date: Fri, 27 Apr 2018 16:51:13 -0400 Subject: [PATCH 4/4] Reorder curr dir test again, and use os.chdir --- tests/test_core.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/tests/test_core.py b/tests/test_core.py index 3e591c41..1547e245 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -38,17 +38,6 @@ def test_warns_if_file_does_not_exist(): assert str(w[0].message) == "File doesn't exist .does_not_exist" -def test_load_dotenv_in_current_dir(): - dotenv_path = '.env' - with open(dotenv_path, 'w') as f: - f.write("TOTO=bla\n") - assert 'TOTO' not in os.environ - success = load_dotenv(verbose=True) - assert success - assert os.environ['TOTO'] == 'bla' - sh.rm(dotenv_path) - - def test_find_dotenv(): """ Create a temporary folder structure like the following: @@ -118,6 +107,19 @@ def test_load_dotenv_override(cli): sh.rm(dotenv_path) +def test_load_dotenv_in_current_dir(): + # make sure were are here! + os.chdir(os.path.dirname(os.path.realpath(__file__))) + dotenv_path = '.env' + with open(dotenv_path, 'w') as f: + f.write("TOTO=bla\n") + assert 'TOTO' not in os.environ + success = load_dotenv(verbose=True) + assert success + assert os.environ['TOTO'] == 'bla' + sh.rm(dotenv_path) + + def test_ipython(): tmpdir = os.path.realpath(tempfile.mkdtemp()) os.chdir(tmpdir)