Debian Codegraph @

Lib/test/test_venv.py

raw
1 """
2 Test harness for the venv module.
3
4 Copyright (C) 2011-2012 Vinay Sajip.
5 Licensed to the PSF under a contributor agreement.
6 """
7
8 import contextlib
9 import ensurepip
10 import os
11 import os.path
12 import pathlib
13 import re
14 import shlex
15 import shutil
16 import struct
17 import subprocess
18 import sys
19 import sysconfig
20 import tempfile
21 from test.support import (captured_stdout, captured_stderr,
22 skip_if_broken_multiprocessing_synchronize, verbose,
23 requires_subprocess, is_android, is_apple_mobile,
24 is_wasm32,
25 requires_venv_with_pip, TEST_HOME_DIR,
26 requires_resource, copy_python_src_ignore)
27 from test.support.os_helper import (can_symlink, EnvironmentVarGuard, rmtree,
28 TESTFN, FakePath)
29 import unittest
30 import venv
31 from unittest.mock import patch, Mock
32
33 try:
34 import ctypes
35 except ImportError:
36 ctypes = None
37
38 # Platforms that set sys._base_executable can create venvs from within
39 # another venv, so no need to skip tests that require venv.create().
40 requireVenvCreate = unittest.skipUnless(
41 sys.prefix == sys.base_prefix
42 or sys._base_executable != sys.executable,
43 'cannot run venv.create from within a venv on this platform')
44
45 if is_android or is_apple_mobile or is_wasm32:
46 raise unittest.SkipTest("venv is not available on this platform")
47
48 @requires_subprocess()
49 def check_output(cmd, encoding=None):
50 p = subprocess.Popen(cmd,
51 stdout=subprocess.PIPE,
52 stderr=subprocess.PIPE,
53 env={**os.environ, "PYTHONHOME": ""})
54 out, err = p.communicate()
55 if p.returncode:
56 if verbose and err:
57 print(err.decode(encoding or 'utf-8', 'backslashreplace'))
58 raise subprocess.CalledProcessError(
59 p.returncode, cmd, out, err)
60 if encoding:
61 return (
62 out.decode(encoding, 'backslashreplace'),
63 err.decode(encoding, 'backslashreplace'),
64 )
65 return out, err
66
67 class BaseTest(unittest.TestCase):
68 """Base class for venv tests."""
69 maxDiff = 80 * 50
70
71 def setUp(self):
72 self.env_dir = os.path.realpath(tempfile.mkdtemp())
73 if os.name == 'nt':
74 self.bindir = 'Scripts'
75 self.lib = ('Lib',)
76 self.include = 'Include'
77 else:
78 self.bindir = 'bin'
79 self.lib = ('lib', f'python{sysconfig._get_python_version_abi()}')
80 self.include = 'include'
81 executable = sys._base_executable
82 self.exe = os.path.split(executable)[-1]
83 if (sys.platform == 'win32'
84 and os.path.lexists(executable)
85 and not os.path.exists(executable)):
86 self.cannot_link_exe = True
87 else:
88 self.cannot_link_exe = False
89
90 def tearDown(self):
91 rmtree(self.env_dir)
92
93 def envpy(self, *, real_env_dir=False):
94 if real_env_dir:
95 env_dir = os.path.realpath(self.env_dir)
96 else:
97 env_dir = self.env_dir
98 return os.path.join(env_dir, self.bindir, self.exe)
99
100 def run_with_capture(self, func, *args, **kwargs):
101 with captured_stdout() as output:
102 with captured_stderr() as error:
103 func(*args, **kwargs)
104 return output.getvalue(), error.getvalue()
105
106 def get_env_file(self, *args):
107 return os.path.join(self.env_dir, *args)
108
109 def get_text_file_contents(self, *args, encoding='utf-8'):
110 with open(self.get_env_file(*args), 'r', encoding=encoding) as f:
111 result = f.read()
112 return result
113
114 class BasicTest(BaseTest):
115 """Test venv module functionality."""
116
117 def isdir(self, *args):
118 fn = self.get_env_file(*args)
119 self.assertTrue(os.path.isdir(fn))
120
121 def test_defaults_with_str_path(self):
122 """
123 Test the create function with default arguments and a str path.
124 """
125 rmtree(self.env_dir)
126 self.run_with_capture(venv.create, self.env_dir)
127 self._check_output_of_default_create()
128
129 def test_defaults_with_pathlike(self):
130 """
131 Test the create function with default arguments and a path-like path.
132 """
133 rmtree(self.env_dir)
134 self.run_with_capture(venv.create, FakePath(self.env_dir))
135 self._check_output_of_default_create()
136
137 def _check_output_of_default_create(self):
138 self.isdir(self.bindir)
139 self.isdir(self.include)
140 self.isdir(*self.lib)
141 # Issue 21197
142 p = self.get_env_file('lib64')
143 conditions = ((struct.calcsize('P') == 8) and (os.name == 'posix') and
144 (sys.platform != 'darwin'))
145 if conditions:
146 self.assertTrue(os.path.islink(p))
147 else:
148 self.assertFalse(os.path.exists(p))
149 data = self.get_text_file_contents('pyvenv.cfg')
150 executable = sys._base_executable
151 path = os.path.dirname(executable)
152 self.assertIn('home = %s' % path, data)
153 self.assertIn('executable = %s' %
154 os.path.realpath(sys.executable), data)
155 copies = '' if os.name=='nt' else ' --copies'
156 cmd = (f'command = {sys.executable} -m venv{copies} --without-pip '
157 f'--without-scm-ignore-files {self.env_dir}')
158 self.assertIn(cmd, data)
159 fn = self.get_env_file(self.bindir, self.exe)
160 if not os.path.exists(fn): # diagnostics for Windows buildbot failures
161 bd = self.get_env_file(self.bindir)
162 print('Contents of %r:' % bd)
163 print(' %r' % os.listdir(bd))
164 self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn)
165
166 def test_config_file_command_key(self):
167 options = [
168 (None, None, None), # Default case.
169 ('--copies', 'symlinks', False),
170 ('--without-pip', 'with_pip', False),
171 ('--system-site-packages', 'system_site_packages', True),
172 ('--clear', 'clear', True),
173 ('--upgrade', 'upgrade', True),
174 ('--upgrade-deps', 'upgrade_deps', True),
175 ('--prompt="foobar"', 'prompt', 'foobar'),
176 ('--without-scm-ignore-files', 'scm_ignore_files', frozenset()),
177 ]
178 for opt, attr, value in options:
179 with self.subTest(opt=opt, attr=attr, value=value):
180 rmtree(self.env_dir)
181 if not attr:
182 kwargs = {}
183 else:
184 kwargs = {attr: value}
185 b = venv.EnvBuilder(**kwargs)
186 b.upgrade_dependencies = Mock() # avoid pip command to upgrade deps
187 b._setup_pip = Mock() # avoid pip setup
188 self.run_with_capture(b.create, self.env_dir)
189 data = self.get_text_file_contents('pyvenv.cfg')
190 if not attr or opt.endswith('git'):
191 for opt in ('--system-site-packages', '--clear', '--upgrade',
192 '--upgrade-deps', '--prompt'):
193 self.assertNotRegex(data, rf'command = .* {opt}')
194 elif os.name=='nt' and attr=='symlinks':
195 pass
196 else:
197 self.assertRegex(data, rf'command = .* {opt}')
198
199 def test_prompt(self):
200 env_name = os.path.split(self.env_dir)[1]
201
202 rmtree(self.env_dir)
203 builder = venv.EnvBuilder()
204 self.run_with_capture(builder.create, self.env_dir)
205 context = builder.ensure_directories(self.env_dir)
206 data = self.get_text_file_contents('pyvenv.cfg')
207 self.assertEqual(context.prompt, env_name)
208 self.assertNotIn("prompt = ", data)
209
210 rmtree(self.env_dir)
211 builder = venv.EnvBuilder(prompt='My prompt')
212 self.run_with_capture(builder.create, self.env_dir)
213 context = builder.ensure_directories(self.env_dir)
214 data = self.get_text_file_contents('pyvenv.cfg')
215 self.assertEqual(context.prompt, 'My prompt')
216 self.assertIn("prompt = 'My prompt'\n", data)
217
218 rmtree(self.env_dir)
219 builder = venv.EnvBuilder(prompt='.')
220 cwd = os.path.basename(os.getcwd())
221 self.run_with_capture(builder.create, self.env_dir)
222 context = builder.ensure_directories(self.env_dir)
223 data = self.get_text_file_contents('pyvenv.cfg')
224 self.assertEqual(context.prompt, cwd)
225 self.assertIn("prompt = '%s'\n" % cwd, data)
226
227 def test_upgrade_dependencies(self):
228 builder = venv.EnvBuilder()
229 bin_path = 'bin'
230 python_exe = os.path.split(sys.executable)[1]
231 expected_exe = os.path.basename(sys._base_executable)
232
233 if sys.platform == 'win32':
234 bin_path = 'Scripts'
235 if os.path.normcase(os.path.splitext(python_exe)[0]).endswith('_d'):
236 expected_exe = 'python_d'
237 else:
238 expected_exe = 'python'
239 python_exe = expected_exe + '.exe'
240
241 with tempfile.TemporaryDirectory() as fake_env_dir:
242 expect_exe = os.path.normcase(
243 os.path.join(fake_env_dir, bin_path, expected_exe)
244 )
245 if sys.platform == 'win32':
246 expect_exe = os.path.normcase(os.path.realpath(expect_exe))
247
248 def pip_cmd_checker(cmd, **kwargs):
249 self.assertEqual(
250 cmd[1:],
251 [
252 '-m',
253 'pip',
254 'install',
255 '--upgrade',
256 'pip',
257 ]
258 )
259 exe_dir = os.path.normcase(os.path.dirname(cmd[0]))
260 expected_dir = os.path.normcase(os.path.dirname(expect_exe))
261 self.assertEqual(exe_dir, expected_dir)
262
263 fake_context = builder.ensure_directories(fake_env_dir)
264 with patch('venv.subprocess.check_output', pip_cmd_checker):
265 builder.upgrade_dependencies(fake_context)
266
267 @requireVenvCreate
268 def test_prefixes(self):
269 """
270 Test that the prefix values are as expected.
271 """
272 # check a venv's prefixes
273 rmtree(self.env_dir)
274 self.run_with_capture(venv.create, self.env_dir)
275 cmd = [self.envpy(), '-c', None]
276 for prefix, expected in (
277 ('prefix', self.env_dir),
278 ('exec_prefix', self.env_dir),
279 ('base_prefix', sys.base_prefix),
280 ('base_exec_prefix', sys.base_exec_prefix)):
281 cmd[2] = 'import sys; print(sys.%s)' % prefix
282 out, err = check_output(cmd)
283 self.assertEqual(pathlib.Path(out.strip().decode()),
284 pathlib.Path(expected), prefix)
285
286 @requireVenvCreate
287 def test_sysconfig(self):
288 """
289 Test that the sysconfig functions work in a virtual environment.
290 """
291 rmtree(self.env_dir)
292 self.run_with_capture(venv.create, self.env_dir, symlinks=False)
293 cmd = [self.envpy(), '-c', None]
294 for call, expected in (
295 # installation scheme
296 ('get_preferred_scheme("prefix")', 'venv'),
297 ('get_default_scheme()', 'venv'),
298 # build environment
299 ('is_python_build()', str(sysconfig.is_python_build())),
300 ('get_makefile_filename()', sysconfig.get_makefile_filename()),
301 ('get_config_h_filename()', sysconfig.get_config_h_filename()),
302 ('get_config_var("Py_GIL_DISABLED")',
303 str(sysconfig.get_config_var("Py_GIL_DISABLED")))):
304 with self.subTest(call):
305 cmd[2] = 'import sysconfig; print(sysconfig.%s)' % call
306 out, err = check_output(cmd, encoding='utf-8')
307 self.assertEqual(out.strip(), expected, err)
308 for attr, expected in (
309 ('executable', self.envpy()),
310 # Usually compare to sys.executable, but if we're running in our own
311 # venv then we really need to compare to our base executable
312 ('_base_executable', sys._base_executable),
313 ):
314 with self.subTest(attr):
315 cmd[2] = f'import sys; print(sys.{attr})'
316 out, err = check_output(cmd, encoding='utf-8')
317 self.assertEqual(out.strip(), expected, err)
318
319 @requireVenvCreate
320 @unittest.skipUnless(can_symlink(), 'Needs symlinks')
321 def test_sysconfig_symlinks(self):
322 """
323 Test that the sysconfig functions work in a virtual environment.
324 """
325 rmtree(self.env_dir)
326 self.run_with_capture(venv.create, self.env_dir, symlinks=True)
327 cmd = [self.envpy(), '-c', None]
328 for call, expected in (
329 # installation scheme
330 ('get_preferred_scheme("prefix")', 'venv'),
331 ('get_default_scheme()', 'venv'),
332 # build environment
333 ('is_python_build()', str(sysconfig.is_python_build())),
334 ('get_makefile_filename()', sysconfig.get_makefile_filename()),
335 ('get_config_h_filename()', sysconfig.get_config_h_filename()),
336 ('get_config_var("Py_GIL_DISABLED")',
337 str(sysconfig.get_config_var("Py_GIL_DISABLED")))):
338 with self.subTest(call):
339 cmd[2] = 'import sysconfig; print(sysconfig.%s)' % call
340 out, err = check_output(cmd, encoding='utf-8')
341 self.assertEqual(out.strip(), expected, err)
342 for attr, expected in (
343 ('executable', self.envpy()),
344 # Usually compare to sys.executable, but if we're running in our own
345 # venv then we really need to compare to our base executable
346 # HACK: Test fails on POSIX with unversioned binary (PR gh-113033)
347 #('_base_executable', sys._base_executable),
348 ):
349 with self.subTest(attr):
350 cmd[2] = f'import sys; print(sys.{attr})'
351 out, err = check_output(cmd, encoding='utf-8')
352 self.assertEqual(out.strip(), expected, err)
353
354 if sys.platform == 'win32':
355 ENV_SUBDIRS = (
356 ('Scripts',),
357 ('Include',),
358 ('Lib',),
359 ('Lib', 'site-packages'),
360 )
361 else:
362 ENV_SUBDIRS = (
363 ('bin',),
364 ('include',),
365 ('lib',),
366 ('lib', 'python%d.%d' % sys.version_info[:2]),
367 ('lib', 'python%d.%d' % sys.version_info[:2], 'site-packages'),
368 )
369
370 def create_contents(self, paths, filename):
371 """
372 Create some files in the environment which are unrelated
373 to the virtual environment.
374 """
375 for subdirs in paths:
376 d = os.path.join(self.env_dir, *subdirs)
377 os.mkdir(d)
378 fn = os.path.join(d, filename)
379 with open(fn, 'wb') as f:
380 f.write(b'Still here?')
381
382 @unittest.skipUnless(hasattr(os, 'listxattr'), 'test requires os.listxattr')
383 def test_install_scripts_selinux(self):
384 """
385 gh-145417: Test that install_scripts does not copy SELinux context
386 when copying scripts.
387 """
388 with patch('os.listxattr') as listxattr_mock:
389 venv.create(self.env_dir)
390 listxattr_mock.assert_not_called()
391
392 def test_overwrite_existing(self):
393 """
394 Test creating environment in an existing directory.
395 """
396 self.create_contents(self.ENV_SUBDIRS, 'foo')
397 venv.create(self.env_dir)
398 for subdirs in self.ENV_SUBDIRS:
399 fn = os.path.join(self.env_dir, *(subdirs + ('foo',)))
400 self.assertTrue(os.path.exists(fn))
401 with open(fn, 'rb') as f:
402 self.assertEqual(f.read(), b'Still here?')
403
404 builder = venv.EnvBuilder(clear=True)
405 builder.create(self.env_dir)
406 for subdirs in self.ENV_SUBDIRS:
407 fn = os.path.join(self.env_dir, *(subdirs + ('foo',)))
408 self.assertFalse(os.path.exists(fn))
409
410 def clear_directory(self, path):
411 for fn in os.listdir(path):
412 fn = os.path.join(path, fn)
413 if os.path.islink(fn) or os.path.isfile(fn):
414 os.remove(fn)
415 elif os.path.isdir(fn):
416 rmtree(fn)
417
418 def test_unoverwritable_fails(self):
419 #create a file clashing with directories in the env dir
420 for paths in self.ENV_SUBDIRS[:3]:
421 fn = os.path.join(self.env_dir, *paths)
422 with open(fn, 'wb') as f:
423 f.write(b'')
424 self.assertRaises((ValueError, OSError), venv.create, self.env_dir)
425 self.clear_directory(self.env_dir)
426
427 def test_upgrade(self):
428 """
429 Test upgrading an existing environment directory.
430 """
431 # See Issue #21643: the loop needs to run twice to ensure
432 # that everything works on the upgrade (the first run just creates
433 # the venv).
434 for upgrade in (False, True):
435 builder = venv.EnvBuilder(upgrade=upgrade)
436 self.run_with_capture(builder.create, self.env_dir)
437 self.isdir(self.bindir)
438 self.isdir(self.include)
439 self.isdir(*self.lib)
440 fn = self.get_env_file(self.bindir, self.exe)
441 if not os.path.exists(fn):
442 # diagnostics for Windows buildbot failures
443 bd = self.get_env_file(self.bindir)
444 print('Contents of %r:' % bd)
445 print(' %r' % os.listdir(bd))
446 self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn)
447
448 def test_isolation(self):
449 """
450 Test isolation from system site-packages
451 """
452 for ssp, s in ((True, 'true'), (False, 'false')):
453 builder = venv.EnvBuilder(clear=True, system_site_packages=ssp)
454 builder.create(self.env_dir)
455 data = self.get_text_file_contents('pyvenv.cfg')
456 self.assertIn('include-system-site-packages = %s\n' % s, data)
457
458 @unittest.skipUnless(can_symlink(), 'Needs symlinks')
459 def test_symlinking(self):
460 """
461 Test symlinking works as expected
462 """
463 for usl in (False, True):
464 builder = venv.EnvBuilder(clear=True, symlinks=usl)
465 builder.create(self.env_dir)
466 fn = self.get_env_file(self.bindir, self.exe)
467 # Don't test when False, because e.g. 'python' is always
468 # symlinked to 'python3.3' in the env, even when symlinking in
469 # general isn't wanted.
470 if usl:
471 if self.cannot_link_exe:
472 # Symlinking is skipped when our executable is already a
473 # special app symlink
474 self.assertFalse(os.path.islink(fn))
475 else:
476 self.assertTrue(os.path.islink(fn))
477
478 # If a venv is created from a source build and that venv is used to
479 # run the test, the pyvenv.cfg in the venv created in the test will
480 # point to the venv being used to run the test, and we lose the link
481 # to the source build - so Python can't initialise properly.
482 @requireVenvCreate
483 def test_executable(self):
484 """
485 Test that the sys.executable value is as expected.
486 """
487 rmtree(self.env_dir)
488 self.run_with_capture(venv.create, self.env_dir)
489 envpy = self.envpy(real_env_dir=True)
490 out, err = check_output([envpy, '-c',
491 'import sys; print(sys.executable)'])
492 self.assertEqual(out.strip(), envpy.encode())
493
494 @unittest.skipUnless(can_symlink(), 'Needs symlinks')
495 def test_executable_symlinks(self):
496 """
497 Test that the sys.executable value is as expected.
498 """
499 rmtree(self.env_dir)
500 builder = venv.EnvBuilder(clear=True, symlinks=True)
501 builder.create(self.env_dir)
502 envpy = self.envpy(real_env_dir=True)
503 out, err = check_output([envpy, '-c',
504 'import sys; print(sys.executable)'])
505 self.assertEqual(out.strip(), envpy.encode())
506
507 # gh-124651: test quoted strings
508 @unittest.skipIf(os.name == 'nt', 'contains invalid characters on Windows')
509 def test_special_chars_bash(self):
510 """
511 Test that the template strings are quoted properly (bash)
512 """
513 rmtree(self.env_dir)
514 bash = shutil.which('bash')
515 if bash is None:
516 self.skipTest('bash required for this test')
517 env_name = '"\';&&$e|\'"'
518 env_dir = os.path.join(os.path.realpath(self.env_dir), env_name)
519 builder = venv.EnvBuilder(clear=True)
520 builder.create(env_dir)
521 activate = os.path.join(env_dir, self.bindir, 'activate')
522 test_script = os.path.join(self.env_dir, 'test_special_chars.sh')
523 with open(test_script, "w") as f:
524 f.write(f'source {shlex.quote(activate)}\n'
525 'python -c \'import sys; print(sys.executable)\'\n'
526 'python -c \'import os; print(os.environ["VIRTUAL_ENV"])\'\n'
527 'deactivate\n')
528 out, err = check_output([bash, test_script])
529 lines = out.splitlines()
530 self.assertTrue(env_name.encode() in lines[0])
531 self.assertEndsWith(lines[1], env_name.encode())
532
533 # gh-124651: test quoted strings
534 @unittest.skipIf(os.name == 'nt', 'contains invalid characters on Windows')
535 @unittest.skipIf(sys.platform.startswith('netbsd'),
536 "NetBSD csh fails with quoted special chars; see gh-139308")
537 def test_special_chars_csh(self):
538 """
539 Test that the template strings are quoted properly (csh)
540 """
541 rmtree(self.env_dir)
542 csh = shutil.which('tcsh') or shutil.which('csh')
543 if csh is None:
544 self.skipTest('csh required for this test')
545 env_name = '"\';&&$e|\'"'
546 env_dir = os.path.join(os.path.realpath(self.env_dir), env_name)
547 builder = venv.EnvBuilder(clear=True)
548 builder.create(env_dir)
549 activate = os.path.join(env_dir, self.bindir, 'activate.csh')
550 test_script = os.path.join(self.env_dir, 'test_special_chars.csh')
551 with open(test_script, "w") as f:
552 f.write(f'source {shlex.quote(activate)}\n'
553 'python -c \'import sys; print(sys.executable)\'\n'
554 'python -c \'import os; print(os.environ["VIRTUAL_ENV"])\'\n'
555 'deactivate\n')
556 out, err = check_output([csh, test_script])
557 lines = out.splitlines()
558 self.assertTrue(env_name.encode() in lines[0])
559 self.assertEndsWith(lines[1], env_name.encode())
560
561 # gh-124651: test quoted strings on Windows
562 @unittest.skipUnless(os.name == 'nt', 'only relevant on Windows')
563 def test_special_chars_windows(self):
564 """
565 Test that the template strings are quoted properly on Windows
566 """
567 rmtree(self.env_dir)
568 env_name = "'&&^$e"
569 env_dir = os.path.join(os.path.realpath(self.env_dir), env_name)
570 builder = venv.EnvBuilder(clear=True)
571 builder.create(env_dir)
572 activate = os.path.join(env_dir, self.bindir, 'activate.bat')
573 test_batch = os.path.join(self.env_dir, 'test_special_chars.bat')
574 with open(test_batch, "w") as f:
575 f.write('@echo off\n'
576 f'"{activate}" & '
577 f'{self.exe} -c "import sys; print(sys.executable)" & '
578 f'{self.exe} -c "import os; print(os.environ[\'VIRTUAL_ENV\'])" & '
579 'deactivate')
580 out, err = check_output([test_batch])
581 lines = out.splitlines()
582 self.assertTrue(env_name.encode() in lines[0])
583 self.assertEndsWith(lines[1], env_name.encode())
584
585 @unittest.skipUnless(os.name == 'nt', 'only relevant on Windows')
586 def test_unicode_in_batch_file(self):
587 """
588 Test handling of Unicode paths
589 """
590 rmtree(self.env_dir)
591 env_dir = os.path.join(os.path.realpath(self.env_dir), 'ϼўТλФЙ')
592 builder = venv.EnvBuilder(clear=True)
593 builder.create(env_dir)
594 activate = os.path.join(env_dir, self.bindir, 'activate.bat')
595 out, err = check_output(
596 [activate, '&', self.exe, '-c', 'print(0)'],
597 encoding='oem',
598 )
599 self.assertEqual(out.strip(), '0')
600
601 @unittest.skipUnless(os.name == 'nt' and can_symlink(),
602 'symlinks on Windows')
603 def test_failed_symlink(self):
604 """
605 Test handling of failed symlinks on Windows.
606 """
607 rmtree(self.env_dir)
608 env_dir = os.path.join(os.path.realpath(self.env_dir), 'venv')
609 with patch('os.symlink') as mock_symlink:
610 mock_symlink.side_effect = OSError()
611 builder = venv.EnvBuilder(clear=True, symlinks=True)
612 _, err = self.run_with_capture(builder.create, env_dir)
613 filepath_regex = r"'[A-Z]:\\\\(?:[^\\\\]+\\\\)*[^\\\\]+'"
614 self.assertRegex(err, rf"Unable to symlink {filepath_regex} to {filepath_regex}")
615
616 @requireVenvCreate
617 def test_multiprocessing(self):
618 """
619 Test that the multiprocessing is able to spawn.
620 """
621 # bpo-36342: Instantiation of a Pool object imports the
622 # multiprocessing.synchronize module. Skip the test if this module
623 # cannot be imported.
624 skip_if_broken_multiprocessing_synchronize()
625
626 rmtree(self.env_dir)
627 self.run_with_capture(venv.create, self.env_dir)
628 out, err = check_output([self.envpy(real_env_dir=True), '-c',
629 'from multiprocessing import Pool; '
630 'pool = Pool(1); '
631 'print(pool.apply_async("Python".lower).get(3)); '
632 'pool.terminate()'])
633 self.assertEqual(out.strip(), "python".encode())
634
635 @requireVenvCreate
636 def test_multiprocessing_recursion(self):
637 """
638 Test that the multiprocessing is able to spawn itself
639 """
640 skip_if_broken_multiprocessing_synchronize()
641
642 rmtree(self.env_dir)
643 self.run_with_capture(venv.create, self.env_dir)
644 script = os.path.join(TEST_HOME_DIR, '_test_venv_multiprocessing.py')
645 subprocess.check_call([self.envpy(real_env_dir=True), "-I", script])
646
647 @unittest.skipIf(os.name == 'nt', 'not relevant on Windows')
648 def test_deactivate_with_strict_bash_opts(self):
649 bash = shutil.which("bash")
650 if bash is None:
651 self.skipTest("bash required for this test")
652 rmtree(self.env_dir)
653 builder = venv.EnvBuilder(clear=True)
654 builder.create(self.env_dir)
655 activate = os.path.join(self.env_dir, self.bindir, "activate")
656 test_script = os.path.join(self.env_dir, "test_strict.sh")
657 with open(test_script, "w") as f:
658 f.write("set -euo pipefail\n"
659 f"source {activate}\n"
660 "deactivate\n")
661 out, err = check_output([bash, test_script])
662 self.assertEqual(out, "".encode())
663 self.assertEqual(err, "".encode())
664
665 # gh-149701: Test exit code is zero even when hashing is disabled
666 @unittest.skipIf(os.name == 'nt', 'not relevant on Windows')
667 def test_deactivate_with_strict_bash_opts_and_hashing_disabled(self):
668 bash = shutil.which("bash")
669 if bash is None:
670 self.skipTest("bash required for this test")
671 rmtree(self.env_dir)
672 builder = venv.EnvBuilder(clear=True)
673 builder.create(self.env_dir)
674 activate = os.path.join(self.env_dir, self.bindir, "activate")
675 test_script = os.path.join(self.env_dir, "test_hash_disabled.sh")
676 with open(test_script, "w") as f:
677 f.write("set -euo pipefail\n"
678 "set +h\n" # disable hashing
679 f"source {activate}\n"
680 "deactivate")
681 out, err = check_output([bash, test_script])
682 self.assertEqual(out, "".encode())
683 self.assertEqual(err, "".encode())
684
685
686 @unittest.skipUnless(sys.platform == 'darwin', 'only relevant on macOS')
687 def test_macos_env(self):
688 rmtree(self.env_dir)
689 builder = venv.EnvBuilder()
690 builder.create(self.env_dir)
691
692 out, err = check_output([self.envpy(real_env_dir=True), '-c',
693 'import os; print("__PYVENV_LAUNCHER__" in os.environ)'])
694 self.assertEqual(out.strip(), 'False'.encode())
695
696 def test_pathsep_error(self):
697 """
698 Test that venv creation fails when the target directory contains
699 the path separator.
700 """
701 rmtree(self.env_dir)
702 bad_itempath = self.env_dir + os.pathsep
703 self.assertRaises(ValueError, venv.create, bad_itempath)
704 self.assertRaises(ValueError, venv.create, FakePath(bad_itempath))
705
706 @unittest.skipIf(os.name == 'nt', 'not relevant on Windows')
707 @requireVenvCreate
708 def test_zippath_from_non_installed_posix(self):
709 """
710 Test that when create venv from non-installed python, the zip path
711 value is as expected.
712 """
713 rmtree(self.env_dir)
714 # First try to create a non-installed python. It's not a real full
715 # functional non-installed python, but enough for this test.
716 platlibdir = sys.platlibdir
717 non_installed_dir = os.path.realpath(tempfile.mkdtemp())
718 self.addCleanup(rmtree, non_installed_dir)
719 bindir = os.path.join(non_installed_dir, self.bindir)
720 os.mkdir(bindir)
721 python_exe = os.path.basename(sys.executable)
722 shutil.copy2(sys.executable, os.path.join(bindir, python_exe))
723 libdir = os.path.join(non_installed_dir, platlibdir, self.lib[1])
724 os.makedirs(libdir)
725 landmark = os.path.join(libdir, "os.py")
726 abi_thread = "t" if sysconfig.get_config_var("Py_GIL_DISABLED") else ""
727 stdlib_zip = f"python{sys.version_info.major}{sys.version_info.minor}{abi_thread}"
728 zip_landmark = os.path.join(non_installed_dir,
729 platlibdir,
730 stdlib_zip)
731 additional_pythonpath_for_non_installed = []
732
733 # Copy stdlib files to the non-installed python so venv can
734 # correctly calculate the prefix.
735 for eachpath in sys.path:
736 if eachpath.endswith(".zip"):
737 if os.path.isfile(eachpath):
738 shutil.copyfile(
739 eachpath,
740 os.path.join(non_installed_dir, platlibdir))
741 elif os.path.isfile(os.path.join(eachpath, "os.py")):
742 names = os.listdir(eachpath)
743 ignored_names = copy_python_src_ignore(eachpath, names)
744 for name in names:
745 if name in ignored_names:
746 continue
747 if name == "site-packages":
748 continue
749 fn = os.path.join(eachpath, name)
750 if os.path.isfile(fn):
751 shutil.copy(fn, libdir)
752 elif os.path.isdir(fn):
753 shutil.copytree(fn, os.path.join(libdir, name),
754 ignore=copy_python_src_ignore)
755 else:
756 additional_pythonpath_for_non_installed.append(
757 eachpath)
758 cmd = [os.path.join(non_installed_dir, self.bindir, python_exe),
759 "-m",
760 "venv",
761 "--without-pip",
762 "--without-scm-ignore-files",
763 self.env_dir]
764 # Our fake non-installed python is not fully functional because
765 # it cannot find the extensions. Set PYTHONPATH so it can run the
766 # venv module correctly.
767 pythonpath = os.pathsep.join(
768 additional_pythonpath_for_non_installed)
769 # For python built with shared enabled. We need to set
770 # LD_LIBRARY_PATH so the non-installed python can find and link
771 # libpython.so
772 ld_library_path = sysconfig.get_config_var("LIBDIR")
773 if not ld_library_path or sysconfig.is_python_build():
774 ld_library_path = os.path.abspath(os.path.dirname(sys.executable))
775 if sys.platform == 'darwin':
776 ld_library_path_env = "DYLD_LIBRARY_PATH"
777 else:
778 ld_library_path_env = "LD_LIBRARY_PATH"
779 child_env = {
780 "PYTHONPATH": pythonpath,
781 ld_library_path_env: ld_library_path,
782 }
783 if asan_options := os.environ.get("ASAN_OPTIONS"):
784 # prevent https://github.com/python/cpython/issues/104839
785 child_env["ASAN_OPTIONS"] = asan_options
786 subprocess.check_call(cmd, env=child_env)
787 # Now check the venv created from the non-installed python has
788 # correct zip path in pythonpath.
789 target_python = os.path.join(self.env_dir, self.bindir, python_exe)
790 cmd = [target_python, '-S', '-c', 'import sys; print(sys.path)']
791 out, err = check_output(cmd)
792 self.assertTrue(zip_landmark.encode() in out)
793
794 @requireVenvCreate
795 def test_activate_shell_script_has_no_dos_newlines(self):
796 """
797 Test that the `activate` shell script contains no CR LF.
798 This is relevant for Cygwin, as the Windows build might have
799 converted line endings accidentally.
800 """
801 venv_dir = pathlib.Path(self.env_dir)
802 rmtree(venv_dir)
803 [[scripts_dir], *_] = self.ENV_SUBDIRS
804 script_path = venv_dir / scripts_dir / "activate"
805 venv.create(venv_dir)
806 with open(script_path, 'rb') as script:
807 for i, line in enumerate(script, 1):
808 error_message = f"CR LF found in line {i}"
809 self.assertNotEndsWith(line, b'\r\n', error_message)
810
811 @requireVenvCreate
812 def test_scm_ignore_files_git(self):
813 """
814 Test that a .gitignore file is created when "git" is specified.
815 The file should contain a `*\n` line.
816 """
817 self.run_with_capture(venv.create, self.env_dir,
818 scm_ignore_files={'git'})
819 file_lines = self.get_text_file_contents('.gitignore').splitlines()
820 self.assertIn('*', file_lines)
821
822 @requireVenvCreate
823 def test_create_scm_ignore_files_multiple(self):
824 """
825 Test that ``scm_ignore_files`` can work with multiple SCMs.
826 """
827 bzrignore_name = ".bzrignore"
828 contents = "# For Bazaar.\n*\n"
829
830 class BzrEnvBuilder(venv.EnvBuilder):
831 def create_bzr_ignore_file(self, context):
832 gitignore_path = os.path.join(context.env_dir, bzrignore_name)
833 with open(gitignore_path, 'w', encoding='utf-8') as file:
834 file.write(contents)
835
836 builder = BzrEnvBuilder(scm_ignore_files={'git', 'bzr'})
837 self.run_with_capture(builder.create, self.env_dir)
838
839 gitignore_lines = self.get_text_file_contents('.gitignore').splitlines()
840 self.assertIn('*', gitignore_lines)
841
842 bzrignore = self.get_text_file_contents(bzrignore_name)
843 self.assertEqual(bzrignore, contents)
844
845 @requireVenvCreate
846 def test_create_scm_ignore_files_empty(self):
847 """
848 Test that no default ignore files are created when ``scm_ignore_files``
849 is empty.
850 """
851 # scm_ignore_files is set to frozenset() by default.
852 self.run_with_capture(venv.create, self.env_dir)
853 with self.assertRaises(FileNotFoundError):
854 self.get_text_file_contents('.gitignore')
855
856 self.assertIn("--without-scm-ignore-files",
857 self.get_text_file_contents('pyvenv.cfg'))
858
859 @requireVenvCreate
860 def test_cli_with_scm_ignore_files(self):
861 """
862 Test that default SCM ignore files are created by default via the CLI.
863 """
864 self.run_with_capture(venv.main, ['--without-pip', self.env_dir])
865
866 gitignore_lines = self.get_text_file_contents('.gitignore').splitlines()
867 self.assertIn('*', gitignore_lines)
868
869 @requireVenvCreate
870 def test_cli_without_scm_ignore_files(self):
871 """
872 Test that ``--without-scm-ignore-files`` doesn't create SCM ignore files.
873 """
874 args = ['--without-pip', '--without-scm-ignore-files', self.env_dir]
875 self.run_with_capture(venv.main, args)
876
877 with self.assertRaises(FileNotFoundError):
878 self.get_text_file_contents('.gitignore')
879
880 def test_venv_same_path(self):
881 same_path = venv.EnvBuilder._same_path
882 if sys.platform == 'win32':
883 # Case-insensitive, and handles short/long names
884 tests = [
885 (True, TESTFN, TESTFN),
886 (True, TESTFN.lower(), TESTFN.upper()),
887 ]
888 import _winapi
889 # ProgramFiles is the most reliable path that will have short/long
890 progfiles = os.getenv('ProgramFiles')
891 if progfiles:
892 tests = [
893 *tests,
894 (True, progfiles, progfiles),
895 (True, _winapi.GetShortPathName(progfiles), _winapi.GetLongPathName(progfiles)),
896 ]
897 else:
898 # Just a simple case-sensitive comparison
899 tests = [
900 (True, TESTFN, TESTFN),
901 (False, TESTFN.lower(), TESTFN.upper()),
902 ]
903 for r, path1, path2 in tests:
904 with self.subTest(f"{path1}-{path2}"):
905 if r:
906 self.assertTrue(same_path(path1, path2))
907 else:
908 self.assertFalse(same_path(path1, path2))
909
910 # gh-126084: venvwlauncher should run pythonw, not python
911 @requireVenvCreate
912 @unittest.skipUnless(os.name == 'nt', 'only relevant on Windows')
913 def test_venvwlauncher(self):
914 """
915 Test that the GUI launcher runs the GUI python.
916 """
917 rmtree(self.env_dir)
918 venv.create(self.env_dir)
919 exename = self.exe
920 # Retain the debug suffix if present
921 if "python" in exename and not "pythonw" in exename:
922 exename = exename.replace("python", "pythonw")
923 envpyw = os.path.join(self.env_dir, self.bindir, exename)
924 try:
925 subprocess.check_call([envpyw, "-c", "import sys; "
926 "assert sys._base_executable.endswith('%s')" % exename])
927 except subprocess.CalledProcessError:
928 self.fail("venvwlauncher.exe did not run %s" % exename)
929
930
931 @requireVenvCreate
932 class EnsurePipTest(BaseTest):
933 """Test venv module installation of pip."""
934 def assert_pip_not_installed(self):
935 out, err = check_output([self.envpy(real_env_dir=True), '-c',
936 'try:\n import pip\nexcept ImportError:\n print("OK")'])
937 # We force everything to text, so unittest gives the detailed diff
938 # if we get unexpected results
939 err = err.decode("latin-1") # Force to text, prevent decoding errors
940 self.assertEqual(err, "")
941 out = out.decode("latin-1") # Force to text, prevent decoding errors
942 self.assertEqual(out.strip(), "OK")
943
944
945 def test_no_pip_by_default(self):
946 rmtree(self.env_dir)
947 self.run_with_capture(venv.create, self.env_dir)
948 self.assert_pip_not_installed()
949
950 def test_explicit_no_pip(self):
951 rmtree(self.env_dir)
952 self.run_with_capture(venv.create, self.env_dir, with_pip=False)
953 self.assert_pip_not_installed()
954
955 def test_devnull(self):
956 # Fix for issue #20053 uses os.devnull to force a config file to
957 # appear empty. However http://bugs.python.org/issue20541 means
958 # that doesn't currently work properly on Windows. Once that is
959 # fixed, the "win_location" part of test_with_pip should be restored
960 with open(os.devnull, "rb") as f:
961 self.assertEqual(f.read(), b"")
962
963 self.assertTrue(os.path.exists(os.devnull))
964
965 def do_test_with_pip(self, system_site_packages):
966 rmtree(self.env_dir)
967 with EnvironmentVarGuard() as envvars:
968 # pip's cross-version compatibility may trigger deprecation
969 # warnings in current versions of Python. Ensure related
970 # environment settings don't cause venv to fail.
971 envvars["PYTHONWARNINGS"] = "ignore"
972 # ensurepip is different enough from a normal pip invocation
973 # that we want to ensure it ignores the normal pip environment
974 # variable settings. We set PIP_NO_INSTALL here specifically
975 # to check that ensurepip (and hence venv) ignores it.
976 # See http://bugs.python.org/issue19734
977 envvars["PIP_NO_INSTALL"] = "1"
978 # Also check that we ignore the pip configuration file
979 # See http://bugs.python.org/issue20053
980 with tempfile.TemporaryDirectory() as home_dir:
981 envvars["HOME"] = home_dir
982 bad_config = "[global]\nno-install=1"
983 # Write to both config file names on all platforms to reduce
984 # cross-platform variation in test code behaviour
985 win_location = ("pip", "pip.ini")
986 posix_location = (".pip", "pip.conf")
987 # Skips win_location due to http://bugs.python.org/issue20541
988 for dirname, fname in (posix_location,):
989 dirpath = os.path.join(home_dir, dirname)
990 os.mkdir(dirpath)
991 fpath = os.path.join(dirpath, fname)
992 with open(fpath, 'w') as f:
993 f.write(bad_config)
994
995 # Actually run the create command with all that unhelpful
996 # config in place to ensure we ignore it
997 with self.nicer_error():
998 self.run_with_capture(venv.create, self.env_dir,
999 system_site_packages=system_site_packages,
1000 with_pip=True)
1001 # Ensure pip is available in the virtual environment
1002 # Ignore DeprecationWarning since pip code is not part of Python
1003 out, err = check_output([self.envpy(real_env_dir=True),
1004 '-W', 'ignore::DeprecationWarning',
1005 '-W', 'ignore::ImportWarning', '-I',
1006 '-m', 'pip', '--version'])
1007 # We force everything to text, so unittest gives the detailed diff
1008 # if we get unexpected results
1009 err = err.decode("latin-1") # Force to text, prevent decoding errors
1010 self.assertEqual(err, "")
1011 out = out.decode("latin-1") # Force to text, prevent decoding errors
1012 expected_version = "pip {}".format(ensurepip.version())
1013 self.assertStartsWith(out, expected_version)
1014 env_dir = os.fsencode(self.env_dir).decode("latin-1")
1015 self.assertIn(env_dir, out)
1016
1017 # http://bugs.python.org/issue19728
1018 # Check the private uninstall command provided for the Windows
1019 # installers works (at least in a virtual environment)
1020 with EnvironmentVarGuard() as envvars:
1021 with self.nicer_error():
1022 # It seems ensurepip._uninstall calls subprocesses which do not
1023 # inherit the interpreter settings.
1024 envvars["PYTHONWARNINGS"] = "ignore"
1025 out, err = check_output([self.envpy(real_env_dir=True),
1026 '-W', 'ignore::DeprecationWarning',
1027 '-W', 'ignore::ImportWarning', '-I',
1028 '-m', 'ensurepip._uninstall'])
1029 # We force everything to text, so unittest gives the detailed diff
1030 # if we get unexpected results
1031 err = err.decode("latin-1") # Force to text, prevent decoding errors
1032 # Ignore the warning:
1033 # "The directory '$HOME/.cache/pip/http' or its parent directory
1034 # is not owned by the current user and the cache has been disabled.
1035 # Please check the permissions and owner of that directory. If
1036 # executing pip with sudo, you may want sudo's -H flag."
1037 # where $HOME is replaced by the HOME environment variable.
1038 err = re.sub("^(WARNING: )?The directory .* or its parent directory "
1039 "is not owned or is not writable by the current user.*$", "",
1040 err, flags=re.MULTILINE)
1041 # Ignore warning about missing optional module:
1042 try:
1043 import ssl
1044 except ImportError:
1045 err = re.sub(
1046 "^WARNING: Disabling truststore since ssl support is missing$",
1047 "",
1048 err, flags=re.MULTILINE)
1049 self.assertEqual(err.rstrip(), "")
1050 # Being fairly specific regarding the expected behaviour for the
1051 # initial bundling phase in Python 3.4. If the output changes in
1052 # future pip versions, this test can likely be relaxed further.
1053 out = out.decode("latin-1") # Force to text, prevent decoding errors
1054 self.assertIn("Successfully uninstalled pip", out)
1055 # Check pip is now gone from the virtual environment. This only
1056 # applies in the system_site_packages=False case, because in the
1057 # other case, pip may still be available in the system site-packages
1058 if not system_site_packages:
1059 self.assert_pip_not_installed()
1060
1061 @contextlib.contextmanager
1062 def nicer_error(self):
1063 """
1064 Capture output from a failed subprocess for easier debugging.
1065
1066 The output this handler produces can be a little hard to read,
1067 but at least it has all the details.
1068 """
1069 try:
1070 yield
1071 except subprocess.CalledProcessError as exc:
1072 out = (exc.output or b'').decode(errors="replace")
1073 err = (exc.stderr or b'').decode(errors="replace")
1074 self.fail(
1075 f"{exc}\n\n"
1076 f"**Subprocess Output**\n{out}\n\n"
1077 f"**Subprocess Error**\n{err}"
1078 )
1079
1080 @requires_venv_with_pip()
1081 @requires_resource('cpu')
1082 def test_with_pip(self):
1083 self.do_test_with_pip(False)
1084 self.do_test_with_pip(True)
1085
1086
1087 if __name__ == "__main__":
1088 unittest.main()