test_sandbox.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. """develop tests
  2. """
  3. import os
  4. import types
  5. import pytest
  6. import pkg_resources
  7. import setuptools.sandbox
  8. from setuptools.sandbox import DirectorySandbox
  9. class TestSandbox:
  10. def test_devnull(self, tmpdir):
  11. sandbox = DirectorySandbox(str(tmpdir))
  12. sandbox.run(self._file_writer(os.devnull))
  13. @staticmethod
  14. def _file_writer(path):
  15. def do_write():
  16. with open(path, 'w') as f:
  17. f.write('xxx')
  18. return do_write
  19. def test_win32com(self, tmpdir):
  20. """
  21. win32com should not be prevented from caching COM interfaces
  22. in gen_py.
  23. """
  24. win32com = pytest.importorskip('win32com')
  25. gen_py = win32com.__gen_path__
  26. target = os.path.join(gen_py, 'test_write')
  27. sandbox = DirectorySandbox(str(tmpdir))
  28. try:
  29. # attempt to create gen_py file
  30. sandbox.run(self._file_writer(target))
  31. finally:
  32. if os.path.exists(target):
  33. os.remove(target)
  34. def test_setup_py_with_BOM(self):
  35. """
  36. It should be possible to execute a setup.py with a Byte Order Mark
  37. """
  38. target = pkg_resources.resource_filename(__name__,
  39. 'script-with-bom.py')
  40. namespace = types.ModuleType('namespace')
  41. setuptools.sandbox._execfile(target, vars(namespace))
  42. assert namespace.result == 'passed'
  43. def test_setup_py_with_CRLF(self, tmpdir):
  44. setup_py = tmpdir / 'setup.py'
  45. with setup_py.open('wb') as stream:
  46. stream.write(b'"degenerate script"\r\n')
  47. setuptools.sandbox._execfile(str(setup_py), globals())
  48. class TestExceptionSaver:
  49. def test_exception_trapped(self):
  50. with setuptools.sandbox.ExceptionSaver():
  51. raise ValueError("details")
  52. def test_exception_resumed(self):
  53. with setuptools.sandbox.ExceptionSaver() as saved_exc:
  54. raise ValueError("details")
  55. with pytest.raises(ValueError) as caught:
  56. saved_exc.resume()
  57. assert isinstance(caught.value, ValueError)
  58. assert str(caught.value) == 'details'
  59. def test_exception_reconstructed(self):
  60. orig_exc = ValueError("details")
  61. with setuptools.sandbox.ExceptionSaver() as saved_exc:
  62. raise orig_exc
  63. with pytest.raises(ValueError) as caught:
  64. saved_exc.resume()
  65. assert isinstance(caught.value, ValueError)
  66. assert caught.value is not orig_exc
  67. def test_no_exception_passes_quietly(self):
  68. with setuptools.sandbox.ExceptionSaver() as saved_exc:
  69. pass
  70. saved_exc.resume()
  71. def test_unpickleable_exception(self):
  72. class CantPickleThis(Exception):
  73. "This Exception is unpickleable because it's not in globals"
  74. with setuptools.sandbox.ExceptionSaver() as saved_exc:
  75. raise CantPickleThis('detail')
  76. with pytest.raises(setuptools.sandbox.UnpickleableException) as caught:
  77. saved_exc.resume()
  78. assert str(caught.value) == "CantPickleThis('detail',)"