Vinay Sajip | 4221142 | 2012-05-26 20:36:12 +0100 | [diff] [blame] | 1 | """ |
| 2 | Test harness for the venv module. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 3 | |
Vinay Sajip | 4221142 | 2012-05-26 20:36:12 +0100 | [diff] [blame] | 4 | Copyright (C) 2011-2012 Vinay Sajip. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 5 | """ |
| 6 | |
| 7 | import os |
| 8 | import os.path |
| 9 | import shutil |
| 10 | import sys |
| 11 | import tempfile |
| 12 | from test.support import (captured_stdout, captured_stderr, run_unittest, |
| 13 | can_symlink) |
| 14 | import unittest |
| 15 | import venv |
| 16 | |
| 17 | class 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 | |
| 52 | class 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 Sajip | 7e20349 | 2012-05-27 17:30:09 +0100 | [diff] [blame^] | 79 | if not os.path.exists(fn): # diagnostics for Windows buildbot failures |
| 80 | print('Contents of %r:' % self.bindir) |
| 81 | print(' %r' % os.listdir(self.bindir)) |
| 82 | self.assertTrue(os.path.exists(fn), 'File %r should exist.' % fn) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 83 | |
| 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 | |
| 92 | def test_isolation(self): |
| 93 | """ |
| 94 | Test isolation from system site-packages |
| 95 | """ |
| 96 | for ssp, s in ((True, 'true'), (False, 'false')): |
| 97 | builder = venv.EnvBuilder(clear=True, system_site_packages=ssp) |
| 98 | builder.create(self.env_dir) |
| 99 | data = self.get_text_file_contents('pyvenv.cfg') |
| 100 | self.assertIn('include-system-site-packages = %s\n' % s, data) |
| 101 | |
| 102 | @unittest.skipUnless(can_symlink(), 'Needs symlinks') |
| 103 | def test_symlinking(self): |
| 104 | """ |
| 105 | Test symlinking works as expected |
| 106 | """ |
| 107 | for usl in (False, True): |
| 108 | builder = venv.EnvBuilder(clear=True, symlinks=usl) |
| 109 | if (usl and sys.platform == 'darwin' and |
| 110 | '__PYTHONV_LAUNCHER__' in os.environ): |
| 111 | self.assertRaises(ValueError, builder.create, self.env_dir) |
| 112 | else: |
| 113 | builder.create(self.env_dir) |
| 114 | fn = self.get_env_file(self.bindir, self.exe) |
| 115 | # Don't test when False, because e.g. 'python' is always |
| 116 | # symlinked to 'python3.3' in the env, even when symlinking in |
| 117 | # general isn't wanted. |
| 118 | if usl: |
| 119 | self.assertTrue(os.path.islink(fn)) |
| 120 | |
| 121 | def test_main(): |
| 122 | run_unittest(BasicTest) |
| 123 | |
| 124 | if __name__ == "__main__": |
| 125 | test_main() |