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