Skip to content

Commit

Permalink
Handle strings in pyprject.toml's tool.setuptools.dynamic.*.file (#3782)
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri committed Jan 20, 2023
2 parents f23c915 + e99c7e0 commit 0a15141
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
2 changes: 2 additions & 0 deletions changelog.d/3782.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed problem with ``file`` directive in ``tool.setuptools.dynamic``
(``pyproject.toml``) when value is a simple string instead of list.
4 changes: 3 additions & 1 deletion setuptools/config/pyprojecttoml.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,12 @@ def _ensure_previously_set(self, dist: "Distribution", field: str):
def _expand_directive(
self, specifier: str, directive, package_dir: Mapping[str, str]
):
from setuptools.extern.more_itertools import always_iterable # type: ignore

with _ignore_errors(self.ignore_option_errors):
root_dir = self.root_dir
if "file" in directive:
self._referenced_files.update(directive["file"])
self._referenced_files.update(always_iterable(directive["file"]))
return _expand.read_files(directive["file"], root_dir)
if "attr" in directive:
return _expand.read_attr(directive["attr"], package_dir, root_dir)
Expand Down
23 changes: 19 additions & 4 deletions setuptools/tests/test_sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ def test_sdist_with_latin1_encoded_filename(self):
"setup.cfg - long_description and version": """
[metadata]
name = testing
version = file: VERSION.txt
version = file: src/VERSION.txt
license_files = DOWHATYOUWANT
long_description = file: README.rst, USAGE.rst
""",
Expand All @@ -513,15 +513,25 @@ def test_sdist_with_latin1_encoded_filename(self):
license = {file = "DOWHATYOUWANT"}
dynamic = ["version"]
[tool.setuptools.dynamic]
version = {file = ["VERSION.txt"]}
version = {file = ["src/VERSION.txt"]}
""",
"pyproject.toml - directive with str instead of list": """
[project]
name = "testing"
readme = "USAGE.rst"
license = {file = "DOWHATYOUWANT"}
dynamic = ["version"]
[tool.setuptools.dynamic]
version = {file = "src/VERSION.txt"}
"""
}

@pytest.mark.parametrize("config", _EXAMPLE_DIRECTIVES.keys())
def test_add_files_referenced_by_config_directives(self, tmp_path, config):
config_file, _, _ = config.partition(" - ")
config_text = self._EXAMPLE_DIRECTIVES[config]
(tmp_path / 'VERSION.txt').write_text("0.42", encoding="utf-8")
(tmp_path / 'src').mkdir()
(tmp_path / 'src/VERSION.txt').write_text("0.42", encoding="utf-8")
(tmp_path / 'README.rst').write_text("hello world!", encoding="utf-8")
(tmp_path / 'USAGE.rst').write_text("hello world!", encoding="utf-8")
(tmp_path / 'DOWHATYOUWANT').write_text("hello world!", encoding="utf-8")
Expand All @@ -536,9 +546,14 @@ def test_add_files_referenced_by_config_directives(self, tmp_path, config):
with quiet():
cmd.run()

assert 'VERSION.txt' in cmd.filelist.files
assert (
'src/VERSION.txt' in cmd.filelist.files
or 'src\\VERSION.txt' in cmd.filelist.files
)
assert 'USAGE.rst' in cmd.filelist.files
assert 'DOWHATYOUWANT' in cmd.filelist.files
assert '/' not in cmd.filelist.files
assert '\\' not in cmd.filelist.files

def test_pyproject_toml_in_sdist(self, tmpdir):
"""
Expand Down

0 comments on commit 0a15141

Please sign in to comment.