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 |
Victor Stinner | 87d6e13 | 2016-03-14 18:21:58 +0100 | [diff] [blame] | 11 | import re |
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 |
Serhiy Storchaka | 5e0df74 | 2017-11-10 12:09:39 +0200 | [diff] [blame] | 16 | from test.support import (captured_stdout, captured_stderr, requires_zlib, |
Victor Stinner | 866c4e2 | 2014-10-10 14:23:00 +0200 | [diff] [blame] | 17 | can_symlink, EnvironmentVarGuard, rmtree) |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 18 | import threading |
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 | |
Victor Stinner | b347788 | 2016-03-25 12:27:02 +0100 | [diff] [blame] | 22 | try: |
| 23 | import ctypes |
| 24 | except ImportError: |
| 25 | ctypes = None |
| 26 | |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 27 | skipInVenv = unittest.skipIf(sys.prefix != sys.base_prefix, |
| 28 | 'Test not appropriate in a venv') |
| 29 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 30 | class BaseTest(unittest.TestCase): |
| 31 | """Base class for venv tests.""" |
Victor Stinner | bdc337b | 2016-03-25 12:30:40 +0100 | [diff] [blame] | 32 | maxDiff = 80 * 50 |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 33 | |
| 34 | def setUp(self): |
Ned Deily | 045bd53 | 2012-07-13 15:48:04 -0700 | [diff] [blame] | 35 | self.env_dir = os.path.realpath(tempfile.mkdtemp()) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 36 | if os.name == 'nt': |
| 37 | self.bindir = 'Scripts' |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 38 | self.lib = ('Lib',) |
| 39 | self.include = 'Include' |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 40 | else: |
| 41 | self.bindir = 'bin' |
Serhiy Storchaka | 885bdc4 | 2016-02-11 13:10:36 +0200 | [diff] [blame] | 42 | self.lib = ('lib', 'python%d.%d' % sys.version_info[:2]) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 43 | self.include = 'include' |
Vinay Sajip | 2895244 | 2012-06-25 00:47:46 +0100 | [diff] [blame] | 44 | if sys.platform == 'darwin' and '__PYVENV_LAUNCHER__' in os.environ: |
| 45 | executable = os.environ['__PYVENV_LAUNCHER__'] |
Vinay Sajip | 382a7c0 | 2012-05-28 16:34:47 +0100 | [diff] [blame] | 46 | else: |
| 47 | executable = sys.executable |
| 48 | self.exe = os.path.split(executable)[-1] |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 49 | |
| 50 | def tearDown(self): |
Victor Stinner | 866c4e2 | 2014-10-10 14:23:00 +0200 | [diff] [blame] | 51 | rmtree(self.env_dir) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 52 | |
| 53 | def run_with_capture(self, func, *args, **kwargs): |
| 54 | with captured_stdout() as output: |
| 55 | with captured_stderr() as error: |
| 56 | func(*args, **kwargs) |
| 57 | return output.getvalue(), error.getvalue() |
| 58 | |
| 59 | def get_env_file(self, *args): |
| 60 | return os.path.join(self.env_dir, *args) |
| 61 | |
| 62 | def get_text_file_contents(self, *args): |
| 63 | with open(self.get_env_file(*args), 'r') as f: |
| 64 | result = f.read() |
| 65 | return result |
| 66 | |
| 67 | class BasicTest(BaseTest): |
| 68 | """Test venv module functionality.""" |
| 69 | |
Vinay Sajip | b3b49cd | 2012-05-27 18:39:22 +0100 | [diff] [blame] | 70 | def isdir(self, *args): |
| 71 | fn = self.get_env_file(*args) |
| 72 | self.assertTrue(os.path.isdir(fn)) |
| 73 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 74 | def test_defaults(self): |
| 75 | """ |
| 76 | Test the create function with default arguments. |
| 77 | """ |
Victor Stinner | 866c4e2 | 2014-10-10 14:23:00 +0200 | [diff] [blame] | 78 | rmtree(self.env_dir) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 79 | self.run_with_capture(venv.create, self.env_dir) |
Vinay Sajip | b3b49cd | 2012-05-27 18:39:22 +0100 | [diff] [blame] | 80 | self.isdir(self.bindir) |
| 81 | self.isdir(self.include) |
| 82 | self.isdir(*self.lib) |
Vinay Sajip | 1e53f8d | 2014-04-15 11:18:10 +0100 | [diff] [blame] | 83 | # Issue 21197 |
| 84 | p = self.get_env_file('lib64') |
| 85 | conditions = ((struct.calcsize('P') == 8) and (os.name == 'posix') and |
| 86 | (sys.platform != 'darwin')) |
| 87 | if conditions: |
| 88 | self.assertTrue(os.path.islink(p)) |
| 89 | else: |
| 90 | self.assertFalse(os.path.exists(p)) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 91 | data = self.get_text_file_contents('pyvenv.cfg') |
Vinay Sajip | 2895244 | 2012-06-25 00:47:46 +0100 | [diff] [blame] | 92 | if sys.platform == 'darwin' and ('__PYVENV_LAUNCHER__' |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 93 | in os.environ): |
Vinay Sajip | 2895244 | 2012-06-25 00:47:46 +0100 | [diff] [blame] | 94 | executable = os.environ['__PYVENV_LAUNCHER__'] |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 95 | else: |
| 96 | executable = sys.executable |
| 97 | path = os.path.dirname(executable) |
| 98 | self.assertIn('home = %s' % path, data) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 99 | fn = self.get_env_file(self.bindir, self.exe) |
Vinay Sajip | 7e20349 | 2012-05-27 17:30:09 +0100 | [diff] [blame] | 100 | if not os.path.exists(fn): # diagnostics for Windows buildbot failures |
Vinay Sajip | b3b49cd | 2012-05-27 18:39:22 +0100 | [diff] [blame] | 101 | bd = self.get_env_file(self.bindir) |
| 102 | print('Contents of %r:' % bd) |
| 103 | print(' %r' % os.listdir(bd)) |
Vinay Sajip | 7e20349 | 2012-05-27 17:30:09 +0100 | [diff] [blame] | 104 | self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 105 | |
Vinay Sajip | fd0f84b | 2016-08-06 10:43:44 +0100 | [diff] [blame] | 106 | def test_prompt(self): |
| 107 | env_name = os.path.split(self.env_dir)[1] |
| 108 | |
| 109 | builder = venv.EnvBuilder() |
| 110 | context = builder.ensure_directories(self.env_dir) |
| 111 | self.assertEqual(context.prompt, '(%s) ' % env_name) |
| 112 | |
| 113 | builder = venv.EnvBuilder(prompt='My prompt') |
| 114 | context = builder.ensure_directories(self.env_dir) |
| 115 | self.assertEqual(context.prompt, '(My prompt) ') |
| 116 | |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 117 | @skipInVenv |
Vinay Sajip | 3874e54 | 2012-07-03 16:56:40 +0100 | [diff] [blame] | 118 | def test_prefixes(self): |
| 119 | """ |
| 120 | Test that the prefix values are as expected. |
| 121 | """ |
| 122 | #check our prefixes |
| 123 | self.assertEqual(sys.base_prefix, sys.prefix) |
| 124 | self.assertEqual(sys.base_exec_prefix, sys.exec_prefix) |
| 125 | |
| 126 | # check a venv's prefixes |
Victor Stinner | 866c4e2 | 2014-10-10 14:23:00 +0200 | [diff] [blame] | 127 | rmtree(self.env_dir) |
Vinay Sajip | 3874e54 | 2012-07-03 16:56:40 +0100 | [diff] [blame] | 128 | self.run_with_capture(venv.create, self.env_dir) |
| 129 | envpy = os.path.join(self.env_dir, self.bindir, self.exe) |
| 130 | cmd = [envpy, '-c', None] |
| 131 | for prefix, expected in ( |
| 132 | ('prefix', self.env_dir), |
| 133 | ('prefix', self.env_dir), |
| 134 | ('base_prefix', sys.prefix), |
| 135 | ('base_exec_prefix', sys.exec_prefix)): |
| 136 | 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() |
Antoine Pitrou | 9c92a69 | 2012-08-05 00:33:10 +0200 | [diff] [blame] | 140 | self.assertEqual(out.strip(), expected.encode()) |
Vinay Sajip | 3874e54 | 2012-07-03 16:56:40 +0100 | [diff] [blame] | 141 | |
Vinay Sajip | bd40d3e | 2012-10-11 17:22:45 +0100 | [diff] [blame] | 142 | if sys.platform == 'win32': |
| 143 | ENV_SUBDIRS = ( |
| 144 | ('Scripts',), |
| 145 | ('Include',), |
| 146 | ('Lib',), |
| 147 | ('Lib', 'site-packages'), |
| 148 | ) |
| 149 | else: |
| 150 | ENV_SUBDIRS = ( |
| 151 | ('bin',), |
| 152 | ('include',), |
| 153 | ('lib',), |
| 154 | ('lib', 'python%d.%d' % sys.version_info[:2]), |
| 155 | ('lib', 'python%d.%d' % sys.version_info[:2], 'site-packages'), |
| 156 | ) |
| 157 | |
| 158 | def create_contents(self, paths, filename): |
| 159 | """ |
| 160 | Create some files in the environment which are unrelated |
| 161 | to the virtual environment. |
| 162 | """ |
| 163 | for subdirs in paths: |
| 164 | d = os.path.join(self.env_dir, *subdirs) |
| 165 | os.mkdir(d) |
| 166 | fn = os.path.join(d, filename) |
| 167 | with open(fn, 'wb') as f: |
| 168 | f.write(b'Still here?') |
| 169 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 170 | def test_overwrite_existing(self): |
| 171 | """ |
Vinay Sajip | bd40d3e | 2012-10-11 17:22:45 +0100 | [diff] [blame] | 172 | Test creating environment in an existing directory. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 173 | """ |
Vinay Sajip | bd40d3e | 2012-10-11 17:22:45 +0100 | [diff] [blame] | 174 | self.create_contents(self.ENV_SUBDIRS, 'foo') |
| 175 | venv.create(self.env_dir) |
| 176 | for subdirs in self.ENV_SUBDIRS: |
| 177 | fn = os.path.join(self.env_dir, *(subdirs + ('foo',))) |
| 178 | self.assertTrue(os.path.exists(fn)) |
| 179 | with open(fn, 'rb') as f: |
| 180 | self.assertEqual(f.read(), b'Still here?') |
| 181 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 182 | builder = venv.EnvBuilder(clear=True) |
| 183 | builder.create(self.env_dir) |
Vinay Sajip | bd40d3e | 2012-10-11 17:22:45 +0100 | [diff] [blame] | 184 | for subdirs in self.ENV_SUBDIRS: |
| 185 | fn = os.path.join(self.env_dir, *(subdirs + ('foo',))) |
| 186 | self.assertFalse(os.path.exists(fn)) |
| 187 | |
| 188 | def clear_directory(self, path): |
| 189 | for fn in os.listdir(path): |
| 190 | fn = os.path.join(path, fn) |
| 191 | if os.path.islink(fn) or os.path.isfile(fn): |
| 192 | os.remove(fn) |
| 193 | elif os.path.isdir(fn): |
Victor Stinner | 866c4e2 | 2014-10-10 14:23:00 +0200 | [diff] [blame] | 194 | rmtree(fn) |
Vinay Sajip | bd40d3e | 2012-10-11 17:22:45 +0100 | [diff] [blame] | 195 | |
| 196 | def test_unoverwritable_fails(self): |
| 197 | #create a file clashing with directories in the env dir |
| 198 | for paths in self.ENV_SUBDIRS[:3]: |
| 199 | fn = os.path.join(self.env_dir, *paths) |
| 200 | with open(fn, 'wb') as f: |
| 201 | f.write(b'') |
| 202 | self.assertRaises((ValueError, OSError), venv.create, self.env_dir) |
| 203 | self.clear_directory(self.env_dir) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 204 | |
Vinay Sajip | b3b49cd | 2012-05-27 18:39:22 +0100 | [diff] [blame] | 205 | def test_upgrade(self): |
| 206 | """ |
| 207 | Test upgrading an existing environment directory. |
| 208 | """ |
Vinay Sajip | b9b965f | 2014-06-03 16:47:51 +0100 | [diff] [blame] | 209 | # See Issue #21643: the loop needs to run twice to ensure |
| 210 | # that everything works on the upgrade (the first run just creates |
| 211 | # the venv). |
| 212 | for upgrade in (False, True): |
| 213 | builder = venv.EnvBuilder(upgrade=upgrade) |
| 214 | self.run_with_capture(builder.create, self.env_dir) |
| 215 | self.isdir(self.bindir) |
| 216 | self.isdir(self.include) |
| 217 | self.isdir(*self.lib) |
| 218 | fn = self.get_env_file(self.bindir, self.exe) |
| 219 | if not os.path.exists(fn): |
| 220 | # diagnostics for Windows buildbot failures |
| 221 | bd = self.get_env_file(self.bindir) |
| 222 | print('Contents of %r:' % bd) |
| 223 | print(' %r' % os.listdir(bd)) |
| 224 | self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn) |
Vinay Sajip | b3b49cd | 2012-05-27 18:39:22 +0100 | [diff] [blame] | 225 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 226 | def test_isolation(self): |
| 227 | """ |
| 228 | Test isolation from system site-packages |
| 229 | """ |
| 230 | for ssp, s in ((True, 'true'), (False, 'false')): |
| 231 | builder = venv.EnvBuilder(clear=True, system_site_packages=ssp) |
| 232 | builder.create(self.env_dir) |
| 233 | data = self.get_text_file_contents('pyvenv.cfg') |
| 234 | self.assertIn('include-system-site-packages = %s\n' % s, data) |
| 235 | |
| 236 | @unittest.skipUnless(can_symlink(), 'Needs symlinks') |
| 237 | def test_symlinking(self): |
| 238 | """ |
| 239 | Test symlinking works as expected |
| 240 | """ |
| 241 | for usl in (False, True): |
| 242 | builder = venv.EnvBuilder(clear=True, symlinks=usl) |
Vinay Sajip | 90db661 | 2012-07-17 17:33:46 +0100 | [diff] [blame] | 243 | builder.create(self.env_dir) |
| 244 | fn = self.get_env_file(self.bindir, self.exe) |
| 245 | # Don't test when False, because e.g. 'python' is always |
| 246 | # symlinked to 'python3.3' in the env, even when symlinking in |
| 247 | # general isn't wanted. |
| 248 | if usl: |
| 249 | self.assertTrue(os.path.islink(fn)) |
| 250 | |
| 251 | # If a venv is created from a source build and that venv is used to |
| 252 | # run the test, the pyvenv.cfg in the venv created in the test will |
| 253 | # point to the venv being used to run the test, and we lose the link |
| 254 | # to the source build - so Python can't initialise properly. |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 255 | @skipInVenv |
Vinay Sajip | 90db661 | 2012-07-17 17:33:46 +0100 | [diff] [blame] | 256 | def test_executable(self): |
| 257 | """ |
| 258 | Test that the sys.executable value is as expected. |
| 259 | """ |
Victor Stinner | 866c4e2 | 2014-10-10 14:23:00 +0200 | [diff] [blame] | 260 | rmtree(self.env_dir) |
Vinay Sajip | 90db661 | 2012-07-17 17:33:46 +0100 | [diff] [blame] | 261 | 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() |
Antoine Pitrou | 9c92a69 | 2012-08-05 00:33:10 +0200 | [diff] [blame] | 267 | self.assertEqual(out.strip(), envpy.encode()) |
Vinay Sajip | 90db661 | 2012-07-17 17:33:46 +0100 | [diff] [blame] | 268 | |
| 269 | @unittest.skipUnless(can_symlink(), 'Needs symlinks') |
| 270 | def test_executable_symlinks(self): |
| 271 | """ |
| 272 | Test that the sys.executable value is as expected. |
| 273 | """ |
Victor Stinner | 866c4e2 | 2014-10-10 14:23:00 +0200 | [diff] [blame] | 274 | rmtree(self.env_dir) |
Vinay Sajip | 90db661 | 2012-07-17 17:33:46 +0100 | [diff] [blame] | 275 | builder = venv.EnvBuilder(clear=True, symlinks=True) |
| 276 | 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() |
Antoine Pitrou | 9c92a69 | 2012-08-05 00:33:10 +0200 | [diff] [blame] | 282 | self.assertEqual(out.strip(), envpy.encode()) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 283 | |
Miss Islington (bot) | a3d6c1b | 2018-02-19 17:45:02 -0800 | [diff] [blame] | 284 | @unittest.skipUnless(os.name == 'nt', 'only relevant on Windows') |
| 285 | def test_unicode_in_batch_file(self): |
| 286 | """ |
| 287 | Test isolation from system site-packages |
| 288 | """ |
| 289 | rmtree(self.env_dir) |
| 290 | env_dir = os.path.join(os.path.realpath(self.env_dir), 'ϼўТλФЙ') |
| 291 | builder = venv.EnvBuilder(clear=True) |
| 292 | builder.create(env_dir) |
| 293 | activate = os.path.join(env_dir, self.bindir, 'activate.bat') |
| 294 | 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) |
| 301 | self.assertEqual(out.strip(), '0') |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 302 | |
| 303 | @skipInVenv |
| 304 | class EnsurePipTest(BaseTest): |
| 305 | """Test venv module installation of pip.""" |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 306 | def assert_pip_not_installed(self): |
| 307 | envpy = os.path.join(os.path.realpath(self.env_dir), |
| 308 | self.bindir, self.exe) |
| 309 | try_import = 'try:\n import pip\nexcept 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 | # We force everything to text, so unittest gives the detailed diff |
| 315 | # if we get unexpected results |
| 316 | err = err.decode("latin-1") # Force to text, prevent decoding errors |
| 317 | self.assertEqual(err, "") |
| 318 | out = out.decode("latin-1") # Force to text, prevent decoding errors |
| 319 | self.assertEqual(out.strip(), "OK") |
| 320 | |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 321 | |
| 322 | def test_no_pip_by_default(self): |
Victor Stinner | 866c4e2 | 2014-10-10 14:23:00 +0200 | [diff] [blame] | 323 | rmtree(self.env_dir) |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 324 | self.run_with_capture(venv.create, self.env_dir) |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 325 | self.assert_pip_not_installed() |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 326 | |
| 327 | def test_explicit_no_pip(self): |
Victor Stinner | 866c4e2 | 2014-10-10 14:23:00 +0200 | [diff] [blame] | 328 | rmtree(self.env_dir) |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 329 | self.run_with_capture(venv.create, self.env_dir, with_pip=False) |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 330 | self.assert_pip_not_installed() |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 331 | |
Victor Stinner | bdc337b | 2016-03-25 12:30:40 +0100 | [diff] [blame] | 332 | def test_devnull(self): |
Nick Coghlan | 456ab5d | 2014-02-05 23:54:55 +1000 | [diff] [blame] | 333 | # 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] | 334 | # appear empty. However http://bugs.python.org/issue20541 means |
| 335 | # that doesn't currently work properly on Windows. Once that is |
| 336 | # fixed, the "win_location" part of test_with_pip should be restored |
Nick Coghlan | 456ab5d | 2014-02-05 23:54:55 +1000 | [diff] [blame] | 337 | with open(os.devnull, "rb") as f: |
| 338 | self.assertEqual(f.read(), b"") |
| 339 | |
Victor Stinner | bdc337b | 2016-03-25 12:30:40 +0100 | [diff] [blame] | 340 | # Issue #20541: os.path.exists('nul') is False on Windows |
| 341 | if os.devnull.lower() == 'nul': |
| 342 | self.assertFalse(os.path.exists(os.devnull)) |
| 343 | else: |
| 344 | self.assertTrue(os.path.exists(os.devnull)) |
| 345 | |
Vinay Sajip | db6322c | 2017-02-02 19:05:19 +0000 | [diff] [blame] | 346 | def do_test_with_pip(self, system_site_packages): |
Victor Stinner | 866c4e2 | 2014-10-10 14:23:00 +0200 | [diff] [blame] | 347 | rmtree(self.env_dir) |
Nick Coghlan | d76cdc1 | 2013-11-23 11:37:28 +1000 | [diff] [blame] | 348 | 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 Coghlan | 6256fcb | 2013-12-23 16:16:07 +1000 | [diff] [blame] | 353 | # 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 Coghlan | 6edd82a | 2014-02-04 23:02:36 +1000 | [diff] [blame] | 357 | # See http://bugs.python.org/issue19734 |
Nick Coghlan | 6256fcb | 2013-12-23 16:16:07 +1000 | [diff] [blame] | 358 | envvars["PIP_NO_INSTALL"] = "1" |
Nick Coghlan | 6edd82a | 2014-02-04 23:02:36 +1000 | [diff] [blame] | 359 | # 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 Coghlan | 11c5afd | 2014-02-07 23:46:38 +1000 | [diff] [blame] | 368 | # Skips win_location due to http://bugs.python.org/issue20541 |
| 369 | for dirname, fname in (posix_location,): |
Nick Coghlan | 6edd82a | 2014-02-04 23:02:36 +1000 | [diff] [blame] | 370 | 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, |
Vinay Sajip | db6322c | 2017-02-02 19:05:19 +0000 | [diff] [blame] | 380 | system_site_packages=system_site_packages, |
Nick Coghlan | 6edd82a | 2014-02-04 23:02:36 +1000 | [diff] [blame] | 381 | with_pip=True) |
| 382 | except subprocess.CalledProcessError as exc: |
| 383 | # The output this produces can be a little hard to read, |
| 384 | # but at least it has all the details |
| 385 | details = exc.output.decode(errors="replace") |
| 386 | msg = "{}\n\n**Subprocess Output**\n{}" |
| 387 | self.fail(msg.format(exc, details)) |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 388 | # Ensure pip is available in the virtual environment |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 389 | envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe) |
Victor Stinner | 895862a | 2017-11-20 09:47:03 -0800 | [diff] [blame] | 390 | # Ignore DeprecationWarning since pip code is not part of Python |
| 391 | cmd = [envpy, '-W', 'ignore::DeprecationWarning', '-I', |
| 392 | '-m', 'pip', '--version'] |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 393 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
Nick Coghlan | 1d1d834 | 2013-11-24 16:49:20 +1000 | [diff] [blame] | 394 | stderr=subprocess.PIPE) |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 395 | out, err = p.communicate() |
Nick Coghlan | 6fd12f2 | 2013-11-24 11:36:31 +1000 | [diff] [blame] | 396 | # We force everything to text, so unittest gives the detailed diff |
| 397 | # if we get unexpected results |
| 398 | err = err.decode("latin-1") # Force to text, prevent decoding errors |
| 399 | self.assertEqual(err, "") |
| 400 | out = out.decode("latin-1") # Force to text, prevent decoding errors |
Nick Coghlan | 1b1b178 | 2013-11-30 15:56:58 +1000 | [diff] [blame] | 401 | expected_version = "pip {}".format(ensurepip.version()) |
| 402 | self.assertEqual(out[:len(expected_version)], expected_version) |
Nick Coghlan | 6fd12f2 | 2013-11-24 11:36:31 +1000 | [diff] [blame] | 403 | env_dir = os.fsencode(self.env_dir).decode("latin-1") |
Nick Coghlan | 6fd12f2 | 2013-11-24 11:36:31 +1000 | [diff] [blame] | 404 | self.assertIn(env_dir, out) |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 405 | |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 406 | # http://bugs.python.org/issue19728 |
| 407 | # Check the private uninstall command provided for the Windows |
| 408 | # installers works (at least in a virtual environment) |
Victor Stinner | 895862a | 2017-11-20 09:47:03 -0800 | [diff] [blame] | 409 | cmd = [envpy, '-W', 'ignore::DeprecationWarning', '-I', |
| 410 | '-m', 'ensurepip._uninstall'] |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 411 | with EnvironmentVarGuard() as envvars: |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 412 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
| 413 | stderr=subprocess.PIPE) |
| 414 | out, err = p.communicate() |
| 415 | # We force everything to text, so unittest gives the detailed diff |
| 416 | # if we get unexpected results |
| 417 | err = err.decode("latin-1") # Force to text, prevent decoding errors |
Victor Stinner | 87d6e13 | 2016-03-14 18:21:58 +0100 | [diff] [blame] | 418 | # Ignore the warning: |
| 419 | # "The directory '$HOME/.cache/pip/http' or its parent directory |
| 420 | # is not owned by the current user and the cache has been disabled. |
| 421 | # Please check the permissions and owner of that directory. If |
| 422 | # executing pip with sudo, you may want sudo's -H flag." |
| 423 | # where $HOME is replaced by the HOME environment variable. |
| 424 | err = re.sub("^The directory .* or its parent directory is not owned " |
| 425 | "by the current user .*$", "", err, flags=re.MULTILINE) |
| 426 | self.assertEqual(err.rstrip(), "") |
Nick Coghlan | 8ddd59e | 2013-11-30 18:35:32 +1000 | [diff] [blame] | 427 | # Being fairly specific regarding the expected behaviour for the |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 428 | # initial bundling phase in Python 3.4. If the output changes in |
Nick Coghlan | 8ddd59e | 2013-11-30 18:35:32 +1000 | [diff] [blame] | 429 | # future pip versions, this test can likely be relaxed further. |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 430 | out = out.decode("latin-1") # Force to text, prevent decoding errors |
Nick Coghlan | 8ddd59e | 2013-11-30 18:35:32 +1000 | [diff] [blame] | 431 | self.assertIn("Successfully uninstalled pip", out) |
| 432 | self.assertIn("Successfully uninstalled setuptools", out) |
Vinay Sajip | db6322c | 2017-02-02 19:05:19 +0000 | [diff] [blame] | 433 | # Check pip is now gone from the virtual environment. This only |
| 434 | # applies in the system_site_packages=False case, because in the |
| 435 | # other case, pip may still be available in the system site-packages |
| 436 | if not system_site_packages: |
| 437 | self.assert_pip_not_installed() |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 438 | |
Vinay Sajip | db6322c | 2017-02-02 19:05:19 +0000 | [diff] [blame] | 439 | # Issue #26610: pip/pep425tags.py requires ctypes |
| 440 | @unittest.skipUnless(ctypes, 'pip requires ctypes') |
Serhiy Storchaka | 5e0df74 | 2017-11-10 12:09:39 +0200 | [diff] [blame] | 441 | @requires_zlib |
Vinay Sajip | db6322c | 2017-02-02 19:05:19 +0000 | [diff] [blame] | 442 | def test_with_pip(self): |
| 443 | self.do_test_with_pip(False) |
| 444 | self.do_test_with_pip(True) |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 445 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 446 | if __name__ == "__main__": |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 447 | unittest.main() |