test_twisted.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for miscellaneous behaviors of the top-level L{twisted} package (ie, for
  5. the code in C{twisted/__init__.py}.
  6. """
  7. from __future__ import division, absolute_import
  8. import sys
  9. import twisted
  10. from types import ModuleType, FunctionType
  11. from twisted import _checkRequirements
  12. from twisted.python.compat import _PY3
  13. from twisted.python import reflect
  14. from twisted.trial.unittest import TestCase, SkipTest
  15. # This is somewhat generally useful and should probably be part of a public API
  16. # somewhere. See #5977.
  17. class SetAsideModule(object):
  18. """
  19. L{SetAsideModule} is a context manager for temporarily removing a module
  20. from C{sys.modules}.
  21. @ivar name: The name of the module to remove.
  22. """
  23. def __init__(self, name):
  24. self.name = name
  25. def _unimport(self, name):
  26. """
  27. Find the given module and all of its hierarchically inferior modules in
  28. C{sys.modules}, remove them from it, and return whatever was found.
  29. """
  30. modules = dict([
  31. (moduleName, module)
  32. for (moduleName, module)
  33. in list(sys.modules.items())
  34. if (moduleName == self.name or
  35. moduleName.startswith(self.name + "."))])
  36. for name in modules:
  37. del sys.modules[name]
  38. return modules
  39. def __enter__(self):
  40. self.modules = self._unimport(self.name)
  41. def __exit__(self, excType, excValue, traceback):
  42. self._unimport(self.name)
  43. sys.modules.update(self.modules)
  44. def _install(modules):
  45. """
  46. Take a mapping defining a package and turn it into real C{ModuleType}
  47. instances in C{sys.modules}.
  48. Consider these example::
  49. a = {"foo": "bar"}
  50. b = {"twisted": {"__version__": "42.6"}}
  51. c = {"twisted": {"plugin": {"getPlugins": stub}}}
  52. C{_install(a)} will place an item into C{sys.modules} with C{"foo"} as the
  53. key and C{"bar" as the value.
  54. C{_install(b)} will place an item into C{sys.modules} with C{"twisted"} as
  55. the key. The value will be a new module object. The module will have a
  56. C{"__version__"} attribute with C{"42.6"} as the value.
  57. C{_install(c)} will place an item into C{sys.modules} with C{"twisted"} as
  58. the key. The value will be a new module object with a C{"plugin"}
  59. attribute. An item will also be placed into C{sys.modules} with the key
  60. C{"twisted.plugin"} which refers to that module object. That module will
  61. have an attribute C{"getPlugins"} with a value of C{stub}.
  62. @param modules: A mapping from names to definitions of modules. The names
  63. are native strings like C{"twisted"} or C{"unittest"}. Values may be
  64. arbitrary objects. Any value which is not a dictionary will be added to
  65. C{sys.modules} unmodified. Any dictionary value indicates the value is
  66. a new module and its items define the attributes of that module. The
  67. definition of this structure is recursive, so a value in the dictionary
  68. may be a dictionary to trigger another level of processing.
  69. @return: L{None}
  70. """
  71. result = {}
  72. _makePackages(None, modules, result)
  73. sys.modules.update(result)
  74. def _makePackages(parent, attributes, result):
  75. """
  76. Construct module objects (for either modules or packages).
  77. @param parent: L{None} or a module object which is the Python package
  78. containing all of the modules being created by this function call. Its
  79. name will be prepended to the name of all created modules.
  80. @param attributes: A mapping giving the attributes of the particular module
  81. object this call is creating.
  82. @param result: A mapping which is populated with all created module names.
  83. This is suitable for use in updating C{sys.modules}.
  84. @return: A mapping of all of the attributes created by this call. This is
  85. suitable for populating the dictionary of C{parent}.
  86. @see: L{_install}.
  87. """
  88. attrs = {}
  89. for (name, value) in list(attributes.items()):
  90. if parent is None:
  91. if isinstance(value, dict):
  92. module = ModuleType(name)
  93. module.__dict__.update(_makePackages(module, value, result))
  94. result[name] = module
  95. else:
  96. result[name] = value
  97. else:
  98. if isinstance(value, dict):
  99. module = ModuleType(parent.__name__ + '.' + name)
  100. module.__dict__.update(_makePackages(module, value, result))
  101. result[parent.__name__ + '.' + name] = module
  102. attrs[name] = module
  103. else:
  104. attrs[name] = value
  105. return attrs
  106. class RequirementsTests(TestCase):
  107. """
  108. Tests for the import-time requirements checking.
  109. @ivar unsupportedPythonVersion: The newest version of Python 2.x which is
  110. not supported by Twisted.
  111. @type unsupportedPythonVersion: C{tuple}
  112. @ivar supportedPythonVersion: The oldest version of Python 2.x which is
  113. supported by Twisted.
  114. @type supportedPythonVersion: C{tuple}
  115. @ivar Py3unsupportedPythonVersion: The newest version of Python 3.x which
  116. is not supported by Twisted.
  117. @type Py3unsupportedPythonVersion: C{tuple}
  118. @ivar Py3supportedPythonVersion: The oldest version of Python 3.x which is
  119. supported by Twisted.
  120. @type supportedPythonVersion: C{tuple}
  121. @ivar Py3supportedZopeInterfaceVersion: The oldest version of
  122. C{zope.interface} which is supported by Twisted.
  123. @type supportedZopeInterfaceVersion: C{tuple}
  124. """
  125. unsupportedPythonVersion = (2, 6)
  126. supportedPythonVersion = (2, 7)
  127. Py3unsupportedPythonVersion = (3, 2)
  128. Py3supportedPythonVersion = (3, 3)
  129. def setUp(self):
  130. """
  131. Save the original value of C{sys.version_info} so it can be restored
  132. after the tests mess with it.
  133. """
  134. self.version = sys.version_info
  135. def tearDown(self):
  136. """
  137. Restore the original values saved in L{setUp}.
  138. """
  139. sys.version_info = self.version
  140. def test_oldPython(self):
  141. """
  142. L{_checkRequirements} raises L{ImportError} when run on a version of
  143. Python that is too old.
  144. """
  145. sys.version_info = self.unsupportedPythonVersion
  146. with self.assertRaises(ImportError) as raised:
  147. _checkRequirements()
  148. self.assertEqual("Twisted requires Python %d.%d or later."
  149. % self.supportedPythonVersion,
  150. str(raised.exception))
  151. def test_newPython(self):
  152. """
  153. L{_checkRequirements} returns L{None} when run on a version of Python
  154. that is sufficiently new.
  155. """
  156. sys.version_info = self.supportedPythonVersion
  157. self.assertIsNone(_checkRequirements())
  158. def test_oldPythonPy3(self):
  159. """
  160. L{_checkRequirements} raises L{ImportError} when run on a version of
  161. Python that is too old.
  162. """
  163. sys.version_info = self.Py3unsupportedPythonVersion
  164. with self.assertRaises(ImportError) as raised:
  165. _checkRequirements()
  166. self.assertEqual("Twisted on Python 3 requires Python %d.%d or later."
  167. % self.Py3supportedPythonVersion,
  168. str(raised.exception))
  169. def test_newPythonPy3(self):
  170. """
  171. L{_checkRequirements} returns L{None} when run on a version of Python
  172. that is sufficiently new.
  173. """
  174. sys.version_info = self.Py3supportedPythonVersion
  175. self.assertIsNone(_checkRequirements())
  176. class MakePackagesTests(TestCase):
  177. """
  178. Tests for L{_makePackages}, a helper for populating C{sys.modules} with
  179. fictional modules.
  180. """
  181. def test_nonModule(self):
  182. """
  183. A non-C{dict} value in the attributes dictionary passed to L{_makePackages}
  184. is preserved unchanged in the return value.
  185. """
  186. modules = {}
  187. _makePackages(None, dict(reactor='reactor'), modules)
  188. self.assertEqual(modules, dict(reactor='reactor'))
  189. def test_moduleWithAttribute(self):
  190. """
  191. A C{dict} value in the attributes dictionary passed to L{_makePackages}
  192. is turned into a L{ModuleType} instance with attributes populated from
  193. the items of that C{dict} value.
  194. """
  195. modules = {}
  196. _makePackages(None, dict(twisted=dict(version='123')), modules)
  197. self.assertIsInstance(modules, dict)
  198. self.assertIsInstance(modules['twisted'], ModuleType)
  199. self.assertEqual('twisted', modules['twisted'].__name__)
  200. self.assertEqual('123', modules['twisted'].version)
  201. def test_packageWithModule(self):
  202. """
  203. Processing of the attributes dictionary is recursive, so a C{dict} value
  204. it contains may itself contain a C{dict} value to the same effect.
  205. """
  206. modules = {}
  207. _makePackages(None, dict(twisted=dict(web=dict(version='321'))), modules)
  208. self.assertIsInstance(modules, dict)
  209. self.assertIsInstance(modules['twisted'], ModuleType)
  210. self.assertEqual('twisted', modules['twisted'].__name__)
  211. self.assertIsInstance(modules['twisted'].web, ModuleType)
  212. self.assertEqual('twisted.web', modules['twisted'].web.__name__)
  213. self.assertEqual('321', modules['twisted'].web.version)
  214. def _functionOnlyImplementer(*interfaces):
  215. """
  216. A fake implementation of L{zope.interface.implementer} which always behaves
  217. like the version of that function provided by zope.interface 3.5 and older.
  218. """
  219. def check(obj):
  220. """
  221. If the decorated object is not a function, raise an exception.
  222. """
  223. if not isinstance(obj, FunctionType):
  224. raise TypeError(
  225. "Can't use implementer with classes. "
  226. "Use one of the class-declaration functions instead.")
  227. return check
  228. def _classSupportingImplementer(*interfaces):
  229. """
  230. A fake implementation of L{zope.interface.implementer} which always
  231. succeeds. For the use it is put to, this is like the version of that
  232. function provided by zope.interface 3.6 and newer.
  233. """
  234. def check(obj):
  235. """
  236. Do nothing at all.
  237. """
  238. return check
  239. class _SuccessInterface(object):
  240. """
  241. A fake implementation of L{zope.interface.Interface} with no behavior. For
  242. the use it is put to, this is equivalent to the behavior of the C{Interface}
  243. provided by all versions of zope.interface.
  244. """
  245. # Definition of a module somewhat like zope.interface 3.5.
  246. _zope35 = {
  247. 'zope': {
  248. 'interface': {
  249. 'Interface': _SuccessInterface,
  250. 'implementer': _functionOnlyImplementer,
  251. },
  252. },
  253. }
  254. # Definition of a module somewhat like zope.interface 3.6.
  255. _zope36 = {
  256. 'zope': {
  257. 'interface': {
  258. 'Interface': _SuccessInterface,
  259. 'implementer': _classSupportingImplementer,
  260. },
  261. },
  262. }
  263. class _Zope38OnPython3Module(object):
  264. """
  265. A pseudo-module which raises an exception when its C{interface} attribute is
  266. accessed. This is like the behavior of zope.interface 3.8 and earlier when
  267. used with Python 3.3.
  268. """
  269. __path__ = []
  270. __name__ = 'zope'
  271. @property
  272. def interface(self):
  273. raise Exception(
  274. "zope.interface.exceptions.InvalidInterface: "
  275. "Concrete attribute, __qualname__")
  276. # Definition of a module somewhat like zope.interface 3.8 when it is used on Python 3.
  277. _zope38 = {
  278. 'zope': _Zope38OnPython3Module(),
  279. }
  280. # Definition of a module somewhat like zope.interface 4.0.
  281. _zope40 = {
  282. 'zope': {
  283. 'interface': {
  284. 'Interface': _SuccessInterface,
  285. 'implementer': _classSupportingImplementer,
  286. },
  287. },
  288. }
  289. class ZopeInterfaceTestsMixin(object):
  290. """
  291. Verify the C{zope.interface} fakes, only possible when a specific version of
  292. the real C{zope.interface} package is installed on the system.
  293. Subclass this and override C{install} to properly install and then remove
  294. the given version of C{zope.interface}.
  295. """
  296. def test_zope35(self):
  297. """
  298. Version 3.5 of L{zope.interface} has a C{implementer} method which
  299. cannot be used as a class decorator.
  300. """
  301. with SetAsideModule("zope"):
  302. self.install((3, 5))
  303. from zope.interface import Interface, implementer
  304. class IDummy(Interface):
  305. pass
  306. try:
  307. @implementer(IDummy)
  308. class Dummy(object):
  309. pass
  310. except TypeError as exc:
  311. self.assertEqual(
  312. "Can't use implementer with classes. "
  313. "Use one of the class-declaration functions instead.",
  314. str(exc))
  315. def test_zope36(self):
  316. """
  317. Version 3.6 of L{zope.interface} has a C{implementer} method which can
  318. be used as a class decorator.
  319. """
  320. with SetAsideModule("zope"):
  321. self.install((3, 6))
  322. from zope.interface import Interface, implementer
  323. class IDummy(Interface):
  324. pass
  325. @implementer(IDummy)
  326. class Dummy(object):
  327. pass
  328. if _PY3:
  329. def test_zope38(self):
  330. """
  331. Version 3.8 of L{zope.interface} does not even import on Python 3.
  332. """
  333. with SetAsideModule("zope"):
  334. self.install((3, 8))
  335. try:
  336. from zope import interface
  337. # It is imported just to check errors at import so we
  338. # silence the linter.
  339. interface
  340. except Exception as exc:
  341. self.assertEqual(
  342. "zope.interface.exceptions.InvalidInterface: "
  343. "Concrete attribute, __qualname__",
  344. str(exc))
  345. else:
  346. self.fail(
  347. "InvalidInterface was not raised by zope.interface import")
  348. def test_zope40(self):
  349. """
  350. Version 4.0 of L{zope.interface} can import on Python 3 and, also on
  351. Python 3, has an C{Interface} class which can be subclassed.
  352. """
  353. with SetAsideModule("zope"):
  354. self.install((4, 0))
  355. from zope.interface import Interface
  356. class IDummy(Interface):
  357. pass
  358. class FakeZopeInterfaceTests(TestCase, ZopeInterfaceTestsMixin):
  359. """
  360. Apply the zope.interface tests to the fakes implemented in this module.
  361. """
  362. versions = {
  363. (3, 5): _zope35,
  364. (3, 6): _zope36,
  365. (3, 8): _zope38,
  366. (4, 0): _zope40,
  367. }
  368. def install(self, version):
  369. """
  370. Grab one of the fake module implementations and install it into
  371. C{sys.modules} for use by the test.
  372. """
  373. _install(self.versions[version])
  374. class RealZopeInterfaceTests(TestCase, ZopeInterfaceTestsMixin):
  375. """
  376. Apply whichever tests from L{ZopeInterfaceTestsMixin} are applicable to the
  377. system-installed version of zope.interface.
  378. """
  379. def install(self, version):
  380. """
  381. Check to see if the system-installed version of zope.interface matches
  382. the version requested. If so, do nothing. If not, skip the test (if
  383. the desired version is not installed, there is no way to test its
  384. behavior). If the version of zope.interface cannot be determined
  385. (because pkg_resources is not installed), skip the test.
  386. """
  387. # Use an unrelated, but unreliable, route to try to determine what
  388. # version of zope.interface is installed on the system. It's sort of
  389. # okay to use this unreliable scheme here, since if it fails it only
  390. # means we won't be able to run the tests. Hopefully someone else
  391. # managed to run the tests somewhere else.
  392. try:
  393. import pkg_resources
  394. except ImportError as e:
  395. raise SkipTest(
  396. "Cannot determine system version of zope.interface: %s" % (e,))
  397. else:
  398. try:
  399. pkg = pkg_resources.get_distribution("zope.interface")
  400. except pkg_resources.DistributionNotFound as e:
  401. raise SkipTest(
  402. "Cannot determine system version of zope.interface: %s" % (
  403. e,))
  404. installed = pkg.version
  405. versionTuple = tuple(
  406. int(part) for part in installed.split('.')[:len(version)])
  407. if versionTuple == version:
  408. pass
  409. else:
  410. raise SkipTest("Mismatched system version of zope.interface")
  411. class OldSubprojectDeprecationBase(TestCase):
  412. """
  413. Base L{TestCase} for verifying each former subproject has a deprecated
  414. C{__version__} and a removed C{_version.py}.
  415. """
  416. subproject = None
  417. def test_deprecated(self):
  418. """
  419. The C{__version__} attribute of former subprojects is deprecated.
  420. """
  421. module = reflect.namedAny("twisted.{}".format(self.subproject))
  422. self.assertEqual(module.__version__, twisted.__version__)
  423. warningsShown = self.flushWarnings()
  424. self.assertEqual(1, len(warningsShown))
  425. self.assertEqual(
  426. "twisted.{}.__version__ was deprecated in Twisted 16.0.0: "
  427. "Use twisted.__version__ instead.".format(self.subproject),
  428. warningsShown[0]['message'])
  429. def test_noversionpy(self):
  430. """
  431. Former subprojects no longer have an importable C{_version.py}.
  432. """
  433. with self.assertRaises(AttributeError):
  434. reflect.namedAny(
  435. "twisted.{}._version".format(self.subproject))
  436. if _PY3:
  437. subprojects = ["conch", "web", "names"]
  438. else:
  439. subprojects = ["mail", "conch", "runner", "web", "words", "names", "news",
  440. "pair"]
  441. for subproject in subprojects:
  442. class SubprojectTestCase(OldSubprojectDeprecationBase):
  443. """
  444. See L{OldSubprojectDeprecationBase}.
  445. """
  446. subproject = subproject
  447. newName = subproject.title() + "VersionDeprecationTests"
  448. SubprojectTestCase.__name__ = newName
  449. if _PY3:
  450. SubprojectTestCase.__qualname__= ".".join(
  451. OldSubprojectDeprecationBase.__qualname__.split()[0:-1] +
  452. [newName])
  453. globals().update({subproject.title() +
  454. "VersionDeprecationTests": SubprojectTestCase})
  455. del SubprojectTestCase
  456. del newName
  457. del OldSubprojectDeprecationBase