Skip to content

Commit cb424e2

Browse files
committed
Add tests for sdist tar.
1 parent a9de409 commit cb424e2

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

metadata_please/sdist.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ def from_zip_sdist(zf: ZipFile) -> bytes:
3030
def basic_metadata_from_zip_sdist(zf: ZipFile) -> BasicMetadata:
3131
requires = [f for f in zf.namelist() if f.endswith("/requires.txt")]
3232
requires.sort(key=len)
33-
if not requires:
34-
return BasicMetadata((), frozenset(), "-")
35-
36-
requires_data = zf.read(requires[0])
37-
assert requires_data is not None
33+
if requires:
34+
requires_data = zf.read(requires[0])
35+
assert requires_data is not None
36+
else:
37+
requires_data = b""
3838

3939
pkg_info = next(f for f in zf.namelist() if f.endswith("/PKG-INFO"))
4040
pkg_info_data = zf.read(pkg_info)
@@ -71,17 +71,17 @@ def basic_metadata_from_tar_sdist(tf: TarFile) -> BasicMetadata:
7171
# XXX Why do ZipFile and TarFile not have a common interface ?!
7272
requires = [f for f in tf.getnames() if f.endswith("/requires.txt")]
7373
requires.sort(key=len)
74-
if not requires:
75-
return BasicMetadata((), frozenset())
76-
77-
requires_fo = tf.extractfile(requires[0])
78-
assert requires_fo is not None
74+
if requires:
75+
requires_fo = tf.extractfile(requires[0])
76+
assert requires_fo is not None
77+
else:
78+
requires_fo = b""
7979

8080
pkg_info = next(f for f in tf.getnames() if f.endswith("/PKG-INFO"))
8181

8282
pkg_info_fo = tf.extractfile(pkg_info)
8383
assert pkg_info_fo is not None
8484

8585
return BasicMetadata.from_sdist_pkg_info_and_requires(
86-
pkg_info_fo.read(), requires_fo.read()
86+
pkg_info_fo.read(), requires_fo and requires_fo.read()
8787
)

metadata_please/tests/sdist.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def test_requires_as_expected(self) -> None:
1515
z = MemoryZipFile(
1616
{
1717
"foo/__init__.py": b"",
18+
"foo.egg-info/PKG-INFO": b"\n",
1819
"foo.egg-info/requires.txt": b"""\
1920
a
2021
[e]
@@ -36,6 +37,7 @@ def test_basic_metadata(self) -> None:
3637
z = MemoryZipFile(
3738
{
3839
"foo/__init__.py": b"",
40+
"foo.egg-info/PKG-INFO": b"\n",
3941
"foo.egg-info/requires.txt": b"""\
4042
a
4143
[e]
@@ -68,6 +70,7 @@ def test_basic_metadata_absl_py_09(self) -> None:
6870
z = MemoryZipFile(
6971
{
7072
"foo/__init__.py": b"",
73+
"foo.egg-info/PKG-INFO": b"\n",
7174
"foo.egg-info/requires.txt": b"""\
7275
six
7376
@@ -94,7 +97,7 @@ def test_basic_metadata_absl_py_09(self) -> None:
9497
class TarSdistTest(unittest.TestCase):
9598
def test_requires_as_expected(self) -> None:
9699
t = MemoryTarFile(
97-
["foo.egg-info/requires.txt", "foo/__init__.py"],
100+
["foo.egg-info/PKG-INFO", "foo.egg-info/requires.txt", "foo/__init__.py"],
98101
read_value=b"""\
99102
a
100103
[e]
@@ -113,7 +116,7 @@ def test_requires_as_expected(self) -> None:
113116

114117
def test_basic_metadata(self) -> None:
115118
t = MemoryTarFile(
116-
["foo.egg-info/requires.txt", "foo/__init__.py"],
119+
["foo.egg-info/PKG-INFO", "foo.egg-info/requires.txt", "foo/__init__.py"],
117120
read_value=b"""\
118121
a
119122
[e]
@@ -126,3 +129,18 @@ def test_basic_metadata(self) -> None:
126129
bm.reqs,
127130
)
128131
self.assertEqual({"e"}, bm.provides_extra)
132+
133+
def test_metadata_fields_from_tar_sdist(self) -> None:
134+
t = MemoryTarFile(
135+
["foo.egg-info/PKG-INFO", "foo/__init__.py"],
136+
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""",
137+
)
138+
bm = basic_metadata_from_tar_sdist(t) # type: ignore
139+
self.assertEqual("1.2.58", bm.version)
140+
self.assertEqual("Some Summary", bm.summary)
141+
self.assertEqual("http://example.com", bm.url)
142+
self.assertEqual("Chicken", bm.author)
143+
self.assertEqual("[email protected]", bm.author_email)
144+
self.assertEqual("farm,animals", bm.keywords)
145+
self.assertEqual("text/markdown", bm.long_description_content_type)
146+
self.assertEqual(None, bm.description)

0 commit comments

Comments
 (0)