def test_deferred_close_by_archive(self): """ Test archive deferred close without a stream. """ z = ZipFile(self.f, 'r') self.assertIsNotNone(z._a) self.assertIsNone(z._stream) z.close() self.assertIsNone(z._a)
def test_deferred_close_by_archive(self): """ Test archive deferred close without a stream. """ f = file(ZIPPATH, mode='r') z = ZipFile(f, 'r') self.assertIsNotNone(z._a) self.assertIsNone(z._stream) z.close() self.assertIsNone(z._a)
def test_deferred_close_by_stream(self): """ Ensure archive closes self if stream is closed first. """ z = ZipFile(self.f, 'r') stream = z.readstream(FILENAMES[0]) stream.close() # Make sure archive stays open after stream is closed. self.assertIsNotNone(z._a) self.assertIsNone(z._stream) z.close() self.assertIsNone(z._a) self.assertTrue(stream.closed)
def test_deferred_close_by_stream(self): """ Ensure archive closes self if stream is closed first. """ f = file(ZIPPATH, mode='r') z = ZipFile(f, 'r') stream = z.readstream(FILENAMES[0]) stream.close() # Make sure archive stays open after stream is closed. self.assertIsNotNone(z._a) self.assertIsNone(z._stream) z.close() self.assertIsNone(z._a) self.assertTrue(stream.closed)
def test_close_stream_first(self): """ Ensure that archive stays open after being closed if a stream is open. Further, ensure closing the stream closes the archive. """ z = ZipFile(self.f, 'r') stream = z.readstream(FILENAMES[0]) z.close() try: stream.read() except: self.fail("Reading stream from closed archive failed!") stream.close() # Now the archive should close. self.assertIsNone(z._a) self.assertTrue(stream.closed) self.assertIsNone(z._stream)
def test_writestream_unbuffered(self): f = file(ZIPPATH, mode='w') z = ZipFile(f, 'w') for fname in FILENAMES: full_path = os.path.join(TMPDIR, fname) i = file(full_path) o = z.writestream(fname, os.path.getsize(full_path)) while True: data = i.read(1) if not data: break o.write(data) o.close() i.close() z.close()
def test_close_stream_first(self): """ Ensure that archive stays open after being closed if a stream is open. Further, ensure closing the stream closes the archive. """ f = file(ZIPPATH, mode='r') z = ZipFile(f, 'r') stream = z.readstream(FILENAMES[0]) z.close() try: stream.read() except: self.fail("Reading stream from closed archive failed!") stream.close() # Now the archive should close. self.assertIsNone(z._a) self.assertTrue(stream.closed) self.assertIsNone(z._stream)
def test_filenames(self): f = file(ZIPPATH, mode='r') z = ZipFile(f, 'r') names = [] for e in z: names.append(e.filename) self.assertEqual(names, FILENAMES, 'File names differ in archive.')
def test_writestream_unbuffered(self): z = ZipFile(self.f, 'w') for fname in FILENAMES: full_path = os.path.join(TMPDIR, fname) i = open(full_path) o = z.writestream(fname, os.path.getsize(full_path)) while True: data = i.read(1) if not data: break if PY3: o.write(data) else: o.write(unicode(data)) o.close() i.close() z.close()
def test_iterate(self): f = file(ZIPPATH, mode='r') z = ZipFile(f, 'r') count = 0 for e in z: count += 1 self.assertEqual( count, len(FILENAMES), 'Did not enumerate correct number of items in archive.')
def test_filenames(self): z = ZipFile(self.f, 'r') names = [] for e in z: if PY3: names.append(e.filename) else: names.append(e.filename[0]) self.assertEqual(names, FILENAMES, 'File names differ in archive.')
def test_deferred_close_by_archive(self): """ Test archive deferred close without a stream. """ f = file(ZIPPATH, mode='w') z = ZipFile(f, 'w') o = z.writestream(FILENAMES[0]) z.close() self.assertIsNotNone(z._a) self.assertIsNotNone(z._stream) o.write('testdata') o.close() self.assertIsNone(z._a) self.assertIsNone(z._stream) z.close()
def test_deferred_close_by_archive(self): """ Test archive deferred close without a stream. """ z = ZipFile(self.f, 'w') o = z.writestream(FILENAMES[0]) z.close() self.assertIsNotNone(z._a) self.assertIsNotNone(z._stream) if PY3: o.write('testdata') else: o.write(unicode('testdata')) o.close() self.assertIsNone(z._a) self.assertIsNone(z._stream) z.close()
def test_writepath(self): f = file(ZIPPATH, mode='w') z = ZipFile(f, 'w') for fname in FILENAMES: z.writepath(file(os.path.join(TMPDIR, fname), 'r')) z.close()
def test_writepath_directory(self): """ Test writing a directory. """ z = ZipFile(self.f, 'w') z.writepath(None, pathname='/testdir', folder=True) z.writepath(None, pathname='/testdir/testinside', folder=True) z.close() self.f.close() f = open(ZIPPATH, mode='r') z = ZipFile(f, 'r') entries = z.infolist() assert len(entries) == 2 assert entries[0].isdir() z.close() f.close()
def test_writepath(self): z = ZipFile(self.f, 'w') for fname in FILENAMES: with open(os.path.join(TMPDIR, fname), 'r') as f: z.writepath(f) z.close()
def test_writepath_directory(self): """ Test writing a directory. """ f = file(ZIPPATH, mode='w') z = ZipFile(f, 'w') z.writepath(None, pathname='/testdir', folder=True) z.writepath(None, pathname='/testdir/testinside', folder=True) z.close() f.close() f = file(ZIPPATH, mode='r') z = ZipFile(f, 'r') entries = z.infolist() assert len(entries) == 2 assert entries[0].isdir() z.close() f.close()