-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtar_reader_test.py
More file actions
executable file
·56 lines (40 loc) · 1.8 KB
/
tar_reader_test.py
File metadata and controls
executable file
·56 lines (40 loc) · 1.8 KB
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
#!/usr/bin/env python
import mock
import tarfile
import unittest
from tar_reader import NotValidArchive as NotValidArchive
from tar_reader import TarReader as Reader
TAR_FILE_NAME = 'tar_file'
MEMBERS = []
class MockTarFile(object):
def close(self):
pass
def getmembers(self):
return MEMBERS
class TarReaderTestCase(unittest.TestCase):
def setUp(self):
self.reader = Reader(TAR_FILE_NAME)
self.mock_tar_file = MockTarFile()
def test_peek_with_true_tar_file(self):
with mock.patch.object(tarfile, 'is_tarfile') as mock_is_tarfile:
with mock.patch.object(tarfile, 'open') as mock_open:
with mock.patch.object(MockTarFile,
'getmembers') as mock_getmembers:
with mock.patch.object(MockTarFile, 'close') as mock_close:
mock_is_tarfile.return_value = True
mock_open.return_value = self.mock_tar_file
self.reader.peek()
mock_is_tarfile.assert_called_once_with(TAR_FILE_NAME)
mock_open.assert_called_once_with(TAR_FILE_NAME, "r")
mock_getmembers.assert_called_once_with()
mock_close.assert_called_once_with()
def test_peek_with_false_tar_file(self):
with mock.patch.object(tarfile, 'is_tarfile') as mock_is_tarfile:
mock_is_tarfile.return_value = False
self.assertRaises(NotValidArchive, self.reader.peek)
def test_peek_with_invalid_tar_file(self):
with mock.patch.object(tarfile, 'is_tarfile') as mock_is_tarfile:
mock_is_tarfile.side_effect = IOError()
self.assertRaises(NotValidArchive, self.reader.peek)
if __name__ == "__main__":
unittest.main()