Skip to content

Commit

Permalink
Attempt to fix windows path crash. closes #1975
Browse files Browse the repository at this point in the history
  • Loading branch information
daspecster committed Jul 12, 2016
1 parent a1620a9 commit bbfefee
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
9 changes: 5 additions & 4 deletions gcloud/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,11 @@ def _default_service_project_id():
search_paths.append(os.path.expanduser(DEFAULT_CONFIGURATION_PATH))
except ImportError:
pass
win32_config_path = os.path.join(os.getenv('APPDATA', ''),
'gcloud', 'configurations',
'config_default')
search_paths.append(win32_config_path)

windows_config_path = os.path.join(os.getenv('APPDATA', ''),
'gcloud', 'configurations',
'config_default')
search_paths.append(windows_config_path)
config = configparser.RawConfigParser()
config.read(search_paths)

Expand Down
28 changes: 20 additions & 8 deletions gcloud/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,34 +183,41 @@ def test_no_environment(self):
class Test__get_default_service_project_id(unittest2.TestCase):
config_path = '.config/gcloud/configurations/'
config_file = 'config_default'
temp_APPDATA = ''

def setUp(self):
import tempfile
import os

self.temp_config_path = tempfile.mkdtemp()
self.temp_APPDATA = os.getenv('APPDATA')
if self.temp_APPDATA: # pragma: NO COVER Windows
os.environ['APPDATA'] = self.temp_config_path

self.config_path = os.path.join(os.getenv('APPDATA', '~/.config'),
'gcloud', 'configurations')
conf_path = os.path.join(self.temp_config_path, self.config_path)
os.makedirs(conf_path)
full_config_path = os.path.join(conf_path, self.config_file)
self.temp_config_file = os.path.join(conf_path, self.config_file)

self.temp_config_file = full_config_path

with open(full_config_path, 'w') as conf_file:
with open(self.temp_config_file, 'w') as conf_file:
conf_file.write('[core]\nproject = test-project-id')

def tearDown(self):
import shutil

shutil.rmtree(self.temp_config_path)
import os
if os.path.exists(self.temp_config_path):
shutil.rmtree(self.temp_config_path)
if self.temp_APPDATA: # pragma: NO COVER Windows
os.environ['APPDATA'] = self.temp_APPDATA

def callFUT(self, project_id=None):
import os
from gcloud._helpers import _default_service_project_id
from gcloud._testing import _Monkey

def mock_expanduser(path=''):
if project_id and path.startswith('~'):
def mock_expanduser(path=None):
if project_id and path:
__import__('pwd') # Simulate actual expanduser imports.
return self.temp_config_file
return ''
Expand All @@ -224,6 +231,9 @@ def test_read_from_cli_info(self):

def test_gae_without_expanduser(self):
import sys
import shutil
shutil.rmtree(self.temp_config_path)

try:
sys.modules['pwd'] = None # Blocks pwd from being imported.
project_id = self.callFUT('test-project-id')
Expand All @@ -232,6 +242,8 @@ def test_gae_without_expanduser(self):
del sys.modules['pwd'] # Unblocks importing of pwd.

def test_info_value_not_present(self):
import shutil
shutil.rmtree(self.temp_config_path)
project_id = self.callFUT()
self.assertEqual(None, project_id)

Expand Down

0 comments on commit bbfefee

Please sign in to comment.