mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
bpo-34011: Fixes missing venv files and other tests (GH-9458)
This commit is contained in:
parent
bc85475058
commit
f14c28f397
8 changed files with 146 additions and 112 deletions
|
@ -27,6 +27,17 @@ except ImportError:
|
|||
skipInVenv = unittest.skipIf(sys.prefix != sys.base_prefix,
|
||||
'Test not appropriate in a venv')
|
||||
|
||||
def check_output(cmd, encoding=None):
|
||||
p = subprocess.Popen(cmd,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
encoding=encoding)
|
||||
out, err = p.communicate()
|
||||
if p.returncode:
|
||||
raise subprocess.CalledProcessError(
|
||||
p.returncode, cmd, None, out, err)
|
||||
return out, err
|
||||
|
||||
class BaseTest(unittest.TestCase):
|
||||
"""Base class for venv tests."""
|
||||
maxDiff = 80 * 50
|
||||
|
@ -134,9 +145,7 @@ class BasicTest(BaseTest):
|
|||
('base_prefix', sys.prefix),
|
||||
('base_exec_prefix', sys.exec_prefix)):
|
||||
cmd[2] = 'import sys; print(sys.%s)' % prefix
|
||||
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE)
|
||||
out, err = p.communicate()
|
||||
out, err = check_output(cmd)
|
||||
self.assertEqual(out.strip(), expected.encode())
|
||||
|
||||
if sys.platform == 'win32':
|
||||
|
@ -259,11 +268,10 @@ class BasicTest(BaseTest):
|
|||
"""
|
||||
rmtree(self.env_dir)
|
||||
self.run_with_capture(venv.create, self.env_dir)
|
||||
envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe)
|
||||
cmd = [envpy, '-c', 'import sys; print(sys.executable)']
|
||||
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE)
|
||||
out, err = p.communicate()
|
||||
envpy = os.path.join(os.path.realpath(self.env_dir),
|
||||
self.bindir, self.exe)
|
||||
out, err = check_output([envpy, '-c',
|
||||
'import sys; print(sys.executable)'])
|
||||
self.assertEqual(out.strip(), envpy.encode())
|
||||
|
||||
@unittest.skipUnless(can_symlink(), 'Needs symlinks')
|
||||
|
@ -274,17 +282,16 @@ class BasicTest(BaseTest):
|
|||
rmtree(self.env_dir)
|
||||
builder = venv.EnvBuilder(clear=True, symlinks=True)
|
||||
builder.create(self.env_dir)
|
||||
envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe)
|
||||
cmd = [envpy, '-c', 'import sys; print(sys.executable)']
|
||||
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE)
|
||||
out, err = p.communicate()
|
||||
envpy = os.path.join(os.path.realpath(self.env_dir),
|
||||
self.bindir, self.exe)
|
||||
out, err = check_output([envpy, '-c',
|
||||
'import sys; print(sys.executable)'])
|
||||
self.assertEqual(out.strip(), envpy.encode())
|
||||
|
||||
@unittest.skipUnless(os.name == 'nt', 'only relevant on Windows')
|
||||
def test_unicode_in_batch_file(self):
|
||||
"""
|
||||
Test isolation from system site-packages
|
||||
Test handling of Unicode paths
|
||||
"""
|
||||
rmtree(self.env_dir)
|
||||
env_dir = os.path.join(os.path.realpath(self.env_dir), 'ϼўТλФЙ')
|
||||
|
@ -292,12 +299,10 @@ class BasicTest(BaseTest):
|
|||
builder.create(env_dir)
|
||||
activate = os.path.join(env_dir, self.bindir, 'activate.bat')
|
||||
envpy = os.path.join(env_dir, self.bindir, self.exe)
|
||||
cmd = [activate, '&', self.exe, '-c', 'print(0)']
|
||||
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE, encoding='oem',
|
||||
shell=True)
|
||||
out, err = p.communicate()
|
||||
print(err)
|
||||
out, err = check_output(
|
||||
[activate, '&', self.exe, '-c', 'print(0)'],
|
||||
encoding='oem',
|
||||
)
|
||||
self.assertEqual(out.strip(), '0')
|
||||
|
||||
@skipInVenv
|
||||
|
@ -306,11 +311,8 @@ class EnsurePipTest(BaseTest):
|
|||
def assert_pip_not_installed(self):
|
||||
envpy = os.path.join(os.path.realpath(self.env_dir),
|
||||
self.bindir, self.exe)
|
||||
try_import = 'try:\n import pip\nexcept ImportError:\n print("OK")'
|
||||
cmd = [envpy, '-c', try_import]
|
||||
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE)
|
||||
out, err = p.communicate()
|
||||
out, err = check_output([envpy, '-c',
|
||||
'try:\n import pip\nexcept ImportError:\n print("OK")'])
|
||||
# We force everything to text, so unittest gives the detailed diff
|
||||
# if we get unexpected results
|
||||
err = err.decode("latin-1") # Force to text, prevent decoding errors
|
||||
|
@ -388,11 +390,8 @@ class EnsurePipTest(BaseTest):
|
|||
# Ensure pip is available in the virtual environment
|
||||
envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe)
|
||||
# Ignore DeprecationWarning since pip code is not part of Python
|
||||
cmd = [envpy, '-W', 'ignore::DeprecationWarning', '-I',
|
||||
'-m', 'pip', '--version']
|
||||
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE)
|
||||
out, err = p.communicate()
|
||||
out, err = check_output([envpy, '-W', 'ignore::DeprecationWarning', '-I',
|
||||
'-m', 'pip', '--version'])
|
||||
# We force everything to text, so unittest gives the detailed diff
|
||||
# if we get unexpected results
|
||||
err = err.decode("latin-1") # Force to text, prevent decoding errors
|
||||
|
@ -406,12 +405,10 @@ class EnsurePipTest(BaseTest):
|
|||
# http://bugs.python.org/issue19728
|
||||
# Check the private uninstall command provided for the Windows
|
||||
# installers works (at least in a virtual environment)
|
||||
cmd = [envpy, '-W', 'ignore::DeprecationWarning', '-I',
|
||||
'-m', 'ensurepip._uninstall']
|
||||
with EnvironmentVarGuard() as envvars:
|
||||
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE)
|
||||
out, err = p.communicate()
|
||||
out, err = check_output([envpy,
|
||||
'-W', 'ignore::DeprecationWarning', '-I',
|
||||
'-m', 'ensurepip._uninstall'])
|
||||
# We force everything to text, so unittest gives the detailed diff
|
||||
# if we get unexpected results
|
||||
err = err.decode("latin-1") # Force to text, prevent decoding errors
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue