OLD | NEW |
(Empty) | |
| 1 """develop tests |
| 2 """ |
| 3 import sys |
| 4 import os |
| 5 import shutil |
| 6 import unittest |
| 7 import tempfile |
| 8 import types |
| 9 |
| 10 import pkg_resources |
| 11 import setuptools.sandbox |
| 12 from setuptools.sandbox import DirectorySandbox, SandboxViolation |
| 13 |
| 14 def has_win32com(): |
| 15 """ |
| 16 Run this to determine if the local machine has win32com, and if it |
| 17 does, include additional tests. |
| 18 """ |
| 19 if not sys.platform.startswith('win32'): |
| 20 return False |
| 21 try: |
| 22 __import__('win32com') |
| 23 except ImportError: |
| 24 return False |
| 25 return True |
| 26 |
| 27 class TestSandbox(unittest.TestCase): |
| 28 |
| 29 def setUp(self): |
| 30 self.dir = tempfile.mkdtemp() |
| 31 |
| 32 def tearDown(self): |
| 33 shutil.rmtree(self.dir) |
| 34 |
| 35 def test_devnull(self): |
| 36 sandbox = DirectorySandbox(self.dir) |
| 37 sandbox.run(self._file_writer(os.devnull)) |
| 38 |
| 39 def _file_writer(path): |
| 40 def do_write(): |
| 41 f = open(path, 'w') |
| 42 f.write('xxx') |
| 43 f.close() |
| 44 return do_write |
| 45 |
| 46 _file_writer = staticmethod(_file_writer) |
| 47 |
| 48 if has_win32com(): |
| 49 def test_win32com(self): |
| 50 """ |
| 51 win32com should not be prevented from caching COM interfaces |
| 52 in gen_py. |
| 53 """ |
| 54 import win32com |
| 55 gen_py = win32com.__gen_path__ |
| 56 target = os.path.join(gen_py, 'test_write') |
| 57 sandbox = DirectorySandbox(self.dir) |
| 58 try: |
| 59 try: |
| 60 sandbox.run(self._file_writer(target)) |
| 61 except SandboxViolation: |
| 62 self.fail("Could not create gen_py file due to SandboxViolat
ion") |
| 63 finally: |
| 64 if os.path.exists(target): os.remove(target) |
| 65 |
| 66 def test_setup_py_with_BOM(self): |
| 67 """ |
| 68 It should be possible to execute a setup.py with a Byte Order Mark |
| 69 """ |
| 70 target = pkg_resources.resource_filename(__name__, |
| 71 'script-with-bom.py') |
| 72 namespace = types.ModuleType('namespace') |
| 73 setuptools.sandbox._execfile(target, vars(namespace)) |
| 74 assert namespace.result == 'passed' |
| 75 |
| 76 def test_setup_py_with_CRLF(self): |
| 77 setup_py = os.path.join(self.dir, 'setup.py') |
| 78 with open(setup_py, 'wb') as stream: |
| 79 stream.write(b'"degenerate script"\r\n') |
| 80 setuptools.sandbox._execfile(setup_py, globals()) |
| 81 |
| 82 if __name__ == '__main__': |
| 83 unittest.main() |
OLD | NEW |