mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
Implemented PEP 405 (Python virtual environments).
This commit is contained in:
parent
f2bdc3690a
commit
7ded1f0f69
41 changed files with 1454 additions and 66 deletions
|
@ -564,7 +564,7 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
|
|||
random.shuffle(selected)
|
||||
if trace:
|
||||
import trace, tempfile
|
||||
tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,
|
||||
tracer = trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix,
|
||||
tempfile.gettempdir()],
|
||||
trace=False, count=True)
|
||||
|
||||
|
|
|
@ -228,7 +228,7 @@ def test_main(verbose=None):
|
|||
|
||||
def test_coverage(coverdir):
|
||||
trace = support.import_module('trace')
|
||||
tracer=trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,],
|
||||
tracer=trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix,],
|
||||
trace=0, count=1)
|
||||
tracer.run('reload(cmd);test_main()')
|
||||
r=tracer.results()
|
||||
|
|
|
@ -2543,7 +2543,7 @@ import sys, re, io
|
|||
|
||||
def test_coverage(coverdir):
|
||||
trace = support.import_module('trace')
|
||||
tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,],
|
||||
tracer = trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix,],
|
||||
trace=0, count=1)
|
||||
tracer.run('test_main()')
|
||||
r = tracer.results()
|
||||
|
|
|
@ -189,6 +189,8 @@ class ProcessTestCase(BaseTestCase):
|
|||
p.wait()
|
||||
self.assertEqual(p.stderr, None)
|
||||
|
||||
@unittest.skipIf(sys.base_prefix != sys.prefix,
|
||||
'Test is not venv-compatible')
|
||||
def test_executable_with_cwd(self):
|
||||
python_dir = os.path.dirname(os.path.realpath(sys.executable))
|
||||
p = subprocess.Popen(["somethingyoudonthave", "-c",
|
||||
|
@ -197,6 +199,8 @@ class ProcessTestCase(BaseTestCase):
|
|||
p.wait()
|
||||
self.assertEqual(p.returncode, 47)
|
||||
|
||||
@unittest.skipIf(sys.base_prefix != sys.prefix,
|
||||
'Test is not venv-compatible')
|
||||
@unittest.skipIf(sysconfig.is_python_build(),
|
||||
"need an installed Python. See #7774")
|
||||
def test_executable_without_cwd(self):
|
||||
|
|
|
@ -419,6 +419,7 @@ class SysModuleTest(unittest.TestCase):
|
|||
self.assertIsInstance(sys.builtin_module_names, tuple)
|
||||
self.assertIsInstance(sys.copyright, str)
|
||||
self.assertIsInstance(sys.exec_prefix, str)
|
||||
self.assertIsInstance(sys.base_exec_prefix, str)
|
||||
self.assertIsInstance(sys.executable, str)
|
||||
self.assertEqual(len(sys.float_info), 11)
|
||||
self.assertEqual(sys.float_info.radix, 2)
|
||||
|
@ -450,6 +451,7 @@ class SysModuleTest(unittest.TestCase):
|
|||
self.assertEqual(sys.maxunicode, 0x10FFFF)
|
||||
self.assertIsInstance(sys.platform, str)
|
||||
self.assertIsInstance(sys.prefix, str)
|
||||
self.assertIsInstance(sys.base_prefix, str)
|
||||
self.assertIsInstance(sys.version, str)
|
||||
vi = sys.version_info
|
||||
self.assertIsInstance(vi[:], tuple)
|
||||
|
@ -541,6 +543,8 @@ class SysModuleTest(unittest.TestCase):
|
|||
out = p.communicate()[0].strip()
|
||||
self.assertEqual(out, b'?')
|
||||
|
||||
@unittest.skipIf(sys.base_prefix != sys.prefix,
|
||||
'Test is not venv-compatible')
|
||||
def test_executable(self):
|
||||
# sys.executable should be absolute
|
||||
self.assertEqual(os.path.abspath(sys.executable), sys.executable)
|
||||
|
|
|
@ -260,12 +260,17 @@ class TestSysConfig(unittest.TestCase):
|
|||
# the global scheme mirrors the distinction between prefix and
|
||||
# exec-prefix but not the user scheme, so we have to adapt the paths
|
||||
# before comparing (issue #9100)
|
||||
adapt = sys.prefix != sys.exec_prefix
|
||||
adapt = sys.base_prefix != sys.base_exec_prefix
|
||||
for name in ('stdlib', 'platstdlib', 'purelib', 'platlib'):
|
||||
global_path = get_path(name, 'posix_prefix')
|
||||
if adapt:
|
||||
global_path = global_path.replace(sys.exec_prefix, sys.prefix)
|
||||
base = base.replace(sys.exec_prefix, sys.prefix)
|
||||
global_path = global_path.replace(sys.exec_prefix, sys.base_prefix)
|
||||
base = base.replace(sys.exec_prefix, sys.base_prefix)
|
||||
elif sys.base_prefix != sys.prefix:
|
||||
# virtual environment? Likewise, we have to adapt the paths
|
||||
# before comparing
|
||||
global_path = global_path.replace(sys.base_prefix, sys.prefix)
|
||||
base = base.replace(sys.base_prefix, sys.prefix)
|
||||
user_path = get_path(name, 'posix_user')
|
||||
self.assertEqual(user_path, global_path.replace(base, user, 1))
|
||||
|
||||
|
|
|
@ -316,8 +316,8 @@ class TestCoverage(unittest.TestCase):
|
|||
# Ignore all files, nothing should be traced nor printed
|
||||
libpath = os.path.normpath(os.path.dirname(os.__file__))
|
||||
# sys.prefix does not work when running from a checkout
|
||||
tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix, libpath],
|
||||
trace=0, count=1)
|
||||
tracer = trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix,
|
||||
libpath], trace=0, count=1)
|
||||
with captured_stdout() as stdout:
|
||||
self._coverage(tracer)
|
||||
if os.path.exists(TESTFN):
|
||||
|
|
139
Lib/test/test_venv.py
Normal file
139
Lib/test/test_venv.py
Normal file
|
@ -0,0 +1,139 @@
|
|||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright 2011 by Vinay Sajip. All Rights Reserved.
|
||||
#
|
||||
# Permission to use, copy, modify, and distribute this software and its
|
||||
# documentation for any purpose and without fee is hereby granted,
|
||||
# provided that the above copyright notice appear in all copies and that
|
||||
# both that copyright notice and this permission notice appear in
|
||||
# supporting documentation, and that the name of Vinay Sajip
|
||||
# not be used in advertising or publicity pertaining to distribution
|
||||
# of the software without specific, written prior permission.
|
||||
# VINAY SAJIP DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
|
||||
# ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
|
||||
# VINAY SAJIP BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
|
||||
# ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
|
||||
# IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
||||
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
"""Test harness for the venv module. Run all tests.
|
||||
|
||||
Copyright (C) 2011 Vinay Sajip. All Rights Reserved.
|
||||
"""
|
||||
|
||||
import os
|
||||
import os.path
|
||||
import shutil
|
||||
import sys
|
||||
import tempfile
|
||||
from test.support import (captured_stdout, captured_stderr, run_unittest,
|
||||
can_symlink)
|
||||
import unittest
|
||||
import venv
|
||||
|
||||
class BaseTest(unittest.TestCase):
|
||||
"""Base class for venv tests."""
|
||||
|
||||
def setUp(self):
|
||||
self.env_dir = tempfile.mkdtemp()
|
||||
if os.name == 'nt':
|
||||
self.bindir = 'Scripts'
|
||||
self.ps3name = 'pysetup3-script.py'
|
||||
self.lib = ('Lib',)
|
||||
self.include = 'Include'
|
||||
self.exe = 'python.exe'
|
||||
else:
|
||||
self.bindir = 'bin'
|
||||
self.ps3name = 'pysetup3'
|
||||
self.lib = ('lib', 'python%s' % sys.version[:3])
|
||||
self.include = 'include'
|
||||
self.exe = 'python'
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(self.env_dir)
|
||||
|
||||
def run_with_capture(self, func, *args, **kwargs):
|
||||
with captured_stdout() as output:
|
||||
with captured_stderr() as error:
|
||||
func(*args, **kwargs)
|
||||
return output.getvalue(), error.getvalue()
|
||||
|
||||
def get_env_file(self, *args):
|
||||
return os.path.join(self.env_dir, *args)
|
||||
|
||||
def get_text_file_contents(self, *args):
|
||||
with open(self.get_env_file(*args), 'r') as f:
|
||||
result = f.read()
|
||||
return result
|
||||
|
||||
class BasicTest(BaseTest):
|
||||
"""Test venv module functionality."""
|
||||
|
||||
def test_defaults(self):
|
||||
"""
|
||||
Test the create function with default arguments.
|
||||
"""
|
||||
def isdir(*args):
|
||||
fn = self.get_env_file(*args)
|
||||
self.assertTrue(os.path.isdir(fn))
|
||||
|
||||
shutil.rmtree(self.env_dir)
|
||||
self.run_with_capture(venv.create, self.env_dir)
|
||||
isdir(self.bindir)
|
||||
isdir(self.include)
|
||||
isdir(*self.lib)
|
||||
data = self.get_text_file_contents('pyvenv.cfg')
|
||||
if sys.platform == 'darwin' and ('__PYTHONV_LAUNCHER__'
|
||||
in os.environ):
|
||||
executable = os.environ['__PYTHONV_LAUNCHER__']
|
||||
else:
|
||||
executable = sys.executable
|
||||
path = os.path.dirname(executable)
|
||||
self.assertIn('home = %s' % path, data)
|
||||
data = self.get_text_file_contents(self.bindir, self.ps3name)
|
||||
self.assertTrue(data.startswith('#!%s%s' % (self.env_dir, os.sep)))
|
||||
fn = self.get_env_file(self.bindir, self.exe)
|
||||
self.assertTrue(os.path.exists(fn))
|
||||
|
||||
def test_overwrite_existing(self):
|
||||
"""
|
||||
Test control of overwriting an existing environment directory.
|
||||
"""
|
||||
self.assertRaises(ValueError, venv.create, self.env_dir)
|
||||
builder = venv.EnvBuilder(clear=True)
|
||||
builder.create(self.env_dir)
|
||||
|
||||
def test_isolation(self):
|
||||
"""
|
||||
Test isolation from system site-packages
|
||||
"""
|
||||
for ssp, s in ((True, 'true'), (False, 'false')):
|
||||
builder = venv.EnvBuilder(clear=True, system_site_packages=ssp)
|
||||
builder.create(self.env_dir)
|
||||
data = self.get_text_file_contents('pyvenv.cfg')
|
||||
self.assertIn('include-system-site-packages = %s\n' % s, data)
|
||||
|
||||
@unittest.skipUnless(can_symlink(), 'Needs symlinks')
|
||||
def test_symlinking(self):
|
||||
"""
|
||||
Test symlinking works as expected
|
||||
"""
|
||||
for usl in (False, True):
|
||||
builder = venv.EnvBuilder(clear=True, symlinks=usl)
|
||||
if (usl and sys.platform == 'darwin' and
|
||||
'__PYTHONV_LAUNCHER__' in os.environ):
|
||||
self.assertRaises(ValueError, builder.create, self.env_dir)
|
||||
else:
|
||||
builder.create(self.env_dir)
|
||||
fn = self.get_env_file(self.bindir, self.exe)
|
||||
# Don't test when False, because e.g. 'python' is always
|
||||
# symlinked to 'python3.3' in the env, even when symlinking in
|
||||
# general isn't wanted.
|
||||
if usl:
|
||||
self.assertTrue(os.path.islink(fn))
|
||||
|
||||
def test_main():
|
||||
run_unittest(BasicTest)
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_main()
|
Loading…
Add table
Add a link
Reference in a new issue