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
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()