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_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_unsupported_hash_algorithm(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,sha000=bv-QV3RciQC2v3zL8Uvhd_arp40J5A9xmyubN34OVwo,25')) exc = pytest.raises(WheelError, WheelFile, wheel_path) exc.match("^Unsupported hash algorithm: sha000$")
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_missing_record(wheel_path): with ZipFile(wheel_path, 'w') as zf: zf.writestr(native('hello/héllö.py'), as_bytes('print("Héllö, w0rld!")\n')) exc = pytest.raises(WheelError, WheelFile, wheel_path) exc.match("^Missing test-1.0.dist-info/RECORD file$")
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')) with ZipFile(wheel_path, 'r') as zf: infolist = zf.infolist() assert len(infolist) == 2 assert infolist[0].filename == native('hello/héllö.py') assert infolist[0].file_size == 25 assert infolist[1].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' 'test-1.0.dist-info/RECORD,,\n')
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'$"))
def close(self): # Write RECORD if self.fp is not None and self.mode == 'w' and self._file_hashes: content = '\n'.join( '{},{}={},{}'.format(fname, algorithm, hash_, self._file_sizes[fname]) for fname, (algorithm, hash_) in self._file_hashes.items()) content += '\n{},,\n'.format(self.record_path) zinfo = ZipInfo(native(self.record_path), date_time=get_zipinfo_datetime()) zinfo.compress_type = ZIP_DEFLATED self.writestr(zinfo, as_bytes(content)) super(WheelFile, self).close()
def close(self): # Write RECORD if self.fp is not None and self.mode == "w" and self._file_hashes: content = "\n".join( "{},{}={},{}".format(fname, algorithm, hash_, self._file_sizes[fname]) for fname, (algorithm, hash_) in self._file_hashes.items()) content += "\n{},,\n".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(content)) ZipFile.close(self)
def close(self): # Write RECORD 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)