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 | 2895244 | 2012-06-25 00:47:46 +0100 | [diff] [blame] | 5 | Licensed to the PSF under a contributor agreement. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 6 | """ |
| 7 | |
| 8 | import os |
| 9 | import os.path |
| 10 | import shutil |
Vinay Sajip | 3874e54 | 2012-07-03 16:56:40 +0100 | [diff] [blame] | 11 | import subprocess |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 12 | import sys |
| 13 | import tempfile |
| 14 | from test.support import (captured_stdout, captured_stderr, run_unittest, |
| 15 | can_symlink) |
| 16 | import unittest |
| 17 | import venv |
| 18 | |
| 19 | class BaseTest(unittest.TestCase): |
| 20 | """Base class for venv tests.""" |
| 21 | |
| 22 | def setUp(self): |
Ned Deily | 045bd53 | 2012-07-13 15:48:04 -0700 | [diff] [blame] | 23 | self.env_dir = os.path.realpath(tempfile.mkdtemp()) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 24 | if os.name == 'nt': |
| 25 | self.bindir = 'Scripts' |
Éric Araujo | aa789ac | 2012-06-24 13:51:22 -0400 | [diff] [blame] | 26 | self.pydocname = 'pydoc.py' |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 27 | self.lib = ('Lib',) |
| 28 | self.include = 'Include' |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 29 | else: |
| 30 | self.bindir = 'bin' |
Éric Araujo | aa789ac | 2012-06-24 13:51:22 -0400 | [diff] [blame] | 31 | self.pydocname = 'pydoc' |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 32 | self.lib = ('lib', 'python%s' % sys.version[:3]) |
| 33 | self.include = 'include' |
Vinay Sajip | 2895244 | 2012-06-25 00:47:46 +0100 | [diff] [blame] | 34 | if sys.platform == 'darwin' and '__PYVENV_LAUNCHER__' in os.environ: |
| 35 | executable = os.environ['__PYVENV_LAUNCHER__'] |
Vinay Sajip | 382a7c0 | 2012-05-28 16:34:47 +0100 | [diff] [blame] | 36 | else: |
| 37 | executable = sys.executable |
| 38 | self.exe = os.path.split(executable)[-1] |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 39 | |
| 40 | def tearDown(self): |
| 41 | shutil.rmtree(self.env_dir) |
| 42 | |
| 43 | def run_with_capture(self, func, *args, **kwargs): |
| 44 | with captured_stdout() as output: |
| 45 | with captured_stderr() as error: |
| 46 | func(*args, **kwargs) |
| 47 | return output.getvalue(), error.getvalue() |
| 48 | |
| 49 | def get_env_file(self, *args): |
| 50 | return os.path.join(self.env_dir, *args) |
| 51 | |
| 52 | def get_text_file_contents(self, *args): |
| 53 | with open(self.get_env_file(*args), 'r') as f: |
| 54 | result = f.read() |
| 55 | return result |
| 56 | |
| 57 | class BasicTest(BaseTest): |
| 58 | """Test venv module functionality.""" |
| 59 | |
Vinay Sajip | b3b49cd | 2012-05-27 18:39:22 +0100 | [diff] [blame] | 60 | def isdir(self, *args): |
| 61 | fn = self.get_env_file(*args) |
| 62 | self.assertTrue(os.path.isdir(fn)) |
| 63 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 64 | def test_defaults(self): |
| 65 | """ |
| 66 | Test the create function with default arguments. |
| 67 | """ |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 68 | shutil.rmtree(self.env_dir) |
| 69 | self.run_with_capture(venv.create, self.env_dir) |
Vinay Sajip | b3b49cd | 2012-05-27 18:39:22 +0100 | [diff] [blame] | 70 | self.isdir(self.bindir) |
| 71 | self.isdir(self.include) |
| 72 | self.isdir(*self.lib) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 73 | data = self.get_text_file_contents('pyvenv.cfg') |
Vinay Sajip | 2895244 | 2012-06-25 00:47:46 +0100 | [diff] [blame] | 74 | if sys.platform == 'darwin' and ('__PYVENV_LAUNCHER__' |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 75 | in os.environ): |
Vinay Sajip | 2895244 | 2012-06-25 00:47:46 +0100 | [diff] [blame] | 76 | executable = os.environ['__PYVENV_LAUNCHER__'] |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 77 | else: |
| 78 | executable = sys.executable |
| 79 | path = os.path.dirname(executable) |
| 80 | self.assertIn('home = %s' % path, data) |
Éric Araujo | aa789ac | 2012-06-24 13:51:22 -0400 | [diff] [blame] | 81 | data = self.get_text_file_contents(self.bindir, self.pydocname) |
| 82 | self.assertTrue(data.startswith('#!%s%s' % (self.env_dir, os.sep))) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 83 | fn = self.get_env_file(self.bindir, self.exe) |
Vinay Sajip | 7e20349 | 2012-05-27 17:30:09 +0100 | [diff] [blame] | 84 | if not os.path.exists(fn): # diagnostics for Windows buildbot failures |
Vinay Sajip | b3b49cd | 2012-05-27 18:39:22 +0100 | [diff] [blame] | 85 | bd = self.get_env_file(self.bindir) |
| 86 | print('Contents of %r:' % bd) |
| 87 | print(' %r' % os.listdir(bd)) |
Vinay Sajip | 7e20349 | 2012-05-27 17:30:09 +0100 | [diff] [blame] | 88 | self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 89 | |
Vinay Sajip | 509d87d | 2012-07-15 16:12:54 +0100 | [diff] [blame] | 90 | @unittest.skipIf(sys.prefix != sys.base_prefix, 'Test not appropriate ' |
| 91 | 'in a venv') |
Vinay Sajip | 3874e54 | 2012-07-03 16:56:40 +0100 | [diff] [blame] | 92 | def test_prefixes(self): |
| 93 | """ |
| 94 | Test that the prefix values are as expected. |
| 95 | """ |
| 96 | #check our prefixes |
| 97 | self.assertEqual(sys.base_prefix, sys.prefix) |
| 98 | self.assertEqual(sys.base_exec_prefix, sys.exec_prefix) |
| 99 | |
| 100 | # check a venv's prefixes |
| 101 | shutil.rmtree(self.env_dir) |
| 102 | self.run_with_capture(venv.create, self.env_dir) |
| 103 | envpy = os.path.join(self.env_dir, self.bindir, self.exe) |
| 104 | cmd = [envpy, '-c', None] |
| 105 | for prefix, expected in ( |
| 106 | ('prefix', self.env_dir), |
| 107 | ('prefix', self.env_dir), |
| 108 | ('base_prefix', sys.prefix), |
| 109 | ('base_exec_prefix', sys.exec_prefix)): |
| 110 | cmd[2] = 'import sys; print(sys.%s)' % prefix |
| 111 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
| 112 | stderr=subprocess.PIPE) |
| 113 | out, err = p.communicate() |
Antoine Pitrou | 9c92a69 | 2012-08-05 00:33:10 +0200 | [diff] [blame] | 114 | self.assertEqual(out.strip(), expected.encode()) |
Vinay Sajip | 3874e54 | 2012-07-03 16:56:40 +0100 | [diff] [blame] | 115 | |
Vinay Sajip | bd40d3e | 2012-10-11 17:22:45 +0100 | [diff] [blame^] | 116 | if sys.platform == 'win32': |
| 117 | ENV_SUBDIRS = ( |
| 118 | ('Scripts',), |
| 119 | ('Include',), |
| 120 | ('Lib',), |
| 121 | ('Lib', 'site-packages'), |
| 122 | ) |
| 123 | else: |
| 124 | ENV_SUBDIRS = ( |
| 125 | ('bin',), |
| 126 | ('include',), |
| 127 | ('lib',), |
| 128 | ('lib', 'python%d.%d' % sys.version_info[:2]), |
| 129 | ('lib', 'python%d.%d' % sys.version_info[:2], 'site-packages'), |
| 130 | ) |
| 131 | |
| 132 | def create_contents(self, paths, filename): |
| 133 | """ |
| 134 | Create some files in the environment which are unrelated |
| 135 | to the virtual environment. |
| 136 | """ |
| 137 | for subdirs in paths: |
| 138 | d = os.path.join(self.env_dir, *subdirs) |
| 139 | os.mkdir(d) |
| 140 | fn = os.path.join(d, filename) |
| 141 | with open(fn, 'wb') as f: |
| 142 | f.write(b'Still here?') |
| 143 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 144 | def test_overwrite_existing(self): |
| 145 | """ |
Vinay Sajip | bd40d3e | 2012-10-11 17:22:45 +0100 | [diff] [blame^] | 146 | Test creating environment in an existing directory. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 147 | """ |
Vinay Sajip | bd40d3e | 2012-10-11 17:22:45 +0100 | [diff] [blame^] | 148 | self.create_contents(self.ENV_SUBDIRS, 'foo') |
| 149 | venv.create(self.env_dir) |
| 150 | for subdirs in self.ENV_SUBDIRS: |
| 151 | fn = os.path.join(self.env_dir, *(subdirs + ('foo',))) |
| 152 | self.assertTrue(os.path.exists(fn)) |
| 153 | with open(fn, 'rb') as f: |
| 154 | self.assertEqual(f.read(), b'Still here?') |
| 155 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 156 | builder = venv.EnvBuilder(clear=True) |
| 157 | builder.create(self.env_dir) |
Vinay Sajip | bd40d3e | 2012-10-11 17:22:45 +0100 | [diff] [blame^] | 158 | for subdirs in self.ENV_SUBDIRS: |
| 159 | fn = os.path.join(self.env_dir, *(subdirs + ('foo',))) |
| 160 | self.assertFalse(os.path.exists(fn)) |
| 161 | |
| 162 | def clear_directory(self, path): |
| 163 | for fn in os.listdir(path): |
| 164 | fn = os.path.join(path, fn) |
| 165 | if os.path.islink(fn) or os.path.isfile(fn): |
| 166 | os.remove(fn) |
| 167 | elif os.path.isdir(fn): |
| 168 | shutil.rmtree(fn) |
| 169 | |
| 170 | def test_unoverwritable_fails(self): |
| 171 | #create a file clashing with directories in the env dir |
| 172 | for paths in self.ENV_SUBDIRS[:3]: |
| 173 | fn = os.path.join(self.env_dir, *paths) |
| 174 | with open(fn, 'wb') as f: |
| 175 | f.write(b'') |
| 176 | self.assertRaises((ValueError, OSError), venv.create, self.env_dir) |
| 177 | self.clear_directory(self.env_dir) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 178 | |
Vinay Sajip | b3b49cd | 2012-05-27 18:39:22 +0100 | [diff] [blame] | 179 | def test_upgrade(self): |
| 180 | """ |
| 181 | Test upgrading an existing environment directory. |
| 182 | """ |
| 183 | builder = venv.EnvBuilder(upgrade=True) |
| 184 | self.run_with_capture(builder.create, self.env_dir) |
| 185 | self.isdir(self.bindir) |
| 186 | self.isdir(self.include) |
| 187 | self.isdir(*self.lib) |
| 188 | fn = self.get_env_file(self.bindir, self.exe) |
| 189 | if not os.path.exists(fn): # diagnostics for Windows buildbot failures |
| 190 | bd = self.get_env_file(self.bindir) |
| 191 | print('Contents of %r:' % bd) |
| 192 | print(' %r' % os.listdir(bd)) |
| 193 | self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn) |
| 194 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 195 | def test_isolation(self): |
| 196 | """ |
| 197 | Test isolation from system site-packages |
| 198 | """ |
| 199 | for ssp, s in ((True, 'true'), (False, 'false')): |
| 200 | builder = venv.EnvBuilder(clear=True, system_site_packages=ssp) |
| 201 | builder.create(self.env_dir) |
| 202 | data = self.get_text_file_contents('pyvenv.cfg') |
| 203 | self.assertIn('include-system-site-packages = %s\n' % s, data) |
| 204 | |
| 205 | @unittest.skipUnless(can_symlink(), 'Needs symlinks') |
| 206 | def test_symlinking(self): |
| 207 | """ |
| 208 | Test symlinking works as expected |
| 209 | """ |
| 210 | for usl in (False, True): |
| 211 | builder = venv.EnvBuilder(clear=True, symlinks=usl) |
Vinay Sajip | 90db661 | 2012-07-17 17:33:46 +0100 | [diff] [blame] | 212 | builder.create(self.env_dir) |
| 213 | fn = self.get_env_file(self.bindir, self.exe) |
| 214 | # Don't test when False, because e.g. 'python' is always |
| 215 | # symlinked to 'python3.3' in the env, even when symlinking in |
| 216 | # general isn't wanted. |
| 217 | if usl: |
| 218 | self.assertTrue(os.path.islink(fn)) |
| 219 | |
| 220 | # If a venv is created from a source build and that venv is used to |
| 221 | # run the test, the pyvenv.cfg in the venv created in the test will |
| 222 | # point to the venv being used to run the test, and we lose the link |
| 223 | # to the source build - so Python can't initialise properly. |
| 224 | @unittest.skipIf(sys.prefix != sys.base_prefix, 'Test not appropriate ' |
| 225 | 'in a venv') |
| 226 | def test_executable(self): |
| 227 | """ |
| 228 | Test that the sys.executable value is as expected. |
| 229 | """ |
| 230 | shutil.rmtree(self.env_dir) |
| 231 | self.run_with_capture(venv.create, self.env_dir) |
| 232 | envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe) |
| 233 | cmd = [envpy, '-c', 'import sys; print(sys.executable)'] |
| 234 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
| 235 | stderr=subprocess.PIPE) |
| 236 | out, err = p.communicate() |
Antoine Pitrou | 9c92a69 | 2012-08-05 00:33:10 +0200 | [diff] [blame] | 237 | self.assertEqual(out.strip(), envpy.encode()) |
Vinay Sajip | 90db661 | 2012-07-17 17:33:46 +0100 | [diff] [blame] | 238 | |
| 239 | @unittest.skipUnless(can_symlink(), 'Needs symlinks') |
| 240 | def test_executable_symlinks(self): |
| 241 | """ |
| 242 | Test that the sys.executable value is as expected. |
| 243 | """ |
| 244 | shutil.rmtree(self.env_dir) |
| 245 | builder = venv.EnvBuilder(clear=True, symlinks=True) |
| 246 | builder.create(self.env_dir) |
| 247 | envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe) |
| 248 | cmd = [envpy, '-c', 'import sys; print(sys.executable)'] |
| 249 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
| 250 | stderr=subprocess.PIPE) |
| 251 | out, err = p.communicate() |
Antoine Pitrou | 9c92a69 | 2012-08-05 00:33:10 +0200 | [diff] [blame] | 252 | self.assertEqual(out.strip(), envpy.encode()) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 253 | |
| 254 | def test_main(): |
| 255 | run_unittest(BasicTest) |
| 256 | |
| 257 | if __name__ == "__main__": |
| 258 | test_main() |