1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/mozbase/mozfile/tests/test_extract.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,133 @@ 1.4 +#!/usr/bin/env python 1.5 + 1.6 +import mozfile 1.7 +import os 1.8 +import shutil 1.9 +import tarfile 1.10 +import tempfile 1.11 +import stubs 1.12 +import unittest 1.13 +import zipfile 1.14 + 1.15 + 1.16 +class TestExtract(unittest.TestCase): 1.17 + """test extracting archives""" 1.18 + 1.19 + def ensure_directory_contents(self, directory): 1.20 + """ensure the directory contents match""" 1.21 + for f in stubs.files: 1.22 + path = os.path.join(directory, *f) 1.23 + exists = os.path.exists(path) 1.24 + if not exists: 1.25 + print "%s does not exist" % (os.path.join(f)) 1.26 + self.assertTrue(exists) 1.27 + if exists: 1.28 + contents = file(path).read().strip() 1.29 + self.assertTrue(contents == f[-1]) 1.30 + 1.31 + def test_extract_zipfile(self): 1.32 + """test extracting a zipfile""" 1.33 + _zipfile = self.create_zip() 1.34 + self.assertTrue(os.path.exists(_zipfile)) 1.35 + try: 1.36 + dest = tempfile.mkdtemp() 1.37 + try: 1.38 + mozfile.extract_zip(_zipfile, dest) 1.39 + self.ensure_directory_contents(dest) 1.40 + finally: 1.41 + shutil.rmtree(dest) 1.42 + finally: 1.43 + os.remove(_zipfile) 1.44 + 1.45 + def test_extract_tarball(self): 1.46 + """test extracting a tarball""" 1.47 + tarball = self.create_tarball() 1.48 + self.assertTrue(os.path.exists(tarball)) 1.49 + try: 1.50 + dest = tempfile.mkdtemp() 1.51 + try: 1.52 + mozfile.extract_tarball(tarball, dest) 1.53 + self.ensure_directory_contents(dest) 1.54 + finally: 1.55 + shutil.rmtree(dest) 1.56 + finally: 1.57 + os.remove(tarball) 1.58 + 1.59 + def test_extract(self): 1.60 + """test the generalized extract function""" 1.61 + 1.62 + # test extracting a tarball 1.63 + tarball = self.create_tarball() 1.64 + self.assertTrue(os.path.exists(tarball)) 1.65 + try: 1.66 + dest = tempfile.mkdtemp() 1.67 + try: 1.68 + mozfile.extract(tarball, dest) 1.69 + self.ensure_directory_contents(dest) 1.70 + finally: 1.71 + shutil.rmtree(dest) 1.72 + finally: 1.73 + os.remove(tarball) 1.74 + 1.75 + # test extracting a zipfile 1.76 + _zipfile = self.create_zip() 1.77 + self.assertTrue(os.path.exists(_zipfile)) 1.78 + try: 1.79 + dest = tempfile.mkdtemp() 1.80 + try: 1.81 + mozfile.extract_zip(_zipfile, dest) 1.82 + self.ensure_directory_contents(dest) 1.83 + finally: 1.84 + shutil.rmtree(dest) 1.85 + finally: 1.86 + os.remove(_zipfile) 1.87 + 1.88 + # test extracting some non-archive; this should fail 1.89 + fd, filename = tempfile.mkstemp() 1.90 + os.write(fd, 'This is not a zipfile or tarball') 1.91 + os.close(fd) 1.92 + exception = None 1.93 + try: 1.94 + dest = tempfile.mkdtemp() 1.95 + mozfile.extract(filename, dest) 1.96 + except Exception, exception: 1.97 + pass 1.98 + finally: 1.99 + os.remove(filename) 1.100 + os.rmdir(dest) 1.101 + self.assertTrue(isinstance(exception, Exception)) 1.102 + 1.103 + ### utility functions 1.104 + 1.105 + def create_tarball(self): 1.106 + """create a stub tarball for testing""" 1.107 + tempdir = stubs.create_stub() 1.108 + filename = tempfile.mktemp(suffix='.tar') 1.109 + archive = tarfile.TarFile(filename, mode='w') 1.110 + try: 1.111 + for path in stubs.files: 1.112 + archive.add(os.path.join(tempdir, *path), arcname=os.path.join(*path)) 1.113 + except: 1.114 + os.remove(archive) 1.115 + raise 1.116 + finally: 1.117 + shutil.rmtree(tempdir) 1.118 + archive.close() 1.119 + return filename 1.120 + 1.121 + def create_zip(self): 1.122 + """create a stub zipfile for testing""" 1.123 + 1.124 + tempdir = stubs.create_stub() 1.125 + filename = tempfile.mktemp(suffix='.zip') 1.126 + archive = zipfile.ZipFile(filename, mode='w') 1.127 + try: 1.128 + for path in stubs.files: 1.129 + archive.write(os.path.join(tempdir, *path), arcname=os.path.join(*path)) 1.130 + except: 1.131 + os.remove(filename) 1.132 + raise 1.133 + finally: 1.134 + shutil.rmtree(tempdir) 1.135 + archive.close() 1.136 + return filename