Improve ArchiveChecksumError error message

This commit is contained in:
David Dalcino
2021-10-04 07:42:06 -07:00
parent 4b99ee5755
commit a06ba49e63
2 changed files with 10 additions and 6 deletions

View File

@@ -77,7 +77,7 @@ def getUrl(url: str, timeout) -> str:
return result
def downloadBinaryFile(url: str, out: str, hash_algo: str, exp: str, timeout):
def downloadBinaryFile(url: str, out: str, hash_algo: str, exp: bytes, timeout):
logger = getLogger("aqt.helper")
filename = Path(url).name
with requests.Session() as session:
@@ -109,7 +109,8 @@ def downloadBinaryFile(url: str, out: str, hash_algo: str, exp: str, timeout):
if exp is not None and hash.digest() != exp:
raise ArchiveChecksumError(
f"Downloaded file {filename} is corrupted! Detect checksum error.\n"
f"Expected {exp}, Actual {hash.digest()}"
f"Expect {exp.hex()}: {url}\n"
f"Actual {hash.digest().hex()}: {out}"
)

View File

@@ -129,12 +129,15 @@ def test_helper_downloadBinary_wrong_checksum(tmp_path, monkeypatch):
actual_hash = binascii.unhexlify("1d41a93e4a585bb01e4518d4af431933")
wrong_hash = binascii.unhexlify("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
expected_err = (
f"Downloaded file test.xml is corrupted! Detect checksum error.\nExpected {wrong_hash}, Actual {actual_hash}"
)
out = tmp_path.joinpath("test.xml")
url = "http://example.com/test.xml"
expected_err = (
f"Downloaded file test.xml is corrupted! Detect checksum error."
f"\nExpect {wrong_hash.hex()}: {url}"
f"\nActual {actual_hash.hex()}: {out}"
)
with pytest.raises(ArchiveChecksumError) as e:
helper.downloadBinaryFile("http://example.com/test.xml", out, "md5", wrong_hash, 60)
helper.downloadBinaryFile(url, str(out), "md5", wrong_hash, 60)
assert e.type == ArchiveChecksumError
assert format(e.value) == expected_err