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 | 3874e54 | 2012-07-03 16:56:40 +0100 | [diff] [blame] | 90 | def test_prefixes(self): |
| 91 | """ |
| 92 | Test that the prefix values are as expected. |
| 93 | """ |
| 94 | #check our prefixes |
| 95 | self.assertEqual(sys.base_prefix, sys.prefix) |
| 96 | self.assertEqual(sys.base_exec_prefix, sys.exec_prefix) |
| 97 | |
| 98 | # check a venv's prefixes |
| 99 | shutil.rmtree(self.env_dir) |
| 100 | self.run_with_capture(venv.create, self.env_dir) |
| 101 | envpy = os.path.join(self.env_dir, self.bindir, self.exe) |
| 102 | cmd = [envpy, '-c', None] |
| 103 | for prefix, expected in ( |
| 104 | ('prefix', self.env_dir), |
| 105 | ('prefix', self.env_dir), |
| 106 | ('base_prefix', sys.prefix), |
| 107 | ('base_exec_prefix', sys.exec_prefix)): |
| 108 | cmd[2] = 'import sys; print(sys.%s)' % prefix |
| 109 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
| 110 | stderr=subprocess.PIPE) |
| 111 | out, err = p.communicate() |
| 112 | self.assertEqual(out[:-1], expected.encode()) |
| 113 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 114 | def test_overwrite_existing(self): |
| 115 | """ |
| 116 | Test control of overwriting an existing environment directory. |
| 117 | """ |
| 118 | self.assertRaises(ValueError, venv.create, self.env_dir) |
| 119 | builder = venv.EnvBuilder(clear=True) |
| 120 | builder.create(self.env_dir) |
| 121 | |
Vinay Sajip | b3b49cd | 2012-05-27 18:39:22 +0100 | [diff] [blame] | 122 | def test_upgrade(self): |
| 123 | """ |
| 124 | Test upgrading an existing environment directory. |
| 125 | """ |
| 126 | builder = venv.EnvBuilder(upgrade=True) |
| 127 | self.run_with_capture(builder.create, self.env_dir) |
| 128 | self.isdir(self.bindir) |
| 129 | self.isdir(self.include) |
| 130 | self.isdir(*self.lib) |
| 131 | fn = self.get_env_file(self.bindir, self.exe) |
| 132 | if not os.path.exists(fn): # diagnostics for Windows buildbot failures |
| 133 | bd = self.get_env_file(self.bindir) |
| 134 | print('Contents of %r:' % bd) |
| 135 | print(' %r' % os.listdir(bd)) |
| 136 | self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn) |
| 137 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 138 | def test_isolation(self): |
| 139 | """ |
| 140 | Test isolation from system site-packages |
| 141 | """ |
| 142 | for ssp, s in ((True, 'true'), (False, 'false')): |
| 143 | builder = venv.EnvBuilder(clear=True, system_site_packages=ssp) |
| 144 | builder.create(self.env_dir) |
| 145 | data = self.get_text_file_contents('pyvenv.cfg') |
| 146 | self.assertIn('include-system-site-packages = %s\n' % s, data) |
| 147 | |
| 148 | @unittest.skipUnless(can_symlink(), 'Needs symlinks') |
| 149 | def test_symlinking(self): |
| 150 | """ |
| 151 | Test symlinking works as expected |
| 152 | """ |
| 153 | for usl in (False, True): |
| 154 | builder = venv.EnvBuilder(clear=True, symlinks=usl) |
| 155 | if (usl and sys.platform == 'darwin' and |
Vinay Sajip | 2895244 | 2012-06-25 00:47:46 +0100 | [diff] [blame] | 156 | '__PYVENV_LAUNCHER__' in os.environ): |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 157 | self.assertRaises(ValueError, builder.create, self.env_dir) |
| 158 | else: |
| 159 | builder.create(self.env_dir) |
| 160 | fn = self.get_env_file(self.bindir, self.exe) |
| 161 | # Don't test when False, because e.g. 'python' is always |
| 162 | # symlinked to 'python3.3' in the env, even when symlinking in |
| 163 | # general isn't wanted. |
| 164 | if usl: |
| 165 | self.assertTrue(os.path.islink(fn)) |
| 166 | |
| 167 | def test_main(): |
| 168 | run_unittest(BasicTest) |
| 169 | |
| 170 | if __name__ == "__main__": |
| 171 | test_main() |