You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

packages.py 4.8KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. #
  4. """
  5. Classes and functions used by L{twisted.trial.test.test_util}
  6. and L{twisted.trial.test.test_loader}.
  7. """
  8. from __future__ import division, absolute_import
  9. import sys
  10. import os
  11. from twisted.python.compat import _PY3
  12. from twisted.trial import unittest
  13. if _PY3:
  14. # Python 3 has some funny import caching, which we don't want.
  15. # invalidate_caches clears it out for us.
  16. from importlib import invalidate_caches as invalidateImportCaches
  17. else:
  18. def invalidateImportCaches():
  19. """
  20. On python 2, import caches don't need to be invalidated.
  21. """
  22. testModule = """
  23. from twisted.trial import unittest
  24. class FooTest(unittest.SynchronousTestCase):
  25. def testFoo(self):
  26. pass
  27. """
  28. dosModule = testModule.replace('\n', '\r\n')
  29. testSample = """
  30. '''This module is used by test_loader to test the Trial test loading
  31. functionality. Do NOT change the number of tests in this module.
  32. Do NOT change the names the tests in this module.
  33. '''
  34. import unittest as pyunit
  35. from twisted.trial import unittest
  36. class FooTest(unittest.SynchronousTestCase):
  37. def test_foo(self):
  38. pass
  39. def test_bar(self):
  40. pass
  41. class PyunitTest(pyunit.TestCase):
  42. def test_foo(self):
  43. pass
  44. def test_bar(self):
  45. pass
  46. class NotATest(object):
  47. def test_foo(self):
  48. pass
  49. class AlphabetTest(unittest.SynchronousTestCase):
  50. def test_a(self):
  51. pass
  52. def test_b(self):
  53. pass
  54. def test_c(self):
  55. pass
  56. """
  57. testInheritanceSample = """
  58. '''This module is used by test_loader to test the Trial test loading
  59. functionality. Do NOT change the number of tests in this module.
  60. Do NOT change the names the tests in this module.
  61. '''
  62. from twisted.trial import unittest
  63. class X(object):
  64. def test_foo(self):
  65. pass
  66. class A(unittest.SynchronousTestCase, X):
  67. pass
  68. class B(unittest.SynchronousTestCase, X):
  69. pass
  70. """
  71. class PackageTest(unittest.SynchronousTestCase):
  72. files = [
  73. ('badpackage/__init__.py', 'frotz\n'),
  74. ('badpackage/test_module.py', ''),
  75. ('unimportablepackage/__init__.py', ''),
  76. ('unimportablepackage/test_module.py', 'import notarealmoduleok\n'),
  77. ('package2/__init__.py', ''),
  78. ('package2/test_module.py', 'import frotz\n'),
  79. ('package/__init__.py', ''),
  80. ('package/frotz.py', 'frotz\n'),
  81. ('package/test_bad_module.py',
  82. 'raise ZeroDivisionError("fake error")'),
  83. ('package/test_dos_module.py', dosModule),
  84. ('package/test_import_module.py', 'import frotz'),
  85. ('package/test_module.py', testModule),
  86. ('goodpackage/__init__.py', ''),
  87. ('goodpackage/test_sample.py', testSample),
  88. ('goodpackage/sub/__init__.py', ''),
  89. ('goodpackage/sub/test_sample.py', testSample),
  90. ('inheritancepackage/__init__.py', ''),
  91. ('inheritancepackage/test_x.py', testInheritanceSample),
  92. ]
  93. def _toModuleName(self, filename):
  94. name = os.path.splitext(filename)[0]
  95. segs = name.split('/')
  96. if segs[-1] == '__init__':
  97. segs = segs[:-1]
  98. return '.'.join(segs)
  99. def getModules(self):
  100. """
  101. Return matching module names for files listed in C{self.files}.
  102. """
  103. return [self._toModuleName(filename) for (filename, code) in self.files]
  104. def cleanUpModules(self):
  105. modules = self.getModules()
  106. modules.sort()
  107. modules.reverse()
  108. for module in modules:
  109. try:
  110. del sys.modules[module]
  111. except KeyError:
  112. pass
  113. def createFiles(self, files, parentDir='.'):
  114. for filename, contents in self.files:
  115. filename = os.path.join(parentDir, filename)
  116. self._createDirectory(filename)
  117. with open(filename, 'w') as fd:
  118. fd.write(contents)
  119. def _createDirectory(self, filename):
  120. directory = os.path.dirname(filename)
  121. if not os.path.exists(directory):
  122. os.makedirs(directory)
  123. def setUp(self, parentDir=None):
  124. invalidateImportCaches()
  125. if parentDir is None:
  126. parentDir = self.mktemp()
  127. self.parent = parentDir
  128. self.createFiles(self.files, parentDir)
  129. def tearDown(self):
  130. self.cleanUpModules()
  131. class SysPathManglingTest(PackageTest):
  132. def setUp(self, parent=None):
  133. invalidateImportCaches()
  134. self.oldPath = sys.path[:]
  135. self.newPath = sys.path[:]
  136. if parent is None:
  137. parent = self.mktemp()
  138. PackageTest.setUp(self, parent)
  139. self.newPath.append(self.parent)
  140. self.mangleSysPath(self.newPath)
  141. def tearDown(self):
  142. PackageTest.tearDown(self)
  143. self.mangleSysPath(self.oldPath)
  144. def mangleSysPath(self, pathVar):
  145. sys.path[:] = pathVar