blob: 96968444c60d82db9b34c7f1da7ff61007073f16 [file] [log] [blame]
Vinay Sajip42211422012-05-26 20:36:12 +01001"""
2Test harness for the venv module.
Vinay Sajip7ded1f02012-05-26 03:45:29 +01003
Vinay Sajip42211422012-05-26 20:36:12 +01004Copyright (C) 2011-2012 Vinay Sajip.
Vinay Sajip28952442012-06-25 00:47:46 +01005Licensed to the PSF under a contributor agreement.
Vinay Sajip7ded1f02012-05-26 03:45:29 +01006"""
7
8import os
9import os.path
10import shutil
Vinay Sajip3874e542012-07-03 16:56:40 +010011import subprocess
Vinay Sajip7ded1f02012-05-26 03:45:29 +010012import sys
13import tempfile
14from test.support import (captured_stdout, captured_stderr, run_unittest,
15 can_symlink)
16import unittest
17import venv
18
19class BaseTest(unittest.TestCase):
20 """Base class for venv tests."""
21
22 def setUp(self):
Ned Deily045bd532012-07-13 15:48:04 -070023 self.env_dir = os.path.realpath(tempfile.mkdtemp())
Vinay Sajip7ded1f02012-05-26 03:45:29 +010024 if os.name == 'nt':
25 self.bindir = 'Scripts'
Éric Araujoaa789ac2012-06-24 13:51:22 -040026 self.pydocname = 'pydoc.py'
Vinay Sajip7ded1f02012-05-26 03:45:29 +010027 self.lib = ('Lib',)
28 self.include = 'Include'
Vinay Sajip7ded1f02012-05-26 03:45:29 +010029 else:
30 self.bindir = 'bin'
Éric Araujoaa789ac2012-06-24 13:51:22 -040031 self.pydocname = 'pydoc'
Vinay Sajip7ded1f02012-05-26 03:45:29 +010032 self.lib = ('lib', 'python%s' % sys.version[:3])
33 self.include = 'include'
Vinay Sajip28952442012-06-25 00:47:46 +010034 if sys.platform == 'darwin' and '__PYVENV_LAUNCHER__' in os.environ:
35 executable = os.environ['__PYVENV_LAUNCHER__']
Vinay Sajip382a7c02012-05-28 16:34:47 +010036 else:
37 executable = sys.executable
38 self.exe = os.path.split(executable)[-1]
Vinay Sajip7ded1f02012-05-26 03:45:29 +010039
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
57class BasicTest(BaseTest):
58 """Test venv module functionality."""
59
Vinay Sajipb3b49cd2012-05-27 18:39:22 +010060 def isdir(self, *args):
61 fn = self.get_env_file(*args)
62 self.assertTrue(os.path.isdir(fn))
63
Vinay Sajip7ded1f02012-05-26 03:45:29 +010064 def test_defaults(self):
65 """
66 Test the create function with default arguments.
67 """
Vinay Sajip7ded1f02012-05-26 03:45:29 +010068 shutil.rmtree(self.env_dir)
69 self.run_with_capture(venv.create, self.env_dir)
Vinay Sajipb3b49cd2012-05-27 18:39:22 +010070 self.isdir(self.bindir)
71 self.isdir(self.include)
72 self.isdir(*self.lib)
Vinay Sajip7ded1f02012-05-26 03:45:29 +010073 data = self.get_text_file_contents('pyvenv.cfg')
Vinay Sajip28952442012-06-25 00:47:46 +010074 if sys.platform == 'darwin' and ('__PYVENV_LAUNCHER__'
Vinay Sajip7ded1f02012-05-26 03:45:29 +010075 in os.environ):
Vinay Sajip28952442012-06-25 00:47:46 +010076 executable = os.environ['__PYVENV_LAUNCHER__']
Vinay Sajip7ded1f02012-05-26 03:45:29 +010077 else:
78 executable = sys.executable
79 path = os.path.dirname(executable)
80 self.assertIn('home = %s' % path, data)
Éric Araujoaa789ac2012-06-24 13:51:22 -040081 data = self.get_text_file_contents(self.bindir, self.pydocname)
82 self.assertTrue(data.startswith('#!%s%s' % (self.env_dir, os.sep)))
Vinay Sajip7ded1f02012-05-26 03:45:29 +010083 fn = self.get_env_file(self.bindir, self.exe)
Vinay Sajip7e203492012-05-27 17:30:09 +010084 if not os.path.exists(fn): # diagnostics for Windows buildbot failures
Vinay Sajipb3b49cd2012-05-27 18:39:22 +010085 bd = self.get_env_file(self.bindir)
86 print('Contents of %r:' % bd)
87 print(' %r' % os.listdir(bd))
Vinay Sajip7e203492012-05-27 17:30:09 +010088 self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn)
Vinay Sajip7ded1f02012-05-26 03:45:29 +010089
Vinay Sajip509d87d2012-07-15 16:12:54 +010090 @unittest.skipIf(sys.prefix != sys.base_prefix, 'Test not appropriate '
91 'in a venv')
Vinay Sajip3874e542012-07-03 16:56:40 +010092 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 Pitrou9c92a692012-08-05 00:33:10 +0200114 self.assertEqual(out.strip(), expected.encode())
Vinay Sajip3874e542012-07-03 16:56:40 +0100115
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100116 def test_overwrite_existing(self):
117 """
118 Test control of overwriting an existing environment directory.
119 """
120 self.assertRaises(ValueError, venv.create, self.env_dir)
121 builder = venv.EnvBuilder(clear=True)
122 builder.create(self.env_dir)
123
Vinay Sajipb3b49cd2012-05-27 18:39:22 +0100124 def test_upgrade(self):
125 """
126 Test upgrading an existing environment directory.
127 """
128 builder = venv.EnvBuilder(upgrade=True)
129 self.run_with_capture(builder.create, self.env_dir)
130 self.isdir(self.bindir)
131 self.isdir(self.include)
132 self.isdir(*self.lib)
133 fn = self.get_env_file(self.bindir, self.exe)
134 if not os.path.exists(fn): # diagnostics for Windows buildbot failures
135 bd = self.get_env_file(self.bindir)
136 print('Contents of %r:' % bd)
137 print(' %r' % os.listdir(bd))
138 self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn)
139
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100140 def test_isolation(self):
141 """
142 Test isolation from system site-packages
143 """
144 for ssp, s in ((True, 'true'), (False, 'false')):
145 builder = venv.EnvBuilder(clear=True, system_site_packages=ssp)
146 builder.create(self.env_dir)
147 data = self.get_text_file_contents('pyvenv.cfg')
148 self.assertIn('include-system-site-packages = %s\n' % s, data)
149
150 @unittest.skipUnless(can_symlink(), 'Needs symlinks')
151 def test_symlinking(self):
152 """
153 Test symlinking works as expected
154 """
155 for usl in (False, True):
156 builder = venv.EnvBuilder(clear=True, symlinks=usl)
Vinay Sajip90db6612012-07-17 17:33:46 +0100157 builder.create(self.env_dir)
158 fn = self.get_env_file(self.bindir, self.exe)
159 # Don't test when False, because e.g. 'python' is always
160 # symlinked to 'python3.3' in the env, even when symlinking in
161 # general isn't wanted.
162 if usl:
163 self.assertTrue(os.path.islink(fn))
164
165 # If a venv is created from a source build and that venv is used to
166 # run the test, the pyvenv.cfg in the venv created in the test will
167 # point to the venv being used to run the test, and we lose the link
168 # to the source build - so Python can't initialise properly.
169 @unittest.skipIf(sys.prefix != sys.base_prefix, 'Test not appropriate '
170 'in a venv')
171 def test_executable(self):
172 """
173 Test that the sys.executable value is as expected.
174 """
175 shutil.rmtree(self.env_dir)
176 self.run_with_capture(venv.create, self.env_dir)
177 envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe)
178 cmd = [envpy, '-c', 'import sys; print(sys.executable)']
179 p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
180 stderr=subprocess.PIPE)
181 out, err = p.communicate()
Antoine Pitrou9c92a692012-08-05 00:33:10 +0200182 self.assertEqual(out.strip(), envpy.encode())
Vinay Sajip90db6612012-07-17 17:33:46 +0100183
184 @unittest.skipUnless(can_symlink(), 'Needs symlinks')
185 def test_executable_symlinks(self):
186 """
187 Test that the sys.executable value is as expected.
188 """
189 shutil.rmtree(self.env_dir)
190 builder = venv.EnvBuilder(clear=True, symlinks=True)
191 builder.create(self.env_dir)
192 envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe)
193 cmd = [envpy, '-c', 'import sys; print(sys.executable)']
194 p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
195 stderr=subprocess.PIPE)
196 out, err = p.communicate()
Antoine Pitrou9c92a692012-08-05 00:33:10 +0200197 self.assertEqual(out.strip(), envpy.encode())
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100198
199def test_main():
200 run_unittest(BasicTest)
201
202if __name__ == "__main__":
203 test_main()