test_myunzip.py 1.94 KB
import unittest
import os
import filecmp
import shutil
import subprocess


EXEC_NAME = './myunzip0'

class TestMyUnzip0(unittest.TestCase):

    def test_uncompressed(self):
        subprocess.run([EXEC_NAME, 'tests/resources/uncompressed.zip'])
        
        # Check that the correct file was extracted
        self.assertTrue(os.path.exists('tests/resources/uncompressed.txt'))

        # Check correct output 
        self.assertTrue(filecmp.cmp('tests/resources/uncompressed.txt', 
                                    'tests/resources/uncompressed_expected.txt'))

        # Remove output 
        os.remove('tests/resources/uncompressed.txt')

    def test_compressed(self):
        subprocess.run([EXEC_NAME, 'tests/resources/compressed.zip'])

        # Check that we extracted a DEFLATE stream 
        self.assertTrue(os.path.exists('tests/resources/compressed.txt.deflate'))

        # Check correct output
        self.assertTrue(filecmp.cmp('tests/resources/compressed_expected.deflate', 
                                    'tests/resources/compressed.txt.deflate'))

        # Remove output 
        os.remove('tests/resources/compressed.txt.deflate')

    def test_make_directories(self): 
        """
        Suppose the zip file was created by invoking:
        # zip example.zip directory/to/example_file
        Then, suppose when we unzip example.zip, the relative directory
        directory/to does not exist. This directory should be created 
        before unzipping. 
        """
        subprocess.run([EXEC_NAME, 'tests/resources/directory_test.zip'])

        # Check that we made the directories.
        self.assertTrue(os.path.exists('tests/resources/a/b/c.txt'))

        # Check output
        self.assertTrue(filecmp.cmp('tests/resources/directory_expected.txt',
                                    'tests/resources/a/b/c.txt'))

        # Remove output
        shutil.rmtree('tests/resources/a')

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