Skip to content

Commit 2121ba3

Browse files
committed
Find first PKG-INFO and update tests to abstract the contents.
1 parent a40ae7e commit 2121ba3

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

metadata_please/sdist.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ def basic_metadata_from_zip_sdist(zf: ZipFile) -> BasicMetadata:
3636
else:
3737
requires_data = b""
3838

39-
pkg_info = next(f for f in zf.namelist() if f.endswith("/PKG-INFO"))
39+
# Find the PKG-INFO file with the shortest path. This is to avoid picking up
40+
# a PKG-INFO file from a nested test directory.
41+
pkg_info = sorted(
42+
(f for f in zf.namelist() if f == "PKG-INFO" or f.endswith("/PKG-INFO")),
43+
key=len,
44+
)[0]
45+
4046
pkg_info_data = zf.read(pkg_info)
4147
assert pkg_info_data is not None
4248

@@ -74,14 +80,20 @@ def basic_metadata_from_tar_sdist(tf: TarFile) -> BasicMetadata:
7480
if requires:
7581
requires_fo = tf.extractfile(requires[0])
7682
assert requires_fo is not None
83+
requires_data = requires_fo.read()
7784
else:
78-
requires_fo = b""
85+
requires_data = b""
7986

80-
pkg_info = next(f for f in tf.getnames() if f.endswith("/PKG-INFO"))
87+
# Find the PKG-INFO file with the shortest path. This is to avoid picking up
88+
# a PKG-INFO file from a nested test directory.
89+
pkg_info = sorted(
90+
(f for f in tf.getnames() if f == "PKG-INFO" or f.endswith("/PKG-INFO")),
91+
key=len,
92+
)[0]
8193

8294
pkg_info_fo = tf.extractfile(pkg_info)
8395
assert pkg_info_fo is not None
8496

8597
return BasicMetadata.from_sdist_pkg_info_and_requires(
86-
pkg_info_fo.read(), requires_fo and requires_fo.read()
98+
pkg_info_fo.read(), requires_data
8799
)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
METADATA_CONTENTS = b"""\
2+
Requires-Dist: foo
3+
Version: 1.2.58
4+
Summary: Some Summary
5+
Home-page: http://example.com
6+
Author: Chicken
7+
Author-email: [email protected]
8+
Keywords: farm,animals
9+
Requires-Python: >=3.6
10+
Description-Content-Type: text/markdown
11+
12+
# Foo
13+
14+
A very important package.
15+
"""

metadata_please/tests/sdist.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
)
99
from ._tar import MemoryTarFile
1010
from ._zip import MemoryZipFile
11+
from .metadata_contents import METADATA_CONTENTS
1112

1213

1314
class ZipSdistTest(unittest.TestCase):
@@ -94,10 +95,13 @@ def test_basic_metadata_absl_py_09(self) -> None:
9495
self.assertEqual({"test"}, bm.provides_extra)
9596

9697
def test_basic_metadata_fields(self) -> None:
98+
"""
99+
Modern setuptools will drop a PKG-INFO file in a sdist that is very similar to the METADATA file in a wheel.
100+
"""
97101
z = MemoryZipFile(
98102
{
99103
"foo/__init__.py": b"",
100-
"foo.egg-info/PKG-INFO": b"Requires-Dist: foo\nVersion: 1.2.58\nSummary: Some Summary\nHome-page: http://example.com\nAuthor: Chicken\nAuthor-email: [email protected]\nKeywords: farm,animals\nRequires-Python: >=3.6\nDescription-Content-Type: text/markdown",
104+
"PKG-INFO": METADATA_CONTENTS,
101105
}
102106
)
103107
bm = basic_metadata_from_zip_sdist(z) # type: ignore
@@ -109,7 +113,7 @@ def test_basic_metadata_fields(self) -> None:
109113
self.assertEqual("[email protected]", bm.author_email)
110114
self.assertEqual("farm,animals", bm.keywords)
111115
self.assertEqual("text/markdown", bm.long_description_content_type)
112-
self.assertEqual(None, bm.description)
116+
self.assertEqual("# Foo\n\nA very important package.\n", bm.description)
113117

114118

115119
class TarSdistTest(unittest.TestCase):
@@ -150,8 +154,8 @@ def test_basic_metadata(self) -> None:
150154

151155
def test_metadata_fields_from_tar_sdist(self) -> None:
152156
t = MemoryTarFile(
153-
["foo.egg-info/PKG-INFO", "foo/__init__.py"],
154-
read_value=b"""Requires-Dist: foo\nVersion: 1.2.58\nSummary: Some Summary\nHome-page: http://example.com\nAuthor: Chicken\nAuthor-email: [email protected]\nKeywords: farm,animals\nRequires-Python: >=3.6\nDescription-Content-Type: text/markdown\n""",
157+
["PKG-INFO", "foo/__init__.py"],
158+
read_value=METADATA_CONTENTS,
155159
)
156160
bm = basic_metadata_from_tar_sdist(t) # type: ignore
157161
self.assertEqual("1.2.58", bm.version)
@@ -161,4 +165,4 @@ def test_metadata_fields_from_tar_sdist(self) -> None:
161165
self.assertEqual("[email protected]", bm.author_email)
162166
self.assertEqual("farm,animals", bm.keywords)
163167
self.assertEqual("text/markdown", bm.long_description_content_type)
164-
self.assertEqual(None, bm.description)
168+
self.assertEqual("# Foo\n\nA very important package.\n", bm.description)

metadata_please/tests/wheel.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import unittest
22

3-
from ..wheel import basic_metadata_from_wheel, from_wheel, InvalidWheel
3+
from ..wheel import InvalidWheel, basic_metadata_from_wheel, from_wheel
44
from ._zip import MemoryZipFile
5+
from .metadata_contents import METADATA_CONTENTS
56

67

78
class WheelTest(unittest.TestCase):
@@ -58,7 +59,7 @@ def test_basic_metadata(self) -> None:
5859
def test_basic_metadata_more_fields(self) -> None:
5960
z = MemoryZipFile(
6061
{
61-
"foo.dist-info/METADATA": b"Requires-Dist: foo\nVersion: 1.2.58\nSummary: Some Summary\nHome-page: http://example.com\nAuthor: Chicken\nAuthor-email: [email protected]\nKeywords: farm,animals\nRequires-Python: >=3.6\nDescription-Content-Type: text/markdown",
62+
"foo.dist-info/METADATA": METADATA_CONTENTS,
6263
"foo/__init__.py": b"",
6364
}
6465
)
@@ -71,4 +72,4 @@ def test_basic_metadata_more_fields(self) -> None:
7172
self.assertEqual("[email protected]", bm.author_email)
7273
self.assertEqual("farm,animals", bm.keywords)
7374
self.assertEqual("text/markdown", bm.long_description_content_type)
74-
self.assertEqual(None, bm.description)
75+
self.assertEqual("# Foo\n\nA very important package.\n", bm.description)

0 commit comments

Comments
 (0)