test.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. import os
  2. import operator
  3. import sys
  4. import contextlib
  5. import itertools
  6. import unittest
  7. from distutils.errors import DistutilsError, DistutilsOptionError
  8. from distutils import log
  9. from unittest import TestLoader
  10. from setuptools.extern import six
  11. from setuptools.extern.six.moves import map, filter
  12. from pkg_resources import (resource_listdir, resource_exists, normalize_path,
  13. working_set, _namespace_packages, evaluate_marker,
  14. add_activation_listener, require, EntryPoint)
  15. from setuptools import Command
  16. from .build_py import _unique_everseen
  17. __metaclass__ = type
  18. class ScanningLoader(TestLoader):
  19. def __init__(self):
  20. TestLoader.__init__(self)
  21. self._visited = set()
  22. def loadTestsFromModule(self, module, pattern=None):
  23. """Return a suite of all tests cases contained in the given module
  24. If the module is a package, load tests from all the modules in it.
  25. If the module has an ``additional_tests`` function, call it and add
  26. the return value to the tests.
  27. """
  28. if module in self._visited:
  29. return None
  30. self._visited.add(module)
  31. tests = []
  32. tests.append(TestLoader.loadTestsFromModule(self, module))
  33. if hasattr(module, "additional_tests"):
  34. tests.append(module.additional_tests())
  35. if hasattr(module, '__path__'):
  36. for file in resource_listdir(module.__name__, ''):
  37. if file.endswith('.py') and file != '__init__.py':
  38. submodule = module.__name__ + '.' + file[:-3]
  39. else:
  40. if resource_exists(module.__name__, file + '/__init__.py'):
  41. submodule = module.__name__ + '.' + file
  42. else:
  43. continue
  44. tests.append(self.loadTestsFromName(submodule))
  45. if len(tests) != 1:
  46. return self.suiteClass(tests)
  47. else:
  48. return tests[0] # don't create a nested suite for only one return
  49. # adapted from jaraco.classes.properties:NonDataProperty
  50. class NonDataProperty:
  51. def __init__(self, fget):
  52. self.fget = fget
  53. def __get__(self, obj, objtype=None):
  54. if obj is None:
  55. return self
  56. return self.fget(obj)
  57. class test(Command):
  58. """Command to run unit tests after in-place build"""
  59. description = "run unit tests after in-place build (deprecated)"
  60. user_options = [
  61. ('test-module=', 'm', "Run 'test_suite' in specified module"),
  62. ('test-suite=', 's',
  63. "Run single test, case or suite (e.g. 'module.test_suite')"),
  64. ('test-runner=', 'r', "Test runner to use"),
  65. ]
  66. def initialize_options(self):
  67. self.test_suite = None
  68. self.test_module = None
  69. self.test_loader = None
  70. self.test_runner = None
  71. def finalize_options(self):
  72. if self.test_suite and self.test_module:
  73. msg = "You may specify a module or a suite, but not both"
  74. raise DistutilsOptionError(msg)
  75. if self.test_suite is None:
  76. if self.test_module is None:
  77. self.test_suite = self.distribution.test_suite
  78. else:
  79. self.test_suite = self.test_module + ".test_suite"
  80. if self.test_loader is None:
  81. self.test_loader = getattr(self.distribution, 'test_loader', None)
  82. if self.test_loader is None:
  83. self.test_loader = "setuptools.command.test:ScanningLoader"
  84. if self.test_runner is None:
  85. self.test_runner = getattr(self.distribution, 'test_runner', None)
  86. @NonDataProperty
  87. def test_args(self):
  88. return list(self._test_args())
  89. def _test_args(self):
  90. if not self.test_suite and sys.version_info >= (2, 7):
  91. yield 'discover'
  92. if self.verbose:
  93. yield '--verbose'
  94. if self.test_suite:
  95. yield self.test_suite
  96. def with_project_on_sys_path(self, func):
  97. """
  98. Backward compatibility for project_on_sys_path context.
  99. """
  100. with self.project_on_sys_path():
  101. func()
  102. @contextlib.contextmanager
  103. def project_on_sys_path(self, include_dists=[]):
  104. with_2to3 = not six.PY2 and getattr(self.distribution, 'use_2to3', False)
  105. if with_2to3:
  106. # If we run 2to3 we can not do this inplace:
  107. # Ensure metadata is up-to-date
  108. self.reinitialize_command('build_py', inplace=0)
  109. self.run_command('build_py')
  110. bpy_cmd = self.get_finalized_command("build_py")
  111. build_path = normalize_path(bpy_cmd.build_lib)
  112. # Build extensions
  113. self.reinitialize_command('egg_info', egg_base=build_path)
  114. self.run_command('egg_info')
  115. self.reinitialize_command('build_ext', inplace=0)
  116. self.run_command('build_ext')
  117. else:
  118. # Without 2to3 inplace works fine:
  119. self.run_command('egg_info')
  120. # Build extensions in-place
  121. self.reinitialize_command('build_ext', inplace=1)
  122. self.run_command('build_ext')
  123. ei_cmd = self.get_finalized_command("egg_info")
  124. old_path = sys.path[:]
  125. old_modules = sys.modules.copy()
  126. try:
  127. project_path = normalize_path(ei_cmd.egg_base)
  128. sys.path.insert(0, project_path)
  129. working_set.__init__()
  130. add_activation_listener(lambda dist: dist.activate())
  131. require('%s==%s' % (ei_cmd.egg_name, ei_cmd.egg_version))
  132. with self.paths_on_pythonpath([project_path]):
  133. yield
  134. finally:
  135. sys.path[:] = old_path
  136. sys.modules.clear()
  137. sys.modules.update(old_modules)
  138. working_set.__init__()
  139. @staticmethod
  140. @contextlib.contextmanager
  141. def paths_on_pythonpath(paths):
  142. """
  143. Add the indicated paths to the head of the PYTHONPATH environment
  144. variable so that subprocesses will also see the packages at
  145. these paths.
  146. Do this in a context that restores the value on exit.
  147. """
  148. nothing = object()
  149. orig_pythonpath = os.environ.get('PYTHONPATH', nothing)
  150. current_pythonpath = os.environ.get('PYTHONPATH', '')
  151. try:
  152. prefix = os.pathsep.join(_unique_everseen(paths))
  153. to_join = filter(None, [prefix, current_pythonpath])
  154. new_path = os.pathsep.join(to_join)
  155. if new_path:
  156. os.environ['PYTHONPATH'] = new_path
  157. yield
  158. finally:
  159. if orig_pythonpath is nothing:
  160. os.environ.pop('PYTHONPATH', None)
  161. else:
  162. os.environ['PYTHONPATH'] = orig_pythonpath
  163. @staticmethod
  164. def install_dists(dist):
  165. """
  166. Install the requirements indicated by self.distribution and
  167. return an iterable of the dists that were built.
  168. """
  169. ir_d = dist.fetch_build_eggs(dist.install_requires)
  170. tr_d = dist.fetch_build_eggs(dist.tests_require or [])
  171. er_d = dist.fetch_build_eggs(
  172. v for k, v in dist.extras_require.items()
  173. if k.startswith(':') and evaluate_marker(k[1:])
  174. )
  175. return itertools.chain(ir_d, tr_d, er_d)
  176. def run(self):
  177. self.announce(
  178. "WARNING: Testing via this command is deprecated and will be "
  179. "removed in a future version. Users looking for a generic test "
  180. "entry point independent of test runner are encouraged to use "
  181. "tox.",
  182. log.WARN,
  183. )
  184. installed_dists = self.install_dists(self.distribution)
  185. cmd = ' '.join(self._argv)
  186. if self.dry_run:
  187. self.announce('skipping "%s" (dry run)' % cmd)
  188. return
  189. self.announce('running "%s"' % cmd)
  190. paths = map(operator.attrgetter('location'), installed_dists)
  191. with self.paths_on_pythonpath(paths):
  192. with self.project_on_sys_path():
  193. self.run_tests()
  194. def run_tests(self):
  195. # Purge modules under test from sys.modules. The test loader will
  196. # re-import them from the build location. Required when 2to3 is used
  197. # with namespace packages.
  198. if not six.PY2 and getattr(self.distribution, 'use_2to3', False):
  199. module = self.test_suite.split('.')[0]
  200. if module in _namespace_packages:
  201. del_modules = []
  202. if module in sys.modules:
  203. del_modules.append(module)
  204. module += '.'
  205. for name in sys.modules:
  206. if name.startswith(module):
  207. del_modules.append(name)
  208. list(map(sys.modules.__delitem__, del_modules))
  209. test = unittest.main(
  210. None, None, self._argv,
  211. testLoader=self._resolve_as_ep(self.test_loader),
  212. testRunner=self._resolve_as_ep(self.test_runner),
  213. exit=False,
  214. )
  215. if not test.result.wasSuccessful():
  216. msg = 'Test failed: %s' % test.result
  217. self.announce(msg, log.ERROR)
  218. raise DistutilsError(msg)
  219. @property
  220. def _argv(self):
  221. return ['unittest'] + self.test_args
  222. @staticmethod
  223. def _resolve_as_ep(val):
  224. """
  225. Load the indicated attribute value, called, as a as if it were
  226. specified as an entry point.
  227. """
  228. if val is None:
  229. return
  230. parsed = EntryPoint.parse("x=" + val)
  231. return parsed.resolve()()