Skip to content

Commit d31f466

Browse files
authored
Merge pull request #2537 from daspecster/handle-broken-config-properly
Return None if project key is missing.
2 parents a55974b + 8ba6bdd commit d31f466

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

core/google/cloud/_helpers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,10 @@ def _default_service_project_id():
240240
config.read(search_paths)
241241

242242
if config.has_section(_GCLOUD_CONFIG_SECTION):
243-
return config.get(_GCLOUD_CONFIG_SECTION, _GCLOUD_CONFIG_KEY)
243+
try:
244+
return config.get(_GCLOUD_CONFIG_SECTION, _GCLOUD_CONFIG_KEY)
245+
except configparser.NoOptionError:
246+
return None
244247

245248

246249
def _compute_engine_id():

core/unit_tests/test__helpers.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,26 @@ def mock_get_path():
254254

255255
self.assertEqual(result, project_id)
256256

257+
def test_nix_missing_prject_key(self):
258+
from google.cloud import _helpers as MUT
259+
from google.cloud._testing import _Monkey
260+
from google.cloud._testing import _NamedTemporaryFile
261+
262+
with _NamedTemporaryFile() as temp:
263+
config_value = '[%s]' % (MUT._GCLOUD_CONFIG_SECTION,)
264+
with open(temp.name, 'w') as config_file:
265+
config_file.write(config_value)
266+
267+
def mock_get_path():
268+
return temp.name
269+
270+
with _Monkey(os, name='not-nt'):
271+
with _Monkey(MUT, _get_nix_config_path=mock_get_path,
272+
_USER_ROOT='not-None'):
273+
result = self._callFUT()
274+
275+
self.assertEqual(result, None)
276+
257277
def test_windows(self):
258278
from google.cloud import _helpers as MUT
259279
from google.cloud._testing import _Monkey
@@ -694,7 +714,6 @@ def test_w_utc_datetime(self):
694714

695715
def test_w_non_utc_datetime(self):
696716
import datetime
697-
from google.cloud._helpers import _UTC
698717

699718
zone = self._make_timezone(offset=datetime.timedelta(hours=-1))
700719
TIMESTAMP = datetime.datetime(2016, 4, 5, 13, 30, 0, tzinfo=zone)
@@ -703,7 +722,6 @@ def test_w_non_utc_datetime(self):
703722

704723
def test_w_non_utc_datetime_and_ignore_zone(self):
705724
import datetime
706-
from google.cloud._helpers import _UTC
707725

708726
zone = self._make_timezone(offset=datetime.timedelta(hours=-1))
709727
TIMESTAMP = datetime.datetime(2016, 4, 5, 13, 30, 0, tzinfo=zone)

0 commit comments

Comments
 (0)