__init__.py 11 KB

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