Vinay Sajip | 4221142 | 2012-05-26 20:36:12 +0100 | [diff] [blame] | 1 | """ |
| 2 | Test harness for the venv module. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 3 | |
Vinay Sajip | 4221142 | 2012-05-26 20:36:12 +0100 | [diff] [blame] | 4 | Copyright (C) 2011-2012 Vinay Sajip. |
Vinay Sajip | 2895244 | 2012-06-25 00:47:46 +0100 | [diff] [blame] | 5 | Licensed to the PSF under a contributor agreement. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 6 | """ |
| 7 | |
Nick Coghlan | 1b1b178 | 2013-11-30 15:56:58 +1000 | [diff] [blame] | 8 | import ensurepip |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 9 | import os |
| 10 | import os.path |
| 11 | import shutil |
Vinay Sajip | 1e53f8d | 2014-04-15 11:18:10 +0100 | [diff] [blame^] | 12 | import struct |
Vinay Sajip | 3874e54 | 2012-07-03 16:56:40 +0100 | [diff] [blame] | 13 | import subprocess |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 14 | import sys |
| 15 | import tempfile |
| 16 | from test.support import (captured_stdout, captured_stderr, run_unittest, |
Nick Coghlan | d76cdc1 | 2013-11-23 11:37:28 +1000 | [diff] [blame] | 17 | can_symlink, EnvironmentVarGuard) |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 18 | import textwrap |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 19 | import unittest |
| 20 | import venv |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 21 | |
| 22 | # pip currently requires ssl support, so we ensure we handle |
| 23 | # it being missing (http://bugs.python.org/issue19744) |
Nick Coghlan | 878d258 | 2013-11-24 12:45:25 +1000 | [diff] [blame] | 24 | try: |
| 25 | import ssl |
| 26 | except ImportError: |
| 27 | ssl = None |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 28 | |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 29 | skipInVenv = unittest.skipIf(sys.prefix != sys.base_prefix, |
| 30 | 'Test not appropriate in a venv') |
| 31 | |
Nick Coghlan | 11c5afd | 2014-02-07 23:46:38 +1000 | [diff] [blame] | 32 | # os.path.exists('nul') is False: http://bugs.python.org/issue20541 |
| 33 | if os.devnull.lower() == 'nul': |
| 34 | failsOnWindows = unittest.expectedFailure |
| 35 | else: |
| 36 | def failsOnWindows(f): |
| 37 | return f |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 38 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 39 | class BaseTest(unittest.TestCase): |
| 40 | """Base class for venv tests.""" |
| 41 | |
| 42 | def setUp(self): |
Ned Deily | 045bd53 | 2012-07-13 15:48:04 -0700 | [diff] [blame] | 43 | self.env_dir = os.path.realpath(tempfile.mkdtemp()) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 44 | if os.name == 'nt': |
| 45 | self.bindir = 'Scripts' |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 46 | self.lib = ('Lib',) |
| 47 | self.include = 'Include' |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 48 | else: |
| 49 | self.bindir = 'bin' |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 50 | self.lib = ('lib', 'python%s' % sys.version[:3]) |
| 51 | self.include = 'include' |
Vinay Sajip | 2895244 | 2012-06-25 00:47:46 +0100 | [diff] [blame] | 52 | if sys.platform == 'darwin' and '__PYVENV_LAUNCHER__' in os.environ: |
| 53 | executable = os.environ['__PYVENV_LAUNCHER__'] |
Vinay Sajip | 382a7c0 | 2012-05-28 16:34:47 +0100 | [diff] [blame] | 54 | else: |
| 55 | executable = sys.executable |
| 56 | self.exe = os.path.split(executable)[-1] |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 57 | |
| 58 | def tearDown(self): |
| 59 | shutil.rmtree(self.env_dir) |
| 60 | |
| 61 | def run_with_capture(self, func, *args, **kwargs): |
| 62 | with captured_stdout() as output: |
| 63 | with captured_stderr() as error: |
| 64 | func(*args, **kwargs) |
| 65 | return output.getvalue(), error.getvalue() |
| 66 | |
| 67 | def get_env_file(self, *args): |
| 68 | return os.path.join(self.env_dir, *args) |
| 69 | |
| 70 | def get_text_file_contents(self, *args): |
| 71 | with open(self.get_env_file(*args), 'r') as f: |
| 72 | result = f.read() |
| 73 | return result |
| 74 | |
| 75 | class BasicTest(BaseTest): |
| 76 | """Test venv module functionality.""" |
| 77 | |
Vinay Sajip | b3b49cd | 2012-05-27 18:39:22 +0100 | [diff] [blame] | 78 | def isdir(self, *args): |
| 79 | fn = self.get_env_file(*args) |
| 80 | self.assertTrue(os.path.isdir(fn)) |
| 81 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 82 | def test_defaults(self): |
| 83 | """ |
| 84 | Test the create function with default arguments. |
| 85 | """ |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 86 | shutil.rmtree(self.env_dir) |
| 87 | self.run_with_capture(venv.create, self.env_dir) |
Vinay Sajip | b3b49cd | 2012-05-27 18:39:22 +0100 | [diff] [blame] | 88 | self.isdir(self.bindir) |
| 89 | self.isdir(self.include) |
| 90 | self.isdir(*self.lib) |
Vinay Sajip | 1e53f8d | 2014-04-15 11:18:10 +0100 | [diff] [blame^] | 91 | # Issue 21197 |
| 92 | p = self.get_env_file('lib64') |
| 93 | conditions = ((struct.calcsize('P') == 8) and (os.name == 'posix') and |
| 94 | (sys.platform != 'darwin')) |
| 95 | if conditions: |
| 96 | self.assertTrue(os.path.islink(p)) |
| 97 | else: |
| 98 | self.assertFalse(os.path.exists(p)) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 99 | data = self.get_text_file_contents('pyvenv.cfg') |
Vinay Sajip | 2895244 | 2012-06-25 00:47:46 +0100 | [diff] [blame] | 100 | if sys.platform == 'darwin' and ('__PYVENV_LAUNCHER__' |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 101 | in os.environ): |
Vinay Sajip | 2895244 | 2012-06-25 00:47:46 +0100 | [diff] [blame] | 102 | executable = os.environ['__PYVENV_LAUNCHER__'] |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 103 | else: |
| 104 | executable = sys.executable |
| 105 | path = os.path.dirname(executable) |
| 106 | self.assertIn('home = %s' % path, data) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 107 | fn = self.get_env_file(self.bindir, self.exe) |
Vinay Sajip | 7e20349 | 2012-05-27 17:30:09 +0100 | [diff] [blame] | 108 | if not os.path.exists(fn): # diagnostics for Windows buildbot failures |
Vinay Sajip | b3b49cd | 2012-05-27 18:39:22 +0100 | [diff] [blame] | 109 | bd = self.get_env_file(self.bindir) |
| 110 | print('Contents of %r:' % bd) |
| 111 | print(' %r' % os.listdir(bd)) |
Vinay Sajip | 7e20349 | 2012-05-27 17:30:09 +0100 | [diff] [blame] | 112 | self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 113 | |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 114 | @skipInVenv |
Vinay Sajip | 3874e54 | 2012-07-03 16:56:40 +0100 | [diff] [blame] | 115 | def test_prefixes(self): |
| 116 | """ |
| 117 | Test that the prefix values are as expected. |
| 118 | """ |
| 119 | #check our prefixes |
| 120 | self.assertEqual(sys.base_prefix, sys.prefix) |
| 121 | self.assertEqual(sys.base_exec_prefix, sys.exec_prefix) |
| 122 | |
| 123 | # check a venv's prefixes |
| 124 | shutil.rmtree(self.env_dir) |
| 125 | self.run_with_capture(venv.create, self.env_dir) |
| 126 | envpy = os.path.join(self.env_dir, self.bindir, self.exe) |
| 127 | cmd = [envpy, '-c', None] |
| 128 | for prefix, expected in ( |
| 129 | ('prefix', self.env_dir), |
| 130 | ('prefix', self.env_dir), |
| 131 | ('base_prefix', sys.prefix), |
| 132 | ('base_exec_prefix', sys.exec_prefix)): |
| 133 | cmd[2] = 'import sys; print(sys.%s)' % prefix |
| 134 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
| 135 | stderr=subprocess.PIPE) |
| 136 | out, err = p.communicate() |
Antoine Pitrou | 9c92a69 | 2012-08-05 00:33:10 +0200 | [diff] [blame] | 137 | self.assertEqual(out.strip(), expected.encode()) |
Vinay Sajip | 3874e54 | 2012-07-03 16:56:40 +0100 | [diff] [blame] | 138 | |
Vinay Sajip | bd40d3e | 2012-10-11 17:22:45 +0100 | [diff] [blame] | 139 | if sys.platform == 'win32': |
| 140 | ENV_SUBDIRS = ( |
| 141 | ('Scripts',), |
| 142 | ('Include',), |
| 143 | ('Lib',), |
| 144 | ('Lib', 'site-packages'), |
| 145 | ) |
| 146 | else: |
| 147 | ENV_SUBDIRS = ( |
| 148 | ('bin',), |
| 149 | ('include',), |
| 150 | ('lib',), |
| 151 | ('lib', 'python%d.%d' % sys.version_info[:2]), |
| 152 | ('lib', 'python%d.%d' % sys.version_info[:2], 'site-packages'), |
| 153 | ) |
| 154 | |
| 155 | def create_contents(self, paths, filename): |
| 156 | """ |
| 157 | Create some files in the environment which are unrelated |
| 158 | to the virtual environment. |
| 159 | """ |
| 160 | for subdirs in paths: |
| 161 | d = os.path.join(self.env_dir, *subdirs) |
| 162 | os.mkdir(d) |
| 163 | fn = os.path.join(d, filename) |
| 164 | with open(fn, 'wb') as f: |
| 165 | f.write(b'Still here?') |
| 166 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 167 | def test_overwrite_existing(self): |
| 168 | """ |
Vinay Sajip | bd40d3e | 2012-10-11 17:22:45 +0100 | [diff] [blame] | 169 | Test creating environment in an existing directory. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 170 | """ |
Vinay Sajip | bd40d3e | 2012-10-11 17:22:45 +0100 | [diff] [blame] | 171 | self.create_contents(self.ENV_SUBDIRS, 'foo') |
| 172 | venv.create(self.env_dir) |
| 173 | for subdirs in self.ENV_SUBDIRS: |
| 174 | fn = os.path.join(self.env_dir, *(subdirs + ('foo',))) |
| 175 | self.assertTrue(os.path.exists(fn)) |
| 176 | with open(fn, 'rb') as f: |
| 177 | self.assertEqual(f.read(), b'Still here?') |
| 178 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 179 | builder = venv.EnvBuilder(clear=True) |
| 180 | builder.create(self.env_dir) |
Vinay Sajip | bd40d3e | 2012-10-11 17:22:45 +0100 | [diff] [blame] | 181 | for subdirs in self.ENV_SUBDIRS: |
| 182 | fn = os.path.join(self.env_dir, *(subdirs + ('foo',))) |
| 183 | self.assertFalse(os.path.exists(fn)) |
| 184 | |
| 185 | def clear_directory(self, path): |
| 186 | for fn in os.listdir(path): |
| 187 | fn = os.path.join(path, fn) |
| 188 | if os.path.islink(fn) or os.path.isfile(fn): |
| 189 | os.remove(fn) |
| 190 | elif os.path.isdir(fn): |
| 191 | shutil.rmtree(fn) |
| 192 | |
| 193 | def test_unoverwritable_fails(self): |
| 194 | #create a file clashing with directories in the env dir |
| 195 | for paths in self.ENV_SUBDIRS[:3]: |
| 196 | fn = os.path.join(self.env_dir, *paths) |
| 197 | with open(fn, 'wb') as f: |
| 198 | f.write(b'') |
| 199 | self.assertRaises((ValueError, OSError), venv.create, self.env_dir) |
| 200 | self.clear_directory(self.env_dir) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 201 | |
Vinay Sajip | b3b49cd | 2012-05-27 18:39:22 +0100 | [diff] [blame] | 202 | def test_upgrade(self): |
| 203 | """ |
| 204 | Test upgrading an existing environment directory. |
| 205 | """ |
| 206 | builder = venv.EnvBuilder(upgrade=True) |
| 207 | self.run_with_capture(builder.create, self.env_dir) |
| 208 | self.isdir(self.bindir) |
| 209 | self.isdir(self.include) |
| 210 | self.isdir(*self.lib) |
| 211 | fn = self.get_env_file(self.bindir, self.exe) |
| 212 | if not os.path.exists(fn): # diagnostics for Windows buildbot failures |
| 213 | bd = self.get_env_file(self.bindir) |
| 214 | print('Contents of %r:' % bd) |
| 215 | print(' %r' % os.listdir(bd)) |
| 216 | self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn) |
| 217 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 218 | def test_isolation(self): |
| 219 | """ |
| 220 | Test isolation from system site-packages |
| 221 | """ |
| 222 | for ssp, s in ((True, 'true'), (False, 'false')): |
| 223 | builder = venv.EnvBuilder(clear=True, system_site_packages=ssp) |
| 224 | builder.create(self.env_dir) |
| 225 | data = self.get_text_file_contents('pyvenv.cfg') |
| 226 | self.assertIn('include-system-site-packages = %s\n' % s, data) |
| 227 | |
| 228 | @unittest.skipUnless(can_symlink(), 'Needs symlinks') |
| 229 | def test_symlinking(self): |
| 230 | """ |
| 231 | Test symlinking works as expected |
| 232 | """ |
| 233 | for usl in (False, True): |
| 234 | builder = venv.EnvBuilder(clear=True, symlinks=usl) |
Vinay Sajip | 90db661 | 2012-07-17 17:33:46 +0100 | [diff] [blame] | 235 | builder.create(self.env_dir) |
| 236 | fn = self.get_env_file(self.bindir, self.exe) |
| 237 | # Don't test when False, because e.g. 'python' is always |
| 238 | # symlinked to 'python3.3' in the env, even when symlinking in |
| 239 | # general isn't wanted. |
| 240 | if usl: |
| 241 | self.assertTrue(os.path.islink(fn)) |
| 242 | |
| 243 | # If a venv is created from a source build and that venv is used to |
| 244 | # run the test, the pyvenv.cfg in the venv created in the test will |
| 245 | # point to the venv being used to run the test, and we lose the link |
| 246 | # to the source build - so Python can't initialise properly. |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 247 | @skipInVenv |
Vinay Sajip | 90db661 | 2012-07-17 17:33:46 +0100 | [diff] [blame] | 248 | def test_executable(self): |
| 249 | """ |
| 250 | Test that the sys.executable value is as expected. |
| 251 | """ |
| 252 | shutil.rmtree(self.env_dir) |
| 253 | self.run_with_capture(venv.create, self.env_dir) |
| 254 | envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe) |
| 255 | cmd = [envpy, '-c', 'import sys; print(sys.executable)'] |
| 256 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
| 257 | stderr=subprocess.PIPE) |
| 258 | out, err = p.communicate() |
Antoine Pitrou | 9c92a69 | 2012-08-05 00:33:10 +0200 | [diff] [blame] | 259 | self.assertEqual(out.strip(), envpy.encode()) |
Vinay Sajip | 90db661 | 2012-07-17 17:33:46 +0100 | [diff] [blame] | 260 | |
| 261 | @unittest.skipUnless(can_symlink(), 'Needs symlinks') |
| 262 | def test_executable_symlinks(self): |
| 263 | """ |
| 264 | Test that the sys.executable value is as expected. |
| 265 | """ |
| 266 | shutil.rmtree(self.env_dir) |
| 267 | builder = venv.EnvBuilder(clear=True, symlinks=True) |
| 268 | builder.create(self.env_dir) |
| 269 | envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe) |
| 270 | cmd = [envpy, '-c', 'import sys; print(sys.executable)'] |
| 271 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
| 272 | stderr=subprocess.PIPE) |
| 273 | out, err = p.communicate() |
Antoine Pitrou | 9c92a69 | 2012-08-05 00:33:10 +0200 | [diff] [blame] | 274 | self.assertEqual(out.strip(), envpy.encode()) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 275 | |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 276 | |
| 277 | @skipInVenv |
| 278 | class EnsurePipTest(BaseTest): |
| 279 | """Test venv module installation of pip.""" |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 280 | def assert_pip_not_installed(self): |
| 281 | envpy = os.path.join(os.path.realpath(self.env_dir), |
| 282 | self.bindir, self.exe) |
| 283 | try_import = 'try:\n import pip\nexcept ImportError:\n print("OK")' |
| 284 | cmd = [envpy, '-c', try_import] |
| 285 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
| 286 | stderr=subprocess.PIPE) |
| 287 | out, err = p.communicate() |
| 288 | # We force everything to text, so unittest gives the detailed diff |
| 289 | # if we get unexpected results |
| 290 | err = err.decode("latin-1") # Force to text, prevent decoding errors |
| 291 | self.assertEqual(err, "") |
| 292 | out = out.decode("latin-1") # Force to text, prevent decoding errors |
| 293 | self.assertEqual(out.strip(), "OK") |
| 294 | |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 295 | |
| 296 | def test_no_pip_by_default(self): |
| 297 | shutil.rmtree(self.env_dir) |
| 298 | self.run_with_capture(venv.create, self.env_dir) |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 299 | self.assert_pip_not_installed() |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 300 | |
| 301 | def test_explicit_no_pip(self): |
| 302 | shutil.rmtree(self.env_dir) |
| 303 | self.run_with_capture(venv.create, self.env_dir, with_pip=False) |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 304 | self.assert_pip_not_installed() |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 305 | |
Nick Coghlan | 11c5afd | 2014-02-07 23:46:38 +1000 | [diff] [blame] | 306 | @failsOnWindows |
Nick Coghlan | 456ab5d | 2014-02-05 23:54:55 +1000 | [diff] [blame] | 307 | def test_devnull_exists_and_is_empty(self): |
| 308 | # Fix for issue #20053 uses os.devnull to force a config file to |
Nick Coghlan | 11c5afd | 2014-02-07 23:46:38 +1000 | [diff] [blame] | 309 | # appear empty. However http://bugs.python.org/issue20541 means |
| 310 | # that doesn't currently work properly on Windows. Once that is |
| 311 | # fixed, the "win_location" part of test_with_pip should be restored |
Nick Coghlan | d49fa5e | 2014-02-07 22:28:18 +1000 | [diff] [blame] | 312 | self.assertTrue(os.path.exists(os.devnull)) |
Nick Coghlan | 456ab5d | 2014-02-05 23:54:55 +1000 | [diff] [blame] | 313 | with open(os.devnull, "rb") as f: |
| 314 | self.assertEqual(f.read(), b"") |
| 315 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 316 | # Requesting pip fails without SSL (http://bugs.python.org/issue19744) |
| 317 | @unittest.skipIf(ssl is None, ensurepip._MISSING_SSL_MESSAGE) |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 318 | def test_with_pip(self): |
| 319 | shutil.rmtree(self.env_dir) |
Nick Coghlan | d76cdc1 | 2013-11-23 11:37:28 +1000 | [diff] [blame] | 320 | with EnvironmentVarGuard() as envvars: |
| 321 | # pip's cross-version compatibility may trigger deprecation |
| 322 | # warnings in current versions of Python. Ensure related |
| 323 | # environment settings don't cause venv to fail. |
| 324 | envvars["PYTHONWARNINGS"] = "e" |
Nick Coghlan | 6256fcb | 2013-12-23 16:16:07 +1000 | [diff] [blame] | 325 | # ensurepip is different enough from a normal pip invocation |
| 326 | # that we want to ensure it ignores the normal pip environment |
| 327 | # variable settings. We set PIP_NO_INSTALL here specifically |
| 328 | # to check that ensurepip (and hence venv) ignores it. |
Nick Coghlan | 6edd82a | 2014-02-04 23:02:36 +1000 | [diff] [blame] | 329 | # See http://bugs.python.org/issue19734 |
Nick Coghlan | 6256fcb | 2013-12-23 16:16:07 +1000 | [diff] [blame] | 330 | envvars["PIP_NO_INSTALL"] = "1" |
Nick Coghlan | 6edd82a | 2014-02-04 23:02:36 +1000 | [diff] [blame] | 331 | # Also check that we ignore the pip configuration file |
| 332 | # See http://bugs.python.org/issue20053 |
| 333 | with tempfile.TemporaryDirectory() as home_dir: |
| 334 | envvars["HOME"] = home_dir |
| 335 | bad_config = "[global]\nno-install=1" |
| 336 | # Write to both config file names on all platforms to reduce |
| 337 | # cross-platform variation in test code behaviour |
| 338 | win_location = ("pip", "pip.ini") |
| 339 | posix_location = (".pip", "pip.conf") |
Nick Coghlan | 11c5afd | 2014-02-07 23:46:38 +1000 | [diff] [blame] | 340 | # Skips win_location due to http://bugs.python.org/issue20541 |
| 341 | for dirname, fname in (posix_location,): |
Nick Coghlan | 6edd82a | 2014-02-04 23:02:36 +1000 | [diff] [blame] | 342 | dirpath = os.path.join(home_dir, dirname) |
| 343 | os.mkdir(dirpath) |
| 344 | fpath = os.path.join(dirpath, fname) |
| 345 | with open(fpath, 'w') as f: |
| 346 | f.write(bad_config) |
| 347 | |
| 348 | # Actually run the create command with all that unhelpful |
| 349 | # config in place to ensure we ignore it |
| 350 | try: |
| 351 | self.run_with_capture(venv.create, self.env_dir, |
| 352 | with_pip=True) |
| 353 | except subprocess.CalledProcessError as exc: |
| 354 | # The output this produces can be a little hard to read, |
| 355 | # but at least it has all the details |
| 356 | details = exc.output.decode(errors="replace") |
| 357 | msg = "{}\n\n**Subprocess Output**\n{}" |
| 358 | self.fail(msg.format(exc, details)) |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 359 | # Ensure pip is available in the virtual environment |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 360 | envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe) |
Nick Coghlan | 1d1d834 | 2013-11-24 16:49:20 +1000 | [diff] [blame] | 361 | cmd = [envpy, '-Im', 'pip', '--version'] |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 362 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
Nick Coghlan | 1d1d834 | 2013-11-24 16:49:20 +1000 | [diff] [blame] | 363 | stderr=subprocess.PIPE) |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 364 | out, err = p.communicate() |
Nick Coghlan | 6fd12f2 | 2013-11-24 11:36:31 +1000 | [diff] [blame] | 365 | # We force everything to text, so unittest gives the detailed diff |
| 366 | # if we get unexpected results |
| 367 | err = err.decode("latin-1") # Force to text, prevent decoding errors |
| 368 | self.assertEqual(err, "") |
| 369 | out = out.decode("latin-1") # Force to text, prevent decoding errors |
Nick Coghlan | 1b1b178 | 2013-11-30 15:56:58 +1000 | [diff] [blame] | 370 | expected_version = "pip {}".format(ensurepip.version()) |
| 371 | self.assertEqual(out[:len(expected_version)], expected_version) |
Nick Coghlan | 6fd12f2 | 2013-11-24 11:36:31 +1000 | [diff] [blame] | 372 | env_dir = os.fsencode(self.env_dir).decode("latin-1") |
Nick Coghlan | 6fd12f2 | 2013-11-24 11:36:31 +1000 | [diff] [blame] | 373 | self.assertIn(env_dir, out) |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 374 | |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 375 | # http://bugs.python.org/issue19728 |
| 376 | # Check the private uninstall command provided for the Windows |
| 377 | # installers works (at least in a virtual environment) |
| 378 | cmd = [envpy, '-Im', 'ensurepip._uninstall'] |
| 379 | with EnvironmentVarGuard() as envvars: |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 380 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
| 381 | stderr=subprocess.PIPE) |
| 382 | out, err = p.communicate() |
| 383 | # We force everything to text, so unittest gives the detailed diff |
| 384 | # if we get unexpected results |
| 385 | err = err.decode("latin-1") # Force to text, prevent decoding errors |
| 386 | self.assertEqual(err, "") |
Nick Coghlan | 8ddd59e | 2013-11-30 18:35:32 +1000 | [diff] [blame] | 387 | # Being fairly specific regarding the expected behaviour for the |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 388 | # initial bundling phase in Python 3.4. If the output changes in |
Nick Coghlan | 8ddd59e | 2013-11-30 18:35:32 +1000 | [diff] [blame] | 389 | # future pip versions, this test can likely be relaxed further. |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 390 | out = out.decode("latin-1") # Force to text, prevent decoding errors |
Nick Coghlan | 8ddd59e | 2013-11-30 18:35:32 +1000 | [diff] [blame] | 391 | self.assertIn("Successfully uninstalled pip", out) |
| 392 | self.assertIn("Successfully uninstalled setuptools", out) |
| 393 | # Check pip is now gone from the virtual environment |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 394 | self.assert_pip_not_installed() |
| 395 | |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 396 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 397 | def test_main(): |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 398 | run_unittest(BasicTest, EnsurePipTest) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 399 | |
| 400 | if __name__ == "__main__": |
| 401 | test_main() |