Skip to content

Commit e92ab1f

Browse files
authored
Fix file not found message to have some context (theskumar#245)
'File doesn't exist' doesn't really tell the user much, let's add some context.
1 parent 335dedc commit e92ab1f

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/dotenv/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def _get_stream(self):
7474
yield stream
7575
else:
7676
if self.verbose:
77-
logger.warning("File doesn't exist %s", self.dotenv_path)
77+
logger.info("Python-dotenv could not find configuration file %s.", self.dotenv_path or '.env')
7878
yield StringIO('')
7979

8080
def dict(self):

tests/test_main.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,19 @@ def test_get_key_no_file(tmp_path):
7474
nx_file = str(tmp_path / "nx")
7575
logger = logging.getLogger("dotenv.main")
7676

77-
with mock.patch.object(logger, "warning") as mock_warning:
77+
with mock.patch.object(logger, "info") as mock_info, \
78+
mock.patch.object(logger, "warning") as mock_warning:
7879
result = dotenv.get_key(nx_file, "foo")
7980

8081
assert result is None
82+
mock_info.assert_has_calls(
83+
calls=[
84+
mock.call("Python-dotenv could not find configuration file %s.", nx_file)
85+
],
86+
)
8187
mock_warning.assert_has_calls(
8288
calls=[
83-
mock.call("File doesn't exist %s", nx_file),
84-
mock.call("Key %s not found in %s.", "foo", nx_file),
89+
mock.call("Key %s not found in %s.", "foo", nx_file)
8590
],
8691
)
8792

@@ -228,10 +233,10 @@ def test_load_dotenv_existing_file(dotenv_file):
228233
def test_load_dotenv_no_file_verbose():
229234
logger = logging.getLogger("dotenv.main")
230235

231-
with mock.patch.object(logger, "warning") as mock_warning:
236+
with mock.patch.object(logger, "info") as mock_info:
232237
dotenv.load_dotenv('.does_not_exist', verbose=True)
233238

234-
mock_warning.assert_called_once_with("File doesn't exist %s", ".does_not_exist")
239+
mock_info.assert_called_once_with("Python-dotenv could not find configuration file %s.", ".does_not_exist")
235240

236241

237242
@mock.patch.dict(os.environ, {"a": "c"}, clear=True)

0 commit comments

Comments
 (0)