1- import io
21import os
32import pathlib
43import tarfile
5- import textwrap
6- import zipfile
74
85import pytest
96
107from twine import exceptions
118from twine import sdist
129
1310from .helpers import TESTS_DIR
11+ from .helpers import build_archive
1412
1513
1614@pytest .fixture (
@@ -29,31 +27,6 @@ def archive_format(request):
2927 return request .param
3028
3129
32- def build_archive (path , name , archive_format , files ):
33- filepath = path / f"{ name } .{ archive_format } "
34-
35- if archive_format == "tar.gz" :
36- with tarfile .open (filepath , "x:gz" ) as archive :
37- for mname , content in files .items ():
38- if isinstance (content , tarfile .TarInfo ):
39- content .name = mname
40- archive .addfile (content )
41- else :
42- data = textwrap .dedent (content ).encode ("utf8" )
43- member = tarfile .TarInfo (mname )
44- member .size = len (data )
45- archive .addfile (member , io .BytesIO (data ))
46- return str (filepath )
47-
48- if archive_format == "zip" :
49- with zipfile .ZipFile (filepath , mode = "w" ) as archive :
50- for mname , content in files .items ():
51- archive .writestr (mname , textwrap .dedent (content ))
52- return str (filepath )
53-
54- raise ValueError (format )
55-
56-
5730def test_read_example (example_sdist ):
5831 """Parse metadata from a valid sdist file."""
5932 metadata = example_sdist .read ()
@@ -78,7 +51,7 @@ def test_formar_not_supported():
7851
7952def test_read (archive_format , tmp_path ):
8053 """Read PKG-INFO from a valid sdist."""
81- filename = build_archive (
54+ filepath = build_archive (
8255 tmp_path ,
8356 "test-1.2.3" ,
8457 archive_format ,
@@ -92,15 +65,15 @@ def test_read(archive_format, tmp_path):
9265 },
9366 )
9467
95- metadata = sdist .SDist (filename ).read ()
68+ metadata = sdist .SDist (str ( filepath ) ).read ()
9669 assert b"Metadata-Version: 1.1" in metadata
9770 assert b"Name: test" in metadata
9871 assert b"Version: 1.2.3" in metadata
9972
10073
10174def test_missing_pkg_info (archive_format , tmp_path ):
10275 """Raise an exception when sdist does not contain PKG-INFO."""
103- filename = build_archive (
76+ filepath = build_archive (
10477 tmp_path ,
10578 "test-1.2.3" ,
10679 archive_format ,
@@ -110,12 +83,12 @@ def test_missing_pkg_info(archive_format, tmp_path):
11083 )
11184
11285 with pytest .raises (exceptions .InvalidDistribution , match = "No PKG-INFO in archive" ):
113- sdist .SDist (filename ).read ()
86+ sdist .SDist (str ( filepath ) ).read ()
11487
11588
11689def test_invalid_pkg_info (archive_format , tmp_path ):
11790 """Raise an exception when PKG-INFO does not contain ``Metadata-Version``."""
118- filename = build_archive (
91+ filepath = build_archive (
11992 tmp_path ,
12093 "test-1.2.3" ,
12194 archive_format ,
@@ -129,12 +102,12 @@ def test_invalid_pkg_info(archive_format, tmp_path):
129102 )
130103
131104 with pytest .raises (exceptions .InvalidDistribution , match = "No PKG-INFO in archive" ):
132- sdist .SDist (filename ).read ()
105+ sdist .SDist (str ( filepath ) ).read ()
133106
134107
135108def test_pkg_info_directory (archive_format , tmp_path ):
136109 """Raise an exception when PKG-INFO is a directory."""
137- filename = build_archive (
110+ filepath = build_archive (
138111 tmp_path ,
139112 "test-1.2.3" ,
140113 archive_format ,
@@ -149,7 +122,7 @@ def test_pkg_info_directory(archive_format, tmp_path):
149122 )
150123
151124 with pytest .raises (exceptions .InvalidDistribution , match = "No PKG-INFO in archive" ):
152- sdist .SDist (filename ).read ()
125+ sdist .SDist (str ( filepath ) ).read ()
153126
154127
155128def test_pkg_info_not_regular_file (tmp_path ):
@@ -158,7 +131,7 @@ def test_pkg_info_not_regular_file(tmp_path):
158131 link .type = tarfile .LNKTYPE
159132 link .linkname = "README"
160133
161- filename = build_archive (
134+ filepath = build_archive (
162135 tmp_path ,
163136 "test-1.2.3" ,
164137 "tar.gz" ,
@@ -169,12 +142,12 @@ def test_pkg_info_not_regular_file(tmp_path):
169142 )
170143
171144 with pytest .raises (exceptions .InvalidDistribution , match = "PKG-INFO is not a reg" ):
172- sdist .SDist (filename ).read ()
145+ sdist .SDist (str ( filepath ) ).read ()
173146
174147
175148def test_multiple_top_level (archive_format , tmp_path ):
176149 """Raise an exception when there are too many top-level members."""
177- filename = build_archive (
150+ filepath = build_archive (
178151 tmp_path ,
179152 "test-1.2.3" ,
180153 archive_format ,
@@ -190,7 +163,7 @@ def test_multiple_top_level(archive_format, tmp_path):
190163 )
191164
192165 with pytest .raises (exceptions .InvalidDistribution , match = "Too many top-level" ):
193- sdist .SDist (filename ).read ()
166+ sdist .SDist (str ( filepath ) ).read ()
194167
195168
196169def test_py_version (example_sdist ):
0 commit comments