|
1 | 1 | """Test reading, writing and extracting archives.""" |
2 | 2 |
|
3 | 3 | from __future__ import division, print_function, unicode_literals |
| 4 | + |
4 | 5 | import io |
| 6 | +import json |
5 | 7 |
|
6 | 8 | import libarchive |
7 | 9 | from libarchive.extract import EXTRACT_OWNER, EXTRACT_PERM, EXTRACT_TIME |
8 | 10 | from libarchive.write import memory_writer |
9 | 11 | from mock import patch |
| 12 | +import pytest |
10 | 13 |
|
11 | 14 | from . import check_archive, in_dir, treestat |
12 | 15 |
|
@@ -115,24 +118,30 @@ def test_write_not_fail(write_fail_mock): |
115 | 118 | assert not write_fail_mock.called |
116 | 119 |
|
117 | 120 |
|
118 | | -def test_adding_entry_from_memory(): |
119 | | - entry_path = 'this is path' |
120 | | - entry_data = 'content' |
121 | | - entry_size = len(entry_data) |
| 121 | +@pytest.mark.parametrize( |
| 122 | + 'archfmt,data_bytes', |
| 123 | + [('zip', b'content'), |
| 124 | + ('gnutar', b''), |
| 125 | + ('pax', json.dumps({'a': 1, 'b': 2, 'c': 3}).encode()), |
| 126 | + ('7zip', b'lorem\0ipsum')]) |
| 127 | +def test_adding_entry_from_memory(archfmt, data_bytes): |
| 128 | + entry_path = 'testfile.data' |
| 129 | + entry_data = data_bytes |
| 130 | + entry_size = len(data_bytes) |
122 | 131 |
|
123 | 132 | blocks = [] |
124 | 133 |
|
125 | 134 | def write_callback(data): |
126 | 135 | blocks.append(data[:]) |
127 | 136 | return len(data) |
128 | 137 |
|
129 | | - with libarchive.custom_writer(write_callback, 'zip') as archive: |
| 138 | + with libarchive.custom_writer(write_callback, archfmt) as archive: |
130 | 139 | archive.add_file_from_memory(entry_path, entry_size, entry_data) |
131 | 140 |
|
132 | 141 | buf = b''.join(blocks) |
133 | 142 | with libarchive.memory_reader(buf) as memory_archive: |
134 | 143 | for archive_entry in memory_archive: |
135 | | - assert entry_data.encode() == b''.join( |
136 | | - archive_entry.get_blocks() |
137 | | - ) |
| 144 | + expected = entry_data |
| 145 | + actual = b''.join(archive_entry.get_blocks()) |
| 146 | + assert expected == actual |
138 | 147 | assert archive_entry.path == entry_path |
0 commit comments