For zip compression, only compare the CRC-32 checksum of the file contents to avoid checking the time-dependent last-modified timestamp which in some CI builds is off-by-one See https://en.wikipedia.org/wiki/ZIP_(file_format)#File_headers
(result: bytes, expected: bytes, compression: str)
| 119 | |
| 120 | |
| 121 | def assert_equal_zip_safe(result: bytes, expected: bytes, compression: str): |
| 122 | """ |
| 123 | For zip compression, only compare the CRC-32 checksum of the file contents |
| 124 | to avoid checking the time-dependent last-modified timestamp which |
| 125 | in some CI builds is off-by-one |
| 126 | |
| 127 | See https://en.wikipedia.org/wiki/ZIP_(file_format)#File_headers |
| 128 | """ |
| 129 | if compression == "zip": |
| 130 | # Only compare the CRC checksum of the file contents |
| 131 | with ( |
| 132 | zipfile.ZipFile(BytesIO(result)) as exp, |
| 133 | zipfile.ZipFile(BytesIO(expected)) as res, |
| 134 | ): |
| 135 | for res_info, exp_info in zip(res.infolist(), exp.infolist()): |
| 136 | assert res_info.CRC == exp_info.CRC |
| 137 | elif compression == "tar": |
| 138 | with ( |
| 139 | tarfile.open(fileobj=BytesIO(result)) as tar_exp, |
| 140 | tarfile.open(fileobj=BytesIO(expected)) as tar_res, |
| 141 | ): |
| 142 | for tar_res_info, tar_exp_info in zip( |
| 143 | tar_res.getmembers(), tar_exp.getmembers() |
| 144 | ): |
| 145 | actual_file = tar_res.extractfile(tar_res_info) |
| 146 | expected_file = tar_exp.extractfile(tar_exp_info) |
| 147 | assert (actual_file is None) == (expected_file is None) |
| 148 | if actual_file is not None and expected_file is not None: |
| 149 | assert actual_file.read() == expected_file.read() |
| 150 | else: |
| 151 | assert result == expected |
| 152 | |
| 153 | |
| 154 | @pytest.mark.parametrize("encoding", ["utf-8", "cp1251"]) |
no test coverage detected