blob: 50a8e93bf58b6433a57218182343bceb12be6041 [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 Sajip7ded1f02012-05-26 03:45:29 +01005"""
6
7import os
8import os.path
9import shutil
10import sys
11import tempfile
12from test.support import (captured_stdout, captured_stderr, run_unittest,
13 can_symlink)
14import unittest
15import venv
16
17class BaseTest(unittest.TestCase):
18 """Base class for venv tests."""
19
20 def setUp(self):
21 self.env_dir = tempfile.mkdtemp()
22 if os.name == 'nt':
23 self.bindir = 'Scripts'
24 self.ps3name = 'pysetup3-script.py'
25 self.lib = ('Lib',)
26 self.include = 'Include'
Vinay Sajip7ded1f02012-05-26 03:45:29 +010027 else:
28 self.bindir = 'bin'
29 self.ps3name = 'pysetup3'
30 self.lib = ('lib', 'python%s' % sys.version[:3])
31 self.include = 'include'
Vinay Sajip382a7c02012-05-28 16:34:47 +010032 if sys.platform == 'darwin' and '__PYTHONV_LAUNCHER__' in env:
33 executable = os.environ['__PYTHONV_LAUNCHER__']
34 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')
72 if sys.platform == 'darwin' and ('__PYTHONV_LAUNCHER__'
73 in os.environ):
74 executable = os.environ['__PYTHONV_LAUNCHER__']
75 else:
76 executable = sys.executable
77 path = os.path.dirname(executable)
78 self.assertIn('home = %s' % path, data)
79 data = self.get_text_file_contents(self.bindir, self.ps3name)
80 self.assertTrue(data.startswith('#!%s%s' % (self.env_dir, os.sep)))
81 fn = self.get_env_file(self.bindir, self.exe)
Vinay Sajip7e203492012-05-27 17:30:09 +010082 if not os.path.exists(fn): # diagnostics for Windows buildbot failures
Vinay Sajipb3b49cd2012-05-27 18:39:22 +010083 bd = self.get_env_file(self.bindir)
84 print('Contents of %r:' % bd)
85 print(' %r' % os.listdir(bd))
Vinay Sajip7e203492012-05-27 17:30:09 +010086 self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn)
Vinay Sajip7ded1f02012-05-26 03:45:29 +010087
88 def test_overwrite_existing(self):
89 """
90 Test control of overwriting an existing environment directory.
91 """
92 self.assertRaises(ValueError, venv.create, self.env_dir)
93 builder = venv.EnvBuilder(clear=True)
94 builder.create(self.env_dir)
95
Vinay Sajipb3b49cd2012-05-27 18:39:22 +010096 def test_upgrade(self):
97 """
98 Test upgrading an existing environment directory.
99 """
100 builder = venv.EnvBuilder(upgrade=True)
101 self.run_with_capture(builder.create, self.env_dir)
102 self.isdir(self.bindir)
103 self.isdir(self.include)
104 self.isdir(*self.lib)
105 fn = self.get_env_file(self.bindir, self.exe)
106 if not os.path.exists(fn): # diagnostics for Windows buildbot failures
107 bd = self.get_env_file(self.bindir)
108 print('Contents of %r:' % bd)
109 print(' %r' % os.listdir(bd))
110 self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn)
111
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100112 def test_isolation(self):
113 """
114 Test isolation from system site-packages
115 """
116 for ssp, s in ((True, 'true'), (False, 'false')):
117 builder = venv.EnvBuilder(clear=True, system_site_packages=ssp)
118 builder.create(self.env_dir)
119 data = self.get_text_file_contents('pyvenv.cfg')
120 self.assertIn('include-system-site-packages = %s\n' % s, data)
121
122 @unittest.skipUnless(can_symlink(), 'Needs symlinks')
123 def test_symlinking(self):
124 """
125 Test symlinking works as expected
126 """
127 for usl in (False, True):
128 builder = venv.EnvBuilder(clear=True, symlinks=usl)
129 if (usl and sys.platform == 'darwin' and
130 '__PYTHONV_LAUNCHER__' in os.environ):
131 self.assertRaises(ValueError, builder.create, self.env_dir)
132 else:
133 builder.create(self.env_dir)
134 fn = self.get_env_file(self.bindir, self.exe)
135 # Don't test when False, because e.g. 'python' is always
136 # symlinked to 'python3.3' in the env, even when symlinking in
137 # general isn't wanted.
138 if usl:
139 self.assertTrue(os.path.islink(fn))
140
141def test_main():
142 run_unittest(BasicTest)
143
144if __name__ == "__main__":
145 test_main()