27
27
skipInVenv = unittest .skipIf (sys .prefix != sys .base_prefix ,
28
28
'Test not appropriate in a venv' )
29
29
30
+ def check_output (cmd , encoding = None ):
31
+ p = subprocess .Popen (cmd ,
32
+ stdout = subprocess .PIPE ,
33
+ stderr = subprocess .PIPE ,
34
+ encoding = encoding )
35
+ out , err = p .communicate ()
36
+ if p .returncode :
37
+ raise subprocess .CalledProcessError (
38
+ p .returncode , cmd , None , out , err )
39
+ return out , err
40
+
30
41
class BaseTest (unittest .TestCase ):
31
42
"""Base class for venv tests."""
32
43
maxDiff = 80 * 50
@@ -134,9 +145,7 @@ def test_prefixes(self):
134
145
('base_prefix' , sys .prefix ),
135
146
('base_exec_prefix' , sys .exec_prefix )):
136
147
cmd [2 ] = 'import sys; print(sys.%s)' % prefix
137
- p = subprocess .Popen (cmd , stdout = subprocess .PIPE ,
138
- stderr = subprocess .PIPE )
139
- out , err = p .communicate ()
148
+ out , err = check_output (cmd )
140
149
self .assertEqual (out .strip (), expected .encode ())
141
150
142
151
if sys .platform == 'win32' :
@@ -259,11 +268,10 @@ def test_executable(self):
259
268
"""
260
269
rmtree (self .env_dir )
261
270
self .run_with_capture (venv .create , self .env_dir )
262
- envpy = os .path .join (os .path .realpath (self .env_dir ), self .bindir , self .exe )
263
- cmd = [envpy , '-c' , 'import sys; print(sys.executable)' ]
264
- p = subprocess .Popen (cmd , stdout = subprocess .PIPE ,
265
- stderr = subprocess .PIPE )
266
- out , err = p .communicate ()
271
+ envpy = os .path .join (os .path .realpath (self .env_dir ),
272
+ self .bindir , self .exe )
273
+ out , err = check_output ([envpy , '-c' ,
274
+ 'import sys; print(sys.executable)' ])
267
275
self .assertEqual (out .strip (), envpy .encode ())
268
276
269
277
@unittest .skipUnless (can_symlink (), 'Needs symlinks' )
@@ -274,30 +282,27 @@ def test_executable_symlinks(self):
274
282
rmtree (self .env_dir )
275
283
builder = venv .EnvBuilder (clear = True , symlinks = True )
276
284
builder .create (self .env_dir )
277
- envpy = os .path .join (os .path .realpath (self .env_dir ), self .bindir , self .exe )
278
- cmd = [envpy , '-c' , 'import sys; print(sys.executable)' ]
279
- p = subprocess .Popen (cmd , stdout = subprocess .PIPE ,
280
- stderr = subprocess .PIPE )
281
- out , err = p .communicate ()
285
+ envpy = os .path .join (os .path .realpath (self .env_dir ),
286
+ self .bindir , self .exe )
287
+ out , err = check_output ([envpy , '-c' ,
288
+ 'import sys; print(sys.executable)' ])
282
289
self .assertEqual (out .strip (), envpy .encode ())
283
290
284
291
@unittest .skipUnless (os .name == 'nt' , 'only relevant on Windows' )
285
292
def test_unicode_in_batch_file (self ):
286
293
"""
287
- Test isolation from system site-packages
294
+ Test handling of Unicode paths
288
295
"""
289
296
rmtree (self .env_dir )
290
297
env_dir = os .path .join (os .path .realpath (self .env_dir ), 'ϼўТλФЙ' )
291
298
builder = venv .EnvBuilder (clear = True )
292
299
builder .create (env_dir )
293
300
activate = os .path .join (env_dir , self .bindir , 'activate.bat' )
294
301
envpy = os .path .join (env_dir , self .bindir , self .exe )
295
- cmd = [activate , '&' , self .exe , '-c' , 'print(0)' ]
296
- p = subprocess .Popen (cmd , stdout = subprocess .PIPE ,
297
- stderr = subprocess .PIPE , encoding = 'oem' ,
298
- shell = True )
299
- out , err = p .communicate ()
300
- print (err )
302
+ out , err = check_output (
303
+ [activate , '&' , self .exe , '-c' , 'print(0)' ],
304
+ encoding = 'oem' ,
305
+ )
301
306
self .assertEqual (out .strip (), '0' )
302
307
303
308
@skipInVenv
@@ -306,11 +311,8 @@ class EnsurePipTest(BaseTest):
306
311
def assert_pip_not_installed (self ):
307
312
envpy = os .path .join (os .path .realpath (self .env_dir ),
308
313
self .bindir , self .exe )
309
- try_import = 'try:\n import pip\n except ImportError:\n print("OK")'
310
- cmd = [envpy , '-c' , try_import ]
311
- p = subprocess .Popen (cmd , stdout = subprocess .PIPE ,
312
- stderr = subprocess .PIPE )
313
- out , err = p .communicate ()
314
+ out , err = check_output ([envpy , '-c' ,
315
+ 'try:\n import pip\n except ImportError:\n print("OK")' ])
314
316
# We force everything to text, so unittest gives the detailed diff
315
317
# if we get unexpected results
316
318
err = err .decode ("latin-1" ) # Force to text, prevent decoding errors
@@ -388,11 +390,8 @@ def do_test_with_pip(self, system_site_packages):
388
390
# Ensure pip is available in the virtual environment
389
391
envpy = os .path .join (os .path .realpath (self .env_dir ), self .bindir , self .exe )
390
392
# Ignore DeprecationWarning since pip code is not part of Python
391
- cmd = [envpy , '-W' , 'ignore::DeprecationWarning' , '-I' ,
392
- '-m' , 'pip' , '--version' ]
393
- p = subprocess .Popen (cmd , stdout = subprocess .PIPE ,
394
- stderr = subprocess .PIPE )
395
- out , err = p .communicate ()
393
+ out , err = check_output ([envpy , '-W' , 'ignore::DeprecationWarning' , '-I' ,
394
+ '-m' , 'pip' , '--version' ])
396
395
# We force everything to text, so unittest gives the detailed diff
397
396
# if we get unexpected results
398
397
err = err .decode ("latin-1" ) # Force to text, prevent decoding errors
@@ -406,12 +405,10 @@ def do_test_with_pip(self, system_site_packages):
406
405
# http://bugs.python.org/issue19728
407
406
# Check the private uninstall command provided for the Windows
408
407
# installers works (at least in a virtual environment)
409
- cmd = [envpy , '-W' , 'ignore::DeprecationWarning' , '-I' ,
410
- '-m' , 'ensurepip._uninstall' ]
411
408
with EnvironmentVarGuard () as envvars :
412
- p = subprocess . Popen ( cmd , stdout = subprocess . PIPE ,
413
- stderr = subprocess . PIPE )
414
- out , err = p . communicate ( )
409
+ out , err = check_output ([ envpy ,
410
+ '-W' , 'ignore::DeprecationWarning' , '-I' ,
411
+ '-m' , 'ensurepip._uninstall' ] )
415
412
# We force everything to text, so unittest gives the detailed diff
416
413
# if we get unexpected results
417
414
err = err .decode ("latin-1" ) # Force to text, prevent decoding errors
0 commit comments