Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2011 by Vinay Sajip. All Rights Reserved. |
| 4 | # |
| 5 | # Permission to use, copy, modify, and distribute this software and its |
| 6 | # documentation for any purpose and without fee is hereby granted, |
| 7 | # provided that the above copyright notice appear in all copies and that |
| 8 | # both that copyright notice and this permission notice appear in |
| 9 | # supporting documentation, and that the name of Vinay Sajip |
| 10 | # not be used in advertising or publicity pertaining to distribution |
| 11 | # of the software without specific, written prior permission. |
| 12 | # VINAY SAJIP DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING |
| 13 | # ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL |
| 14 | # VINAY SAJIP BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR |
| 15 | # ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER |
| 16 | # IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 17 | # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 18 | |
| 19 | """Test harness for the venv module. Run all tests. |
| 20 | |
| 21 | Copyright (C) 2011 Vinay Sajip. All Rights Reserved. |
| 22 | """ |
| 23 | |
| 24 | import os |
| 25 | import os.path |
| 26 | import shutil |
| 27 | import sys |
| 28 | import tempfile |
| 29 | from test.support import (captured_stdout, captured_stderr, run_unittest, |
| 30 | can_symlink) |
| 31 | import unittest |
| 32 | import venv |
| 33 | |
| 34 | class BaseTest(unittest.TestCase): |
| 35 | """Base class for venv tests.""" |
| 36 | |
| 37 | def setUp(self): |
| 38 | self.env_dir = tempfile.mkdtemp() |
| 39 | if os.name == 'nt': |
| 40 | self.bindir = 'Scripts' |
| 41 | self.ps3name = 'pysetup3-script.py' |
| 42 | self.lib = ('Lib',) |
| 43 | self.include = 'Include' |
| 44 | self.exe = 'python.exe' |
| 45 | else: |
| 46 | self.bindir = 'bin' |
| 47 | self.ps3name = 'pysetup3' |
| 48 | self.lib = ('lib', 'python%s' % sys.version[:3]) |
| 49 | self.include = 'include' |
| 50 | self.exe = 'python' |
| 51 | |
| 52 | def tearDown(self): |
| 53 | shutil.rmtree(self.env_dir) |
| 54 | |
| 55 | def run_with_capture(self, func, *args, **kwargs): |
| 56 | with captured_stdout() as output: |
| 57 | with captured_stderr() as error: |
| 58 | func(*args, **kwargs) |
| 59 | return output.getvalue(), error.getvalue() |
| 60 | |
| 61 | def get_env_file(self, *args): |
| 62 | return os.path.join(self.env_dir, *args) |
| 63 | |
| 64 | def get_text_file_contents(self, *args): |
| 65 | with open(self.get_env_file(*args), 'r') as f: |
| 66 | result = f.read() |
| 67 | return result |
| 68 | |
| 69 | class BasicTest(BaseTest): |
| 70 | """Test venv module functionality.""" |
| 71 | |
| 72 | def test_defaults(self): |
| 73 | """ |
| 74 | Test the create function with default arguments. |
| 75 | """ |
| 76 | def isdir(*args): |
| 77 | fn = self.get_env_file(*args) |
| 78 | self.assertTrue(os.path.isdir(fn)) |
| 79 | |
| 80 | shutil.rmtree(self.env_dir) |
| 81 | self.run_with_capture(venv.create, self.env_dir) |
| 82 | isdir(self.bindir) |
| 83 | isdir(self.include) |
| 84 | isdir(*self.lib) |
| 85 | data = self.get_text_file_contents('pyvenv.cfg') |
| 86 | if sys.platform == 'darwin' and ('__PYTHONV_LAUNCHER__' |
| 87 | in os.environ): |
| 88 | executable = os.environ['__PYTHONV_LAUNCHER__'] |
| 89 | else: |
| 90 | executable = sys.executable |
| 91 | path = os.path.dirname(executable) |
| 92 | self.assertIn('home = %s' % path, data) |
| 93 | data = self.get_text_file_contents(self.bindir, self.ps3name) |
| 94 | self.assertTrue(data.startswith('#!%s%s' % (self.env_dir, os.sep))) |
| 95 | fn = self.get_env_file(self.bindir, self.exe) |
| 96 | self.assertTrue(os.path.exists(fn)) |
| 97 | |
| 98 | def test_overwrite_existing(self): |
| 99 | """ |
| 100 | Test control of overwriting an existing environment directory. |
| 101 | """ |
| 102 | self.assertRaises(ValueError, venv.create, self.env_dir) |
| 103 | builder = venv.EnvBuilder(clear=True) |
| 104 | builder.create(self.env_dir) |
| 105 | |
| 106 | def test_isolation(self): |
| 107 | """ |
| 108 | Test isolation from system site-packages |
| 109 | """ |
| 110 | for ssp, s in ((True, 'true'), (False, 'false')): |
| 111 | builder = venv.EnvBuilder(clear=True, system_site_packages=ssp) |
| 112 | builder.create(self.env_dir) |
| 113 | data = self.get_text_file_contents('pyvenv.cfg') |
| 114 | self.assertIn('include-system-site-packages = %s\n' % s, data) |
| 115 | |
| 116 | @unittest.skipUnless(can_symlink(), 'Needs symlinks') |
| 117 | def test_symlinking(self): |
| 118 | """ |
| 119 | Test symlinking works as expected |
| 120 | """ |
| 121 | for usl in (False, True): |
| 122 | builder = venv.EnvBuilder(clear=True, symlinks=usl) |
| 123 | if (usl and sys.platform == 'darwin' and |
| 124 | '__PYTHONV_LAUNCHER__' in os.environ): |
| 125 | self.assertRaises(ValueError, builder.create, self.env_dir) |
| 126 | else: |
| 127 | builder.create(self.env_dir) |
| 128 | fn = self.get_env_file(self.bindir, self.exe) |
| 129 | # Don't test when False, because e.g. 'python' is always |
| 130 | # symlinked to 'python3.3' in the env, even when symlinking in |
| 131 | # general isn't wanted. |
| 132 | if usl: |
| 133 | self.assertTrue(os.path.islink(fn)) |
| 134 | |
| 135 | def test_main(): |
| 136 | run_unittest(BasicTest) |
| 137 | |
| 138 | if __name__ == "__main__": |
| 139 | test_main() |