diff options
| author | Anderson Bravalheri <andersonbravalheri@gmail.com> | 2022-03-27 12:06:17 +0100 |
|---|---|---|
| committer | Anderson Bravalheri <andersonbravalheri@gmail.com> | 2022-03-27 12:06:17 +0100 |
| commit | 2538f017487fe6ed8827ac93ffca179b4f90377d (patch) | |
| tree | c115d30928d79815cfef699958af739ef45bd87d | |
| parent | 93bae8213b21d23a6de2c40e5bb50ad723ad70a8 (diff) | |
| download | python-setuptools-git-2538f017487fe6ed8827ac93ffca179b4f90377d.tar.gz | |
Prepare to be strict in the future about entry-points in pyproject
| -rw-r--r-- | setuptools/config/_apply_pyprojecttoml.py | 6 | ||||
| -rw-r--r-- | setuptools/config/pyprojecttoml.py | 20 | ||||
| -rw-r--r-- | setuptools/tests/config/test_pyprojecttoml.py | 64 |
3 files changed, 57 insertions, 33 deletions
diff --git a/setuptools/config/_apply_pyprojecttoml.py b/setuptools/config/_apply_pyprojecttoml.py index 421368af..78a07273 100644 --- a/setuptools/config/_apply_pyprojecttoml.py +++ b/setuptools/config/_apply_pyprojecttoml.py @@ -334,9 +334,9 @@ _PREVIOUSLY_DEFINED = { class _WouldIgnoreField(UserWarning): """Inform users that ``pyproject.toml`` would overwrite previously defined metadata: !!\n\n - ############################################## - # field would be ignored by `pyproject.toml` # - ############################################## + ########################################################################## + # configuration would be ignored/result in error due to `pyproject.toml` # + ########################################################################## The following seems to be defined outside of `pyproject.toml`: diff --git a/setuptools/config/pyprojecttoml.py b/setuptools/config/pyprojecttoml.py index a712a258..d2c6c9c5 100644 --- a/setuptools/config/pyprojecttoml.py +++ b/setuptools/config/pyprojecttoml.py @@ -9,7 +9,7 @@ from typing import TYPE_CHECKING, Callable, Dict, Optional, Mapping, Union from setuptools.errors import FileError, OptionError from . import expand as _expand -from ._apply_pyprojecttoml import apply, _PREVIOUSLY_DEFINED +from ._apply_pyprojecttoml import apply, _PREVIOUSLY_DEFINED, _WouldIgnoreField if TYPE_CHECKING: from setuptools.dist import Distribution # noqa @@ -334,10 +334,20 @@ class _ConfigExpander: groups = _expand.entry_points(text) expanded = {"entry-points": groups} - if "scripts" in self.dynamic and "console_scripts" in groups: - expanded["scripts"] = groups.pop("console_scripts") - if "gui-scripts" in self.dynamic and "gui_scripts" in groups: - expanded["gui-scripts"] = groups.pop("gui_scripts") + + def _set_scripts(field: str, group: str): + if group in groups: + value = groups.pop(group) + if field not in self.dynamic: + msg = _WouldIgnoreField.message(field, value) + warnings.warn(msg, _WouldIgnoreField) + # TODO: Don't set field when support for pyproject.toml stabilizes + # instead raise an error as specified in PEP 621 + expanded[field] = value + + _set_scripts("scripts", "console_scripts") + _set_scripts("gui-scripts", "gui_scripts") + return expanded def _obtain_classifiers(self, dist: "Distribution"): diff --git a/setuptools/tests/config/test_pyprojecttoml.py b/setuptools/tests/config/test_pyprojecttoml.py index 63ce7602..4c237014 100644 --- a/setuptools/tests/config/test_pyprojecttoml.py +++ b/setuptools/tests/config/test_pyprojecttoml.py @@ -1,4 +1,5 @@ import logging +import re from configparser import ConfigParser from inspect import cleandoc @@ -6,6 +7,7 @@ import pytest import tomli_w from path import Path as _Path +from setuptools.config._apply_pyprojecttoml import _WouldIgnoreField from setuptools.config.pyprojecttoml import ( read_configuration, expand_configuration, @@ -171,31 +173,43 @@ ENTRY_POINTS = { } -def test_expand_entry_point(tmp_path): - entry_points = ConfigParser() - entry_points.read_dict(ENTRY_POINTS) - with open(tmp_path / "entry-points.txt", "w") as f: - entry_points.write(f) - - tool = {"setuptools": {"dynamic": {"entry-points": {"file": "entry-points.txt"}}}} - project = {"dynamic": ["scripts", "gui-scripts", "entry-points"]} - pyproject = {"project": project, "tool": tool} - expanded = expand_configuration(pyproject, tmp_path) - expanded_project = expanded["project"] - assert len(expanded_project["scripts"]) == 1 - assert expanded_project["scripts"]["a"] == "mod.a:func" - assert len(expanded_project["gui-scripts"]) == 1 - assert expanded_project["gui-scripts"]["b"] == "mod.b:func" - assert len(expanded_project["entry-points"]) == 1 - assert expanded_project["entry-points"]["other"]["c"] == "mod.c:func [extra]" - - project = {"dynamic": ["entry-points"]} - pyproject = {"project": project, "tool": tool} - expanded = expand_configuration(pyproject, tmp_path) - expanded_project = expanded["project"] - assert len(expanded_project["entry-points"]) == 3 - assert "scripts" not in expanded_project - assert "gui-scripts" not in expanded_project +class TestEntryPoints: + def write_entry_points(self, tmp_path): + entry_points = ConfigParser() + entry_points.read_dict(ENTRY_POINTS) + with open(tmp_path / "entry-points.txt", "w") as f: + entry_points.write(f) + + def pyproject(self, dynamic=None): + project = {"dynamic": dynamic or ["scripts", "gui-scripts", "entry-points"]} + tool = {"dynamic": {"entry-points": {"file": "entry-points.txt"}}} + return {"project": project, "tool": {"setuptools": tool}} + + def test_all_listed_in_dynamic(self, tmp_path): + self.write_entry_points(tmp_path) + expanded = expand_configuration(self.pyproject(), tmp_path) + expanded_project = expanded["project"] + assert len(expanded_project["scripts"]) == 1 + assert expanded_project["scripts"]["a"] == "mod.a:func" + assert len(expanded_project["gui-scripts"]) == 1 + assert expanded_project["gui-scripts"]["b"] == "mod.b:func" + assert len(expanded_project["entry-points"]) == 1 + assert expanded_project["entry-points"]["other"]["c"] == "mod.c:func [extra]" + + @pytest.mark.parametrize("missing_dynamic", ("scripts", "gui-scripts")) + def test_scripts_not_listed_in_dynamic(self, tmp_path, missing_dynamic): + self.write_entry_points(tmp_path) + dynamic = {"scripts", "gui-scripts", "entry-points"} - {missing_dynamic} + + msg = f"defined outside of `pyproject.toml`:.*{missing_dynamic}" + with pytest.warns(_WouldIgnoreField, match=re.compile(msg, re.S)): + expanded = expand_configuration(self.pyproject(dynamic), tmp_path) + + expanded_project = expanded["project"] + assert dynamic < set(expanded_project) + assert len(expanded_project["entry-points"]) == 1 + # TODO: Test the following when pyproject.toml support stabilizes: + # >>> assert missing_dynamic not in expanded_project class TestClassifiers: |
