blob: 705fd39471802ecf8a71b6b3f858ca8049d9e8f2 [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
11import sys
12import tempfile
13from test.support import (captured_stdout, captured_stderr, run_unittest,
14 can_symlink)
15import unittest
16import venv
17
18class BaseTest(unittest.TestCase):
19 """Base class for venv tests."""
20
21 def setUp(self):
22 self.env_dir = tempfile.mkdtemp()
23 if os.name == 'nt':
24 self.bindir = 'Scripts'
Éric Araujoaa789ac2012-06-24 13:51:22 -040025 self.pydocname = 'pydoc.py'
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'
Éric Araujoaa789ac2012-06-24 13:51:22 -040030 self.pydocname = 'pydoc'
Vinay Sajip7ded1f02012-05-26 03:45:29 +010031 self.lib = ('lib', 'python%s' % sys.version[:3])
32 self.include = 'include'
Vinay Sajip28952442012-06-25 00:47:46 +010033 if sys.platform == 'darwin' and '__PYVENV_LAUNCHER__' in os.environ:
34 executable = os.environ['__PYVENV_LAUNCHER__']
Vinay Sajip382a7c02012-05-28 16:34:47 +010035 else:
36 executable = sys.executable
37 self.exe = os.path.split(executable)[-1]
Vinay Sajip7ded1f02012-05-26 03:45:29 +010038
39 def tearDown(self):
40 shutil.rmtree(self.env_dir)
41
42 def run_with_capture(self, func, *args, **kwargs):
43 with captured_stdout() as output:
44 with captured_stderr() as error:
45 func(*args, **kwargs)
46 return output.getvalue(), error.getvalue()
47
48 def get_env_file(self, *args):
49 return os.path.join(self.env_dir, *args)
50
51 def get_text_file_contents(self, *args):
52 with open(self.get_env_file(*args), 'r') as f:
53 result = f.read()
54 return result
55
56class BasicTest(BaseTest):
57 """Test venv module functionality."""
58
Vinay Sajipb3b49cd2012-05-27 18:39:22 +010059 def isdir(self, *args):
60 fn = self.get_env_file(*args)
61 self.assertTrue(os.path.isdir(fn))
62
Vinay Sajip7ded1f02012-05-26 03:45:29 +010063 def test_defaults(self):
64 """
65 Test the create function with default arguments.
66 """
Vinay Sajip7ded1f02012-05-26 03:45:29 +010067 shutil.rmtree(self.env_dir)
68 self.run_with_capture(venv.create, self.env_dir)
Vinay Sajipb3b49cd2012-05-27 18:39:22 +010069 self.isdir(self.bindir)
70 self.isdir(self.include)
71 self.isdir(*self.lib)
Vinay Sajip7ded1f02012-05-26 03:45:29 +010072 data = self.get_text_file_contents('pyvenv.cfg')
Vinay Sajip28952442012-06-25 00:47:46 +010073 if sys.platform == 'darwin' and ('__PYVENV_LAUNCHER__'
Vinay Sajip7ded1f02012-05-26 03:45:29 +010074 in os.environ):
Vinay Sajip28952442012-06-25 00:47:46 +010075 executable = os.environ['__PYVENV_LAUNCHER__']
Vinay Sajip7ded1f02012-05-26 03:45:29 +010076 else:
77 executable = sys.executable
78 path = os.path.dirname(executable)
79 self.assertIn('home = %s' % path, data)
Éric Araujoaa789ac2012-06-24 13:51:22 -040080 data = self.get_text_file_contents(self.bindir, self.pydocname)
81 self.assertTrue(data.startswith('#!%s%s' % (self.env_dir, os.sep)))
Vinay Sajip7ded1f02012-05-26 03:45:29 +010082 fn = self.get_env_file(self.bindir, self.exe)
Vinay Sajip7e203492012-05-27 17:30:09 +010083 if not os.path.exists(fn): # diagnostics for Windows buildbot failures
Vinay Sajipb3b49cd2012-05-27 18:39:22 +010084 bd = self.get_env_file(self.bindir)
85 print('Contents of %r:' % bd)
86 print(' %r' % os.listdir(bd))
Vinay Sajip7e203492012-05-27 17:30:09 +010087 self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn)
Vinay Sajip7ded1f02012-05-26 03:45:29 +010088
89 def test_overwrite_existing(self):
90 """
91 Test control of overwriting an existing environment directory.
92 """
93 self.assertRaises(ValueError, venv.create, self.env_dir)
94 builder = venv.EnvBuilder(clear=True)
95 builder.create(self.env_dir)
96
Vinay Sajipb3b49cd2012-05-27 18:39:22 +010097 def test_upgrade(self):
98 """
99 Test upgrading an existing environment directory.
100 """
101 builder = venv.EnvBuilder(upgrade=True)
102 self.run_with_capture(builder.create, self.env_dir)
103 self.isdir(self.bindir)
104 self.isdir(self.include)
105 self.isdir(*self.lib)
106 fn = self.get_env_file(self.bindir, self.exe)
107 if not os.path.exists(fn): # diagnostics for Windows buildbot failures
108 bd = self.get_env_file(self.bindir)
109 print('Contents of %r:' % bd)
110 print(' %r' % os.listdir(bd))
111 self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn)
112
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100113 def test_isolation(self):
114 """
115 Test isolation from system site-packages
116 """
117 for ssp, s in ((True, 'true'), (False, 'false')):
118 builder = venv.EnvBuilder(clear=True, system_site_packages=ssp)
119 builder.create(self.env_dir)
120 data = self.get_text_file_contents('pyvenv.cfg')
121 self.assertIn('include-system-site-packages = %s\n' % s, data)
122
123 @unittest.skipUnless(can_symlink(), 'Needs symlinks')
124 def test_symlinking(self):
125 """
126 Test symlinking works as expected
127 """
128 for usl in (False, True):
129 builder = venv.EnvBuilder(clear=True, symlinks=usl)
130 if (usl and sys.platform == 'darwin' and
Vinay Sajip28952442012-06-25 00:47:46 +0100131 '__PYVENV_LAUNCHER__' in os.environ):
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100132 self.assertRaises(ValueError, builder.create, self.env_dir)
133 else:
134 builder.create(self.env_dir)
135 fn = self.get_env_file(self.bindir, self.exe)
136 # Don't test when False, because e.g. 'python' is always
137 # symlinked to 'python3.3' in the env, even when symlinking in
138 # general isn't wanted.
139 if usl:
140 self.assertTrue(os.path.islink(fn))
141
142def test_main():
143 run_unittest(BasicTest)
144
145if __name__ == "__main__":
146 test_main()