def test_crc16(self): filepath = os.path.join(self.tmpdir, 'test.gz') with GzipFile.open(filepath, mode='w') as gzip: gzip.addfile(self.TEST_FILE, crc16=True) with GzipFile.open(filepath, mode='r') as gzip: info = gzip.getinfo(self.FILE_NAME) self.assertIsNotNone(info.CRC16)
def test_ascii(self): filepath = os.path.join(self.tmpdir, 'test.gz') with GzipFile.open(filepath, mode='w') as gzip: gzip.addfile(self.TEST_FILE, isascii=True) with GzipFile.open(filepath, mode='r') as gzip: info = gzip.getinfo(self.FILE_NAME) self.assertTrue(info.FLG & FTEXT > 0)
def test_best_speed(self): filepath = os.path.join(self.tmpdir, 'test.gz') with GzipFile.open(filepath, mode='w') as gzip: gzip.addfile(self.TEST_FILE, compresslevel=1) with GzipFile.open(filepath, mode='r') as gzip: info = gzip.getinfo(self.FILE_NAME) self.assertEqual(info.XFL, 4)
def test_file_comment(self): filepath = os.path.join(self.tmpdir, 'test.gz') comment = 'onion, shallot and garlic' with GzipFile.open(filepath, mode='w') as gzip: gzip.addfile(self.TEST_FILE, comment=comment) with GzipFile.open(filepath, mode='r') as gzip: info = gzip.getinfo(self.FILE_NAME) self.assertEqual(comment, info.FCOMMENT)
def test_exfield(self): filepath = os.path.join(self.tmpdir, 'test.gz') exfield = b'cp\x02\x08\x00' with GzipFile.open(filepath, mode='w') as gzip: gzip.addfile(self.TEST_FILE, exfield=exfield) with GzipFile.open(filepath, mode='r') as gzip: info = gzip.getinfo(self.FILE_NAME) self.assertEqual(exfield, info.EXFIELD)
def test_write_contents(self): filepath = os.path.join(self.tmpdir, 'test.gz') with GzipFile.open(filepath, mode='w') as gzip: gzip.addfile(self.TEST_FILE) with GzipFile.open(filepath, mode='r') as gzip, \ open(self.TEST_FILE, mode='rb') as orig: fp = gzip.extract(self.FILE_NAME) self.assertEqual(fp.read(), orig.read())
def test_write_attributes(self): filepath = os.path.join(self.tmpdir, 'test.gz') with GzipFile.open(filepath, mode='w') as gzip: gzip.addfile(self.TEST_FILE) with GzipFile.open(filepath, mode='r') as gzip: info = gzip.getinfo(self.FILE_NAME) self.assertEqual(info.CM, 8) self.assertEqual(info.FLG, 0b00001000) self.assertEqual(info.MTIME, int(os.path.getmtime(self.TEST_FILE))) self.assertEqual(info.XFL, 0) self.assertEqual(info.FNAME, os.path.basename(self.TEST_FILE))
def test_write_data(self): filepath = os.path.join(self.tmpdir, 'test.gz') data = b'carrot, beet, radish and turnip' mtime = 1412132400 # 2014-10-01 12:00:00 filename = 'roots_vagetables.txt' with GzipFile.open(filepath, mode='w') as gzip: gzip.adddata(data, mtime=mtime, filename=filename) with GzipFile.open(filepath, mode='r') as gzip: info = gzip.getinfo(filename) self.assertEqual(mtime, info.MTIME) self.assertEqual(filename, info.FNAME) fp = gzip.extract(gzipinfo=info) self.assertEqual(data, fp.read())
def test_extract_file(self): tmpdir = None try: tmpdir = tempfile.mkdtemp() os.chdir(tmpdir) with GzipFile.open(self.TEST_FILE) as gzip: filename = self.FILE_ATTR['FNAME'] gzip.extractfile(filename) stat = os.stat(filename) self.assertEqual(stat.st_mtime, self.FILE_ATTR['MTIME']) self.assertEqual(stat.st_size, len(self.FILE_CONTENTS)) finally: if tmpdir: shutil.rmtree(tmpdir)
def test_extra_bytes(self): with GzipFile.open(self.EXBYTES_FILE) as gzip: self.assertEqual(len(gzip.gzipinfos), 1)
def test_read_emptyfile(self): with self.assertRaises(IOError): with GzipFile.open(self.EMPTY_FILE) as gzip: pass
def test_read_contents(self): with GzipFile.open(self.TEST_FILE) as gzip: fp = gzip.extract(self.FILE_ATTR['FNAME']) self.assertEqual(fp.read(), self.FILE_CONTENTS)
def test_load_file(self): with GzipFile.open(self.TEST_FILE) as gzip: self.assertEqual(len(gzip.gzipinfos), 1)
def test_bad_crc16(self): with self.assertRaises(BadChecksum): with GzipFile.open(self.CRC16_FILE) as gzip: pass
def test_read_multiple(self): with GzipFile.open(self.MULTIPLE_FILE) as gzip: infos = gzip.getinfolist() self.assertEqual(len(infos), 2)
def test_check_attributes(self): with GzipFile.open(self.TEST_FILE) as gzip: info = gzip.gzipinfos[0] for key in self.FILE_ATTR: self.assertEqual(getattr(info, key), self.FILE_ATTR[key])
def test_extra_magic(self): with self.assertRaises(GzipError): with GzipFile.open(self.EXMAGIC_FILE) as gzip: pass
def test_bad_isize(self): with self.assertRaises(BadChecksum): with GzipFile.open(self.ISIZE_FILE) as gzip: pass