blob: 08fd1dc7f43bf28b78985dfc69709ec5255e4d23 [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 Sajip393da322012-05-27 19:05:36 +010032 self.exe = os.path.split(sys.executable)[-1]
Vinay Sajip7ded1f02012-05-26 03:45:29 +010033
34 def tearDown(self):
35 shutil.rmtree(self.env_dir)
36
37 def run_with_capture(self, func, *args, **kwargs):
38 with captured_stdout() as output:
39 with captured_stderr() as error:
40 func(*args, **kwargs)
41 return output.getvalue(), error.getvalue()
42
43 def get_env_file(self, *args):
44 return os.path.join(self.env_dir, *args)
45
46 def get_text_file_contents(self, *args):
47 with open(self.get_env_file(*args), 'r') as f:
48 result = f.read()
49 return result
50
51class BasicTest(BaseTest):
52 """Test venv module functionality."""
53
Vinay Sajipb3b49cd2012-05-27 18:39:22 +010054 def isdir(self, *args):
55 fn = self.get_env_file(*args)
56 self.assertTrue(os.path.isdir(fn))
57
Vinay Sajip7ded1f02012-05-26 03:45:29 +010058 def test_defaults(self):
59 """
60 Test the create function with default arguments.
61 """
Vinay Sajip7ded1f02012-05-26 03:45:29 +010062 shutil.rmtree(self.env_dir)
63 self.run_with_capture(venv.create, self.env_dir)
Vinay Sajipb3b49cd2012-05-27 18:39:22 +010064 self.isdir(self.bindir)
65 self.isdir(self.include)
66 self.isdir(*self.lib)
Vinay Sajip7ded1f02012-05-26 03:45:29 +010067 data = self.get_text_file_contents('pyvenv.cfg')
68 if sys.platform == 'darwin' and ('__PYTHONV_LAUNCHER__'
69 in os.environ):
70 executable = os.environ['__PYTHONV_LAUNCHER__']
71 else:
72 executable = sys.executable
73 path = os.path.dirname(executable)
74 self.assertIn('home = %s' % path, data)
75 data = self.get_text_file_contents(self.bindir, self.ps3name)
76 self.assertTrue(data.startswith('#!%s%s' % (self.env_dir, os.sep)))
77 fn = self.get_env_file(self.bindir, self.exe)
Vinay Sajip7e203492012-05-27 17:30:09 +010078 if not os.path.exists(fn): # diagnostics for Windows buildbot failures
Vinay Sajipb3b49cd2012-05-27 18:39:22 +010079 bd = self.get_env_file(self.bindir)
80 print('Contents of %r:' % bd)
81 print(' %r' % os.listdir(bd))
Vinay Sajip7e203492012-05-27 17:30:09 +010082 self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn)
Vinay Sajip7ded1f02012-05-26 03:45:29 +010083
84 def test_overwrite_existing(self):
85 """
86 Test control of overwriting an existing environment directory.
87 """
88 self.assertRaises(ValueError, venv.create, self.env_dir)
89 builder = venv.EnvBuilder(clear=True)
90 builder.create(self.env_dir)
91
Vinay Sajipb3b49cd2012-05-27 18:39:22 +010092 def test_upgrade(self):
93 """
94 Test upgrading an existing environment directory.
95 """
96 builder = venv.EnvBuilder(upgrade=True)
97 self.run_with_capture(builder.create, self.env_dir)
98 self.isdir(self.bindir)
99 self.isdir(self.include)
100 self.isdir(*self.lib)
101 fn = self.get_env_file(self.bindir, self.exe)
102 if not os.path.exists(fn): # diagnostics for Windows buildbot failures
103 bd = self.get_env_file(self.bindir)
104 print('Contents of %r:' % bd)
105 print(' %r' % os.listdir(bd))
106 self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn)
107
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100108 def test_isolation(self):
109 """
110 Test isolation from system site-packages
111 """
112 for ssp, s in ((True, 'true'), (False, 'false')):
113 builder = venv.EnvBuilder(clear=True, system_site_packages=ssp)
114 builder.create(self.env_dir)
115 data = self.get_text_file_contents('pyvenv.cfg')
116 self.assertIn('include-system-site-packages = %s\n' % s, data)
117
118 @unittest.skipUnless(can_symlink(), 'Needs symlinks')
119 def test_symlinking(self):
120 """
121 Test symlinking works as expected
122 """
123 for usl in (False, True):
124 builder = venv.EnvBuilder(clear=True, symlinks=usl)
125 if (usl and sys.platform == 'darwin' and
126 '__PYTHONV_LAUNCHER__' in os.environ):
127 self.assertRaises(ValueError, builder.create, self.env_dir)
128 else:
129 builder.create(self.env_dir)
130 fn = self.get_env_file(self.bindir, self.exe)
131 # Don't test when False, because e.g. 'python' is always
132 # symlinked to 'python3.3' in the env, even when symlinking in
133 # general isn't wanted.
134 if usl:
135 self.assertTrue(os.path.islink(fn))
136
137def test_main():
138 run_unittest(BasicTest)
139
140if __name__ == "__main__":
141 test_main()