test_myzip.py 1.86 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import unittest
import os
import filecmp
import shutil
import subprocess

# These tests rely on a working "unzip" command line utility
# I use the Info-ZIP one which is widely available

TEST_FILES = [
        'tests/resources/testfile1.bin',
        'tests/resources/testfile2.bin',
        'tests/resources/testfile3.bin',
        'tests/resources/testfile4.bin',
    ]

OUTPUT_DIR = 'tests/output'
OUTPUT_ZIP_NAME = os.path.join(OUTPUT_DIR, 'result.zip')

class TestMyzip(unittest.TestCase):
    def test_zip_utils(self):
        """
        Try to zip up each of the files
        and unzip them
        """

        for zip_exec in ['./myzip0', './myzip']:

            # make an output directory
            try:
                os.makedirs(OUTPUT_DIR)
            except FileExistsError:
                pass
            except:
                self.fail('could not make output directory')

            for filename in TEST_FILES:
                # make sure the test file even exists
                self.assertTrue(os.path.exists(filename))

                # zip up the file
                subprocess.run([zip_exec, OUTPUT_ZIP_NAME, filename])

                # check that zipped verison exists
                self.assertTrue(os.path.exists(OUTPUT_ZIP_NAME))

                # now try to unzip it with "known correct" tool
                subprocess.run(['unzip', '-q', '-o', OUTPUT_ZIP_NAME, '-d', OUTPUT_DIR],
                    stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)


                recovered_file_path = os.path.join(OUTPUT_DIR, filename)

                # make sure recovered file exists, and
                self.assertTrue(os.path.exists(recovered_file_path))

                # check that it is correct
                self.assertTrue(filecmp.cmp(filename, recovered_file_path))

            shutil.rmtree(OUTPUT_DIR)

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