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