blob: 3750c361a295190279f5c76c20c85ecff856c6f2 [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):
23 self.env_dir = tempfile.mkdtemp()
24 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 Sajip3874e542012-07-03 16:56:40 +010090 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 Sajip7ded1f02012-05-26 03:45:29 +0100114 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 Sajipb3b49cd2012-05-27 18:39:22 +0100122 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 Sajip7ded1f02012-05-26 03:45:29 +0100138 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 Sajip28952442012-06-25 00:47:46 +0100156 '__PYVENV_LAUNCHER__' in os.environ):
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100157 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
167def test_main():
168 run_unittest(BasicTest)
169
170if __name__ == "__main__":
171 test_main()