test_dist.py 8.7 KB

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