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