Skip to content

Commit 100ac64

Browse files
committed
Attempt to fix windows path crash. closes #1975
1 parent 2dcad57 commit 100ac64

2 files changed

Lines changed: 25 additions & 12 deletions

File tree

gcloud/_helpers.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,11 @@ def _default_service_project_id():
194194
search_paths.append(os.path.expanduser(DEFAULT_CONFIGURATION_PATH))
195195
except ImportError:
196196
pass
197-
win32_config_path = os.path.join(os.getenv('APPDATA', ''),
198-
'gcloud', 'configurations',
199-
'config_default')
200-
search_paths.append(win32_config_path)
197+
198+
windows_config_path = os.path.join(os.getenv('APPDATA', ''),
199+
'gcloud', 'configurations',
200+
'config_default')
201+
search_paths.append(windows_config_path)
201202
config = configparser.RawConfigParser()
202203
config.read(search_paths)
203204

gcloud/test__helpers.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,34 +183,41 @@ def test_no_environment(self):
183183
class Test__get_default_service_project_id(unittest2.TestCase):
184184
config_path = '.config/gcloud/configurations/'
185185
config_file = 'config_default'
186+
temp_APPDATA = ''
186187

187188
def setUp(self):
188189
import tempfile
189190
import os
190191

191192
self.temp_config_path = tempfile.mkdtemp()
193+
self.temp_APPDATA = os.getenv('APPDATA')
194+
if self.temp_APPDATA: # pragma: NO COVER Windows
195+
os.environ['APPDATA'] = self.temp_config_path
192196

197+
self.config_path = os.path.join(os.getenv('APPDATA', '~/.config'),
198+
'gcloud', 'configurations')
193199
conf_path = os.path.join(self.temp_config_path, self.config_path)
194200
os.makedirs(conf_path)
195-
full_config_path = os.path.join(conf_path, self.config_file)
201+
self.temp_config_file = os.path.join(conf_path, self.config_file)
196202

197-
self.temp_config_file = full_config_path
198-
199-
with open(full_config_path, 'w') as conf_file:
203+
with open(self.temp_config_file, 'w') as conf_file:
200204
conf_file.write('[core]\nproject = test-project-id')
201205

202206
def tearDown(self):
203207
import shutil
204-
205-
shutil.rmtree(self.temp_config_path)
208+
import os
209+
if os.path.exists(self.temp_config_path):
210+
shutil.rmtree(self.temp_config_path)
211+
if self.temp_APPDATA: # pragma: NO COVER Windows
212+
os.environ['APPDATA'] = self.temp_APPDATA
206213

207214
def callFUT(self, project_id=None):
208215
import os
209216
from gcloud._helpers import _default_service_project_id
210217
from gcloud._testing import _Monkey
211218

212-
def mock_expanduser(path=''):
213-
if project_id and path.startswith('~'):
219+
def mock_expanduser(path=None):
220+
if project_id and path:
214221
__import__('pwd') # Simulate actual expanduser imports.
215222
return self.temp_config_file
216223
return ''
@@ -224,6 +231,9 @@ def test_read_from_cli_info(self):
224231

225232
def test_gae_without_expanduser(self):
226233
import sys
234+
import shutil
235+
shutil.rmtree(self.temp_config_path)
236+
227237
try:
228238
sys.modules['pwd'] = None # Blocks pwd from being imported.
229239
project_id = self.callFUT('test-project-id')
@@ -232,6 +242,8 @@ def test_gae_without_expanduser(self):
232242
del sys.modules['pwd'] # Unblocks importing of pwd.
233243

234244
def test_info_value_not_present(self):
245+
import shutil
246+
shutil.rmtree(self.temp_config_path)
235247
project_id = self.callFUT()
236248
self.assertEqual(None, project_id)
237249

0 commit comments

Comments
 (0)