runtests.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. from functools import reduce
  2. import gc
  3. import io
  4. import locale # system locale module, not tornado.locale
  5. import logging
  6. import operator
  7. import textwrap
  8. import sys
  9. import unittest
  10. import warnings
  11. from tornado.httpclient import AsyncHTTPClient
  12. from tornado.httpserver import HTTPServer
  13. from tornado.netutil import Resolver
  14. from tornado.options import define, add_parse_callback
  15. TEST_MODULES = [
  16. "tornado.httputil.doctests",
  17. "tornado.iostream.doctests",
  18. "tornado.util.doctests",
  19. "tornado.test.asyncio_test",
  20. "tornado.test.auth_test",
  21. "tornado.test.autoreload_test",
  22. "tornado.test.concurrent_test",
  23. "tornado.test.curl_httpclient_test",
  24. "tornado.test.escape_test",
  25. "tornado.test.gen_test",
  26. "tornado.test.http1connection_test",
  27. "tornado.test.httpclient_test",
  28. "tornado.test.httpserver_test",
  29. "tornado.test.httputil_test",
  30. "tornado.test.import_test",
  31. "tornado.test.ioloop_test",
  32. "tornado.test.iostream_test",
  33. "tornado.test.locale_test",
  34. "tornado.test.locks_test",
  35. "tornado.test.netutil_test",
  36. "tornado.test.log_test",
  37. "tornado.test.options_test",
  38. "tornado.test.process_test",
  39. "tornado.test.queues_test",
  40. "tornado.test.routing_test",
  41. "tornado.test.simple_httpclient_test",
  42. "tornado.test.tcpclient_test",
  43. "tornado.test.tcpserver_test",
  44. "tornado.test.template_test",
  45. "tornado.test.testing_test",
  46. "tornado.test.twisted_test",
  47. "tornado.test.util_test",
  48. "tornado.test.web_test",
  49. "tornado.test.websocket_test",
  50. "tornado.test.windows_test",
  51. "tornado.test.wsgi_test",
  52. ]
  53. def all():
  54. return unittest.defaultTestLoader.loadTestsFromNames(TEST_MODULES)
  55. def test_runner_factory(stderr):
  56. class TornadoTextTestRunner(unittest.TextTestRunner):
  57. def __init__(self, *args, **kwargs):
  58. kwargs["stream"] = stderr
  59. super(TornadoTextTestRunner, self).__init__(*args, **kwargs)
  60. def run(self, test):
  61. result = super(TornadoTextTestRunner, self).run(test)
  62. if result.skipped:
  63. skip_reasons = set(reason for (test, reason) in result.skipped)
  64. self.stream.write(
  65. textwrap.fill(
  66. "Some tests were skipped because: %s"
  67. % ", ".join(sorted(skip_reasons))
  68. )
  69. )
  70. self.stream.write("\n")
  71. return result
  72. return TornadoTextTestRunner
  73. class LogCounter(logging.Filter):
  74. """Counts the number of WARNING or higher log records."""
  75. def __init__(self, *args, **kwargs):
  76. super(LogCounter, self).__init__(*args, **kwargs)
  77. self.info_count = self.warning_count = self.error_count = 0
  78. def filter(self, record):
  79. if record.levelno >= logging.ERROR:
  80. self.error_count += 1
  81. elif record.levelno >= logging.WARNING:
  82. self.warning_count += 1
  83. elif record.levelno >= logging.INFO:
  84. self.info_count += 1
  85. return True
  86. class CountingStderr(io.IOBase):
  87. def __init__(self, real):
  88. self.real = real
  89. self.byte_count = 0
  90. def write(self, data):
  91. self.byte_count += len(data)
  92. return self.real.write(data)
  93. def flush(self):
  94. return self.real.flush()
  95. def main():
  96. # Be strict about most warnings (This is set in our test running
  97. # scripts to catch import-time warnings, but set it again here to
  98. # be sure). This also turns on warnings that are ignored by
  99. # default, including DeprecationWarnings and python 3.2's
  100. # ResourceWarnings.
  101. warnings.filterwarnings("error")
  102. # setuptools sometimes gives ImportWarnings about things that are on
  103. # sys.path even if they're not being used.
  104. warnings.filterwarnings("ignore", category=ImportWarning)
  105. # Tornado generally shouldn't use anything deprecated, but some of
  106. # our dependencies do (last match wins).
  107. warnings.filterwarnings("ignore", category=DeprecationWarning)
  108. warnings.filterwarnings("error", category=DeprecationWarning, module=r"tornado\..*")
  109. warnings.filterwarnings("ignore", category=PendingDeprecationWarning)
  110. warnings.filterwarnings(
  111. "error", category=PendingDeprecationWarning, module=r"tornado\..*"
  112. )
  113. # The unittest module is aggressive about deprecating redundant methods,
  114. # leaving some without non-deprecated spellings that work on both
  115. # 2.7 and 3.2
  116. warnings.filterwarnings(
  117. "ignore", category=DeprecationWarning, message="Please use assert.* instead"
  118. )
  119. warnings.filterwarnings(
  120. "ignore",
  121. category=PendingDeprecationWarning,
  122. message="Please use assert.* instead",
  123. )
  124. # Twisted 15.0.0 triggers some warnings on py3 with -bb.
  125. warnings.filterwarnings("ignore", category=BytesWarning, module=r"twisted\..*")
  126. if (3,) < sys.version_info < (3, 6):
  127. # Prior to 3.6, async ResourceWarnings were rather noisy
  128. # and even
  129. # `python3.4 -W error -c 'import asyncio; asyncio.get_event_loop()'`
  130. # would generate a warning.
  131. warnings.filterwarnings(
  132. "ignore", category=ResourceWarning, module=r"asyncio\..*"
  133. )
  134. # This deprecation warning is introduced in Python 3.8 and is
  135. # triggered by pycurl. Unforunately, because it is raised in the C
  136. # layer it can't be filtered by module and we must match the
  137. # message text instead (Tornado's C module uses PY_SSIZE_T_CLEAN
  138. # so it's not at risk of running into this issue).
  139. warnings.filterwarnings(
  140. "ignore",
  141. category=DeprecationWarning,
  142. message="PY_SSIZE_T_CLEAN will be required",
  143. )
  144. logging.getLogger("tornado.access").setLevel(logging.CRITICAL)
  145. define(
  146. "httpclient",
  147. type=str,
  148. default=None,
  149. callback=lambda s: AsyncHTTPClient.configure(
  150. s, defaults=dict(allow_ipv6=False)
  151. ),
  152. )
  153. define("httpserver", type=str, default=None, callback=HTTPServer.configure)
  154. define("resolver", type=str, default=None, callback=Resolver.configure)
  155. define(
  156. "debug_gc",
  157. type=str,
  158. multiple=True,
  159. help="A comma-separated list of gc module debug constants, "
  160. "e.g. DEBUG_STATS or DEBUG_COLLECTABLE,DEBUG_OBJECTS",
  161. callback=lambda values: gc.set_debug(
  162. reduce(operator.or_, (getattr(gc, v) for v in values))
  163. ),
  164. )
  165. def set_locale(x):
  166. locale.setlocale(locale.LC_ALL, x)
  167. define("locale", type=str, default=None, callback=set_locale)
  168. log_counter = LogCounter()
  169. add_parse_callback(lambda: logging.getLogger().handlers[0].addFilter(log_counter))
  170. # Certain errors (especially "unclosed resource" errors raised in
  171. # destructors) go directly to stderr instead of logging. Count
  172. # anything written by anything but the test runner as an error.
  173. orig_stderr = sys.stderr
  174. counting_stderr = CountingStderr(orig_stderr)
  175. sys.stderr = counting_stderr # type: ignore
  176. import tornado.testing
  177. kwargs = {}
  178. # HACK: unittest.main will make its own changes to the warning
  179. # configuration, which may conflict with the settings above
  180. # or command-line flags like -bb. Passing warnings=False
  181. # suppresses this behavior, although this looks like an implementation
  182. # detail. http://bugs.python.org/issue15626
  183. kwargs["warnings"] = False
  184. kwargs["testRunner"] = test_runner_factory(orig_stderr)
  185. try:
  186. tornado.testing.main(**kwargs)
  187. finally:
  188. # The tests should run clean; consider it a failure if they
  189. # logged anything at info level or above.
  190. if (
  191. log_counter.info_count > 0
  192. or log_counter.warning_count > 0
  193. or log_counter.error_count > 0
  194. or counting_stderr.byte_count > 0
  195. ):
  196. logging.error(
  197. "logged %d infos, %d warnings, %d errors, and %d bytes to stderr",
  198. log_counter.info_count,
  199. log_counter.warning_count,
  200. log_counter.error_count,
  201. counting_stderr.byte_count,
  202. )
  203. sys.exit(1)
  204. if __name__ == "__main__":
  205. main()