blob: f4ad7c7c5c86a045bcf37bac1c33a6001c916b47 [file] [log] [blame]
Vinay Sajip42211422012-05-26 20:36:12 +01001"""
2Test harness for the venv module.
Vinay Sajip7ded1f02012-05-26 03:45:29 +01003
Vinay Sajip42211422012-05-26 20:36:12 +01004Copyright (C) 2011-2012 Vinay Sajip.
Vinay Sajip28952442012-06-25 00:47:46 +01005Licensed to the PSF under a contributor agreement.
Vinay Sajip7ded1f02012-05-26 03:45:29 +01006"""
7
Nick Coghlan1b1b1782013-11-30 15:56:58 +10008import ensurepip
Vinay Sajip7ded1f02012-05-26 03:45:29 +01009import os
10import os.path
Victor Stinner87d6e132016-03-14 18:21:58 +010011import re
Vinay Sajip1e53f8d2014-04-15 11:18:10 +010012import struct
Vinay Sajip3874e542012-07-03 16:56:40 +010013import subprocess
Vinay Sajip7ded1f02012-05-26 03:45:29 +010014import sys
15import tempfile
Zachary Ware38c707e2015-04-13 15:00:43 -050016from test.support import (captured_stdout, captured_stderr,
Victor Stinner866c4e22014-10-10 14:23:00 +020017 can_symlink, EnvironmentVarGuard, rmtree)
Vinay Sajip7ded1f02012-05-26 03:45:29 +010018import unittest
19import venv
Nick Coghlanae2ee962013-12-23 23:07:07 +100020
21# pip currently requires ssl support, so we ensure we handle
22# it being missing (http://bugs.python.org/issue19744)
Nick Coghlan878d2582013-11-24 12:45:25 +100023try:
24 import ssl
25except ImportError:
26 ssl = None
Vinay Sajip7ded1f02012-05-26 03:45:29 +010027
Berker Peksag1b25eff2016-01-19 02:01:53 +020028try:
29 import threading
30except ImportError:
31 threading = None
32
Victor Stinnerb3477882016-03-25 12:27:02 +010033try:
34 import ctypes
35except ImportError:
36 ctypes = None
37
Nick Coghlan8fbdb092013-11-23 00:30:34 +100038skipInVenv = unittest.skipIf(sys.prefix != sys.base_prefix,
39 'Test not appropriate in a venv')
40
Vinay Sajip7ded1f02012-05-26 03:45:29 +010041class BaseTest(unittest.TestCase):
42 """Base class for venv tests."""
Victor Stinnerbdc337b2016-03-25 12:30:40 +010043 maxDiff = 80 * 50
Vinay Sajip7ded1f02012-05-26 03:45:29 +010044
45 def setUp(self):
Ned Deily045bd532012-07-13 15:48:04 -070046 self.env_dir = os.path.realpath(tempfile.mkdtemp())
Vinay Sajip7ded1f02012-05-26 03:45:29 +010047 if os.name == 'nt':
48 self.bindir = 'Scripts'
Vinay Sajip7ded1f02012-05-26 03:45:29 +010049 self.lib = ('Lib',)
50 self.include = 'Include'
Vinay Sajip7ded1f02012-05-26 03:45:29 +010051 else:
52 self.bindir = 'bin'
Serhiy Storchaka885bdc42016-02-11 13:10:36 +020053 self.lib = ('lib', 'python%d.%d' % sys.version_info[:2])
Vinay Sajip7ded1f02012-05-26 03:45:29 +010054 self.include = 'include'
Vinay Sajip28952442012-06-25 00:47:46 +010055 if sys.platform == 'darwin' and '__PYVENV_LAUNCHER__' in os.environ:
56 executable = os.environ['__PYVENV_LAUNCHER__']
Vinay Sajip382a7c02012-05-28 16:34:47 +010057 else:
58 executable = sys.executable
59 self.exe = os.path.split(executable)[-1]
Vinay Sajip7ded1f02012-05-26 03:45:29 +010060
61 def tearDown(self):
Victor Stinner866c4e22014-10-10 14:23:00 +020062 rmtree(self.env_dir)
Vinay Sajip7ded1f02012-05-26 03:45:29 +010063
64 def run_with_capture(self, func, *args, **kwargs):
65 with captured_stdout() as output:
66 with captured_stderr() as error:
67 func(*args, **kwargs)
68 return output.getvalue(), error.getvalue()
69
70 def get_env_file(self, *args):
71 return os.path.join(self.env_dir, *args)
72
73 def get_text_file_contents(self, *args):
74 with open(self.get_env_file(*args), 'r') as f:
75 result = f.read()
76 return result
77
78class BasicTest(BaseTest):
79 """Test venv module functionality."""
80
Vinay Sajipb3b49cd2012-05-27 18:39:22 +010081 def isdir(self, *args):
82 fn = self.get_env_file(*args)
83 self.assertTrue(os.path.isdir(fn))
84
Vinay Sajip7ded1f02012-05-26 03:45:29 +010085 def test_defaults(self):
86 """
87 Test the create function with default arguments.
88 """
Victor Stinner866c4e22014-10-10 14:23:00 +020089 rmtree(self.env_dir)
Vinay Sajip7ded1f02012-05-26 03:45:29 +010090 self.run_with_capture(venv.create, self.env_dir)
Vinay Sajipb3b49cd2012-05-27 18:39:22 +010091 self.isdir(self.bindir)
92 self.isdir(self.include)
93 self.isdir(*self.lib)
Vinay Sajip1e53f8d2014-04-15 11:18:10 +010094 # Issue 21197
95 p = self.get_env_file('lib64')
96 conditions = ((struct.calcsize('P') == 8) and (os.name == 'posix') and
97 (sys.platform != 'darwin'))
98 if conditions:
99 self.assertTrue(os.path.islink(p))
100 else:
101 self.assertFalse(os.path.exists(p))
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100102 data = self.get_text_file_contents('pyvenv.cfg')
Vinay Sajip28952442012-06-25 00:47:46 +0100103 if sys.platform == 'darwin' and ('__PYVENV_LAUNCHER__'
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100104 in os.environ):
Vinay Sajip28952442012-06-25 00:47:46 +0100105 executable = os.environ['__PYVENV_LAUNCHER__']
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100106 else:
107 executable = sys.executable
108 path = os.path.dirname(executable)
109 self.assertIn('home = %s' % path, data)
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100110 fn = self.get_env_file(self.bindir, self.exe)
Vinay Sajip7e203492012-05-27 17:30:09 +0100111 if not os.path.exists(fn): # diagnostics for Windows buildbot failures
Vinay Sajipb3b49cd2012-05-27 18:39:22 +0100112 bd = self.get_env_file(self.bindir)
113 print('Contents of %r:' % bd)
114 print(' %r' % os.listdir(bd))
Vinay Sajip7e203492012-05-27 17:30:09 +0100115 self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn)
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100116
Vinay Sajipfd0f84b2016-08-06 10:43:44 +0100117 def test_prompt(self):
118 env_name = os.path.split(self.env_dir)[1]
119
120 builder = venv.EnvBuilder()
121 context = builder.ensure_directories(self.env_dir)
122 self.assertEqual(context.prompt, '(%s) ' % env_name)
123
124 builder = venv.EnvBuilder(prompt='My prompt')
125 context = builder.ensure_directories(self.env_dir)
126 self.assertEqual(context.prompt, '(My prompt) ')
127
Nick Coghlan8fbdb092013-11-23 00:30:34 +1000128 @skipInVenv
Vinay Sajip3874e542012-07-03 16:56:40 +0100129 def test_prefixes(self):
130 """
131 Test that the prefix values are as expected.
132 """
133 #check our prefixes
134 self.assertEqual(sys.base_prefix, sys.prefix)
135 self.assertEqual(sys.base_exec_prefix, sys.exec_prefix)
136
137 # check a venv's prefixes
Victor Stinner866c4e22014-10-10 14:23:00 +0200138 rmtree(self.env_dir)
Vinay Sajip3874e542012-07-03 16:56:40 +0100139 self.run_with_capture(venv.create, self.env_dir)
140 envpy = os.path.join(self.env_dir, self.bindir, self.exe)
141 cmd = [envpy, '-c', None]
142 for prefix, expected in (
143 ('prefix', self.env_dir),
144 ('prefix', self.env_dir),
145 ('base_prefix', sys.prefix),
146 ('base_exec_prefix', sys.exec_prefix)):
147 cmd[2] = 'import sys; print(sys.%s)' % prefix
148 p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
149 stderr=subprocess.PIPE)
150 out, err = p.communicate()
Antoine Pitrou9c92a692012-08-05 00:33:10 +0200151 self.assertEqual(out.strip(), expected.encode())
Vinay Sajip3874e542012-07-03 16:56:40 +0100152
Vinay Sajipbd40d3e2012-10-11 17:22:45 +0100153 if sys.platform == 'win32':
154 ENV_SUBDIRS = (
155 ('Scripts',),
156 ('Include',),
157 ('Lib',),
158 ('Lib', 'site-packages'),
159 )
160 else:
161 ENV_SUBDIRS = (
162 ('bin',),
163 ('include',),
164 ('lib',),
165 ('lib', 'python%d.%d' % sys.version_info[:2]),
166 ('lib', 'python%d.%d' % sys.version_info[:2], 'site-packages'),
167 )
168
169 def create_contents(self, paths, filename):
170 """
171 Create some files in the environment which are unrelated
172 to the virtual environment.
173 """
174 for subdirs in paths:
175 d = os.path.join(self.env_dir, *subdirs)
176 os.mkdir(d)
177 fn = os.path.join(d, filename)
178 with open(fn, 'wb') as f:
179 f.write(b'Still here?')
180
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100181 def test_overwrite_existing(self):
182 """
Vinay Sajipbd40d3e2012-10-11 17:22:45 +0100183 Test creating environment in an existing directory.
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100184 """
Vinay Sajipbd40d3e2012-10-11 17:22:45 +0100185 self.create_contents(self.ENV_SUBDIRS, 'foo')
186 venv.create(self.env_dir)
187 for subdirs in self.ENV_SUBDIRS:
188 fn = os.path.join(self.env_dir, *(subdirs + ('foo',)))
189 self.assertTrue(os.path.exists(fn))
190 with open(fn, 'rb') as f:
191 self.assertEqual(f.read(), b'Still here?')
192
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100193 builder = venv.EnvBuilder(clear=True)
194 builder.create(self.env_dir)
Vinay Sajipbd40d3e2012-10-11 17:22:45 +0100195 for subdirs in self.ENV_SUBDIRS:
196 fn = os.path.join(self.env_dir, *(subdirs + ('foo',)))
197 self.assertFalse(os.path.exists(fn))
198
199 def clear_directory(self, path):
200 for fn in os.listdir(path):
201 fn = os.path.join(path, fn)
202 if os.path.islink(fn) or os.path.isfile(fn):
203 os.remove(fn)
204 elif os.path.isdir(fn):
Victor Stinner866c4e22014-10-10 14:23:00 +0200205 rmtree(fn)
Vinay Sajipbd40d3e2012-10-11 17:22:45 +0100206
207 def test_unoverwritable_fails(self):
208 #create a file clashing with directories in the env dir
209 for paths in self.ENV_SUBDIRS[:3]:
210 fn = os.path.join(self.env_dir, *paths)
211 with open(fn, 'wb') as f:
212 f.write(b'')
213 self.assertRaises((ValueError, OSError), venv.create, self.env_dir)
214 self.clear_directory(self.env_dir)
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100215
Vinay Sajipb3b49cd2012-05-27 18:39:22 +0100216 def test_upgrade(self):
217 """
218 Test upgrading an existing environment directory.
219 """
Vinay Sajipb9b965f2014-06-03 16:47:51 +0100220 # See Issue #21643: the loop needs to run twice to ensure
221 # that everything works on the upgrade (the first run just creates
222 # the venv).
223 for upgrade in (False, True):
224 builder = venv.EnvBuilder(upgrade=upgrade)
225 self.run_with_capture(builder.create, self.env_dir)
226 self.isdir(self.bindir)
227 self.isdir(self.include)
228 self.isdir(*self.lib)
229 fn = self.get_env_file(self.bindir, self.exe)
230 if not os.path.exists(fn):
231 # diagnostics for Windows buildbot failures
232 bd = self.get_env_file(self.bindir)
233 print('Contents of %r:' % bd)
234 print(' %r' % os.listdir(bd))
235 self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn)
Vinay Sajipb3b49cd2012-05-27 18:39:22 +0100236
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100237 def test_isolation(self):
238 """
239 Test isolation from system site-packages
240 """
241 for ssp, s in ((True, 'true'), (False, 'false')):
242 builder = venv.EnvBuilder(clear=True, system_site_packages=ssp)
243 builder.create(self.env_dir)
244 data = self.get_text_file_contents('pyvenv.cfg')
245 self.assertIn('include-system-site-packages = %s\n' % s, data)
246
247 @unittest.skipUnless(can_symlink(), 'Needs symlinks')
248 def test_symlinking(self):
249 """
250 Test symlinking works as expected
251 """
252 for usl in (False, True):
253 builder = venv.EnvBuilder(clear=True, symlinks=usl)
Vinay Sajip90db6612012-07-17 17:33:46 +0100254 builder.create(self.env_dir)
255 fn = self.get_env_file(self.bindir, self.exe)
256 # Don't test when False, because e.g. 'python' is always
257 # symlinked to 'python3.3' in the env, even when symlinking in
258 # general isn't wanted.
259 if usl:
260 self.assertTrue(os.path.islink(fn))
261
262 # If a venv is created from a source build and that venv is used to
263 # run the test, the pyvenv.cfg in the venv created in the test will
264 # point to the venv being used to run the test, and we lose the link
265 # to the source build - so Python can't initialise properly.
Nick Coghlan8fbdb092013-11-23 00:30:34 +1000266 @skipInVenv
Vinay Sajip90db6612012-07-17 17:33:46 +0100267 def test_executable(self):
268 """
269 Test that the sys.executable value is as expected.
270 """
Victor Stinner866c4e22014-10-10 14:23:00 +0200271 rmtree(self.env_dir)
Vinay Sajip90db6612012-07-17 17:33:46 +0100272 self.run_with_capture(venv.create, self.env_dir)
273 envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe)
274 cmd = [envpy, '-c', 'import sys; print(sys.executable)']
275 p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
276 stderr=subprocess.PIPE)
277 out, err = p.communicate()
Antoine Pitrou9c92a692012-08-05 00:33:10 +0200278 self.assertEqual(out.strip(), envpy.encode())
Vinay Sajip90db6612012-07-17 17:33:46 +0100279
280 @unittest.skipUnless(can_symlink(), 'Needs symlinks')
281 def test_executable_symlinks(self):
282 """
283 Test that the sys.executable value is as expected.
284 """
Victor Stinner866c4e22014-10-10 14:23:00 +0200285 rmtree(self.env_dir)
Vinay Sajip90db6612012-07-17 17:33:46 +0100286 builder = venv.EnvBuilder(clear=True, symlinks=True)
287 builder.create(self.env_dir)
288 envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe)
289 cmd = [envpy, '-c', 'import sys; print(sys.executable)']
290 p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
291 stderr=subprocess.PIPE)
292 out, err = p.communicate()
Antoine Pitrou9c92a692012-08-05 00:33:10 +0200293 self.assertEqual(out.strip(), envpy.encode())
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100294
Nick Coghlan8fbdb092013-11-23 00:30:34 +1000295
296@skipInVenv
297class EnsurePipTest(BaseTest):
298 """Test venv module installation of pip."""
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000299 def assert_pip_not_installed(self):
300 envpy = os.path.join(os.path.realpath(self.env_dir),
301 self.bindir, self.exe)
302 try_import = 'try:\n import pip\nexcept ImportError:\n print("OK")'
303 cmd = [envpy, '-c', try_import]
304 p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
305 stderr=subprocess.PIPE)
306 out, err = p.communicate()
307 # We force everything to text, so unittest gives the detailed diff
308 # if we get unexpected results
309 err = err.decode("latin-1") # Force to text, prevent decoding errors
310 self.assertEqual(err, "")
311 out = out.decode("latin-1") # Force to text, prevent decoding errors
312 self.assertEqual(out.strip(), "OK")
313
Nick Coghlan8fbdb092013-11-23 00:30:34 +1000314
315 def test_no_pip_by_default(self):
Victor Stinner866c4e22014-10-10 14:23:00 +0200316 rmtree(self.env_dir)
Nick Coghlan8fbdb092013-11-23 00:30:34 +1000317 self.run_with_capture(venv.create, self.env_dir)
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000318 self.assert_pip_not_installed()
Nick Coghlan8fbdb092013-11-23 00:30:34 +1000319
320 def test_explicit_no_pip(self):
Victor Stinner866c4e22014-10-10 14:23:00 +0200321 rmtree(self.env_dir)
Nick Coghlan8fbdb092013-11-23 00:30:34 +1000322 self.run_with_capture(venv.create, self.env_dir, with_pip=False)
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000323 self.assert_pip_not_installed()
Nick Coghlan8fbdb092013-11-23 00:30:34 +1000324
Victor Stinnerbdc337b2016-03-25 12:30:40 +0100325 def test_devnull(self):
Nick Coghlan456ab5d2014-02-05 23:54:55 +1000326 # Fix for issue #20053 uses os.devnull to force a config file to
Nick Coghlan11c5afd2014-02-07 23:46:38 +1000327 # appear empty. However http://bugs.python.org/issue20541 means
328 # that doesn't currently work properly on Windows. Once that is
329 # fixed, the "win_location" part of test_with_pip should be restored
Nick Coghlan456ab5d2014-02-05 23:54:55 +1000330 with open(os.devnull, "rb") as f:
331 self.assertEqual(f.read(), b"")
332
Victor Stinnerbdc337b2016-03-25 12:30:40 +0100333 # Issue #20541: os.path.exists('nul') is False on Windows
334 if os.devnull.lower() == 'nul':
335 self.assertFalse(os.path.exists(os.devnull))
336 else:
337 self.assertTrue(os.path.exists(os.devnull))
338
339
Nick Coghlanae2ee962013-12-23 23:07:07 +1000340 # Requesting pip fails without SSL (http://bugs.python.org/issue19744)
341 @unittest.skipIf(ssl is None, ensurepip._MISSING_SSL_MESSAGE)
Berker Peksag1b25eff2016-01-19 02:01:53 +0200342 @unittest.skipUnless(threading, 'some dependencies of pip import threading'
343 ' module unconditionally')
Victor Stinnerb3477882016-03-25 12:27:02 +0100344 # Issue #26610: pip/pep425tags.py requires ctypes
345 @unittest.skipUnless(ctypes, 'pip requires ctypes')
Nick Coghlan8fbdb092013-11-23 00:30:34 +1000346 def test_with_pip(self):
Victor Stinner866c4e22014-10-10 14:23:00 +0200347 rmtree(self.env_dir)
Nick Coghland76cdc12013-11-23 11:37:28 +1000348 with EnvironmentVarGuard() as envvars:
349 # pip's cross-version compatibility may trigger deprecation
350 # warnings in current versions of Python. Ensure related
351 # environment settings don't cause venv to fail.
352 envvars["PYTHONWARNINGS"] = "e"
Nick Coghlan6256fcb2013-12-23 16:16:07 +1000353 # ensurepip is different enough from a normal pip invocation
354 # that we want to ensure it ignores the normal pip environment
355 # variable settings. We set PIP_NO_INSTALL here specifically
356 # to check that ensurepip (and hence venv) ignores it.
Nick Coghlan6edd82a2014-02-04 23:02:36 +1000357 # See http://bugs.python.org/issue19734
Nick Coghlan6256fcb2013-12-23 16:16:07 +1000358 envvars["PIP_NO_INSTALL"] = "1"
Nick Coghlan6edd82a2014-02-04 23:02:36 +1000359 # Also check that we ignore the pip configuration file
360 # See http://bugs.python.org/issue20053
361 with tempfile.TemporaryDirectory() as home_dir:
362 envvars["HOME"] = home_dir
363 bad_config = "[global]\nno-install=1"
364 # Write to both config file names on all platforms to reduce
365 # cross-platform variation in test code behaviour
366 win_location = ("pip", "pip.ini")
367 posix_location = (".pip", "pip.conf")
Nick Coghlan11c5afd2014-02-07 23:46:38 +1000368 # Skips win_location due to http://bugs.python.org/issue20541
369 for dirname, fname in (posix_location,):
Nick Coghlan6edd82a2014-02-04 23:02:36 +1000370 dirpath = os.path.join(home_dir, dirname)
371 os.mkdir(dirpath)
372 fpath = os.path.join(dirpath, fname)
373 with open(fpath, 'w') as f:
374 f.write(bad_config)
375
376 # Actually run the create command with all that unhelpful
377 # config in place to ensure we ignore it
378 try:
379 self.run_with_capture(venv.create, self.env_dir,
380 with_pip=True)
381 except subprocess.CalledProcessError as exc:
382 # The output this produces can be a little hard to read,
383 # but at least it has all the details
384 details = exc.output.decode(errors="replace")
385 msg = "{}\n\n**Subprocess Output**\n{}"
386 self.fail(msg.format(exc, details))
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000387 # Ensure pip is available in the virtual environment
Nick Coghlan8fbdb092013-11-23 00:30:34 +1000388 envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe)
Nick Coghlan1d1d8342013-11-24 16:49:20 +1000389 cmd = [envpy, '-Im', 'pip', '--version']
Nick Coghlan8fbdb092013-11-23 00:30:34 +1000390 p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
Nick Coghlan1d1d8342013-11-24 16:49:20 +1000391 stderr=subprocess.PIPE)
Nick Coghlan8fbdb092013-11-23 00:30:34 +1000392 out, err = p.communicate()
Nick Coghlan6fd12f22013-11-24 11:36:31 +1000393 # We force everything to text, so unittest gives the detailed diff
394 # if we get unexpected results
395 err = err.decode("latin-1") # Force to text, prevent decoding errors
396 self.assertEqual(err, "")
397 out = out.decode("latin-1") # Force to text, prevent decoding errors
Nick Coghlan1b1b1782013-11-30 15:56:58 +1000398 expected_version = "pip {}".format(ensurepip.version())
399 self.assertEqual(out[:len(expected_version)], expected_version)
Nick Coghlan6fd12f22013-11-24 11:36:31 +1000400 env_dir = os.fsencode(self.env_dir).decode("latin-1")
Nick Coghlan6fd12f22013-11-24 11:36:31 +1000401 self.assertIn(env_dir, out)
Nick Coghlan8fbdb092013-11-23 00:30:34 +1000402
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000403 # http://bugs.python.org/issue19728
404 # Check the private uninstall command provided for the Windows
405 # installers works (at least in a virtual environment)
406 cmd = [envpy, '-Im', 'ensurepip._uninstall']
407 with EnvironmentVarGuard() as envvars:
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000408 p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
409 stderr=subprocess.PIPE)
410 out, err = p.communicate()
411 # We force everything to text, so unittest gives the detailed diff
412 # if we get unexpected results
413 err = err.decode("latin-1") # Force to text, prevent decoding errors
Victor Stinner87d6e132016-03-14 18:21:58 +0100414 # Ignore the warning:
415 # "The directory '$HOME/.cache/pip/http' or its parent directory
416 # is not owned by the current user and the cache has been disabled.
417 # Please check the permissions and owner of that directory. If
418 # executing pip with sudo, you may want sudo's -H flag."
419 # where $HOME is replaced by the HOME environment variable.
420 err = re.sub("^The directory .* or its parent directory is not owned "
421 "by the current user .*$", "", err, flags=re.MULTILINE)
422 self.assertEqual(err.rstrip(), "")
Nick Coghlan8ddd59e2013-11-30 18:35:32 +1000423 # Being fairly specific regarding the expected behaviour for the
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000424 # initial bundling phase in Python 3.4. If the output changes in
Nick Coghlan8ddd59e2013-11-30 18:35:32 +1000425 # future pip versions, this test can likely be relaxed further.
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000426 out = out.decode("latin-1") # Force to text, prevent decoding errors
Nick Coghlan8ddd59e2013-11-30 18:35:32 +1000427 self.assertIn("Successfully uninstalled pip", out)
428 self.assertIn("Successfully uninstalled setuptools", out)
429 # Check pip is now gone from the virtual environment
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000430 self.assert_pip_not_installed()
431
Nick Coghlan8fbdb092013-11-23 00:30:34 +1000432
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100433if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500434 unittest.main()