Skip to content

Commit c0f5002

Browse files
MartyMacGyverChangaco
authored andcommitted
enhance the docs and tests of add_file_from_memory
1 parent 8999c11 commit c0f5002

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

libarchive/write.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ def add_file_from_memory(
8484
8585
:param entry_path: where entry should be places in archive
8686
:type entry_path: str
87-
:param entry_size: entire size of entry
87+
:param entry_size: entire size of entry in bytes
8888
:type entry_size: int
89-
:param entry_data: content of entry
89+
:param entry_data: content of entry as a list of byte-like objects
9090
:type entry_data: iterable
9191
:param filetype: which type of file: normal, symlink etc.
9292
should entry be created as

tests/test_rwx.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
from __future__ import division, print_function, unicode_literals
44
import io
5+
import sys
6+
import json
57

68
import libarchive
79
from libarchive.extract import EXTRACT_OWNER, EXTRACT_PERM, EXTRACT_TIME
810
from libarchive.write import memory_writer
911
from mock import patch
1012

13+
import pytest
14+
1115
from . import check_archive, in_dir, treestat
1216

1317

@@ -115,24 +119,45 @@ def test_write_not_fail(write_fail_mock):
115119
assert not write_fail_mock.called
116120

117121

118-
def test_adding_entry_from_memory():
119-
entry_path = 'this is path'
120-
entry_data = 'content'
121-
entry_size = len(entry_data)
122+
@pytest.mark.parametrize(
123+
'archfmt,data_bytes',
124+
[('zip', b'content'),
125+
('gnutar', 'a_string'.encode()),
126+
('pax', json.dumps({'a': 1, 'b': 2, 'c': 3}).encode())])
127+
def test_adding_entry_from_memory(archfmt, data_bytes):
128+
if sys.version_info[0] < 3:
129+
string_types = (str, unicode) # noqa: F821
130+
else:
131+
string_types = (str,)
132+
133+
entry_path = 'testfile.data'
134+
135+
# entry_data must be a list of byte-like objects for maximum compatibility
136+
# (Use of other data types can have undesirable side-effects)
137+
# entry_size is the total size in bytes of the entry_data items
138+
entry_data = [data_bytes]
139+
entry_size = sum(len(bdata) for bdata in entry_data)
122140

123141
blocks = []
124142

125143
def write_callback(data):
126144
blocks.append(data[:])
127145
return len(data)
128146

129-
with libarchive.custom_writer(write_callback, 'zip') as archive:
130-
archive.add_file_from_memory(entry_path, entry_size, entry_data)
147+
with libarchive.custom_writer(write_callback, archfmt) as archive:
148+
archive.add_file_from_memory(
149+
entry_path, entry_size, entry_data)
131150

132151
buf = b''.join(blocks)
133152
with libarchive.memory_reader(buf) as memory_archive:
134153
for archive_entry in memory_archive:
135-
assert entry_data.encode() == b''.join(
136-
archive_entry.get_blocks()
154+
expected = b''.join(
155+
[a.encode()
156+
if isinstance(a, string_types)
157+
else a for a in entry_data],
158+
)
159+
actual = b''.join(
160+
archive_entry.get_blocks(),
137161
)
162+
assert expected == actual
138163
assert archive_entry.path == entry_path

0 commit comments

Comments
 (0)