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