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' |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 26 | self.lib = ('Lib',) |
| 27 | self.include = 'Include' |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 28 | else: |
| 29 | self.bindir = 'bin' |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 30 | self.lib = ('lib', 'python%s' % sys.version[:3]) |
| 31 | self.include = 'include' |
Vinay Sajip | 2895244 | 2012-06-25 00:47:46 +0100 | [diff] [blame] | 32 | if sys.platform == 'darwin' and '__PYVENV_LAUNCHER__' in os.environ: |
| 33 | executable = os.environ['__PYVENV_LAUNCHER__'] |
Vinay Sajip | 382a7c0 | 2012-05-28 16:34:47 +0100 | [diff] [blame] | 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') |
Vinay Sajip | 2895244 | 2012-06-25 00:47:46 +0100 | [diff] [blame] | 72 | if sys.platform == 'darwin' and ('__PYVENV_LAUNCHER__' |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 73 | in os.environ): |
Vinay Sajip | 2895244 | 2012-06-25 00:47:46 +0100 | [diff] [blame] | 74 | executable = os.environ['__PYVENV_LAUNCHER__'] |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 75 | else: |
| 76 | executable = sys.executable |
| 77 | path = os.path.dirname(executable) |
| 78 | self.assertIn('home = %s' % path, data) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 79 | fn = self.get_env_file(self.bindir, self.exe) |
Vinay Sajip | 7e20349 | 2012-05-27 17:30:09 +0100 | [diff] [blame] | 80 | if not os.path.exists(fn): # diagnostics for Windows buildbot failures |
Vinay Sajip | b3b49cd | 2012-05-27 18:39:22 +0100 | [diff] [blame] | 81 | bd = self.get_env_file(self.bindir) |
| 82 | print('Contents of %r:' % bd) |
| 83 | print(' %r' % os.listdir(bd)) |
Vinay Sajip | 7e20349 | 2012-05-27 17:30:09 +0100 | [diff] [blame] | 84 | self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 85 | |
Vinay Sajip | 509d87d | 2012-07-15 16:12:54 +0100 | [diff] [blame] | 86 | @unittest.skipIf(sys.prefix != sys.base_prefix, 'Test not appropriate ' |
| 87 | 'in a venv') |
Vinay Sajip | 3874e54 | 2012-07-03 16:56:40 +0100 | [diff] [blame] | 88 | def test_prefixes(self): |
| 89 | """ |
| 90 | Test that the prefix values are as expected. |
| 91 | """ |
| 92 | #check our prefixes |
| 93 | self.assertEqual(sys.base_prefix, sys.prefix) |
| 94 | self.assertEqual(sys.base_exec_prefix, sys.exec_prefix) |
| 95 | |
| 96 | # check a venv's prefixes |
| 97 | shutil.rmtree(self.env_dir) |
| 98 | self.run_with_capture(venv.create, self.env_dir) |
| 99 | envpy = os.path.join(self.env_dir, self.bindir, self.exe) |
| 100 | cmd = [envpy, '-c', None] |
| 101 | for prefix, expected in ( |
| 102 | ('prefix', self.env_dir), |
| 103 | ('prefix', self.env_dir), |
| 104 | ('base_prefix', sys.prefix), |
| 105 | ('base_exec_prefix', sys.exec_prefix)): |
| 106 | cmd[2] = 'import sys; print(sys.%s)' % prefix |
| 107 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
| 108 | stderr=subprocess.PIPE) |
| 109 | out, err = p.communicate() |
Antoine Pitrou | 9c92a69 | 2012-08-05 00:33:10 +0200 | [diff] [blame] | 110 | self.assertEqual(out.strip(), expected.encode()) |
Vinay Sajip | 3874e54 | 2012-07-03 16:56:40 +0100 | [diff] [blame] | 111 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 112 | def test_overwrite_existing(self): |
| 113 | """ |
| 114 | Test control of overwriting an existing environment directory. |
| 115 | """ |
| 116 | self.assertRaises(ValueError, venv.create, self.env_dir) |
| 117 | builder = venv.EnvBuilder(clear=True) |
| 118 | builder.create(self.env_dir) |
| 119 | |
Vinay Sajip | b3b49cd | 2012-05-27 18:39:22 +0100 | [diff] [blame] | 120 | def test_upgrade(self): |
| 121 | """ |
| 122 | Test upgrading an existing environment directory. |
| 123 | """ |
| 124 | builder = venv.EnvBuilder(upgrade=True) |
| 125 | self.run_with_capture(builder.create, self.env_dir) |
| 126 | self.isdir(self.bindir) |
| 127 | self.isdir(self.include) |
| 128 | self.isdir(*self.lib) |
| 129 | fn = self.get_env_file(self.bindir, self.exe) |
| 130 | if not os.path.exists(fn): # diagnostics for Windows buildbot failures |
| 131 | bd = self.get_env_file(self.bindir) |
| 132 | print('Contents of %r:' % bd) |
| 133 | print(' %r' % os.listdir(bd)) |
| 134 | self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn) |
| 135 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 136 | def test_isolation(self): |
| 137 | """ |
| 138 | Test isolation from system site-packages |
| 139 | """ |
| 140 | for ssp, s in ((True, 'true'), (False, 'false')): |
| 141 | builder = venv.EnvBuilder(clear=True, system_site_packages=ssp) |
| 142 | builder.create(self.env_dir) |
| 143 | data = self.get_text_file_contents('pyvenv.cfg') |
| 144 | self.assertIn('include-system-site-packages = %s\n' % s, data) |
| 145 | |
| 146 | @unittest.skipUnless(can_symlink(), 'Needs symlinks') |
| 147 | def test_symlinking(self): |
| 148 | """ |
| 149 | Test symlinking works as expected |
| 150 | """ |
| 151 | for usl in (False, True): |
| 152 | builder = venv.EnvBuilder(clear=True, symlinks=usl) |
Vinay Sajip | 90db661 | 2012-07-17 17:33:46 +0100 | [diff] [blame] | 153 | builder.create(self.env_dir) |
| 154 | fn = self.get_env_file(self.bindir, self.exe) |
| 155 | # Don't test when False, because e.g. 'python' is always |
| 156 | # symlinked to 'python3.3' in the env, even when symlinking in |
| 157 | # general isn't wanted. |
| 158 | if usl: |
| 159 | self.assertTrue(os.path.islink(fn)) |
| 160 | |
| 161 | # If a venv is created from a source build and that venv is used to |
| 162 | # run the test, the pyvenv.cfg in the venv created in the test will |
| 163 | # point to the venv being used to run the test, and we lose the link |
| 164 | # to the source build - so Python can't initialise properly. |
| 165 | @unittest.skipIf(sys.prefix != sys.base_prefix, 'Test not appropriate ' |
| 166 | 'in a venv') |
| 167 | def test_executable(self): |
| 168 | """ |
| 169 | Test that the sys.executable value is as expected. |
| 170 | """ |
| 171 | shutil.rmtree(self.env_dir) |
| 172 | self.run_with_capture(venv.create, self.env_dir) |
| 173 | envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe) |
| 174 | cmd = [envpy, '-c', 'import sys; print(sys.executable)'] |
| 175 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
| 176 | stderr=subprocess.PIPE) |
| 177 | out, err = p.communicate() |
Antoine Pitrou | 9c92a69 | 2012-08-05 00:33:10 +0200 | [diff] [blame] | 178 | self.assertEqual(out.strip(), envpy.encode()) |
Vinay Sajip | 90db661 | 2012-07-17 17:33:46 +0100 | [diff] [blame] | 179 | |
| 180 | @unittest.skipUnless(can_symlink(), 'Needs symlinks') |
| 181 | def test_executable_symlinks(self): |
| 182 | """ |
| 183 | Test that the sys.executable value is as expected. |
| 184 | """ |
| 185 | shutil.rmtree(self.env_dir) |
| 186 | builder = venv.EnvBuilder(clear=True, symlinks=True) |
| 187 | builder.create(self.env_dir) |
| 188 | envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe) |
| 189 | cmd = [envpy, '-c', 'import sys; print(sys.executable)'] |
| 190 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
| 191 | stderr=subprocess.PIPE) |
| 192 | out, err = p.communicate() |
Antoine Pitrou | 9c92a69 | 2012-08-05 00:33:10 +0200 | [diff] [blame] | 193 | self.assertEqual(out.strip(), envpy.encode()) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 194 | |
| 195 | def test_main(): |
| 196 | run_unittest(BasicTest) |
| 197 | |
| 198 | if __name__ == "__main__": |
| 199 | test_main() |