blob: 8e41a47fd0559f91ed19688a5e82060f153a68b5 [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
55 def test_defaults(self):
56 """
57 Test the create function with default arguments.
58 """
59 def isdir(*args):
60 fn = self.get_env_file(*args)
61 self.assertTrue(os.path.isdir(fn))
62
63 shutil.rmtree(self.env_dir)
64 self.run_with_capture(venv.create, self.env_dir)
65 isdir(self.bindir)
66 isdir(self.include)
67 isdir(*self.lib)
68 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 Sajip42211422012-05-26 20:36:12 +010079 self.assertTrue(os.path.exists(fn), 'File %r exists' % fn)
Vinay Sajip7ded1f02012-05-26 03:45:29 +010080
81 def test_overwrite_existing(self):
82 """
83 Test control of overwriting an existing environment directory.
84 """
85 self.assertRaises(ValueError, venv.create, self.env_dir)
86 builder = venv.EnvBuilder(clear=True)
87 builder.create(self.env_dir)
88
89 def test_isolation(self):
90 """
91 Test isolation from system site-packages
92 """
93 for ssp, s in ((True, 'true'), (False, 'false')):
94 builder = venv.EnvBuilder(clear=True, system_site_packages=ssp)
95 builder.create(self.env_dir)
96 data = self.get_text_file_contents('pyvenv.cfg')
97 self.assertIn('include-system-site-packages = %s\n' % s, data)
98
99 @unittest.skipUnless(can_symlink(), 'Needs symlinks')
100 def test_symlinking(self):
101 """
102 Test symlinking works as expected
103 """
104 for usl in (False, True):
105 builder = venv.EnvBuilder(clear=True, symlinks=usl)
106 if (usl and sys.platform == 'darwin' and
107 '__PYTHONV_LAUNCHER__' in os.environ):
108 self.assertRaises(ValueError, builder.create, self.env_dir)
109 else:
110 builder.create(self.env_dir)
111 fn = self.get_env_file(self.bindir, self.exe)
112 # Don't test when False, because e.g. 'python' is always
113 # symlinked to 'python3.3' in the env, even when symlinking in
114 # general isn't wanted.
115 if usl:
116 self.assertTrue(os.path.islink(fn))
117
118def test_main():
119 run_unittest(BasicTest)
120
121if __name__ == "__main__":
122 test_main()