1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414import pathlib
15- import re
1615import zipfile
1716
18- import pretend
1917import pytest
2018
2119from twine import exceptions
@@ -33,26 +31,24 @@ def test_version_parsing(example_wheel):
3331 assert example_wheel .py_version == "py2.py3"
3432
3533
36- def test_version_parsing_missing_pyver (monkeypatch , example_wheel ):
37- wheel .wheel_file_re = pretend .stub (match = lambda a : None )
38- assert example_wheel .py_version == "any"
34+ @pytest .mark .parametrize (
35+ "file_name,valid" ,
36+ [
37+ ("name-1.2.3-py313-none-any.whl" , True ),
38+ ("name-1.2.3-42-py313-none-any.whl" , True ),
39+ ("long_name-1.2.3-py3-none-any.whl" , True ),
40+ ("missing_components-1.2.3.whl" , False ),
41+ ],
42+ )
43+ def test_parse_wheel_file_name (file_name , valid ):
44+ assert bool (wheel .wheel_file_re .match (file_name )) == valid
3945
4046
41- def test_find_metadata_files ():
42- names = [
43- "package/lib/__init__.py" ,
44- "package/lib/version.py" ,
45- "package/METADATA.txt" ,
46- "package/METADATA.json" ,
47- "package/METADATA" ,
48- ]
49- expected = [
50- ["package" , "METADATA" ],
51- ["package" , "METADATA.json" ],
52- ["package" , "METADATA.txt" ],
53- ]
54- candidates = wheel .Wheel .find_candidate_metadata_files (names )
55- assert expected == candidates
47+ def test_invalid_file_name (monkeypatch ):
48+ parent = pathlib .Path (__file__ ).parent
49+ file_name = str (parent / "fixtures" / "twine-1.5.0-py2.whl" )
50+ with pytest .raises (exceptions .InvalidDistribution , match = "Invalid wheel filename" ):
51+ wheel .Wheel (file_name )
5652
5753
5854def test_read_valid (example_wheel ):
@@ -64,33 +60,35 @@ def test_read_valid(example_wheel):
6460
6561def test_read_non_existent_wheel_file_name ():
6662 """Raise an exception when wheel file doesn't exist."""
67- file_name = str (pathlib .Path ("/foo/bar/baz.whl" ).resolve ())
68- with pytest .raises (
69- exceptions .InvalidDistribution , match = re .escape (f"No such file: { file_name } " )
70- ):
63+ file_name = str (pathlib .Path ("/foo/bar/baz-1.2.3-py3-none-any.whl" ).resolve ())
64+ with pytest .raises (exceptions .InvalidDistribution , match = "No such file" ):
7165 wheel .Wheel (file_name ).read ()
7266
7367
7468def test_read_invalid_wheel_extension ():
7569 """Raise an exception when file is missing .whl extension."""
7670 file_name = str (pathlib .Path (__file__ ).parent / "fixtures" / "twine-1.5.0.tar.gz" )
77- with pytest .raises (
78- exceptions .InvalidDistribution ,
79- match = re .escape (f"Not a known archive format for file: { file_name } " ),
80- ):
71+ with pytest .raises (exceptions .InvalidDistribution , match = "Invalid wheel filename" ):
72+ wheel .Wheel (file_name ).read ()
73+
74+
75+ def test_read_wheel_missing_metadata (example_wheel , monkeypatch ):
76+ """Raise an exception when a wheel file is missing METADATA."""
77+
78+ def patch (self , name ):
79+ raise KeyError
80+
81+ monkeypatch .setattr (zipfile .ZipFile , "read" , patch )
82+ parent = pathlib .Path (__file__ ).parent
83+ file_name = str (parent / "fixtures" / "twine-1.5.0-py2.py3-none-any.whl" )
84+ with pytest .raises (exceptions .InvalidDistribution , match = "No METADATA in archive" ):
8185 wheel .Wheel (file_name ).read ()
8286
8387
84- def test_read_wheel_empty_metadata (tmpdir ):
88+ def test_read_wheel_empty_metadata (example_wheel , monkeypatch ):
8589 """Raise an exception when a wheel file is missing METADATA."""
86- whl_file = tmpdir .mkdir ("wheel" ).join ("not-a-wheel.whl" )
87- with zipfile .ZipFile (whl_file , "w" ) as zip_file :
88- zip_file .writestr ("METADATA" , "" )
89-
90- with pytest .raises (
91- exceptions .InvalidDistribution ,
92- match = re .escape (
93- f"No METADATA in archive or METADATA missing 'Metadata-Version': { whl_file } "
94- ),
95- ):
96- wheel .Wheel (whl_file ).read ()
90+ monkeypatch .setattr (zipfile .ZipFile , "read" , lambda self , name : b"" )
91+ parent = pathlib .Path (__file__ ).parent
92+ file_name = str (parent / "fixtures" / "twine-1.5.0-py2.py3-none-any.whl" )
93+ with pytest .raises (exceptions .InvalidDistribution , match = "No METADATA in archive" ):
94+ wheel .Wheel (file_name ).read ()
0 commit comments