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