Vinay Sajip | 4221142 | 2012-05-26 20:36:12 +0100 | [diff] [blame] | 1 | """ |
| 2 | Test harness for the venv module. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 3 | |
Vinay Sajip | 4221142 | 2012-05-26 20:36:12 +0100 | [diff] [blame] | 4 | Copyright (C) 2011-2012 Vinay Sajip. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 5 | """ |
| 6 | |
| 7 | import os |
| 8 | import os.path |
| 9 | import shutil |
| 10 | import sys |
| 11 | import tempfile |
| 12 | from test.support import (captured_stdout, captured_stderr, run_unittest, |
| 13 | can_symlink) |
| 14 | import unittest |
| 15 | import venv |
| 16 | |
| 17 | class BaseTest(unittest.TestCase): |
| 18 | """Base class for venv tests.""" |
| 19 | |
| 20 | def setUp(self): |
| 21 | self.env_dir = tempfile.mkdtemp() |
| 22 | if os.name == 'nt': |
| 23 | self.bindir = 'Scripts' |
| 24 | self.ps3name = 'pysetup3-script.py' |
| 25 | self.lib = ('Lib',) |
| 26 | self.include = 'Include' |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 27 | else: |
| 28 | self.bindir = 'bin' |
| 29 | self.ps3name = 'pysetup3' |
| 30 | self.lib = ('lib', 'python%s' % sys.version[:3]) |
| 31 | self.include = 'include' |
Vinay Sajip | 382a7c0 | 2012-05-28 16:34:47 +0100 | [diff] [blame^] | 32 | if sys.platform == 'darwin' and '__PYTHONV_LAUNCHER__' in env: |
| 33 | executable = os.environ['__PYTHONV_LAUNCHER__'] |
| 34 | else: |
| 35 | executable = sys.executable |
| 36 | self.exe = os.path.split(executable)[-1] |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 37 | |
| 38 | def tearDown(self): |
| 39 | shutil.rmtree(self.env_dir) |
| 40 | |
| 41 | def run_with_capture(self, func, *args, **kwargs): |
| 42 | with captured_stdout() as output: |
| 43 | with captured_stderr() as error: |
| 44 | func(*args, **kwargs) |
| 45 | return output.getvalue(), error.getvalue() |
| 46 | |
| 47 | def get_env_file(self, *args): |
| 48 | return os.path.join(self.env_dir, *args) |
| 49 | |
| 50 | def get_text_file_contents(self, *args): |
| 51 | with open(self.get_env_file(*args), 'r') as f: |
| 52 | result = f.read() |
| 53 | return result |
| 54 | |
| 55 | class BasicTest(BaseTest): |
| 56 | """Test venv module functionality.""" |
| 57 | |
Vinay Sajip | b3b49cd | 2012-05-27 18:39:22 +0100 | [diff] [blame] | 58 | def isdir(self, *args): |
| 59 | fn = self.get_env_file(*args) |
| 60 | self.assertTrue(os.path.isdir(fn)) |
| 61 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 62 | def test_defaults(self): |
| 63 | """ |
| 64 | Test the create function with default arguments. |
| 65 | """ |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 66 | shutil.rmtree(self.env_dir) |
| 67 | self.run_with_capture(venv.create, self.env_dir) |
Vinay Sajip | b3b49cd | 2012-05-27 18:39:22 +0100 | [diff] [blame] | 68 | self.isdir(self.bindir) |
| 69 | self.isdir(self.include) |
| 70 | self.isdir(*self.lib) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 71 | data = self.get_text_file_contents('pyvenv.cfg') |
| 72 | if sys.platform == 'darwin' and ('__PYTHONV_LAUNCHER__' |
| 73 | in os.environ): |
| 74 | executable = os.environ['__PYTHONV_LAUNCHER__'] |
| 75 | else: |
| 76 | executable = sys.executable |
| 77 | path = os.path.dirname(executable) |
| 78 | self.assertIn('home = %s' % path, data) |
| 79 | data = self.get_text_file_contents(self.bindir, self.ps3name) |
| 80 | self.assertTrue(data.startswith('#!%s%s' % (self.env_dir, os.sep))) |
| 81 | fn = self.get_env_file(self.bindir, self.exe) |
Vinay Sajip | 7e20349 | 2012-05-27 17:30:09 +0100 | [diff] [blame] | 82 | if not os.path.exists(fn): # diagnostics for Windows buildbot failures |
Vinay Sajip | b3b49cd | 2012-05-27 18:39:22 +0100 | [diff] [blame] | 83 | bd = self.get_env_file(self.bindir) |
| 84 | print('Contents of %r:' % bd) |
| 85 | print(' %r' % os.listdir(bd)) |
Vinay Sajip | 7e20349 | 2012-05-27 17:30:09 +0100 | [diff] [blame] | 86 | self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 87 | |
| 88 | def test_overwrite_existing(self): |
| 89 | """ |
| 90 | Test control of overwriting an existing environment directory. |
| 91 | """ |
| 92 | self.assertRaises(ValueError, venv.create, self.env_dir) |
| 93 | builder = venv.EnvBuilder(clear=True) |
| 94 | builder.create(self.env_dir) |
| 95 | |
Vinay Sajip | b3b49cd | 2012-05-27 18:39:22 +0100 | [diff] [blame] | 96 | def test_upgrade(self): |
| 97 | """ |
| 98 | Test upgrading an existing environment directory. |
| 99 | """ |
| 100 | builder = venv.EnvBuilder(upgrade=True) |
| 101 | self.run_with_capture(builder.create, self.env_dir) |
| 102 | self.isdir(self.bindir) |
| 103 | self.isdir(self.include) |
| 104 | self.isdir(*self.lib) |
| 105 | fn = self.get_env_file(self.bindir, self.exe) |
| 106 | if not os.path.exists(fn): # diagnostics for Windows buildbot failures |
| 107 | bd = self.get_env_file(self.bindir) |
| 108 | print('Contents of %r:' % bd) |
| 109 | print(' %r' % os.listdir(bd)) |
| 110 | self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn) |
| 111 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 112 | def test_isolation(self): |
| 113 | """ |
| 114 | Test isolation from system site-packages |
| 115 | """ |
| 116 | for ssp, s in ((True, 'true'), (False, 'false')): |
| 117 | builder = venv.EnvBuilder(clear=True, system_site_packages=ssp) |
| 118 | builder.create(self.env_dir) |
| 119 | data = self.get_text_file_contents('pyvenv.cfg') |
| 120 | self.assertIn('include-system-site-packages = %s\n' % s, data) |
| 121 | |
| 122 | @unittest.skipUnless(can_symlink(), 'Needs symlinks') |
| 123 | def test_symlinking(self): |
| 124 | """ |
| 125 | Test symlinking works as expected |
| 126 | """ |
| 127 | for usl in (False, True): |
| 128 | builder = venv.EnvBuilder(clear=True, symlinks=usl) |
| 129 | if (usl and sys.platform == 'darwin' and |
| 130 | '__PYTHONV_LAUNCHER__' in os.environ): |
| 131 | self.assertRaises(ValueError, builder.create, self.env_dir) |
| 132 | else: |
| 133 | builder.create(self.env_dir) |
| 134 | fn = self.get_env_file(self.bindir, self.exe) |
| 135 | # Don't test when False, because e.g. 'python' is always |
| 136 | # symlinked to 'python3.3' in the env, even when symlinking in |
| 137 | # general isn't wanted. |
| 138 | if usl: |
| 139 | self.assertTrue(os.path.islink(fn)) |
| 140 | |
| 141 | def test_main(): |
| 142 | run_unittest(BasicTest) |
| 143 | |
| 144 | if __name__ == "__main__": |
| 145 | test_main() |