test_apply_pyprojecttoml.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. """Make sure that applying the configuration from pyproject.toml is equivalent to
  2. applying a similar configuration from setup.cfg
  3. To run these tests offline, please have a look on ``./downloads/preload.py``
  4. """
  5. from __future__ import annotations
  6. import io
  7. import re
  8. import tarfile
  9. from inspect import cleandoc
  10. from pathlib import Path
  11. from unittest.mock import Mock
  12. import pytest
  13. from ini2toml.api import LiteTranslator
  14. from packaging.metadata import Metadata
  15. import setuptools # noqa: F401 # ensure monkey patch to metadata
  16. from setuptools._static import is_static
  17. from setuptools.command.egg_info import write_requirements
  18. from setuptools.config import expand, pyprojecttoml, setupcfg
  19. from setuptools.config._apply_pyprojecttoml import _MissingDynamic, _some_attrgetter
  20. from setuptools.dist import Distribution
  21. from setuptools.errors import InvalidConfigError, RemovedConfigError
  22. from setuptools.warnings import InformationOnly, SetuptoolsDeprecationWarning
  23. from .downloads import retrieve_file, urls_from_file
  24. HERE = Path(__file__).parent
  25. EXAMPLES_FILE = "setupcfg_examples.txt"
  26. def makedist(path, **attrs):
  27. return Distribution({"src_root": path, **attrs})
  28. def _mock_expand_patterns(patterns, *_, **__):
  29. """
  30. Allow comparing the given patterns for 2 dist objects.
  31. We need to strip special chars to avoid errors when validating.
  32. """
  33. return [
  34. re.sub("[^a-z0-9]+", "", p, flags=re.IGNORECASE) or "empty" for p in patterns
  35. ]
  36. @pytest.mark.parametrize("url", urls_from_file(HERE / EXAMPLES_FILE))
  37. @pytest.mark.filterwarnings("ignore")
  38. @pytest.mark.uses_network
  39. def test_apply_pyproject_equivalent_to_setupcfg(url, monkeypatch, tmp_path):
  40. monkeypatch.setattr(expand, "read_attr", Mock(return_value="0.0.1"))
  41. monkeypatch.setattr(
  42. Distribution, "_expand_patterns", Mock(side_effect=_mock_expand_patterns)
  43. )
  44. setupcfg_example = retrieve_file(url)
  45. pyproject_example = Path(tmp_path, "pyproject.toml")
  46. setupcfg_text = setupcfg_example.read_text(encoding="utf-8")
  47. toml_config = LiteTranslator().translate(setupcfg_text, "setup.cfg")
  48. pyproject_example.write_text(toml_config, encoding="utf-8")
  49. dist_toml = pyprojecttoml.apply_configuration(makedist(tmp_path), pyproject_example)
  50. dist_cfg = setupcfg.apply_configuration(makedist(tmp_path), setupcfg_example)
  51. pkg_info_toml = core_metadata(dist_toml)
  52. pkg_info_cfg = core_metadata(dist_cfg)
  53. assert pkg_info_toml == pkg_info_cfg
  54. if any(getattr(d, "license_files", None) for d in (dist_toml, dist_cfg)):
  55. assert set(dist_toml.license_files) == set(dist_cfg.license_files)
  56. if any(getattr(d, "entry_points", None) for d in (dist_toml, dist_cfg)):
  57. print(dist_cfg.entry_points)
  58. ep_toml = {
  59. (k, *sorted(i.replace(" ", "") for i in v))
  60. for k, v in dist_toml.entry_points.items()
  61. }
  62. ep_cfg = {
  63. (k, *sorted(i.replace(" ", "") for i in v))
  64. for k, v in dist_cfg.entry_points.items()
  65. }
  66. assert ep_toml == ep_cfg
  67. if any(getattr(d, "package_data", None) for d in (dist_toml, dist_cfg)):
  68. pkg_data_toml = {(k, *sorted(v)) for k, v in dist_toml.package_data.items()}
  69. pkg_data_cfg = {(k, *sorted(v)) for k, v in dist_cfg.package_data.items()}
  70. assert pkg_data_toml == pkg_data_cfg
  71. if any(getattr(d, "data_files", None) for d in (dist_toml, dist_cfg)):
  72. data_files_toml = {(k, *sorted(v)) for k, v in dist_toml.data_files}
  73. data_files_cfg = {(k, *sorted(v)) for k, v in dist_cfg.data_files}
  74. assert data_files_toml == data_files_cfg
  75. assert set(dist_toml.install_requires) == set(dist_cfg.install_requires)
  76. if any(getattr(d, "extras_require", None) for d in (dist_toml, dist_cfg)):
  77. extra_req_toml = {(k, *sorted(v)) for k, v in dist_toml.extras_require.items()}
  78. extra_req_cfg = {(k, *sorted(v)) for k, v in dist_cfg.extras_require.items()}
  79. assert extra_req_toml == extra_req_cfg
  80. PEP621_EXAMPLE = """\
  81. [project]
  82. name = "spam"
  83. version = "2020.0.0"
  84. description = "Lovely Spam! Wonderful Spam!"
  85. readme = "README.rst"
  86. requires-python = ">=3.8"
  87. license-files = ["LICENSE.txt"] # Updated to be PEP 639 compliant
  88. keywords = ["egg", "bacon", "sausage", "tomatoes", "Lobster Thermidor"]
  89. authors = [
  90. {email = "hi@pradyunsg.me"},
  91. {name = "Tzu-Ping Chung"}
  92. ]
  93. maintainers = [
  94. {name = "Brett Cannon", email = "brett@python.org"},
  95. {name = "John X. Ãørçeč", email = "john@utf8.org"},
  96. {name = "Γαμα קּ 東", email = "gama@utf8.org"},
  97. ]
  98. classifiers = [
  99. "Development Status :: 4 - Beta",
  100. "Programming Language :: Python"
  101. ]
  102. dependencies = [
  103. "httpx",
  104. "gidgethub[httpx]>4.0.0",
  105. "django>2.1; os_name != 'nt'",
  106. "django>2.0; os_name == 'nt'"
  107. ]
  108. [project.optional-dependencies]
  109. test = [
  110. "pytest < 5.0.0",
  111. "pytest-cov[all]"
  112. ]
  113. [project.urls]
  114. homepage = "http://example.com"
  115. documentation = "http://readthedocs.org"
  116. repository = "http://github.com"
  117. changelog = "http://github.com/me/spam/blob/master/CHANGELOG.md"
  118. [project.scripts]
  119. spam-cli = "spam:main_cli"
  120. [project.gui-scripts]
  121. spam-gui = "spam:main_gui"
  122. [project.entry-points."spam.magical"]
  123. tomatoes = "spam:main_tomatoes"
  124. """
  125. PEP621_INTERNATIONAL_EMAIL_EXAMPLE = """\
  126. [project]
  127. name = "spam"
  128. version = "2020.0.0"
  129. authors = [
  130. {email = "hi@pradyunsg.me"},
  131. {name = "Tzu-Ping Chung"}
  132. ]
  133. maintainers = [
  134. {name = "अंकित अहलावत", email = "ankit@example.com"},
  135. ]
  136. """
  137. PEP621_EXAMPLE_SCRIPT = """
  138. def main_cli(): pass
  139. def main_gui(): pass
  140. def main_tomatoes(): pass
  141. """
  142. PEP639_LICENSE_TEXT = """\
  143. [project]
  144. name = "spam"
  145. version = "2020.0.0"
  146. authors = [
  147. {email = "hi@pradyunsg.me"},
  148. {name = "Tzu-Ping Chung"}
  149. ]
  150. license = {text = "MIT"}
  151. """
  152. PEP639_LICENSE_EXPRESSION = """\
  153. [project]
  154. name = "spam"
  155. version = "2020.0.0"
  156. authors = [
  157. {email = "hi@pradyunsg.me"},
  158. {name = "Tzu-Ping Chung"}
  159. ]
  160. license = "mit or apache-2.0" # should be normalized in metadata
  161. classifiers = [
  162. "Development Status :: 5 - Production/Stable",
  163. "Programming Language :: Python",
  164. ]
  165. """
  166. def _pep621_example_project(
  167. tmp_path,
  168. readme="README.rst",
  169. pyproject_text=PEP621_EXAMPLE,
  170. ):
  171. pyproject = tmp_path / "pyproject.toml"
  172. text = pyproject_text
  173. replacements = {'readme = "README.rst"': f'readme = "{readme}"'}
  174. for orig, subst in replacements.items():
  175. text = text.replace(orig, subst)
  176. pyproject.write_text(text, encoding="utf-8")
  177. (tmp_path / readme).write_text("hello world", encoding="utf-8")
  178. (tmp_path / "LICENSE.txt").write_text("--- LICENSE stub ---", encoding="utf-8")
  179. (tmp_path / "spam.py").write_text(PEP621_EXAMPLE_SCRIPT, encoding="utf-8")
  180. return pyproject
  181. def test_pep621_example(tmp_path):
  182. """Make sure the example in PEP 621 works"""
  183. pyproject = _pep621_example_project(tmp_path)
  184. dist = pyprojecttoml.apply_configuration(makedist(tmp_path), pyproject)
  185. assert set(dist.metadata.license_files) == {"LICENSE.txt"}
  186. @pytest.mark.parametrize(
  187. ("readme", "ctype"),
  188. [
  189. ("Readme.txt", "text/plain"),
  190. ("readme.md", "text/markdown"),
  191. ("text.rst", "text/x-rst"),
  192. ],
  193. )
  194. def test_readme_content_type(tmp_path, readme, ctype):
  195. pyproject = _pep621_example_project(tmp_path, readme)
  196. dist = pyprojecttoml.apply_configuration(makedist(tmp_path), pyproject)
  197. assert dist.metadata.long_description_content_type == ctype
  198. def test_undefined_content_type(tmp_path):
  199. pyproject = _pep621_example_project(tmp_path, "README.tex")
  200. with pytest.raises(ValueError, match="Undefined content type for README.tex"):
  201. pyprojecttoml.apply_configuration(makedist(tmp_path), pyproject)
  202. def test_no_explicit_content_type_for_missing_extension(tmp_path):
  203. pyproject = _pep621_example_project(tmp_path, "README")
  204. dist = pyprojecttoml.apply_configuration(makedist(tmp_path), pyproject)
  205. assert dist.metadata.long_description_content_type is None
  206. @pytest.mark.parametrize(
  207. ("pyproject_text", "expected_maintainers_meta_value"),
  208. (
  209. pytest.param(
  210. PEP621_EXAMPLE,
  211. (
  212. 'Brett Cannon <brett@python.org>, "John X. Ãørçeč" <john@utf8.org>, '
  213. 'Γαμα קּ 東 <gama@utf8.org>'
  214. ),
  215. id='non-international-emails',
  216. ),
  217. pytest.param(
  218. PEP621_INTERNATIONAL_EMAIL_EXAMPLE,
  219. 'Ankit Ahlawat <अंकित@उदाहरण.भारत>',
  220. marks=pytest.mark.xfail(
  221. reason="CPython's `email.headerregistry.Address` only supports "
  222. 'RFC 5322, as of Oct 20, 2025 and latest Python 3.13.0',
  223. strict=True,
  224. ),
  225. id='international-email',
  226. ),
  227. ),
  228. )
  229. def test_utf8_maintainer_in_metadata( # issue-3663
  230. expected_maintainers_meta_value,
  231. pyproject_text,
  232. tmp_path,
  233. ):
  234. pyproject = _pep621_example_project(
  235. tmp_path,
  236. "README",
  237. pyproject_text=pyproject_text,
  238. )
  239. dist = pyprojecttoml.apply_configuration(makedist(tmp_path), pyproject)
  240. assert dist.metadata.maintainer_email == expected_maintainers_meta_value
  241. pkg_file = tmp_path / "PKG-FILE"
  242. with open(pkg_file, "w", encoding="utf-8") as fh:
  243. dist.metadata.write_pkg_file(fh)
  244. content = pkg_file.read_text(encoding="utf-8")
  245. assert f"Maintainer-email: {expected_maintainers_meta_value}" in content
  246. @pytest.mark.parametrize(
  247. (
  248. 'pyproject_text',
  249. 'license',
  250. 'license_expression',
  251. 'content_str',
  252. 'not_content_str',
  253. ),
  254. (
  255. pytest.param(
  256. PEP639_LICENSE_TEXT,
  257. 'MIT',
  258. None,
  259. 'License: MIT',
  260. 'License-Expression: ',
  261. id='license-text',
  262. marks=[
  263. pytest.mark.filterwarnings(
  264. "ignore:.project.license. as a TOML table is deprecated",
  265. )
  266. ],
  267. ),
  268. pytest.param(
  269. PEP639_LICENSE_EXPRESSION,
  270. None,
  271. 'MIT OR Apache-2.0',
  272. 'License-Expression: MIT OR Apache-2.0',
  273. 'License: ',
  274. id='license-expression',
  275. ),
  276. ),
  277. )
  278. def test_license_in_metadata(
  279. license,
  280. license_expression,
  281. content_str,
  282. not_content_str,
  283. pyproject_text,
  284. tmp_path,
  285. ):
  286. pyproject = _pep621_example_project(
  287. tmp_path,
  288. "README",
  289. pyproject_text=pyproject_text,
  290. )
  291. dist = pyprojecttoml.apply_configuration(makedist(tmp_path), pyproject)
  292. assert dist.metadata.license == license
  293. assert dist.metadata.license_expression == license_expression
  294. pkg_file = tmp_path / "PKG-FILE"
  295. with open(pkg_file, "w", encoding="utf-8") as fh:
  296. dist.metadata.write_pkg_file(fh)
  297. content = pkg_file.read_text(encoding="utf-8")
  298. assert "Metadata-Version: 2.4" in content
  299. assert content_str in content
  300. assert not_content_str not in content
  301. def test_license_classifier_with_license_expression(tmp_path):
  302. text = PEP639_LICENSE_EXPRESSION.rsplit("\n", 2)[0]
  303. pyproject = _pep621_example_project(
  304. tmp_path,
  305. "README",
  306. f"{text}\n \"License :: OSI Approved :: MIT License\"\n]",
  307. )
  308. msg = "License classifiers have been superseded by license expressions"
  309. with pytest.raises(InvalidConfigError, match=msg) as exc:
  310. pyprojecttoml.apply_configuration(makedist(tmp_path), pyproject)
  311. assert "License :: OSI Approved :: MIT License" in str(exc.value)
  312. def test_license_classifier_without_license_expression(tmp_path):
  313. text = """\
  314. [project]
  315. name = "spam"
  316. version = "2020.0.0"
  317. license = {text = "mit or apache-2.0"}
  318. classifiers = ["License :: OSI Approved :: MIT License"]
  319. """
  320. pyproject = _pep621_example_project(tmp_path, "README", text)
  321. msg1 = "License classifiers are deprecated(?:.|\n)*MIT License"
  322. msg2 = ".project.license. as a TOML table is deprecated"
  323. with (
  324. pytest.warns(SetuptoolsDeprecationWarning, match=msg1),
  325. pytest.warns(SetuptoolsDeprecationWarning, match=msg2),
  326. ):
  327. dist = pyprojecttoml.apply_configuration(makedist(tmp_path), pyproject)
  328. # Check license classifier is still included
  329. assert dist.metadata.get_classifiers() == ["License :: OSI Approved :: MIT License"]
  330. class TestLicenseFiles:
  331. def base_pyproject(
  332. self,
  333. tmp_path,
  334. additional_text="",
  335. license_toml='license = {file = "LICENSE.txt"}\n',
  336. ):
  337. text = PEP639_LICENSE_EXPRESSION
  338. # Sanity-check
  339. assert 'license = "mit or apache-2.0"' in text
  340. assert 'license-files' not in text
  341. assert "[tool.setuptools]" not in text
  342. text = re.sub(
  343. r"(license = .*)\n",
  344. license_toml,
  345. text,
  346. count=1,
  347. )
  348. assert license_toml in text # sanity check
  349. text = f"{text}\n{additional_text}\n"
  350. pyproject = _pep621_example_project(tmp_path, "README", pyproject_text=text)
  351. return pyproject
  352. def base_pyproject_license_pep639(self, tmp_path, additional_text=""):
  353. return self.base_pyproject(
  354. tmp_path,
  355. additional_text=additional_text,
  356. license_toml='license = "licenseref-Proprietary"'
  357. '\nlicense-files = ["_FILE*"]\n',
  358. )
  359. def test_both_license_and_license_files_defined(self, tmp_path):
  360. setuptools_config = '[tool.setuptools]\nlicense-files = ["_FILE*"]'
  361. pyproject = self.base_pyproject(tmp_path, setuptools_config)
  362. (tmp_path / "_FILE.txt").touch()
  363. (tmp_path / "_FILE.rst").touch()
  364. # Would normally match the `license_files` patterns, but we want to exclude it
  365. # by being explicit. On the other hand, contents should be added to `license`
  366. license = tmp_path / "LICENSE.txt"
  367. license.write_text("LicenseRef-Proprietary\n", encoding="utf-8")
  368. msg1 = "'tool.setuptools.license-files' is deprecated in favor of 'project.license-files'"
  369. msg2 = ".project.license. as a TOML table is deprecated"
  370. with (
  371. pytest.warns(SetuptoolsDeprecationWarning, match=msg1),
  372. pytest.warns(SetuptoolsDeprecationWarning, match=msg2),
  373. ):
  374. dist = pyprojecttoml.apply_configuration(makedist(tmp_path), pyproject)
  375. assert set(dist.metadata.license_files) == {"_FILE.rst", "_FILE.txt"}
  376. assert dist.metadata.license == "LicenseRef-Proprietary\n"
  377. def test_both_license_and_license_files_defined_pep639(self, tmp_path):
  378. # Set license and license-files
  379. pyproject = self.base_pyproject_license_pep639(tmp_path)
  380. (tmp_path / "_FILE.txt").touch()
  381. (tmp_path / "_FILE.rst").touch()
  382. msg = "Normalizing.*LicenseRef"
  383. with pytest.warns(InformationOnly, match=msg):
  384. dist = pyprojecttoml.apply_configuration(makedist(tmp_path), pyproject)
  385. assert set(dist.metadata.license_files) == {"_FILE.rst", "_FILE.txt"}
  386. assert dist.metadata.license is None
  387. assert dist.metadata.license_expression == "LicenseRef-Proprietary"
  388. def test_license_files_defined_twice(self, tmp_path):
  389. # Set project.license-files and tools.setuptools.license-files
  390. setuptools_config = '[tool.setuptools]\nlicense-files = ["_FILE*"]'
  391. pyproject = self.base_pyproject_license_pep639(tmp_path, setuptools_config)
  392. msg = "'project.license-files' is defined already. Remove 'tool.setuptools.license-files'"
  393. with pytest.raises(InvalidConfigError, match=msg):
  394. pyprojecttoml.apply_configuration(makedist(tmp_path), pyproject)
  395. def test_default_patterns(self, tmp_path):
  396. setuptools_config = '[tool.setuptools]\nzip-safe = false'
  397. # ^ used just to trigger section validation
  398. pyproject = self.base_pyproject(tmp_path, setuptools_config, license_toml="")
  399. license_files = "LICENCE-a.html COPYING-abc.txt AUTHORS-xyz NOTICE,def".split()
  400. for fname in license_files:
  401. (tmp_path / fname).write_text(f"{fname}\n", encoding="utf-8")
  402. dist = pyprojecttoml.apply_configuration(makedist(tmp_path), pyproject)
  403. assert (tmp_path / "LICENSE.txt").exists() # from base example
  404. assert set(dist.metadata.license_files) == {*license_files, "LICENSE.txt"}
  405. def test_missing_patterns(self, tmp_path):
  406. pyproject = self.base_pyproject_license_pep639(tmp_path)
  407. assert list(tmp_path.glob("_FILE*")) == [] # sanity check
  408. msg1 = "Cannot find any files for the given pattern.*"
  409. msg2 = "Normalizing 'licenseref-Proprietary' to 'LicenseRef-Proprietary'"
  410. with (
  411. pytest.warns(SetuptoolsDeprecationWarning, match=msg1),
  412. pytest.warns(InformationOnly, match=msg2),
  413. ):
  414. pyprojecttoml.apply_configuration(makedist(tmp_path), pyproject)
  415. def test_deprecated_file_expands_to_text(self, tmp_path):
  416. """Make sure the old example with ``license = {text = ...}`` works"""
  417. assert 'license-files = ["LICENSE.txt"]' in PEP621_EXAMPLE # sanity check
  418. text = PEP621_EXAMPLE.replace(
  419. 'license-files = ["LICENSE.txt"]',
  420. 'license = {file = "LICENSE.txt"}',
  421. )
  422. pyproject = _pep621_example_project(tmp_path, pyproject_text=text)
  423. msg = ".project.license. as a TOML table is deprecated"
  424. with pytest.warns(SetuptoolsDeprecationWarning, match=msg):
  425. dist = pyprojecttoml.apply_configuration(makedist(tmp_path), pyproject)
  426. assert dist.metadata.license == "--- LICENSE stub ---"
  427. assert set(dist.metadata.license_files) == {"LICENSE.txt"} # auto-filled
  428. class TestPyModules:
  429. # https://github.com/pypa/setuptools/issues/4316
  430. def dist(self, name):
  431. toml_config = f"""
  432. [project]
  433. name = "test"
  434. version = "42.0"
  435. [tool.setuptools]
  436. py-modules = [{name!r}]
  437. """
  438. pyproject = Path("pyproject.toml")
  439. pyproject.write_text(cleandoc(toml_config), encoding="utf-8")
  440. return pyprojecttoml.apply_configuration(Distribution({}), pyproject)
  441. @pytest.mark.parametrize("module", ["pip-run", "abc-d.λ-xyz-e"])
  442. def test_valid_module_name(self, tmp_path, monkeypatch, module):
  443. monkeypatch.chdir(tmp_path)
  444. assert module in self.dist(module).py_modules
  445. @pytest.mark.parametrize("module", ["pip run", "-pip-run", "pip-run-stubs"])
  446. def test_invalid_module_name(self, tmp_path, monkeypatch, module):
  447. monkeypatch.chdir(tmp_path)
  448. with pytest.raises(ValueError, match="py-modules"):
  449. self.dist(module).py_modules
  450. class TestExtModules:
  451. def make_dist(self, toml_config):
  452. pyproject = Path("pyproject.toml")
  453. pyproject.write_text(cleandoc(toml_config), encoding="utf-8")
  454. with pytest.warns(pyprojecttoml._ExperimentalConfiguration):
  455. return pyprojecttoml.apply_configuration(Distribution({}), pyproject)
  456. def test_pyproject_sets_attribute(self, tmp_path, monkeypatch):
  457. monkeypatch.chdir(tmp_path)
  458. toml_config = """
  459. [project]
  460. name = "test"
  461. version = "42.0"
  462. [tool.setuptools]
  463. ext-modules = [
  464. {name = "my.ext", sources = ["hello.c", "world.c"]}
  465. ]
  466. """
  467. dist = self.make_dist(toml_config)
  468. assert len(dist.ext_modules) == 1
  469. assert dist.ext_modules[0].name == "my.ext"
  470. assert set(dist.ext_modules[0].sources) == {"hello.c", "world.c"}
  471. def test_pyproject_define_macros_as_tuples(self, tmp_path, monkeypatch):
  472. # https://github.com/pypa/setuptools/issues/4810
  473. monkeypatch.chdir(tmp_path)
  474. toml_config = """
  475. [project]
  476. name = "test"
  477. version = "42.0"
  478. [[tool.setuptools.ext-modules]]
  479. name = "my.ext"
  480. sources = ["hello.c", "world.c"]
  481. define-macros = [["FIRST_SINGLE"], ["SECOND_TWO", "1"]]
  482. """
  483. dist = self.make_dist(toml_config)
  484. assert isinstance(dist.ext_modules[0].define_macros[0], tuple)
  485. assert dist.ext_modules[0].define_macros[0] == ("FIRST_SINGLE",)
  486. assert dist.ext_modules[0].define_macros[1] == ("SECOND_TWO", "1")
  487. class TestDeprecatedFields:
  488. def test_namespace_packages(self, tmp_path):
  489. pyproject = tmp_path / "pyproject.toml"
  490. config = """
  491. [project]
  492. name = "myproj"
  493. version = "42"
  494. [tool.setuptools]
  495. namespace-packages = ["myproj.pkg"]
  496. """
  497. pyproject.write_text(cleandoc(config), encoding="utf-8")
  498. with pytest.raises(RemovedConfigError, match="namespace-packages"):
  499. pyprojecttoml.apply_configuration(makedist(tmp_path), pyproject)
  500. class TestPresetField:
  501. def pyproject(self, tmp_path, dynamic, extra_content=""):
  502. content = f"[project]\nname = 'proj'\ndynamic = {dynamic!r}\n"
  503. if "version" not in dynamic:
  504. content += "version = '42'\n"
  505. file = tmp_path / "pyproject.toml"
  506. file.write_text(content + extra_content, encoding="utf-8")
  507. return file
  508. @pytest.mark.parametrize(
  509. ("attr", "field", "value"),
  510. [
  511. ("license_expression", "license", "MIT"),
  512. pytest.param(
  513. *("license", "license", "Not SPDX"),
  514. marks=[pytest.mark.filterwarnings("ignore:.*license. overwritten")],
  515. ),
  516. ("classifiers", "classifiers", ["Private :: Classifier"]),
  517. ("entry_points", "scripts", {"console_scripts": ["foobar=foobar:main"]}),
  518. ("entry_points", "gui-scripts", {"gui_scripts": ["bazquux=bazquux:main"]}),
  519. pytest.param(
  520. *("install_requires", "dependencies", ["six"]),
  521. marks=[
  522. pytest.mark.filterwarnings("ignore:.*install_requires. overwritten")
  523. ],
  524. ),
  525. ],
  526. )
  527. def test_not_listed_in_dynamic(self, tmp_path, attr, field, value):
  528. """Setuptools cannot set a field if not listed in ``dynamic``"""
  529. pyproject = self.pyproject(tmp_path, [])
  530. dist = makedist(tmp_path, **{attr: value})
  531. msg = re.compile(f"defined outside of `pyproject.toml`:.*{field}", re.DOTALL)
  532. with pytest.warns(_MissingDynamic, match=msg):
  533. dist = pyprojecttoml.apply_configuration(dist, pyproject)
  534. dist_value = _some_attrgetter(f"metadata.{attr}", attr)(dist)
  535. assert not dist_value
  536. @pytest.mark.parametrize(
  537. ("attr", "field", "value"),
  538. [
  539. ("license_expression", "license", "MIT"),
  540. ("install_requires", "dependencies", []),
  541. ("extras_require", "optional-dependencies", {}),
  542. ("install_requires", "dependencies", ["six"]),
  543. ("classifiers", "classifiers", ["Private :: Classifier"]),
  544. ],
  545. )
  546. def test_listed_in_dynamic(self, tmp_path, attr, field, value):
  547. pyproject = self.pyproject(tmp_path, [field])
  548. dist = makedist(tmp_path, **{attr: value})
  549. dist = pyprojecttoml.apply_configuration(dist, pyproject)
  550. dist_value = _some_attrgetter(f"metadata.{attr}", attr)(dist)
  551. assert dist_value == value
  552. def test_license_files_exempt_from_dynamic(self, monkeypatch, tmp_path):
  553. """
  554. license-file is currently not considered in the context of dynamic.
  555. As per 2025-02-19, https://packaging.python.org/en/latest/specifications/pyproject-toml/#license-files
  556. allows setuptools to fill-in `license-files` the way it sees fit:
  557. > If the license-files key is not defined, tools can decide how to handle license files.
  558. > For example they can choose not to include any files or use their own
  559. > logic to discover the appropriate files in the distribution.
  560. Using license_files from setup.py to fill-in the value is in accordance
  561. with this rule.
  562. """
  563. monkeypatch.chdir(tmp_path)
  564. pyproject = self.pyproject(tmp_path, [])
  565. dist = makedist(tmp_path, license_files=["LIC*"])
  566. (tmp_path / "LIC1").write_text("42", encoding="utf-8")
  567. dist = pyprojecttoml.apply_configuration(dist, pyproject)
  568. assert dist.metadata.license_files == ["LIC1"]
  569. def test_warning_overwritten_dependencies(self, tmp_path):
  570. src = "[project]\nname='pkg'\nversion='0.1'\ndependencies=['click']\n"
  571. pyproject = tmp_path / "pyproject.toml"
  572. pyproject.write_text(src, encoding="utf-8")
  573. dist = makedist(tmp_path, install_requires=["wheel"])
  574. with pytest.warns(match="`install_requires` overwritten"):
  575. dist = pyprojecttoml.apply_configuration(dist, pyproject)
  576. assert "wheel" not in dist.install_requires
  577. def test_optional_dependencies_dont_remove_env_markers(self, tmp_path):
  578. """
  579. Internally setuptools converts dependencies with markers to "extras".
  580. If ``install_requires`` is given by ``setup.py``, we have to ensure that
  581. applying ``optional-dependencies`` does not overwrite the mandatory
  582. dependencies with markers (see #3204).
  583. """
  584. # If setuptools replace its internal mechanism that uses `requires.txt`
  585. # this test has to be rewritten to adapt accordingly
  586. extra = "\n[project.optional-dependencies]\nfoo = ['bar>1']\n"
  587. pyproject = self.pyproject(tmp_path, ["dependencies"], extra)
  588. install_req = ['importlib-resources (>=3.0.0) ; python_version < "3.7"']
  589. dist = makedist(tmp_path, install_requires=install_req)
  590. dist = pyprojecttoml.apply_configuration(dist, pyproject)
  591. assert "foo" in dist.extras_require
  592. egg_info = dist.get_command_obj("egg_info")
  593. write_requirements(egg_info, tmp_path, tmp_path / "requires.txt")
  594. reqs = (tmp_path / "requires.txt").read_text(encoding="utf-8")
  595. assert "importlib-resources" in reqs
  596. assert "bar" in reqs
  597. assert ':python_version < "3.7"' in reqs
  598. @pytest.mark.parametrize(
  599. ("field", "group"),
  600. [("scripts", "console_scripts"), ("gui-scripts", "gui_scripts")],
  601. )
  602. @pytest.mark.filterwarnings("error")
  603. def test_scripts_dont_require_dynamic_entry_points(self, tmp_path, field, group):
  604. # Issue 3862
  605. pyproject = self.pyproject(tmp_path, [field])
  606. dist = makedist(tmp_path, entry_points={group: ["foobar=foobar:main"]})
  607. dist = pyprojecttoml.apply_configuration(dist, pyproject)
  608. assert group in dist.entry_points
  609. class TestMeta:
  610. def test_example_file_in_sdist(self, setuptools_sdist):
  611. """Meta test to ensure tests can run from sdist"""
  612. with tarfile.open(setuptools_sdist) as tar:
  613. assert any(name.endswith(EXAMPLES_FILE) for name in tar.getnames())
  614. class TestInteropCommandLineParsing:
  615. def test_version(self, tmp_path, monkeypatch, capsys):
  616. # See pypa/setuptools#4047
  617. # This test can be removed once the CLI interface of setup.py is removed
  618. monkeypatch.chdir(tmp_path)
  619. toml_config = """
  620. [project]
  621. name = "test"
  622. version = "42.0"
  623. """
  624. pyproject = Path(tmp_path, "pyproject.toml")
  625. pyproject.write_text(cleandoc(toml_config), encoding="utf-8")
  626. opts = {"script_args": ["--version"]}
  627. dist = pyprojecttoml.apply_configuration(Distribution(opts), pyproject)
  628. dist.parse_command_line() # <-- there should be no exception here.
  629. captured = capsys.readouterr()
  630. assert "42.0" in captured.out
  631. class TestStaticConfig:
  632. def test_mark_static_fields(self, tmp_path, monkeypatch):
  633. monkeypatch.chdir(tmp_path)
  634. toml_config = """
  635. [project]
  636. name = "test"
  637. version = "42.0"
  638. dependencies = ["hello"]
  639. keywords = ["world"]
  640. classifiers = ["private :: hello world"]
  641. [tool.setuptools]
  642. obsoletes = ["abcd"]
  643. provides = ["abcd"]
  644. platforms = ["abcd"]
  645. """
  646. pyproject = Path(tmp_path, "pyproject.toml")
  647. pyproject.write_text(cleandoc(toml_config), encoding="utf-8")
  648. dist = pyprojecttoml.apply_configuration(Distribution({}), pyproject)
  649. assert is_static(dist.install_requires)
  650. assert is_static(dist.metadata.keywords)
  651. assert is_static(dist.metadata.classifiers)
  652. assert is_static(dist.metadata.obsoletes)
  653. assert is_static(dist.metadata.provides)
  654. assert is_static(dist.metadata.platforms)
  655. # --- Auxiliary Functions ---
  656. def core_metadata(dist) -> str:
  657. with io.StringIO() as buffer:
  658. dist.metadata.write_pkg_file(buffer)
  659. pkg_file_txt = buffer.getvalue()
  660. # Make sure core metadata is valid
  661. Metadata.from_email(pkg_file_txt, validate=True) # can raise exceptions
  662. skip_prefixes: tuple[str, ...] = ()
  663. skip_lines = set()
  664. # ---- DIFF NORMALISATION ----
  665. # PEP 621 is very particular about author/maintainer metadata conversion, so skip
  666. skip_prefixes += ("Author:", "Author-email:", "Maintainer:", "Maintainer-email:")
  667. # May be redundant with Home-page
  668. skip_prefixes += ("Project-URL: Homepage,", "Home-page:")
  669. # May be missing in original (relying on default) but backfilled in the TOML
  670. skip_prefixes += ("Description-Content-Type:",)
  671. # Remove empty lines
  672. skip_lines.add("")
  673. result = []
  674. for line in pkg_file_txt.splitlines():
  675. if line.startswith(skip_prefixes) or line in skip_lines:
  676. continue
  677. result.append(line + "\n")
  678. return "".join(result)