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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
#!/usr/bin/env python
import unittest
import os
import tarfile
import zipfile
import tempfile
import shutil
from sos.archive import TarFileArchive, ZipFileArchive
class ZipFileArchiveTest(unittest.TestCase):
def setUp(self):
self.zf = ZipFileArchive('test')
def tearDown(self):
os.unlink('test.zip')
def check_for_file(self, filename):
zf = zipfile.ZipFile('test.zip', 'r')
zf.getinfo(filename)
zf.close()
def test_create(self):
self.zf.close()
def test_add_file(self):
self.zf.add_file('tests/ziptest')
self.zf.close()
self.check_for_file('tests/ziptest')
def test_add_unicode_file(self):
self.zf.add_file(u'tests/')
self.zf.close()
self.check_for_file('tests/ziptest')
def test_add_dir(self):
self.zf.add_file('tests/')
self.zf.close()
self.check_for_file('tests/ziptest')
def test_add_renamed(self):
self.zf.add_file('tests/ziptest', dest='tests/ziptest_renamed')
self.zf.close()
self.check_for_file('tests/ziptest_renamed')
def test_add_renamed_dir(self):
self.zf.add_file('tests/', 'tests_renamed/')
self.zf.close()
self.check_for_file('tests_renamed/ziptest')
def test_add_string(self):
self.zf.add_string('this is content', 'tests/string_test.txt')
self.zf.close()
self.check_for_file('tests/string_test.txt')
def test_get_file(self):
self.zf.add_string('this is my content', 'tests/string_test.txt')
afp = self.zf.open_file('tests/string_test.txt')
self.assertEquals('this is my content', afp.read())
def test_overwrite_file(self):
self.zf.add_string('this is my content', 'tests/string_test.txt')
self.zf.add_string('this is my new content', 'tests/string_test.txt')
afp = self.zf.open_file('tests/string_test.txt')
self.assertEquals('this is my new content', afp.read())
# Disabled as new api doesnt provide a add_link routine
# def test_make_link(self):
# self.zf.add_file('tests/ziptest')
# self.zf.add_link('tests/ziptest', 'link_name')
#
# self.zf.close()
# try:
# self.check_for_file('test/link_name')
# self.fail("link should not exist")
# except KeyError:
# pass
# Disabled as new api doesnt provide a compress routine
# def test_compress(self):
# self.assertEquals(self.zf.compress("zip"), self.zf.name())
class TarFileArchiveTest(unittest.TestCase):
def setUp(self):
self.tmpdir = tempfile.mkdtemp()
self.tf = TarFileArchive('test', self.tmpdir)
def tearDown(self):
shutil.rmtree(self.tmpdir)
def check_for_file(self, filename):
rtf = tarfile.open(os.path.join(self.tmpdir, 'test.tar'))
rtf.getmember(filename)
rtf.close()
def test_create(self):
self.tf.finalize('auto')
self.assertTrue(os.path.exists(os.path.join(self.tmpdir,
'test.tar')))
def test_add_file(self):
self.tf.add_file('tests/ziptest')
self.tf.finalize('auto')
self.check_for_file('test/tests/ziptest')
# Since commit 179d9bb add_file does not support recursive directory
# addition. Disable this test for now.
# def test_add_dir(self):
# self.tf.add_file('tests/')
# self.tf.close()
#
# self.check_for_file('test/tests/ziptest')
def test_add_renamed(self):
self.tf.add_file('tests/ziptest', dest='tests/ziptest_renamed')
self.tf.finalize('auto')
self.check_for_file('test/tests/ziptest_renamed')
# Since commit 179d9bb add_file does not support recursive directory
# addition. Disable this test for now.
# def test_add_renamed_dir(self):
# self.tf.add_file('tests/', 'tests_renamed/')
# self.tf.close()
#
# self.check_for_file('test/tests_renamed/ziptest')
def test_add_string(self):
self.tf.add_string('this is content', 'tests/string_test.txt')
self.tf.finalize('auto')
self.check_for_file('test/tests/string_test.txt')
def test_get_file(self):
self.tf.add_string('this is my content', 'tests/string_test.txt')
afp = self.tf.open_file('tests/string_test.txt')
self.assertEquals('this is my content', afp.read())
def test_overwrite_file(self):
self.tf.add_string('this is my content', 'tests/string_test.txt')
self.tf.add_string('this is my new content', 'tests/string_test.txt')
afp = self.tf.open_file('tests/string_test.txt')
self.assertEquals('this is my new content', afp.read())
def test_make_link(self):
self.tf.add_file('tests/ziptest')
self.tf.add_link('tests/ziptest', 'link_name')
self.tf.finalize('auto')
self.check_for_file('test/link_name')
def test_compress(self):
self.tf.finalize("auto")
if __name__ == "__main__":
unittest.main()
|