|
12 | 12 | import subprocess |
13 | 13 | import sys |
14 | 14 | import tempfile |
| 15 | +import zlib |
15 | 16 | from pathlib import Path |
16 | 17 |
|
17 | 18 | import pytest |
@@ -234,15 +235,29 @@ def test_threaded_program_can_exit_on_error(tmp_path, mode, threads): |
234 | 235 |
|
235 | 236 | @pytest.mark.parametrize("threads", [1, 2]) |
236 | 237 | def test_flush(tmp_path, threads): |
| 238 | + empty_block_end = b"\x00\x00\xff\xff" |
| 239 | + compressobj = zlib.compressobj(wbits=-15) |
| 240 | + deflate_last_block = compressobj.compress(b"") + compressobj.flush() |
237 | 241 | test_file = tmp_path / "output.gz" |
238 | 242 | with gzip_ng_threaded.open(test_file, "wb", threads=threads) as f: |
239 | 243 | f.write(b"1") |
240 | 244 | f.flush() |
241 | | - assert gzip.decompress(test_file.read_bytes()) == b"1" |
| 245 | + data = test_file.read_bytes() |
| 246 | + assert data[-4:] == empty_block_end |
| 247 | + # Cut off gzip header and end data with an explicit last block to |
| 248 | + # test if the data was compressed correctly. |
| 249 | + deflate_block = data[10:] + deflate_last_block |
| 250 | + assert zlib.decompress(deflate_block, wbits=-15) == b"1" |
242 | 251 | f.write(b"2") |
243 | 252 | f.flush() |
244 | | - assert gzip.decompress(test_file.read_bytes()) == b"12" |
| 253 | + data = test_file.read_bytes() |
| 254 | + assert data[-4:] == empty_block_end |
| 255 | + deflate_block = data[10:] + deflate_last_block |
| 256 | + assert zlib.decompress(deflate_block, wbits=-15) == b"12" |
245 | 257 | f.write(b"3") |
246 | 258 | f.flush() |
247 | | - assert gzip.decompress(test_file.read_bytes()) == b"123" |
| 259 | + data = test_file.read_bytes() |
| 260 | + assert data[-4:] == empty_block_end |
| 261 | + deflate_block = data[10:] + deflate_last_block |
| 262 | + assert zlib.decompress(deflate_block, wbits=-15) == b"123" |
248 | 263 | assert gzip.decompress(test_file.read_bytes()) == b"123" |
0 commit comments