Skip to content

Commit

Permalink
Merge pull request #2537 from daspecster/handle-broken-config-properly
Browse files Browse the repository at this point in the history
Return None if project key is missing.
  • Loading branch information
daspecster authored Oct 12, 2016
2 parents a55974b + 8ba6bdd commit d31f466
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
5 changes: 4 additions & 1 deletion core/google/cloud/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,10 @@ def _default_service_project_id():
config.read(search_paths)

if config.has_section(_GCLOUD_CONFIG_SECTION):
return config.get(_GCLOUD_CONFIG_SECTION, _GCLOUD_CONFIG_KEY)
try:
return config.get(_GCLOUD_CONFIG_SECTION, _GCLOUD_CONFIG_KEY)
except configparser.NoOptionError:
return None


def _compute_engine_id():
Expand Down
22 changes: 20 additions & 2 deletions core/unit_tests/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,26 @@ def mock_get_path():

self.assertEqual(result, project_id)

def test_nix_missing_prject_key(self):
from google.cloud import _helpers as MUT
from google.cloud._testing import _Monkey
from google.cloud._testing import _NamedTemporaryFile

with _NamedTemporaryFile() as temp:
config_value = '[%s]' % (MUT._GCLOUD_CONFIG_SECTION,)
with open(temp.name, 'w') as config_file:
config_file.write(config_value)

def mock_get_path():
return temp.name

with _Monkey(os, name='not-nt'):
with _Monkey(MUT, _get_nix_config_path=mock_get_path,
_USER_ROOT='not-None'):
result = self._callFUT()

self.assertEqual(result, None)

def test_windows(self):
from google.cloud import _helpers as MUT
from google.cloud._testing import _Monkey
Expand Down Expand Up @@ -694,7 +714,6 @@ def test_w_utc_datetime(self):

def test_w_non_utc_datetime(self):
import datetime
from google.cloud._helpers import _UTC

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

def test_w_non_utc_datetime_and_ignore_zone(self):
import datetime
from google.cloud._helpers import _UTC

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

0 comments on commit d31f466

Please sign in to comment.