test_setuptools.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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
  7. from distutils.errors import DistutilsSetupError
  8. from distutils.core import Extension
  9. from distutils.version import LooseVersion
  10. import pytest
  11. import setuptools
  12. import setuptools.dist
  13. import setuptools.depends as dep
  14. from setuptools.depends import Require
  15. def makeSetup(**args):
  16. """Return distribution from 'setup(**args)', without executing commands"""
  17. distutils.core._setup_stop_after = "commandline"
  18. # Don't let system command line leak into tests!
  19. args.setdefault('script_args', ['install'])
  20. try:
  21. return setuptools.setup(**args)
  22. finally:
  23. distutils.core._setup_stop_after = None
  24. needs_bytecode = pytest.mark.skipif(
  25. not hasattr(dep, 'get_module_constant'),
  26. reason="bytecode support not available",
  27. )
  28. class TestDepends:
  29. def testExtractConst(self):
  30. if not hasattr(dep, 'extract_constant'):
  31. # skip on non-bytecode platforms
  32. return
  33. def f1():
  34. global x, y, z
  35. x = "test"
  36. y = z
  37. fc = f1.__code__
  38. # unrecognized name
  39. assert dep.extract_constant(fc, 'q', -1) is None
  40. # constant assigned
  41. dep.extract_constant(fc, 'x', -1) == "test"
  42. # expression assigned
  43. dep.extract_constant(fc, 'y', -1) == -1
  44. # recognized name, not assigned
  45. dep.extract_constant(fc, 'z', -1) is None
  46. def testFindModule(self):
  47. with pytest.raises(ImportError):
  48. dep.find_module('no-such.-thing')
  49. with pytest.raises(ImportError):
  50. dep.find_module('setuptools.non-existent')
  51. f, p, i = dep.find_module('setuptools.tests')
  52. f.close()
  53. @needs_bytecode
  54. def testModuleExtract(self):
  55. from json import __version__
  56. assert dep.get_module_constant('json', '__version__') == __version__
  57. assert dep.get_module_constant('sys', 'version') == sys.version
  58. assert dep.get_module_constant(
  59. 'setuptools.tests.test_setuptools', '__doc__') == __doc__
  60. @needs_bytecode
  61. def testRequire(self):
  62. req = Require('Json', '1.0.3', 'json')
  63. assert req.name == 'Json'
  64. assert req.module == 'json'
  65. assert req.requested_version == '1.0.3'
  66. assert req.attribute == '__version__'
  67. assert req.full_name() == 'Json-1.0.3'
  68. from json 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('Json 3000', '03000', 'json', 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. @needs_bytecode
  83. def test_require_present(self):
  84. # In #1896, this test was failing for months with the only
  85. # complaint coming from test runners (not end users).
  86. # TODO: Evaluate if this code is needed at all.
  87. req = Require('Tests', None, 'tests', homepage="http://example.com")
  88. assert req.format is None
  89. assert req.attribute is None
  90. assert req.requested_version is None
  91. assert req.full_name() == 'Tests'
  92. assert req.homepage == 'http://example.com'
  93. from setuptools.tests import __path__
  94. paths = [os.path.dirname(p) for p in __path__]
  95. assert req.is_present(paths)
  96. assert req.is_current(paths)
  97. class TestDistro:
  98. def setup_method(self, method):
  99. self.e1 = Extension('bar.ext', ['bar.c'])
  100. self.e2 = Extension('c.y', ['y.c'])
  101. self.dist = makeSetup(
  102. packages=['a', 'a.b', 'a.b.c', 'b', 'c'],
  103. py_modules=['b.d', 'x'],
  104. ext_modules=(self.e1, self.e2),
  105. package_dir={},
  106. )
  107. def testDistroType(self):
  108. assert isinstance(self.dist, setuptools.dist.Distribution)
  109. def testExcludePackage(self):
  110. self.dist.exclude_package('a')
  111. assert self.dist.packages == ['b', 'c']
  112. self.dist.exclude_package('b')
  113. assert self.dist.packages == ['c']
  114. assert self.dist.py_modules == ['x']
  115. assert self.dist.ext_modules == [self.e1, self.e2]
  116. self.dist.exclude_package('c')
  117. assert self.dist.packages == []
  118. assert self.dist.py_modules == ['x']
  119. assert self.dist.ext_modules == [self.e1]
  120. # test removals from unspecified options
  121. makeSetup().exclude_package('x')
  122. def testIncludeExclude(self):
  123. # remove an extension
  124. self.dist.exclude(ext_modules=[self.e1])
  125. assert self.dist.ext_modules == [self.e2]
  126. # add it back in
  127. self.dist.include(ext_modules=[self.e1])
  128. assert self.dist.ext_modules == [self.e2, self.e1]
  129. # should not add duplicate
  130. self.dist.include(ext_modules=[self.e1])
  131. assert self.dist.ext_modules == [self.e2, self.e1]
  132. def testExcludePackages(self):
  133. self.dist.exclude(packages=['c', 'b', 'a'])
  134. assert self.dist.packages == []
  135. assert self.dist.py_modules == ['x']
  136. assert self.dist.ext_modules == [self.e1]
  137. def testEmpty(self):
  138. dist = makeSetup()
  139. dist.include(packages=['a'], py_modules=['b'], ext_modules=[self.e2])
  140. dist = makeSetup()
  141. dist.exclude(packages=['a'], py_modules=['b'], ext_modules=[self.e2])
  142. def testContents(self):
  143. assert self.dist.has_contents_for('a')
  144. self.dist.exclude_package('a')
  145. assert not self.dist.has_contents_for('a')
  146. assert self.dist.has_contents_for('b')
  147. self.dist.exclude_package('b')
  148. assert not self.dist.has_contents_for('b')
  149. assert self.dist.has_contents_for('c')
  150. self.dist.exclude_package('c')
  151. assert not self.dist.has_contents_for('c')
  152. def testInvalidIncludeExclude(self):
  153. with pytest.raises(DistutilsSetupError):
  154. self.dist.include(nonexistent_option='x')
  155. with pytest.raises(DistutilsSetupError):
  156. self.dist.exclude(nonexistent_option='x')
  157. with pytest.raises(DistutilsSetupError):
  158. self.dist.include(packages={'x': 'y'})
  159. with pytest.raises(DistutilsSetupError):
  160. self.dist.exclude(packages={'x': 'y'})
  161. with pytest.raises(DistutilsSetupError):
  162. self.dist.include(ext_modules={'x': 'y'})
  163. with pytest.raises(DistutilsSetupError):
  164. self.dist.exclude(ext_modules={'x': 'y'})
  165. with pytest.raises(DistutilsSetupError):
  166. self.dist.include(package_dir=['q'])
  167. with pytest.raises(DistutilsSetupError):
  168. self.dist.exclude(package_dir=['q'])
  169. class TestCommandTests:
  170. def testTestIsCommand(self):
  171. test_cmd = makeSetup().get_command_obj('test')
  172. assert (isinstance(test_cmd, distutils.cmd.Command))
  173. def testLongOptSuiteWNoDefault(self):
  174. ts1 = makeSetup(script_args=['test', '--test-suite=foo.tests.suite'])
  175. ts1 = ts1.get_command_obj('test')
  176. ts1.ensure_finalized()
  177. assert ts1.test_suite == 'foo.tests.suite'
  178. def testDefaultSuite(self):
  179. ts2 = makeSetup(test_suite='bar.tests.suite').get_command_obj('test')
  180. ts2.ensure_finalized()
  181. assert ts2.test_suite == 'bar.tests.suite'
  182. def testDefaultWModuleOnCmdLine(self):
  183. ts3 = makeSetup(
  184. test_suite='bar.tests',
  185. script_args=['test', '-m', 'foo.tests']
  186. ).get_command_obj('test')
  187. ts3.ensure_finalized()
  188. assert ts3.test_module == 'foo.tests'
  189. assert ts3.test_suite == 'foo.tests.test_suite'
  190. def testConflictingOptions(self):
  191. ts4 = makeSetup(
  192. script_args=['test', '-m', 'bar.tests', '-s', 'foo.tests.suite']
  193. ).get_command_obj('test')
  194. with pytest.raises(DistutilsOptionError):
  195. ts4.ensure_finalized()
  196. def testNoSuite(self):
  197. ts5 = makeSetup().get_command_obj('test')
  198. ts5.ensure_finalized()
  199. assert ts5.test_suite is None
  200. @pytest.fixture
  201. def example_source(tmpdir):
  202. tmpdir.mkdir('foo')
  203. (tmpdir / 'foo/bar.py').write('')
  204. (tmpdir / 'readme.txt').write('')
  205. return tmpdir
  206. def test_findall(example_source):
  207. found = list(setuptools.findall(str(example_source)))
  208. expected = ['readme.txt', 'foo/bar.py']
  209. expected = [example_source.join(fn) for fn in expected]
  210. assert found == expected
  211. def test_findall_curdir(example_source):
  212. with example_source.as_cwd():
  213. found = list(setuptools.findall())
  214. expected = ['readme.txt', os.path.join('foo', 'bar.py')]
  215. assert found == expected
  216. @pytest.fixture
  217. def can_symlink(tmpdir):
  218. """
  219. Skip if cannot create a symbolic link
  220. """
  221. link_fn = 'link'
  222. target_fn = 'target'
  223. try:
  224. os.symlink(target_fn, link_fn)
  225. except (OSError, NotImplementedError, AttributeError):
  226. pytest.skip("Cannot create symbolic links")
  227. os.remove(link_fn)
  228. def test_findall_missing_symlink(tmpdir, can_symlink):
  229. with tmpdir.as_cwd():
  230. os.symlink('foo', 'bar')
  231. found = list(setuptools.findall())
  232. assert found == []