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