55Licensed to the PSF under a contributor agreement.
66"""
77
8- # pip isn't working yet
9- # import ensurepip
8+ import ensurepip
109import os
1110import os .path
1211import re
1514import subprocess
1615import sys
1716import tempfile
18- from test .support import captured_stdout , captured_stderr , requires_zlib
17+ from test .support import (captured_stdout , captured_stderr , requires_zlib ,
18+ skip_if_broken_multiprocessing_synchronize )
1919from test .support .os_helper import (can_symlink , EnvironmentVarGuard , rmtree )
20- from test .support .import_helper import import_module
21- import threading
2220import unittest
2321import venv
22+ from unittest .mock import patch
2423
2524try :
2625 import ctypes
@@ -80,8 +79,8 @@ def run_with_capture(self, func, *args, **kwargs):
8079 def get_env_file (self , * args ):
8180 return os .path .join (self .env_dir , * args )
8281
83- def get_text_file_contents (self , * args ):
84- with open (self .get_env_file (* args ), 'r' ) as f :
82+ def get_text_file_contents (self , * args , encoding = 'utf-8' ):
83+ with open (self .get_env_file (* args ), 'r' , encoding = encoding ) as f :
8584 result = f .read ()
8685 return result
8786
@@ -139,6 +138,45 @@ def test_prompt(self):
139138 self .assertEqual (context .prompt , '(My prompt) ' )
140139 self .assertIn ("prompt = 'My prompt'\n " , data )
141140
141+ rmtree (self .env_dir )
142+ builder = venv .EnvBuilder (prompt = '.' )
143+ cwd = os .path .basename (os .getcwd ())
144+ self .run_with_capture (builder .create , self .env_dir )
145+ context = builder .ensure_directories (self .env_dir )
146+ data = self .get_text_file_contents ('pyvenv.cfg' )
147+ self .assertEqual (context .prompt , '(%s) ' % cwd )
148+ self .assertIn ("prompt = '%s'\n " % cwd , data )
149+
150+ def test_upgrade_dependencies (self ):
151+ builder = venv .EnvBuilder ()
152+ bin_path = 'Scripts' if sys .platform == 'win32' else 'bin'
153+ python_exe = os .path .split (sys .executable )[1 ]
154+ with tempfile .TemporaryDirectory () as fake_env_dir :
155+ expect_exe = os .path .normcase (
156+ os .path .join (fake_env_dir , bin_path , python_exe )
157+ )
158+ if sys .platform == 'win32' :
159+ expect_exe = os .path .normcase (os .path .realpath (expect_exe ))
160+
161+ def pip_cmd_checker (cmd ):
162+ cmd [0 ] = os .path .normcase (cmd [0 ])
163+ self .assertEqual (
164+ cmd ,
165+ [
166+ expect_exe ,
167+ '-m' ,
168+ 'pip' ,
169+ 'install' ,
170+ '--upgrade' ,
171+ 'pip' ,
172+ 'setuptools'
173+ ]
174+ )
175+
176+ fake_context = builder .ensure_directories (fake_env_dir )
177+ with patch ('venv.subprocess.check_call' , pip_cmd_checker ):
178+ builder .upgrade_dependencies (fake_context )
179+
142180 @requireVenvCreate
143181 def test_prefixes (self ):
144182 """
@@ -325,10 +363,11 @@ def test_multiprocessing(self):
325363 """
326364 Test that the multiprocessing is able to spawn.
327365 """
328- # Issue bpo-36342: Instanciation of a Pool object imports the
366+ # bpo-36342: Instantiation of a Pool object imports the
329367 # multiprocessing.synchronize module. Skip the test if this module
330368 # cannot be imported.
331- import_module ('multiprocessing.synchronize' )
369+ skip_if_broken_multiprocessing_synchronize ()
370+
332371 rmtree (self .env_dir )
333372 self .run_with_capture (venv .create , self .env_dir )
334373 envpy = os .path .join (os .path .realpath (self .env_dir ),
@@ -413,7 +452,7 @@ def do_test_with_pip(self, system_site_packages):
413452 # pip's cross-version compatibility may trigger deprecation
414453 # warnings in current versions of Python. Ensure related
415454 # environment settings don't cause venv to fail.
416- envvars ["PYTHONWARNINGS" ] = "e "
455+ envvars ["PYTHONWARNINGS" ] = "ignore "
417456 # ensurepip is different enough from a normal pip invocation
418457 # that we want to ensure it ignores the normal pip environment
419458 # variable settings. We set PIP_NO_INSTALL here specifically
@@ -452,7 +491,8 @@ def do_test_with_pip(self, system_site_packages):
452491 # Ensure pip is available in the virtual environment
453492 envpy = os .path .join (os .path .realpath (self .env_dir ), self .bindir , self .exe )
454493 # Ignore DeprecationWarning since pip code is not part of Python
455- out , err = check_output ([envpy , '-W' , 'ignore::DeprecationWarning' , '-I' ,
494+ out , err = check_output ([envpy , '-W' , 'ignore::DeprecationWarning' ,
495+ '-W' , 'ignore::ImportWarning' , '-I' ,
456496 '-m' , 'pip' , '--version' ])
457497 # We force everything to text, so unittest gives the detailed diff
458498 # if we get unexpected results
@@ -468,8 +508,12 @@ def do_test_with_pip(self, system_site_packages):
468508 # Check the private uninstall command provided for the Windows
469509 # installers works (at least in a virtual environment)
470510 with EnvironmentVarGuard () as envvars :
511+ # It seems ensurepip._uninstall calls subprocesses which do not
512+ # inherit the interpreter settings.
513+ envvars ["PYTHONWARNINGS" ] = "ignore"
471514 out , err = check_output ([envpy ,
472- '-W' , 'ignore::DeprecationWarning' , '-I' ,
515+ '-W' , 'ignore::DeprecationWarning' ,
516+ '-W' , 'ignore::ImportWarning' , '-I' ,
473517 '-m' , 'ensurepip._uninstall' ])
474518 # We force everything to text, so unittest gives the detailed diff
475519 # if we get unexpected results
@@ -481,7 +525,7 @@ def do_test_with_pip(self, system_site_packages):
481525 # executing pip with sudo, you may want sudo's -H flag."
482526 # where $HOME is replaced by the HOME environment variable.
483527 err = re .sub ("^(WARNING: )?The directory .* or its parent directory "
484- "is not owned by the current user .*$" , "" ,
528+ "is not owned or is not writable by the current user.*$" , "" ,
485529 err , flags = re .MULTILINE )
486530 self .assertEqual (err .rstrip (), "" )
487531 # Being fairly specific regarding the expected behaviour for the
@@ -497,10 +541,8 @@ def do_test_with_pip(self, system_site_packages):
497541 self .assert_pip_not_installed ()
498542
499543 # Issue #26610: pip/pep425tags.py requires ctypes
500- # TODO: RUSTPYTHON
501544 @unittest .skipUnless (ctypes , 'pip requires ctypes' )
502- @requires_zlib
503- @unittest .expectedFailure
545+ @requires_zlib ()
504546 def test_with_pip (self ):
505547 self .do_test_with_pip (False )
506548 self .do_test_with_pip (True )
0 commit comments