Browse Source

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.
test_fixup
Aaron Sierra 4 years ago
parent
commit
6189b3020e
1 changed files with 32 additions and 1 deletions
  1. +32
    -1
      tests.py

+ 32
- 1
tests.py View File

@@ -28,8 +28,9 @@


import os, unittest, tempfile, random, string, sys import os, unittest, tempfile, random, string, sys
import zipfile import zipfile
import io


from libarchive import is_archive_name, is_archive
from libarchive import Archive, is_archive_name, is_archive
from libarchive.zip import is_zipfile, ZipFile, ZipEntry from libarchive.zip import is_zipfile, ZipFile, ZipEntry


PY3 = sys.version_info[0] == 3 PY3 = sys.version_info[0] == 3
@@ -247,5 +248,35 @@ class TestZipWrite(unittest.TestCase):
self.assertIsNone(z._stream) self.assertIsNone(z._stream)
z.close() z.close()



class TestHighLevelAPI(unittest.TestCase):
def setUp(self):
make_temp_archive()

def _test_listing_content(self, f):
""" Test helper capturing file paths while iterating the archive. """
found = []
with Archive(f) as a:
for entry in a:
found.append(entry.pathname)

self.assertEqual(set(found), set(FILENAMES))

def test_open_by_name(self):
""" Test an archive opened directly by name. """
self._test_listing_content(ZIPPATH)

def test_open_by_named_fobj(self):
""" Test an archive using a file-like object opened by name. """
with open(ZIPPATH, 'rb') as f:
self._test_listing_content(f)

def test_open_by_unnamed_fobj(self):
""" Test an archive using file-like object opened by fileno(). """
with open(ZIPPATH, 'rb') as zf:
with io.FileIO(zf.fileno(), mode='r', closefd=False) as f:
self._test_listing_content(f)


if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

Loading…
Cancel
Save