Skip to content

Commit 5125599

Browse files
committed
Switch to Popen for py26
1 parent 1dcc847 commit 5125599

File tree

2 files changed

+12
-23
lines changed

2 files changed

+12
-23
lines changed

gcloud/_helpers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,9 @@ def _default_service_project_id():
184184
:rtype: str or ``NoneType``
185185
:returns: Project-ID from ``gcloud info`` else ``None``
186186
"""
187-
gcloud_project_conf = subprocess.check_output(['gcloud', 'info'])
187+
command = subprocess.Popen(['gcloud info'],
188+
stdout=subprocess.PIPE, shell=True)
189+
gcloud_project_conf = command.communicate()[0].decode('utf-8')
188190
gcloud_project_conf = gcloud_project_conf.split('\n')
189191

190192
for key in gcloud_project_conf:

gcloud/test__helpers.py

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,8 @@ def setUp(self):
160160

161161
def tearDown(self):
162162
import os
163-
if self.old_env:
164-
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = self.old_env
165-
elif (not self.old_env and
166-
'GOOGLE_APPLICATION_CREDENTIALS' in os.environ):
163+
if (not self.old_env and
164+
'GOOGLE_APPLICATION_CREDENTIALS' in os.environ):
167165
del os.environ['GOOGLE_APPLICATION_CREDENTIALS']
168166

169167
def test_success(self):
@@ -177,9 +175,6 @@ def test_success(self):
177175
self.assertEqual('test-project-id', self._callFUT())
178176

179177
def test_no_environment(self):
180-
import os
181-
if 'GOOGLE_APPLICATION_CREDENTIALS' in os.environ:
182-
del os.environ['GOOGLE_APPLICATION_CREDENTIALS']
183178
self.assertEqual(None, self._callFUT())
184179

185180

@@ -188,30 +183,22 @@ class Test__get_default_service_project_id(unittest2.TestCase):
188183
def callFUT(self, project_result=''):
189184
from gcloud._helpers import _default_service_project_id
190185
import subprocess
191-
self.check_output_called_with = []
192186

193-
def check_output_mock(called_with=None):
194-
self.check_output_called_with = called_with
195-
return project_result
187+
def popen_communicate_mock(popen_object):
188+
popen_object.kill()
189+
return (project_result,)
196190

197191
from gcloud._testing import _Monkey
198-
with _Monkey(subprocess, check_output=check_output_mock):
199-
return _default_service_project_id(), self.check_output_called_with
192+
with _Monkey(subprocess.Popen, communicate=popen_communicate_mock):
193+
return _default_service_project_id()
200194

201195
def test_read_from_cli_info(self):
202-
project_id, called_with = self.callFUT('Project: [test-project-id]')
196+
project_id = self.callFUT(b'Project: [test-project-id]')
203197
self.assertEqual('test-project-id', project_id)
204-
self.assertEqual(['gcloud', 'info'], called_with)
205-
206-
def test_cli_info_not_set(self):
207-
project_id, called_with = self.callFUT()
208-
self.assertEqual(None, project_id)
209-
self.assertEqual(['gcloud', 'info'], called_with)
210198

211199
def test_info_value_not_present(self):
212-
project_id, called_with = self.callFUT('Active Configuration')
200+
project_id = self.callFUT(b'Active Configuration')
213201
self.assertEqual(None, project_id)
214-
self.assertEqual(['gcloud', 'info'], called_with)
215202

216203

217204
class Test__compute_engine_id(unittest2.TestCase):

0 commit comments

Comments
 (0)