1313# limitations under the License.
1414import os
1515import pathlib
16- import re
1716import zipfile
1817
19- import pretend
2018import pytest
2119
2220from twine import exceptions
@@ -40,26 +38,24 @@ def test_version_parsing(example_wheel):
4038 assert example_wheel .py_version == "py2.py3"
4139
4240
43- def test_version_parsing_missing_pyver (monkeypatch , example_wheel ):
44- wheel .wheel_file_re = pretend .stub (match = lambda a : None )
45- assert example_wheel .py_version == "any"
41+ @pytest .mark .parametrize (
42+ "file_name,valid" ,
43+ [
44+ ("name-1.2.3-py313-none-any.whl" , True ),
45+ ("name-1.2.3-42-py313-none-any.whl" , True ),
46+ ("long_name-1.2.3-py3-none-any.whl" , True ),
47+ ("missing_components-1.2.3.whl" , False ),
48+ ],
49+ )
50+ def test_parse_wheel_file_name (file_name , valid ):
51+ assert bool (wheel .wheel_file_re .match (file_name )) == valid
4652
4753
48- def test_find_metadata_files ():
49- names = [
50- "package/lib/__init__.py" ,
51- "package/lib/version.py" ,
52- "package/METADATA.txt" ,
53- "package/METADATA.json" ,
54- "package/METADATA" ,
55- ]
56- expected = [
57- ["package" , "METADATA" ],
58- ["package" , "METADATA.json" ],
59- ["package" , "METADATA.txt" ],
60- ]
61- candidates = wheel .Wheel .find_candidate_metadata_files (names )
62- assert expected == candidates
54+ def test_invalid_file_name (monkeypatch ):
55+ parent = pathlib .Path (__file__ ).parent
56+ file_name = str (parent / "fixtures" / "twine-1.5.0-py2.whl" )
57+ with pytest .raises (exceptions .InvalidDistribution , match = "Invalid wheel filename" ):
58+ wheel .Wheel (file_name )
6359
6460
6561def test_read_valid (example_wheel ):
@@ -71,33 +67,35 @@ def test_read_valid(example_wheel):
7167
7268def test_read_non_existent_wheel_file_name ():
7369 """Raise an exception when wheel file doesn't exist."""
74- file_name = str (pathlib .Path ("/foo/bar/baz.whl" ).resolve ())
75- with pytest .raises (
76- exceptions .InvalidDistribution , match = re .escape (f"No such file: { file_name } " )
77- ):
70+ file_name = str (pathlib .Path ("/foo/bar/baz-1.2.3-py3-none-any.whl" ).resolve ())
71+ with pytest .raises (exceptions .InvalidDistribution , match = "No such file" ):
7872 wheel .Wheel (file_name ).read ()
7973
8074
8175def test_read_invalid_wheel_extension ():
8276 """Raise an exception when file is missing .whl extension."""
8377 file_name = str (pathlib .Path (__file__ ).parent / "fixtures" / "twine-1.5.0.tar.gz" )
84- with pytest .raises (
85- exceptions .InvalidDistribution ,
86- match = re .escape (f"Not a known archive format for file: { file_name } " ),
87- ):
78+ with pytest .raises (exceptions .InvalidDistribution , match = "Invalid wheel filename" ):
79+ wheel .Wheel (file_name ).read ()
80+
81+
82+ def test_read_wheel_missing_metadata (example_wheel , monkeypatch ):
83+ """Raise an exception when a wheel file is missing METADATA."""
84+
85+ def patch (self , name ):
86+ raise KeyError
87+
88+ monkeypatch .setattr (zipfile .ZipFile , "read" , patch )
89+ parent = pathlib .Path (__file__ ).parent
90+ file_name = str (parent / "fixtures" / "twine-1.5.0-py2.py3-none-any.whl" )
91+ with pytest .raises (exceptions .InvalidDistribution , match = "No METADATA in archive" ):
8892 wheel .Wheel (file_name ).read ()
8993
9094
91- def test_read_wheel_empty_metadata (tmpdir ):
95+ def test_read_wheel_empty_metadata (example_wheel , monkeypatch ):
9296 """Raise an exception when a wheel file is missing METADATA."""
93- whl_file = tmpdir .mkdir ("wheel" ).join ("not-a-wheel.whl" )
94- with zipfile .ZipFile (whl_file , "w" ) as zip_file :
95- zip_file .writestr ("METADATA" , "" )
96-
97- with pytest .raises (
98- exceptions .InvalidDistribution ,
99- match = re .escape (
100- f"No METADATA in archive or METADATA missing 'Metadata-Version': { whl_file } "
101- ),
102- ):
103- wheel .Wheel (whl_file ).read ()
97+ monkeypatch .setattr (zipfile .ZipFile , "read" , lambda self , name : b"" )
98+ parent = pathlib .Path (__file__ ).parent
99+ file_name = str (parent / "fixtures" / "twine-1.5.0-py2.py3-none-any.whl" )
100+ with pytest .raises (exceptions .InvalidDistribution , match = "No METADATA in archive" ):
101+ wheel .Wheel (file_name ).read ()
0 commit comments