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