test_sandbox.py 2.1 KB

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