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.

test_rebuild.py 7.2KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. import os
  4. import sys
  5. import types
  6. from twisted.python import rebuild
  7. from twisted.trial.unittest import TestCase
  8. from . import crash_test_dummy
  9. f = crash_test_dummy.foo
  10. class Foo:
  11. pass
  12. class Bar(Foo):
  13. pass
  14. class Baz:
  15. pass
  16. class Buz(Bar, Baz):
  17. pass
  18. class HashRaisesRuntimeError:
  19. """
  20. Things that don't hash (raise an Exception) should be ignored by the
  21. rebuilder.
  22. @ivar hashCalled: C{bool} set to True when __hash__ is called.
  23. """
  24. def __init__(self):
  25. self.hashCalled = False
  26. def __hash__(self):
  27. self.hashCalled = True
  28. raise RuntimeError("not a TypeError!")
  29. # Set in test_hashException
  30. unhashableObject = None
  31. class RebuildTests(TestCase):
  32. """
  33. Simple testcase for rebuilding, to at least exercise the code.
  34. """
  35. def setUp(self):
  36. self.libPath = self.mktemp()
  37. os.mkdir(self.libPath)
  38. self.fakelibPath = os.path.join(self.libPath, "twisted_rebuild_fakelib")
  39. os.mkdir(self.fakelibPath)
  40. open(os.path.join(self.fakelibPath, "__init__.py"), "w").close()
  41. sys.path.insert(0, self.libPath)
  42. def tearDown(self):
  43. sys.path.remove(self.libPath)
  44. def test_FileRebuild(self):
  45. import shutil
  46. import time
  47. from twisted.python.util import sibpath
  48. shutil.copyfile(
  49. sibpath(__file__, "myrebuilder1.py"),
  50. os.path.join(self.fakelibPath, "myrebuilder.py"),
  51. )
  52. from twisted_rebuild_fakelib import myrebuilder # type: ignore[import]
  53. a = myrebuilder.A()
  54. b = myrebuilder.B()
  55. i = myrebuilder.Inherit()
  56. self.assertEqual(a.a(), "a")
  57. # Necessary because the file has not "changed" if a second has not gone
  58. # by in unix. This sucks, but it's not often that you'll be doing more
  59. # than one reload per second.
  60. time.sleep(1.1)
  61. shutil.copyfile(
  62. sibpath(__file__, "myrebuilder2.py"),
  63. os.path.join(self.fakelibPath, "myrebuilder.py"),
  64. )
  65. rebuild.rebuild(myrebuilder)
  66. b2 = myrebuilder.B()
  67. self.assertEqual(b2.b(), "c")
  68. self.assertEqual(b.b(), "c")
  69. self.assertEqual(i.a(), "d")
  70. self.assertEqual(a.a(), "b")
  71. def test_Rebuild(self):
  72. """
  73. Rebuilding an unchanged module.
  74. """
  75. # This test would actually pass if rebuild was a no-op, but it
  76. # ensures rebuild doesn't break stuff while being a less
  77. # complex test than testFileRebuild.
  78. x = crash_test_dummy.X("a")
  79. rebuild.rebuild(crash_test_dummy, doLog=False)
  80. # Instance rebuilding is triggered by attribute access.
  81. x.do()
  82. self.assertEqual(x.__class__, crash_test_dummy.X)
  83. self.assertEqual(f, crash_test_dummy.foo)
  84. def test_ComponentInteraction(self):
  85. x = crash_test_dummy.XComponent()
  86. x.setAdapter(crash_test_dummy.IX, crash_test_dummy.XA)
  87. x.getComponent(crash_test_dummy.IX)
  88. rebuild.rebuild(crash_test_dummy, 0)
  89. newComponent = x.getComponent(crash_test_dummy.IX)
  90. newComponent.method()
  91. self.assertEqual(newComponent.__class__, crash_test_dummy.XA)
  92. # Test that a duplicate registerAdapter is not allowed
  93. from twisted.python import components
  94. self.assertRaises(
  95. ValueError,
  96. components.registerAdapter,
  97. crash_test_dummy.XA,
  98. crash_test_dummy.X,
  99. crash_test_dummy.IX,
  100. )
  101. def test_UpdateInstance(self):
  102. global Foo, Buz
  103. b = Buz()
  104. class Foo:
  105. def foo(self):
  106. """
  107. Dummy method
  108. """
  109. class Buz(Bar, Baz):
  110. x = 10
  111. rebuild.updateInstance(b)
  112. assert hasattr(b, "foo"), "Missing method on rebuilt instance"
  113. assert hasattr(b, "x"), "Missing class attribute on rebuilt instance"
  114. def test_BananaInteraction(self):
  115. from twisted.python import rebuild
  116. from twisted.spread import banana
  117. rebuild.latestClass(banana.Banana)
  118. def test_hashException(self):
  119. """
  120. Rebuilding something that has a __hash__ that raises a non-TypeError
  121. shouldn't cause rebuild to die.
  122. """
  123. global unhashableObject
  124. unhashableObject = HashRaisesRuntimeError()
  125. def _cleanup():
  126. global unhashableObject
  127. unhashableObject = None
  128. self.addCleanup(_cleanup)
  129. rebuild.rebuild(rebuild)
  130. self.assertTrue(unhashableObject.hashCalled)
  131. def test_Sensitive(self):
  132. """
  133. L{twisted.python.rebuild.Sensitive}
  134. """
  135. from twisted.python import rebuild
  136. from twisted.python.rebuild import Sensitive
  137. class TestSensitive(Sensitive):
  138. def test_method(self):
  139. """
  140. Dummy method
  141. """
  142. testSensitive = TestSensitive()
  143. testSensitive.rebuildUpToDate()
  144. self.assertFalse(testSensitive.needRebuildUpdate())
  145. # Test rebuilding a builtin class
  146. newException = rebuild.latestClass(Exception)
  147. self.assertEqual(repr(Exception), repr(newException))
  148. self.assertEqual(newException, testSensitive.latestVersionOf(newException))
  149. # Test types.MethodType on method in class
  150. self.assertEqual(
  151. TestSensitive.test_method,
  152. testSensitive.latestVersionOf(TestSensitive.test_method),
  153. )
  154. # Test types.MethodType on method in instance of class
  155. self.assertEqual(
  156. testSensitive.test_method,
  157. testSensitive.latestVersionOf(testSensitive.test_method),
  158. )
  159. # Test a class
  160. self.assertEqual(TestSensitive, testSensitive.latestVersionOf(TestSensitive))
  161. def myFunction():
  162. """
  163. Dummy method
  164. """
  165. # Test types.FunctionType
  166. self.assertEqual(myFunction, testSensitive.latestVersionOf(myFunction))
  167. class NewStyleTests(TestCase):
  168. """
  169. Tests for rebuilding new-style classes of various sorts.
  170. """
  171. def setUp(self):
  172. self.m = types.ModuleType("whipping")
  173. sys.modules["whipping"] = self.m
  174. def tearDown(self):
  175. del sys.modules["whipping"]
  176. del self.m
  177. def test_slots(self):
  178. """
  179. Try to rebuild a new style class with slots defined.
  180. """
  181. classDefinition = "class SlottedClass:\n" " __slots__ = ['a']\n"
  182. exec(classDefinition, self.m.__dict__)
  183. inst = self.m.SlottedClass()
  184. inst.a = 7
  185. exec(classDefinition, self.m.__dict__)
  186. rebuild.updateInstance(inst)
  187. self.assertEqual(inst.a, 7)
  188. self.assertIs(type(inst), self.m.SlottedClass)
  189. def test_typeSubclass(self):
  190. """
  191. Try to rebuild a base type subclass.
  192. """
  193. classDefinition = "class ListSubclass(list):\n" " pass\n"
  194. exec(classDefinition, self.m.__dict__)
  195. inst = self.m.ListSubclass()
  196. inst.append(2)
  197. exec(classDefinition, self.m.__dict__)
  198. rebuild.updateInstance(inst)
  199. self.assertEqual(inst[0], 2)
  200. self.assertIs(type(inst), self.m.ListSubclass)