Skip to content

Commit 7d3f41b

Browse files
committed
Fix tests by using .zip content for a .whl
Tests were failing, because wheel upload test used .tar.gz content, and zipfile could not open that content to extract METADATA (which was also absent). The fix adds two helpers to avoid repeated code.
1 parent 7aeff69 commit 7d3f41b

File tree

1 file changed

+41
-12
lines changed

1 file changed

+41
-12
lines changed

tests/unit/forklift/test_legacy.py

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,26 @@ def _get_tar_testdata(compression_type=""):
6767
return temp_f.getvalue()
6868

6969

70+
def _get_whl_testdata(name="fake_package", version="1.0"):
71+
temp_f = io.BytesIO()
72+
with zipfile.ZipFile(file=temp_f, mode="w") as zfp:
73+
zfp.writestr(f"{name}-{version}.dist-info/METADATA", "Fake metadata")
74+
return temp_f.getvalue()
75+
76+
77+
def _storage_hash(data):
78+
return hashlib.blake2b(data, digest_size=256 // 8).hexdigest()
79+
80+
7081
_TAR_GZ_PKG_TESTDATA = _get_tar_testdata("gz")
7182
_TAR_GZ_PKG_MD5 = hashlib.md5(_TAR_GZ_PKG_TESTDATA).hexdigest()
7283
_TAR_GZ_PKG_SHA256 = hashlib.sha256(_TAR_GZ_PKG_TESTDATA).hexdigest()
73-
_TAR_GZ_PKG_STORAGE_HASH = hashlib.blake2b(
74-
_TAR_GZ_PKG_TESTDATA, digest_size=256 // 8
75-
).hexdigest()
84+
_TAR_GZ_PKG_STORAGE_HASH = _storage_hash(_TAR_GZ_PKG_TESTDATA)
7685

7786
_TAR_BZ2_PKG_TESTDATA = _get_tar_testdata("bz2")
7887
_TAR_BZ2_PKG_MD5 = hashlib.md5(_TAR_BZ2_PKG_TESTDATA).hexdigest()
7988
_TAR_BZ2_PKG_SHA256 = hashlib.sha256(_TAR_BZ2_PKG_TESTDATA).hexdigest()
80-
_TAR_BZ2_PKG_STORAGE_HASH = hashlib.blake2b(
81-
_TAR_BZ2_PKG_TESTDATA, digest_size=256 // 8
82-
).hexdigest()
89+
_TAR_BZ2_PKG_STORAGE_HASH = _storage_hash(_TAR_BZ2_PKG_TESTDATA)
8390

8491

8592
class TestExcWithMessage:
@@ -2599,6 +2606,8 @@ def test_upload_succeeds_with_wheel(
25992606
RoleFactory.create(user=user, project=project)
26002607

26012608
filename = "{}-{}-cp34-none-{}.whl".format(project.name, release.version, plat)
2609+
filebody = _get_whl_testdata(project.name)
2610+
file_storage_hash = _storage_hash(filebody)
26022611

26032612
db_request.user = user
26042613
db_request.user_agent = "warehouse-tests/6.6.6"
@@ -2609,10 +2618,10 @@ def test_upload_succeeds_with_wheel(
26092618
"version": release.version,
26102619
"filetype": "bdist_wheel",
26112620
"pyversion": "cp34",
2612-
"md5_digest": _TAR_GZ_PKG_MD5,
2621+
"md5_digest": hashlib.md5(filebody).hexdigest(),
26132622
"content": pretend.stub(
26142623
filename=filename,
2615-
file=io.BytesIO(_TAR_GZ_PKG_TESTDATA),
2624+
file=io.BytesIO(filebody),
26162625
type="application/tar",
26172626
),
26182627
}
@@ -2621,7 +2630,10 @@ def test_upload_succeeds_with_wheel(
26212630
@pretend.call_recorder
26222631
def storage_service_store(path, file_path, *, meta):
26232632
with open(file_path, "rb") as fp:
2624-
assert fp.read() == _TAR_GZ_PKG_TESTDATA
2633+
if file_path.endswith(".metadata"):
2634+
assert fp.read() == b"Fake metadata"
2635+
else:
2636+
assert fp.read() == filebody
26252637

26262638
storage_service = pretend.stub(store=storage_service_store)
26272639

@@ -2645,9 +2657,9 @@ def storage_service_store(path, file_path, *, meta):
26452657
pretend.call(
26462658
"/".join(
26472659
[
2648-
_TAR_GZ_PKG_STORAGE_HASH[:2],
2649-
_TAR_GZ_PKG_STORAGE_HASH[2:4],
2650-
_TAR_GZ_PKG_STORAGE_HASH[4:],
2660+
file_storage_hash[:2],
2661+
file_storage_hash[2:4],
2662+
file_storage_hash[4:],
26512663
filename,
26522664
]
26532665
),
@@ -2658,6 +2670,23 @@ def storage_service_store(path, file_path, *, meta):
26582670
"package-type": "bdist_wheel",
26592671
"python-version": "cp34",
26602672
},
2673+
),
2674+
pretend.call(
2675+
"/".join(
2676+
[
2677+
file_storage_hash[:2],
2678+
file_storage_hash[2:4],
2679+
file_storage_hash[4:],
2680+
filename + '.metadata',
2681+
]
2682+
),
2683+
mock.ANY,
2684+
meta={
2685+
"project": project.normalized_name,
2686+
"version": release.version,
2687+
"package-type": "bdist_wheel",
2688+
"python-version": "cp34",
2689+
},
26612690
)
26622691
]
26632692

0 commit comments

Comments
 (0)