test_dist.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. import os
  2. import re
  3. import urllib.parse
  4. import urllib.request
  5. import pytest
  6. from setuptools import Distribution
  7. from setuptools.dist import check_package_data, check_specifier
  8. from .fixtures import make_trivial_sdist
  9. from .test_find_packages import ensure_files
  10. from .textwrap import DALS
  11. from distutils.errors import DistutilsSetupError
  12. def test_dist_fetch_build_egg(tmpdir, setuptools_wheel):
  13. """
  14. Check multiple calls to `Distribution.fetch_build_egg` work as expected.
  15. """
  16. index = tmpdir.mkdir('index')
  17. index_url = urllib.parse.urljoin('file://', urllib.request.pathname2url(str(index)))
  18. def sdist_with_index(distname, version):
  19. dist_dir = index.mkdir(distname)
  20. dist_sdist = f'{distname}-{version}.tar.gz'
  21. make_trivial_sdist(
  22. str(dist_dir.join(dist_sdist)), distname, version, setuptools_wheel
  23. )
  24. with dist_dir.join('index.html').open('w') as fp:
  25. fp.write(
  26. DALS(
  27. """
  28. <!DOCTYPE html><html><body>
  29. <a href="{dist_sdist}" rel="internal">{dist_sdist}</a><br/>
  30. </body></html>
  31. """
  32. ).format(dist_sdist=dist_sdist)
  33. )
  34. sdist_with_index('barbazquux', '3.2.0')
  35. sdist_with_index('barbazquux-runner', '2.11.1')
  36. with tmpdir.join('setup.cfg').open('w') as fp:
  37. fp.write(
  38. DALS(
  39. """
  40. [easy_install]
  41. index_url = {index_url}
  42. """
  43. ).format(index_url=index_url)
  44. )
  45. reqs = """
  46. barbazquux-runner
  47. barbazquux
  48. """.split()
  49. with tmpdir.as_cwd():
  50. dist = Distribution()
  51. dist.parse_config_files()
  52. resolved_dists = [dist.fetch_build_egg(r) for r in reqs]
  53. assert [dist.name for dist in resolved_dists if dist] == reqs
  54. EXAMPLE_BASE_INFO = dict(
  55. name="package",
  56. version="0.0.1",
  57. author="Foo Bar",
  58. author_email="foo@bar.net",
  59. long_description="Long\ndescription",
  60. description="Short description",
  61. keywords=["one", "two"],
  62. )
  63. def test_provides_extras_deterministic_order():
  64. attrs = dict(extras_require=dict(a=['foo'], b=['bar']))
  65. dist = Distribution(attrs)
  66. assert list(dist.metadata.provides_extras) == ['a', 'b']
  67. attrs['extras_require'] = dict(reversed(attrs['extras_require'].items()))
  68. dist = Distribution(attrs)
  69. assert list(dist.metadata.provides_extras) == ['b', 'a']
  70. CHECK_PACKAGE_DATA_TESTS = (
  71. # Valid.
  72. (
  73. {
  74. '': ['*.txt', '*.rst'],
  75. 'hello': ['*.msg'],
  76. },
  77. None,
  78. ),
  79. # Not a dictionary.
  80. (
  81. (
  82. ('', ['*.txt', '*.rst']),
  83. ('hello', ['*.msg']),
  84. ),
  85. (
  86. "'package_data' must be a dictionary mapping package"
  87. " names to lists of string wildcard patterns"
  88. ),
  89. ),
  90. # Invalid key type.
  91. (
  92. {
  93. 400: ['*.txt', '*.rst'],
  94. },
  95. ("keys of 'package_data' dict must be strings (got 400)"),
  96. ),
  97. # Invalid value type.
  98. (
  99. {
  100. 'hello': '*.msg',
  101. },
  102. (
  103. "\"values of 'package_data' dict\" must be of type <tuple[str, ...] | list[str]>"
  104. " (got '*.msg')"
  105. ),
  106. ),
  107. # Invalid value type (generators are single use)
  108. (
  109. {
  110. 'hello': (x for x in "generator"),
  111. },
  112. (
  113. "\"values of 'package_data' dict\" must be of type <tuple[str, ...] | list[str]>"
  114. " (got <generator object"
  115. ),
  116. ),
  117. )
  118. @pytest.mark.parametrize(('package_data', 'expected_message'), CHECK_PACKAGE_DATA_TESTS)
  119. def test_check_package_data(package_data, expected_message):
  120. if expected_message is None:
  121. assert check_package_data(None, 'package_data', package_data) is None
  122. else:
  123. with pytest.raises(DistutilsSetupError, match=re.escape(expected_message)):
  124. check_package_data(None, 'package_data', package_data)
  125. def test_check_specifier():
  126. # valid specifier value
  127. attrs = {'name': 'foo', 'python_requires': '>=3.0, !=3.1'}
  128. dist = Distribution(attrs)
  129. check_specifier(dist, attrs, attrs['python_requires'])
  130. attrs = {'name': 'foo', 'python_requires': ['>=3.0', '!=3.1']}
  131. dist = Distribution(attrs)
  132. check_specifier(dist, attrs, attrs['python_requires'])
  133. # invalid specifier value
  134. attrs = {'name': 'foo', 'python_requires': '>=invalid-version'}
  135. with pytest.raises(DistutilsSetupError):
  136. dist = Distribution(attrs)
  137. def test_metadata_name():
  138. with pytest.raises(DistutilsSetupError, match='missing.*name'):
  139. Distribution()._validate_metadata()
  140. @pytest.mark.parametrize(
  141. ('dist_name', 'py_module'),
  142. [
  143. ("my.pkg", "my_pkg"),
  144. ("my-pkg", "my_pkg"),
  145. ("my_pkg", "my_pkg"),
  146. ("pkg", "pkg"),
  147. ],
  148. )
  149. def test_dist_default_py_modules(tmp_path, dist_name, py_module):
  150. (tmp_path / f"{py_module}.py").touch()
  151. (tmp_path / "setup.py").touch()
  152. (tmp_path / "noxfile.py").touch()
  153. # ^-- make sure common tool files are ignored
  154. attrs = {**EXAMPLE_BASE_INFO, "name": dist_name, "src_root": str(tmp_path)}
  155. # Find `py_modules` corresponding to dist_name if not given
  156. dist = Distribution(attrs)
  157. dist.set_defaults()
  158. assert dist.py_modules == [py_module]
  159. # When `py_modules` is given, don't do anything
  160. dist = Distribution({**attrs, "py_modules": ["explicity_py_module"]})
  161. dist.set_defaults()
  162. assert dist.py_modules == ["explicity_py_module"]
  163. # When `packages` is given, don't do anything
  164. dist = Distribution({**attrs, "packages": ["explicity_package"]})
  165. dist.set_defaults()
  166. assert not dist.py_modules
  167. @pytest.mark.parametrize(
  168. ('dist_name', 'package_dir', 'package_files', 'packages'),
  169. [
  170. ("my.pkg", None, ["my_pkg/__init__.py", "my_pkg/mod.py"], ["my_pkg"]),
  171. ("my-pkg", None, ["my_pkg/__init__.py", "my_pkg/mod.py"], ["my_pkg"]),
  172. ("my_pkg", None, ["my_pkg/__init__.py", "my_pkg/mod.py"], ["my_pkg"]),
  173. ("my.pkg", None, ["my/pkg/__init__.py"], ["my", "my.pkg"]),
  174. (
  175. "my_pkg",
  176. None,
  177. ["src/my_pkg/__init__.py", "src/my_pkg2/__init__.py"],
  178. ["my_pkg", "my_pkg2"],
  179. ),
  180. (
  181. "my_pkg",
  182. {"pkg": "lib", "pkg2": "lib2"},
  183. ["lib/__init__.py", "lib/nested/__init__.pyt", "lib2/__init__.py"],
  184. ["pkg", "pkg.nested", "pkg2"],
  185. ),
  186. ],
  187. )
  188. def test_dist_default_packages(
  189. tmp_path, dist_name, package_dir, package_files, packages
  190. ):
  191. ensure_files(tmp_path, package_files)
  192. (tmp_path / "setup.py").touch()
  193. (tmp_path / "noxfile.py").touch()
  194. # ^-- should not be included by default
  195. attrs = {
  196. **EXAMPLE_BASE_INFO,
  197. "name": dist_name,
  198. "src_root": str(tmp_path),
  199. "package_dir": package_dir,
  200. }
  201. # Find `packages` either corresponding to dist_name or inside src
  202. dist = Distribution(attrs)
  203. dist.set_defaults()
  204. assert not dist.py_modules
  205. assert not dist.py_modules
  206. assert set(dist.packages) == set(packages)
  207. # When `py_modules` is given, don't do anything
  208. dist = Distribution({**attrs, "py_modules": ["explicit_py_module"]})
  209. dist.set_defaults()
  210. assert not dist.packages
  211. assert set(dist.py_modules) == {"explicit_py_module"}
  212. # When `packages` is given, don't do anything
  213. dist = Distribution({**attrs, "packages": ["explicit_package"]})
  214. dist.set_defaults()
  215. assert not dist.py_modules
  216. assert set(dist.packages) == {"explicit_package"}
  217. @pytest.mark.parametrize(
  218. ('dist_name', 'package_dir', 'package_files'),
  219. [
  220. ("my.pkg.nested", None, ["my/pkg/nested/__init__.py"]),
  221. ("my.pkg", None, ["my/pkg/__init__.py", "my/pkg/file.py"]),
  222. ("my_pkg", None, ["my_pkg.py"]),
  223. ("my_pkg", None, ["my_pkg/__init__.py", "my_pkg/nested/__init__.py"]),
  224. ("my_pkg", None, ["src/my_pkg/__init__.py", "src/my_pkg/nested/__init__.py"]),
  225. (
  226. "my_pkg",
  227. {"my_pkg": "lib", "my_pkg.lib2": "lib2"},
  228. ["lib/__init__.py", "lib/nested/__init__.pyt", "lib2/__init__.py"],
  229. ),
  230. # Should not try to guess a name from multiple py_modules/packages
  231. ("UNKNOWN", None, ["src/mod1.py", "src/mod2.py"]),
  232. ("UNKNOWN", None, ["src/pkg1/__ini__.py", "src/pkg2/__init__.py"]),
  233. ],
  234. )
  235. def test_dist_default_name(tmp_path, dist_name, package_dir, package_files):
  236. """Make sure dist.name is discovered from packages/py_modules"""
  237. ensure_files(tmp_path, package_files)
  238. attrs = {
  239. **EXAMPLE_BASE_INFO,
  240. "src_root": "/".join(os.path.split(tmp_path)), # POSIX-style
  241. "package_dir": package_dir,
  242. }
  243. del attrs["name"]
  244. dist = Distribution(attrs)
  245. dist.set_defaults()
  246. assert dist.py_modules or dist.packages
  247. assert dist.get_name() == dist_name