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