|
7 | 7 | from os.path import join |
8 | 8 | import unicodedata |
9 | 9 |
|
| 10 | +import pytest |
| 11 | + |
10 | 12 | from libarchive import memory_reader, memory_writer |
| 13 | +from libarchive.entry import ArchiveEntry, ConsumedArchiveEntry, PassedArchiveEntry |
11 | 14 |
|
12 | 15 | from . import data_dir, get_entries, get_tarinfos |
13 | 16 |
|
@@ -100,3 +103,35 @@ def check_entries(test_file, regen=False, ignore=''): |
100 | 103 | if isinstance(d[key], text_type): |
101 | 104 | d[key] = unicodedata.normalize('NFC', d[key]) |
102 | 105 | assert e1 == e2 |
| 106 | + |
| 107 | + |
| 108 | +def test_the_life_cycle_of_archive_entries(): |
| 109 | + """Check that the `get_blocks` method only works on the current entry, and only once. |
| 110 | + """ |
| 111 | + # Create a test archive in memory |
| 112 | + buf = bytes(bytearray(10_000_000)) |
| 113 | + with memory_writer(buf, 'gnutar') as archive: |
| 114 | + archive.add_files( |
| 115 | + 'README.rst', |
| 116 | + 'libarchive/__init__.py', |
| 117 | + 'libarchive/entry.py', |
| 118 | + ) |
| 119 | + # Read multiple entries of the test archive and check how the evolve |
| 120 | + with memory_reader(buf) as archive: |
| 121 | + archive_iter = iter(archive) |
| 122 | + entry1 = next(archive_iter) |
| 123 | + assert type(entry1) is ArchiveEntry |
| 124 | + for block in entry1.get_blocks(): |
| 125 | + pass |
| 126 | + assert type(entry1) is ConsumedArchiveEntry |
| 127 | + with pytest.raises(TypeError): |
| 128 | + entry1.get_blocks() |
| 129 | + entry2 = next(archive_iter) |
| 130 | + assert type(entry2) is ArchiveEntry |
| 131 | + assert type(entry1) is PassedArchiveEntry |
| 132 | + with pytest.raises(TypeError): |
| 133 | + entry1.get_blocks() |
| 134 | + entry3 = next(archive_iter) |
| 135 | + assert type(entry3) is ArchiveEntry |
| 136 | + assert type(entry2) is PassedArchiveEntry |
| 137 | + assert type(entry1) is PassedArchiveEntry |
0 commit comments