Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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.5KB

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