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