blob: 0fae88bcf9b7c38bf9a870fc4a8689b9871e53a7 [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'
Vinay Sajip7ded1f02012-05-26 03:45:29 +010026 self.lib = ('Lib',)
27 self.include = 'Include'
Vinay Sajip7ded1f02012-05-26 03:45:29 +010028 else:
29 self.bindir = 'bin'
Vinay Sajip7ded1f02012-05-26 03:45:29 +010030 self.lib = ('lib', 'python%s' % sys.version[:3])
31 self.include = 'include'
Vinay Sajip28952442012-06-25 00:47:46 +010032 if sys.platform == 'darwin' and '__PYVENV_LAUNCHER__' in os.environ:
33 executable = os.environ['__PYVENV_LAUNCHER__']
Vinay Sajip382a7c02012-05-28 16:34:47 +010034 else:
35 executable = sys.executable
36 self.exe = os.path.split(executable)[-1]
Vinay Sajip7ded1f02012-05-26 03:45:29 +010037
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
55class BasicTest(BaseTest):
56 """Test venv module functionality."""
57
Vinay Sajipb3b49cd2012-05-27 18:39:22 +010058 def isdir(self, *args):
59 fn = self.get_env_file(*args)
60 self.assertTrue(os.path.isdir(fn))
61
Vinay Sajip7ded1f02012-05-26 03:45:29 +010062 def test_defaults(self):
63 """
64 Test the create function with default arguments.
65 """
Vinay Sajip7ded1f02012-05-26 03:45:29 +010066 shutil.rmtree(self.env_dir)
67 self.run_with_capture(venv.create, self.env_dir)
Vinay Sajipb3b49cd2012-05-27 18:39:22 +010068 self.isdir(self.bindir)
69 self.isdir(self.include)
70 self.isdir(*self.lib)
Vinay Sajip7ded1f02012-05-26 03:45:29 +010071 data = self.get_text_file_contents('pyvenv.cfg')
Vinay Sajip28952442012-06-25 00:47:46 +010072 if sys.platform == 'darwin' and ('__PYVENV_LAUNCHER__'
Vinay Sajip7ded1f02012-05-26 03:45:29 +010073 in os.environ):
Vinay Sajip28952442012-06-25 00:47:46 +010074 executable = os.environ['__PYVENV_LAUNCHER__']
Vinay Sajip7ded1f02012-05-26 03:45:29 +010075 else:
76 executable = sys.executable
77 path = os.path.dirname(executable)
78 self.assertIn('home = %s' % path, data)
Vinay Sajip7ded1f02012-05-26 03:45:29 +010079 fn = self.get_env_file(self.bindir, self.exe)
Vinay Sajip7e203492012-05-27 17:30:09 +010080 if not os.path.exists(fn): # diagnostics for Windows buildbot failures
Vinay Sajipb3b49cd2012-05-27 18:39:22 +010081 bd = self.get_env_file(self.bindir)
82 print('Contents of %r:' % bd)
83 print(' %r' % os.listdir(bd))
Vinay Sajip7e203492012-05-27 17:30:09 +010084 self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn)
Vinay Sajip7ded1f02012-05-26 03:45:29 +010085
Vinay Sajip509d87d2012-07-15 16:12:54 +010086 @unittest.skipIf(sys.prefix != sys.base_prefix, 'Test not appropriate '
87 'in a venv')
Vinay Sajip3874e542012-07-03 16:56:40 +010088 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 Pitrou9c92a692012-08-05 00:33:10 +0200110 self.assertEqual(out.strip(), expected.encode())
Vinay Sajip3874e542012-07-03 16:56:40 +0100111
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100112 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 Sajipb3b49cd2012-05-27 18:39:22 +0100120 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 Sajip7ded1f02012-05-26 03:45:29 +0100136 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 Sajip90db6612012-07-17 17:33:46 +0100153 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 Pitrou9c92a692012-08-05 00:33:10 +0200178 self.assertEqual(out.strip(), envpy.encode())
Vinay Sajip90db6612012-07-17 17:33:46 +0100179
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 Pitrou9c92a692012-08-05 00:33:10 +0200193 self.assertEqual(out.strip(), envpy.encode())
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100194
195def test_main():
196 run_unittest(BasicTest)
197
198if __name__ == "__main__":
199 test_main()