Skip to content

Commit 6189b30

Browse files
author
Aaron Sierra
committed
tests: Add high-level API test
Add a high-level API test that initially focuses on various ways that an Archive object can be directed to use a particular file: 1. filename 2. file-like object opened by name 3. file-like object opened by fileno() The 3rd way is currently broken for Python 2 and Python 3.
1 parent 0244405 commit 6189b30

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

tests.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828

2929
import os, unittest, tempfile, random, string, sys
3030
import zipfile
31+
import io
3132

32-
from libarchive import is_archive_name, is_archive
33+
from libarchive import Archive, is_archive_name, is_archive
3334
from libarchive.zip import is_zipfile, ZipFile, ZipEntry
3435

3536
PY3 = sys.version_info[0] == 3
@@ -247,5 +248,35 @@ def test_deferred_close_by_archive(self):
247248
self.assertIsNone(z._stream)
248249
z.close()
249250

251+
252+
class TestHighLevelAPI(unittest.TestCase):
253+
def setUp(self):
254+
make_temp_archive()
255+
256+
def _test_listing_content(self, f):
257+
""" Test helper capturing file paths while iterating the archive. """
258+
found = []
259+
with Archive(f) as a:
260+
for entry in a:
261+
found.append(entry.pathname)
262+
263+
self.assertEqual(set(found), set(FILENAMES))
264+
265+
def test_open_by_name(self):
266+
""" Test an archive opened directly by name. """
267+
self._test_listing_content(ZIPPATH)
268+
269+
def test_open_by_named_fobj(self):
270+
""" Test an archive using a file-like object opened by name. """
271+
with open(ZIPPATH, 'rb') as f:
272+
self._test_listing_content(f)
273+
274+
def test_open_by_unnamed_fobj(self):
275+
""" Test an archive using file-like object opened by fileno(). """
276+
with open(ZIPPATH, 'rb') as zf:
277+
with io.FileIO(zf.fileno(), mode='r', closefd=False) as f:
278+
self._test_listing_content(f)
279+
280+
250281
if __name__ == '__main__':
251282
unittest.main()

0 commit comments

Comments
 (0)