Add error message to handle case of empty string or missing file for GOOGLE_APPLICATION_CREDENTIALS (#188)
Fixes #161
diff --git a/google/auth/_default.py b/google/auth/_default.py
index 1e91368..7dac642 100644
--- a/google/auth/_default.py
+++ b/google/auth/_default.py
@@ -58,8 +58,12 @@
Raises:
google.auth.exceptions.DefaultCredentialsError: if the file is in the
- wrong format.
+ wrong format or is missing.
"""
+ if not os.path.exists(filename):
+ raise exceptions.DefaultCredentialsError(
+ 'File {} was not found.'.format(filename))
+
with io.open(filename, 'r') as file_obj:
try:
info = json.load(file_obj)
diff --git a/tests/test__default.py b/tests/test__default.py
index 8054ac5..2df8a44 100644
--- a/tests/test__default.py
+++ b/tests/test__default.py
@@ -43,6 +43,13 @@
mock.sentinel.credentials, mock.sentinel.project_id), autospec=True)
+def test__load_credentials_from_missing_file():
+ with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
+ _default._load_credentials_from_file('')
+
+ assert excinfo.match(r'not found')
+
+
def test__load_credentials_from_file_invalid_json(tmpdir):
jsonfile = tmpdir.join('invalid.json')
jsonfile.write('{')