1
- import io
2
1
import os
3
2
import pathlib
4
3
import tarfile
5
- import textwrap
6
- import zipfile
7
4
8
5
import pytest
9
6
10
7
from twine import exceptions
11
8
from twine import sdist
12
9
13
10
from .helpers import TESTS_DIR
11
+ from .helpers import build_archive
14
12
15
13
16
14
@pytest .fixture (
@@ -29,31 +27,6 @@ def archive_format(request):
29
27
return request .param
30
28
31
29
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
-
57
30
def test_read_example (example_sdist ):
58
31
"""Parse metadata from a valid sdist file."""
59
32
metadata = example_sdist .read ()
@@ -78,7 +51,7 @@ def test_formar_not_supported():
78
51
79
52
def test_read (archive_format , tmp_path ):
80
53
"""Read PKG-INFO from a valid sdist."""
81
- filename = build_archive (
54
+ filepath = build_archive (
82
55
tmp_path ,
83
56
"test-1.2.3" ,
84
57
archive_format ,
@@ -92,15 +65,15 @@ def test_read(archive_format, tmp_path):
92
65
},
93
66
)
94
67
95
- metadata = sdist .SDist (filename ).read ()
68
+ metadata = sdist .SDist (str ( filepath ) ).read ()
96
69
assert b"Metadata-Version: 1.1" in metadata
97
70
assert b"Name: test" in metadata
98
71
assert b"Version: 1.2.3" in metadata
99
72
100
73
101
74
def test_missing_pkg_info (archive_format , tmp_path ):
102
75
"""Raise an exception when sdist does not contain PKG-INFO."""
103
- filename = build_archive (
76
+ filepath = build_archive (
104
77
tmp_path ,
105
78
"test-1.2.3" ,
106
79
archive_format ,
@@ -110,12 +83,12 @@ def test_missing_pkg_info(archive_format, tmp_path):
110
83
)
111
84
112
85
with pytest .raises (exceptions .InvalidDistribution , match = "No PKG-INFO in archive" ):
113
- sdist .SDist (filename ).read ()
86
+ sdist .SDist (str ( filepath ) ).read ()
114
87
115
88
116
89
def test_invalid_pkg_info (archive_format , tmp_path ):
117
90
"""Raise an exception when PKG-INFO does not contain ``Metadata-Version``."""
118
- filename = build_archive (
91
+ filepath = build_archive (
119
92
tmp_path ,
120
93
"test-1.2.3" ,
121
94
archive_format ,
@@ -129,12 +102,12 @@ def test_invalid_pkg_info(archive_format, tmp_path):
129
102
)
130
103
131
104
with pytest .raises (exceptions .InvalidDistribution , match = "No PKG-INFO in archive" ):
132
- sdist .SDist (filename ).read ()
105
+ sdist .SDist (str ( filepath ) ).read ()
133
106
134
107
135
108
def test_pkg_info_directory (archive_format , tmp_path ):
136
109
"""Raise an exception when PKG-INFO is a directory."""
137
- filename = build_archive (
110
+ filepath = build_archive (
138
111
tmp_path ,
139
112
"test-1.2.3" ,
140
113
archive_format ,
@@ -149,7 +122,7 @@ def test_pkg_info_directory(archive_format, tmp_path):
149
122
)
150
123
151
124
with pytest .raises (exceptions .InvalidDistribution , match = "No PKG-INFO in archive" ):
152
- sdist .SDist (filename ).read ()
125
+ sdist .SDist (str ( filepath ) ).read ()
153
126
154
127
155
128
def test_pkg_info_not_regular_file (tmp_path ):
@@ -158,7 +131,7 @@ def test_pkg_info_not_regular_file(tmp_path):
158
131
link .type = tarfile .LNKTYPE
159
132
link .linkname = "README"
160
133
161
- filename = build_archive (
134
+ filepath = build_archive (
162
135
tmp_path ,
163
136
"test-1.2.3" ,
164
137
"tar.gz" ,
@@ -169,12 +142,12 @@ def test_pkg_info_not_regular_file(tmp_path):
169
142
)
170
143
171
144
with pytest .raises (exceptions .InvalidDistribution , match = "PKG-INFO is not a reg" ):
172
- sdist .SDist (filename ).read ()
145
+ sdist .SDist (str ( filepath ) ).read ()
173
146
174
147
175
148
def test_multiple_top_level (archive_format , tmp_path ):
176
149
"""Raise an exception when there are too many top-level members."""
177
- filename = build_archive (
150
+ filepath = build_archive (
178
151
tmp_path ,
179
152
"test-1.2.3" ,
180
153
archive_format ,
@@ -190,7 +163,7 @@ def test_multiple_top_level(archive_format, tmp_path):
190
163
)
191
164
192
165
with pytest .raises (exceptions .InvalidDistribution , match = "Too many top-level" ):
193
- sdist .SDist (filename ).read ()
166
+ sdist .SDist (str ( filepath ) ).read ()
194
167
195
168
196
169
def test_py_version (example_sdist ):
0 commit comments