__init__.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. """Tests for the 'setuptools' package"""
  2. import sys
  3. import os
  4. import distutils.core
  5. import distutils.cmd
  6. from distutils.errors import DistutilsOptionError, DistutilsPlatformError
  7. from distutils.errors import DistutilsSetupError
  8. from distutils.core import Extension
  9. from distutils.version import LooseVersion
  10. from setuptools.extern import six
  11. import pytest
  12. import setuptools.dist
  13. import setuptools.depends as dep
  14. from setuptools import Feature
  15. from setuptools.depends import Require
  16. c_type = os.environ.get("LC_CTYPE", os.environ.get("LC_ALL"))
  17. is_ascii = c_type in ("C", "POSIX")
  18. fail_on_ascii = pytest.mark.xfail(is_ascii, reason="Test fails in this locale")
  19. def makeSetup(**args):
  20. """Return distribution from 'setup(**args)', without executing commands"""
  21. distutils.core._setup_stop_after = "commandline"
  22. # Don't let system command line leak into tests!
  23. args.setdefault('script_args',['install'])
  24. try:
  25. return setuptools.setup(**args)
  26. finally:
  27. distutils.core._setup_stop_after = None
  28. needs_bytecode = pytest.mark.skipif(
  29. not hasattr(dep, 'get_module_constant'),
  30. reason="bytecode support not available",
  31. )
  32. class TestDepends:
  33. def testExtractConst(self):
  34. if not hasattr(dep, 'extract_constant'):
  35. # skip on non-bytecode platforms
  36. return
  37. def f1():
  38. global x, y, z
  39. x = "test"
  40. y = z
  41. fc = six.get_function_code(f1)
  42. # unrecognized name
  43. assert dep.extract_constant(fc,'q', -1) is None
  44. # constant assigned
  45. dep.extract_constant(fc,'x', -1) == "test"
  46. # expression assigned
  47. dep.extract_constant(fc,'y', -1) == -1
  48. # recognized name, not assigned
  49. dep.extract_constant(fc,'z', -1) is None
  50. def testFindModule(self):
  51. with pytest.raises(ImportError):
  52. dep.find_module('no-such.-thing')
  53. with pytest.raises(ImportError):
  54. dep.find_module('setuptools.non-existent')
  55. f,p,i = dep.find_module('setuptools.tests')
  56. f.close()
  57. @needs_bytecode
  58. def testModuleExtract(self):
  59. from email import __version__
  60. assert dep.get_module_constant('email','__version__') == __version__
  61. assert dep.get_module_constant('sys','version') == sys.version
  62. assert dep.get_module_constant('setuptools.tests','__doc__') == __doc__
  63. @needs_bytecode
  64. def testRequire(self):
  65. req = Require('Email','1.0.3','email')
  66. assert req.name == 'Email'
  67. assert req.module == 'email'
  68. assert req.requested_version == '1.0.3'
  69. assert req.attribute == '__version__'
  70. assert req.full_name() == 'Email-1.0.3'
  71. from email import __version__
  72. assert req.get_version() == __version__
  73. assert req.version_ok('1.0.9')
  74. assert not req.version_ok('0.9.1')
  75. assert not req.version_ok('unknown')
  76. assert req.is_present()
  77. assert req.is_current()
  78. req = Require('Email 3000','03000','email',format=LooseVersion)
  79. assert req.is_present()
  80. assert not req.is_current()
  81. assert not req.version_ok('unknown')
  82. req = Require('Do-what-I-mean','1.0','d-w-i-m')
  83. assert not req.is_present()
  84. assert not req.is_current()
  85. req = Require('Tests', None, 'tests', homepage="http://example.com")
  86. assert req.format is None
  87. assert req.attribute is None
  88. assert req.requested_version is None
  89. assert req.full_name() == 'Tests'
  90. assert req.homepage == 'http://example.com'
  91. paths = [os.path.dirname(p) for p in __path__]
  92. assert req.is_present(paths)
  93. assert req.is_current(paths)
  94. class TestDistro:
  95. def setup_method(self, method):
  96. self.e1 = Extension('bar.ext',['bar.c'])
  97. self.e2 = Extension('c.y', ['y.c'])
  98. self.dist = makeSetup(
  99. packages=['a', 'a.b', 'a.b.c', 'b', 'c'],
  100. py_modules=['b.d','x'],
  101. ext_modules = (self.e1, self.e2),
  102. package_dir = {},
  103. )
  104. def testDistroType(self):
  105. assert isinstance(self.dist,setuptools.dist.Distribution)
  106. def testExcludePackage(self):
  107. self.dist.exclude_package('a')
  108. assert self.dist.packages == ['b','c']
  109. self.dist.exclude_package('b')
  110. assert self.dist.packages == ['c']
  111. assert self.dist.py_modules == ['x']
  112. assert self.dist.ext_modules == [self.e1, self.e2]
  113. self.dist.exclude_package('c')
  114. assert self.dist.packages == []
  115. assert self.dist.py_modules == ['x']
  116. assert self.dist.ext_modules == [self.e1]
  117. # test removals from unspecified options
  118. makeSetup().exclude_package('x')
  119. def testIncludeExclude(self):
  120. # remove an extension
  121. self.dist.exclude(ext_modules=[self.e1])
  122. assert self.dist.ext_modules == [self.e2]
  123. # add it back in
  124. self.dist.include(ext_modules=[self.e1])
  125. assert self.dist.ext_modules == [self.e2, self.e1]
  126. # should not add duplicate
  127. self.dist.include(ext_modules=[self.e1])
  128. assert self.dist.ext_modules == [self.e2, self.e1]
  129. def testExcludePackages(self):
  130. self.dist.exclude(packages=['c','b','a'])
  131. assert self.dist.packages == []
  132. assert self.dist.py_modules == ['x']
  133. assert self.dist.ext_modules == [self.e1]
  134. def testEmpty(self):
  135. dist = makeSetup()
  136. dist.include(packages=['a'], py_modules=['b'], ext_modules=[self.e2])
  137. dist = makeSetup()
  138. dist.exclude(packages=['a'], py_modules=['b'], ext_modules=[self.e2])
  139. def testContents(self):
  140. assert self.dist.has_contents_for('a')
  141. self.dist.exclude_package('a')
  142. assert not self.dist.has_contents_for('a')
  143. assert self.dist.has_contents_for('b')
  144. self.dist.exclude_package('b')
  145. assert not self.dist.has_contents_for('b')
  146. assert self.dist.has_contents_for('c')
  147. self.dist.exclude_package('c')
  148. assert not self.dist.has_contents_for('c')
  149. def testInvalidIncludeExclude(self):
  150. with pytest.raises(DistutilsSetupError):
  151. self.dist.include(nonexistent_option='x')
  152. with pytest.raises(DistutilsSetupError):
  153. self.dist.exclude(nonexistent_option='x')
  154. with pytest.raises(DistutilsSetupError):
  155. self.dist.include(packages={'x':'y'})
  156. with pytest.raises(DistutilsSetupError):
  157. self.dist.exclude(packages={'x':'y'})
  158. with pytest.raises(DistutilsSetupError):
  159. self.dist.include(ext_modules={'x':'y'})
  160. with pytest.raises(DistutilsSetupError):
  161. self.dist.exclude(ext_modules={'x':'y'})
  162. with pytest.raises(DistutilsSetupError):
  163. self.dist.include(package_dir=['q'])
  164. with pytest.raises(DistutilsSetupError):
  165. self.dist.exclude(package_dir=['q'])
  166. class TestFeatures:
  167. def setup_method(self, method):
  168. self.req = Require('Distutils','1.0.3','distutils')
  169. self.dist = makeSetup(
  170. features={
  171. 'foo': Feature("foo",standard=True,require_features=['baz',self.req]),
  172. 'bar': Feature("bar", standard=True, packages=['pkg.bar'],
  173. py_modules=['bar_et'], remove=['bar.ext'],
  174. ),
  175. 'baz': Feature(
  176. "baz", optional=False, packages=['pkg.baz'],
  177. scripts = ['scripts/baz_it'],
  178. libraries=[('libfoo','foo/foofoo.c')]
  179. ),
  180. 'dwim': Feature("DWIM", available=False, remove='bazish'),
  181. },
  182. script_args=['--without-bar', 'install'],
  183. packages = ['pkg.bar', 'pkg.foo'],
  184. py_modules = ['bar_et', 'bazish'],
  185. ext_modules = [Extension('bar.ext',['bar.c'])]
  186. )
  187. def testDefaults(self):
  188. assert not Feature(
  189. "test",standard=True,remove='x',available=False
  190. ).include_by_default()
  191. assert Feature("test",standard=True,remove='x').include_by_default()
  192. # Feature must have either kwargs, removes, or require_features
  193. with pytest.raises(DistutilsSetupError):
  194. Feature("test")
  195. def testAvailability(self):
  196. with pytest.raises(DistutilsPlatformError):
  197. self.dist.features['dwim'].include_in(self.dist)
  198. def testFeatureOptions(self):
  199. dist = self.dist
  200. assert (
  201. ('with-dwim',None,'include DWIM') in dist.feature_options
  202. )
  203. assert (
  204. ('without-dwim',None,'exclude DWIM (default)') in dist.feature_options
  205. )
  206. assert (
  207. ('with-bar',None,'include bar (default)') in dist.feature_options
  208. )
  209. assert (
  210. ('without-bar',None,'exclude bar') in dist.feature_options
  211. )
  212. assert dist.feature_negopt['without-foo'] == 'with-foo'
  213. assert dist.feature_negopt['without-bar'] == 'with-bar'
  214. assert dist.feature_negopt['without-dwim'] == 'with-dwim'
  215. assert (not 'without-baz' in dist.feature_negopt)
  216. def testUseFeatures(self):
  217. dist = self.dist
  218. assert dist.with_foo == 1
  219. assert dist.with_bar == 0
  220. assert dist.with_baz == 1
  221. assert (not 'bar_et' in dist.py_modules)
  222. assert (not 'pkg.bar' in dist.packages)
  223. assert ('pkg.baz' in dist.packages)
  224. assert ('scripts/baz_it' in dist.scripts)
  225. assert (('libfoo','foo/foofoo.c') in dist.libraries)
  226. assert dist.ext_modules == []
  227. assert dist.require_features == [self.req]
  228. # If we ask for bar, it should fail because we explicitly disabled
  229. # it on the command line
  230. with pytest.raises(DistutilsOptionError):
  231. dist.include_feature('bar')
  232. def testFeatureWithInvalidRemove(self):
  233. with pytest.raises(SystemExit):
  234. makeSetup(features={'x':Feature('x', remove='y')})
  235. class TestCommandTests:
  236. def testTestIsCommand(self):
  237. test_cmd = makeSetup().get_command_obj('test')
  238. assert (isinstance(test_cmd, distutils.cmd.Command))
  239. def testLongOptSuiteWNoDefault(self):
  240. ts1 = makeSetup(script_args=['test','--test-suite=foo.tests.suite'])
  241. ts1 = ts1.get_command_obj('test')
  242. ts1.ensure_finalized()
  243. assert ts1.test_suite == 'foo.tests.suite'
  244. def testDefaultSuite(self):
  245. ts2 = makeSetup(test_suite='bar.tests.suite').get_command_obj('test')
  246. ts2.ensure_finalized()
  247. assert ts2.test_suite == 'bar.tests.suite'
  248. def testDefaultWModuleOnCmdLine(self):
  249. ts3 = makeSetup(
  250. test_suite='bar.tests',
  251. script_args=['test','-m','foo.tests']
  252. ).get_command_obj('test')
  253. ts3.ensure_finalized()
  254. assert ts3.test_module == 'foo.tests'
  255. assert ts3.test_suite == 'foo.tests.test_suite'
  256. def testConflictingOptions(self):
  257. ts4 = makeSetup(
  258. script_args=['test','-m','bar.tests', '-s','foo.tests.suite']
  259. ).get_command_obj('test')
  260. with pytest.raises(DistutilsOptionError):
  261. ts4.ensure_finalized()
  262. def testNoSuite(self):
  263. ts5 = makeSetup().get_command_obj('test')
  264. ts5.ensure_finalized()
  265. assert ts5.test_suite == None