test_apply_pyprojecttoml.py 28 KB

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