2727skipInVenv = unittest .skipIf (sys .prefix != sys .base_prefix ,
2828 'Test not appropriate in a venv' )
2929
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+
3041class BaseTest (unittest .TestCase ):
3142 """Base class for venv tests."""
3243 maxDiff = 80 * 50
@@ -134,9 +145,7 @@ def test_prefixes(self):
134145 ('base_prefix' , sys .prefix ),
135146 ('base_exec_prefix' , sys .exec_prefix )):
136147 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 )
140149 self .assertEqual (out .strip (), expected .encode ())
141150
142151 if sys .platform == 'win32' :
@@ -259,11 +268,10 @@ def test_executable(self):
259268 """
260269 rmtree (self .env_dir )
261270 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)' ])
267275 self .assertEqual (out .strip (), envpy .encode ())
268276
269277 @unittest .skipUnless (can_symlink (), 'Needs symlinks' )
@@ -274,30 +282,27 @@ def test_executable_symlinks(self):
274282 rmtree (self .env_dir )
275283 builder = venv .EnvBuilder (clear = True , symlinks = True )
276284 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)' ])
282289 self .assertEqual (out .strip (), envpy .encode ())
283290
284291 @unittest .skipUnless (os .name == 'nt' , 'only relevant on Windows' )
285292 def test_unicode_in_batch_file (self ):
286293 """
287- Test isolation from system site-packages
294+ Test handling of Unicode paths
288295 """
289296 rmtree (self .env_dir )
290297 env_dir = os .path .join (os .path .realpath (self .env_dir ), 'ϼўТλФЙ' )
291298 builder = venv .EnvBuilder (clear = True )
292299 builder .create (env_dir )
293300 activate = os .path .join (env_dir , self .bindir , 'activate.bat' )
294301 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+ )
301306 self .assertEqual (out .strip (), '0' )
302307
303308@skipInVenv
@@ -306,11 +311,8 @@ class EnsurePipTest(BaseTest):
306311 def assert_pip_not_installed (self ):
307312 envpy = os .path .join (os .path .realpath (self .env_dir ),
308313 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")' ])
314316 # We force everything to text, so unittest gives the detailed diff
315317 # if we get unexpected results
316318 err = err .decode ("latin-1" ) # Force to text, prevent decoding errors
@@ -388,11 +390,8 @@ def do_test_with_pip(self, system_site_packages):
388390 # Ensure pip is available in the virtual environment
389391 envpy = os .path .join (os .path .realpath (self .env_dir ), self .bindir , self .exe )
390392 # 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' ])
396395 # We force everything to text, so unittest gives the detailed diff
397396 # if we get unexpected results
398397 err = err .decode ("latin-1" ) # Force to text, prevent decoding errors
@@ -406,12 +405,10 @@ def do_test_with_pip(self, system_site_packages):
406405 # http://bugs.python.org/issue19728
407406 # Check the private uninstall command provided for the Windows
408407 # installers works (at least in a virtual environment)
409- cmd = [envpy , '-W' , 'ignore::DeprecationWarning' , '-I' ,
410- '-m' , 'ensurepip._uninstall' ]
411408 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' ] )
415412 # We force everything to text, so unittest gives the detailed diff
416413 # if we get unexpected results
417414 err = err .decode ("latin-1" ) # Force to text, prevent decoding errors
0 commit comments