Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_testzip(wheel_path, algorithm, digest):
hash_string = '{}={}'.format(algorithm, digest)
with ZipFile(wheel_path, 'w') as zf:
zf.writestr(native('hello/héllö.py'), as_bytes('print("Héllö, world!")\n'))
zf.writestr('test-1.0.dist-info/RECORD',
as_bytes('hello/héllö.py,{},25'.format(hash_string)))
with WheelFile(wheel_path) as wf:
wf.testzip()
def test_write_str(wheel_path):
with WheelFile(wheel_path, 'w') as wf:
wf.writestr(native('hello/héllö.py'), as_bytes('print("Héllö, world!")\n'))
wf.writestr(native('hello/h,ll,.py'), as_bytes('print("Héllö, world!")\n'))
with ZipFile(wheel_path, 'r') as zf:
infolist = zf.infolist()
assert len(infolist) == 3
assert infolist[0].filename == native('hello/héllö.py')
assert infolist[0].file_size == 25
assert infolist[1].filename == native('hello/h,ll,.py')
assert infolist[1].file_size == 25
assert infolist[2].filename == 'test-1.0.dist-info/RECORD'
record = zf.read('test-1.0.dist-info/RECORD')
assert record == as_bytes(
'hello/héllö.py,sha256=bv-QV3RciQC2v3zL8Uvhd_arp40J5A9xmyubN34OVwo,25\n'
'"hello/h,ll,.py",sha256=bv-QV3RciQC2v3zL8Uvhd_arp40J5A9xmyubN34OVwo,25\n'
def test_testzip(wheel_path, algorithm, digest):
hash_string = '{}={}'.format(algorithm, digest)
with ZipFile(wheel_path, 'w') as zf:
zf.writestr(native('hello/héllö.py'), as_bytes('print("Héllö, world!")\n'))
zf.writestr('test-1.0.dist-info/RECORD',
as_bytes('hello/héllö.py,{},25'.format(hash_string)))
with WheelFile(wheel_path) as wf:
wf.testzip()
def test_testzip_bad_hash(wheel_path):
with ZipFile(wheel_path, 'w') as zf:
zf.writestr(native('hello/héllö.py'), as_bytes('print("Héllö, w0rld!")\n'))
zf.writestr(
'test-1.0.dist-info/RECORD',
as_bytes('hello/héllö.py,sha256=bv-QV3RciQC2v3zL8Uvhd_arp40J5A9xmyubN34OVwo,25'))
with WheelFile(wheel_path) as wf:
exc = pytest.raises(WheelError, wf.testzip)
exc.match(native("^Hash mismatch for file 'hello/héllö.py'$"))
def test_weak_hash_algorithm(wheel_path, algorithm, digest):
hash_string = '{}={}'.format(algorithm, digest)
with ZipFile(wheel_path, 'w') as zf:
zf.writestr(native('hello/héllö.py'), as_bytes('print("Héllö, w0rld!")\n'))
zf.writestr('test-1.0.dist-info/RECORD',
as_bytes('hello/héllö.py,{},25'.format(hash_string)))
exc = pytest.raises(WheelError, WheelFile, wheel_path)
exc.match(r"^Weak hash algorithm \({}\) is not permitted by PEP 427$".format(algorithm))
def test_write_str(wheel_path):
with WheelFile(wheel_path, 'w') as wf:
wf.writestr(native('hello/héllö.py'), as_bytes('print("Héllö, world!")\n'))
wf.writestr(native('hello/h,ll,.py'), as_bytes('print("Héllö, world!")\n'))
with ZipFile(wheel_path, 'r') as zf:
infolist = zf.infolist()
assert len(infolist) == 3
assert infolist[0].filename == native('hello/héllö.py')
assert infolist[0].file_size == 25
assert infolist[1].filename == native('hello/h,ll,.py')
assert infolist[1].file_size == 25
assert infolist[2].filename == 'test-1.0.dist-info/RECORD'
record = zf.read('test-1.0.dist-info/RECORD')
assert record == as_bytes(
'hello/héllö.py,sha256=bv-QV3RciQC2v3zL8Uvhd_arp40J5A9xmyubN34OVwo,25\n'
'"hello/h,ll,.py",sha256=bv-QV3RciQC2v3zL8Uvhd_arp40J5A9xmyubN34OVwo,25\n'
def test_testzip(wheel_path, algorithm, digest):
hash_string = '{}={}'.format(algorithm, digest)
with ZipFile(wheel_path, 'w') as zf:
zf.writestr(native('hello/héllö.py'), as_bytes('print("Héllö, world!")\n'))
zf.writestr('test-1.0.dist-info/RECORD',
as_bytes('hello/héllö.py,{},25'.format(hash_string)))
with WheelFile(wheel_path) as wf:
wf.testzip()
def test_testzip_missing_hash(wheel_path):
with ZipFile(wheel_path, 'w') as zf:
zf.writestr(native('hello/héllö.py'), as_bytes('print("Héllö, world!")\n'))
zf.writestr('test-1.0.dist-info/RECORD', '')
with WheelFile(wheel_path) as wf:
exc = pytest.raises(WheelError, wf.testzip)
exc.match(native("^No hash found for file 'hello/héllö.py'$"))
if self.fp is not None and self.mode == 'w' and self._file_hashes:
data = StringIO()
writer = csv.writer(data, delimiter=',', quotechar='"', lineterminator='\n')
writer.writerows((
(
fname,
algorithm + "=" + hash_,
self._file_sizes[fname]
)
for fname, (algorithm, hash_) in self._file_hashes.items()
))
writer.writerow((format(self.record_path), "", ""))
zinfo = ZipInfo(native(self.record_path), date_time=get_zipinfo_datetime())
zinfo.compress_type = ZIP_DEFLATED
zinfo.external_attr = 0o664 << 16
self.writestr(zinfo, as_bytes(data.getvalue()))
ZipFile.close(self)
if self.fp is not None and self.mode == 'w' and self._file_hashes:
data = StringIO()
writer = csv.writer(data, delimiter=',', quotechar='"', lineterminator='\n')
writer.writerows((
(
fname,
algorithm + "=" + hash_,
self._file_sizes[fname]
)
for fname, (algorithm, hash_) in self._file_hashes.items()
))
writer.writerow((format(self.record_path), "", ""))
zinfo = ZipInfo(native(self.record_path), date_time=get_zipinfo_datetime())
zinfo.compress_type = self.compression
zinfo.external_attr = 0o664 << 16
self.writestr(zinfo, as_bytes(data.getvalue()))
ZipFile.close(self)