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