1 from modulegraph
import zipio
6 if sys
.version_info
[:2] <= (2,6):
7 import unittest2
as unittest
12 TESTDATA
=os
.path
.join(
13 os
.path
.dirname(os
.path
.abspath(__file__
)),
16 class TestModuleGraph (unittest
.TestCase
):
17 def test_locating(self
):
19 self
.assertEqual(zipio
._locate
('/usr/bin/ditto'), ('/usr/bin/ditto', None))
20 self
.assertEqual(zipio
._locate
('/usr/bin/ditto/bar'), ('/usr/bin/ditto', 'bar'))
21 self
.assertEqual(zipio
._locate
('/usr/bin/ditto/foo/bar///bar/'), ('/usr/bin/ditto', 'foo/bar/bar'))
22 self
.assertEqual(zipio
._locate
('/usr/bin/ditto///foo/bar///bar/'), ('/usr/bin/ditto', 'foo/bar/bar'))
23 self
.assertRaises(IOError, zipio
._locate
, '/usr/bin/ditto.bar')
24 self
.assertRaises(IOError, zipio
._locate
, '/foo/bar/baz.txt')
28 fp
= zipio
.open(os
.path
.join(TESTDATA
, 'test.txt'), 'r')
31 self
.assertEqual(data
, 'This is test.txt\n')
33 if sys
.version_info
[0] == 3:
34 fp
= zipio
.open(os
.path
.join(TESTDATA
, 'test.txt'), 'rb')
37 self
.assertEqual(data
, b
'This is test.txt\n')
39 # 2. File inside zipfile
40 fp
= zipio
.open(os
.path
.join(TESTDATA
, 'zipped.egg', 'test.txt'), 'r')
43 self
.assertEqual(data
, 'Zipped up test.txt\n')
45 if sys
.version_info
[0] == 3:
46 fp
= zipio
.open(os
.path
.join(TESTDATA
, 'zipped.egg', 'test.txt'), 'rb')
49 self
.assertEqual(data
, b
'Zipped up test.txt\n')
51 # 3. EXC: Directory inside zipfile
52 self
.assertRaises(IOError, zipio
.open, os
.path
.join(TESTDATA
, 'zipped.egg', 'subdir'))
53 self
.assertRaises(IOError, zipio
.open, os
.path
.join(TESTDATA
, 'zipped.egg', 'subdir2'))
54 self
.assertRaises(IOError, zipio
.open, os
.path
.join(TESTDATA
, 'zipped.egg', 'subdir2/subdir'))
55 self
.assertRaises(IOError, zipio
.open, os
.path
.join(TESTDATA
, 'zipped.egg', 'subdir3'))
56 # TODO: Add subdir4/file.txt, without directory entry
57 self
.assertRaises(IOError, zipio
.open, os
.path
.join(TESTDATA
, 'zipped.egg', 'subdir4'))
59 # 4. EXC: No such file in zipfile
60 self
.assertRaises(IOError, zipio
.open, os
.path
.join(TESTDATA
, 'zipped.egg', 'no-such-file'))
61 self
.assertRaises(IOError, zipio
.open, os
.path
.join(TESTDATA
, 'zipped.egg', 'subdir/no-such-file'))
63 # 5. EXC: No such regular file
64 self
.assertRaises(IOError, zipio
.open, os
.path
.join(TESTDATA
, 'no-such-file.txt'))
67 self
.assertRaises(IOError, zipio
.open, os
.path
.join(TESTDATA
, 'test.txt'), 'w')
68 self
.assertRaises(IOError, zipio
.open, os
.path
.join(TESTDATA
, 'test.txt'), 'a')
69 self
.assertRaises(IOError, zipio
.open, os
.path
.join(TESTDATA
, 'test.txt'), 'r+')
70 self
.assertRaises(IOError, zipio
.open, os
.path
.join(TESTDATA
, 'test.txt'), 'w+')
71 self
.assertRaises(IOError, zipio
.open, os
.path
.join(TESTDATA
, 'test.txt'), 'a+')
73 def test_listdir(self
):
74 # 1. Regular directory
75 self
.assertEqual(set(os
.listdir(os
.path
.join(TESTDATA
, 'subdir'))), set(['file1.txt', 'file2.txt']))
77 # 2. Zipfile with files in directory
78 self
.assertEqual(set(zipio
.listdir(os
.path
.join(TESTDATA
, 'zipped.egg'))), set([
79 'test.txt', 'subdir', 'subdir2', 'subdir3', 'subdir4']))
81 # 3. Zipfile with files in subdirectory
82 self
.assertEqual(set(zipio
.listdir(os
.path
.join(TESTDATA
, 'zipped.egg', 'subdir'))), set(['file1.txt', 'file2.txt']))
83 self
.assertEqual(set(zipio
.listdir(os
.path
.join(TESTDATA
, 'zipped.egg', 'subdir2'))), set(['subdir']))
84 self
.assertEqual(set(zipio
.listdir(os
.path
.join(TESTDATA
, 'zipped.egg', 'subdir4', 'subdir6'))), set(['mydir']))
86 # 4. Zipfile with entry for directory, no files
87 self
.assertEqual(set(zipio
.listdir(os
.path
.join(TESTDATA
, 'zipped.egg', 'subdir3'))), set([]))
89 # 5. EXC: Zipfile without directory
90 self
.assertRaises(IOError, zipio
.listdir
, os
.path
.join(TESTDATA
, 'zipped.egg', 'subdir10'))
92 # 6. EXC: Regular directory doesn't exist
93 self
.assertRaises(IOError, zipio
.listdir
, os
.path
.join(TESTDATA
, 'subdir10'))
95 def test_isfile(self
):
96 self
.assertTrue(zipio
.isfile(os
.path
.join(TESTDATA
, 'test.txt')))
97 self
.assertFalse(zipio
.isfile(os
.path
.join(TESTDATA
, 'subdir')))
98 self
.assertRaises(IOError, zipio
.isfile
, os
.path
.join(TESTDATA
, 'no-such-file'))
99 self
.assertFalse(zipio
.isfile(os
.path
.join(TESTDATA
, 'zipped.egg')))
100 self
.assertFalse(zipio
.isfile(os
.path
.join(TESTDATA
, 'zipped.egg', 'subdir4')))
101 self
.assertTrue(zipio
.isfile(os
.path
.join(TESTDATA
, 'zipped.egg', 'test.txt')))
102 self
.assertFalse(zipio
.isfile(os
.path
.join(TESTDATA
, 'zipped.egg', 'subdir')))
103 self
.assertRaises(IOError, zipio
.isfile
, os
.path
.join(TESTDATA
, 'zipped.egg', 'no-such-file'))
104 self
.assertTrue(zipio
.isfile(os
.path
.join(TESTDATA
, 'zipped.egg', 'subdir2', 'subdir', 'file1.txt')))
106 def test_isdir(self
):
107 self
.assertTrue(zipio
.isdir(TESTDATA
))
108 self
.assertFalse(zipio
.isdir(os
.path
.join(TESTDATA
, 'test.txt')))
109 self
.assertTrue(zipio
.isdir(os
.path
.join(TESTDATA
, 'zipped.egg')))
110 self
.assertTrue(zipio
.isdir(os
.path
.join(TESTDATA
, 'zipped.egg', 'subdir')))
111 self
.assertTrue(zipio
.isdir(os
.path
.join(TESTDATA
, 'zipped.egg', 'subdir2/subdir')))
112 self
.assertTrue(zipio
.isdir(os
.path
.join(TESTDATA
, 'zipped.egg', 'subdir4')))
113 self
.assertFalse(zipio
.isdir(os
.path
.join(TESTDATA
, 'zipped.egg', 'subdir4', 'file.txt')))
114 self
.assertRaises(IOError, zipio
.isdir
, os
.path
.join(TESTDATA
, 'no-such-file'))
115 self
.assertRaises(IOError, zipio
.isdir
, os
.path
.join(TESTDATA
, 'zipped.egg', 'no-such-file'))
116 self
.assertRaises(IOError, zipio
.isdir
, os
.path
.join(TESTDATA
, 'zipped.egg', 'subdir', 'no-such-file'))
118 def test_islink(self
):
119 fn
= os
.path
.join(TESTDATA
, 'symlink')
120 os
.symlink('test.txt', fn
)
122 self
.assertTrue(zipio
.islink(fn
))
127 self
.assertFalse(zipio
.islink(os
.path
.join(TESTDATA
, 'test.txt')))
128 self
.assertFalse(zipio
.islink(os
.path
.join(TESTDATA
, 'subdir')))
129 self
.assertFalse(zipio
.islink(os
.path
.join(TESTDATA
, 'zipped.egg')))
130 self
.assertFalse(zipio
.islink(os
.path
.join(TESTDATA
, 'zipped.egg/subdir')))
131 self
.assertFalse(zipio
.islink(os
.path
.join(TESTDATA
, 'zipped.egg/subdir4')))
132 self
.assertFalse(zipio
.islink(os
.path
.join(TESTDATA
, 'zipped.egg/test.txt')))
133 self
.assertFalse(zipio
.islink(os
.path
.join(TESTDATA
, 'zipped.egg/subdir/file1.txt')))
135 self
.assertRaises(IOError, zipio
.islink
, os
.path
.join(TESTDATA
, 'no-such-file'))
136 self
.assertRaises(IOError, zipio
.islink
, os
.path
.join(TESTDATA
, 'zipped.egg', 'no-such-file'))
139 def test_readlink(self
):
140 fn
= os
.path
.join(TESTDATA
, 'symlink')
141 os
.symlink('test.txt', fn
)
143 self
.assertEqual(zipio
.readlink(fn
), 'test.txt')
148 self
.assertRaises(OSError, zipio
.readlink
, os
.path
.join(TESTDATA
, 'test.txt'))
149 self
.assertRaises(OSError, zipio
.readlink
, os
.path
.join(TESTDATA
, 'subdir'))
150 self
.assertRaises(OSError, zipio
.readlink
, os
.path
.join(TESTDATA
, 'zipped.egg'))
151 self
.assertRaises(OSError, zipio
.readlink
, os
.path
.join(TESTDATA
, 'zipped.egg', 'subdir4'))
152 self
.assertRaises(OSError, zipio
.readlink
, os
.path
.join(TESTDATA
, 'zipped.egg', 'no-such-file'))
153 self
.assertRaises(OSError, zipio
.readlink
, os
.path
.join(TESTDATA
, 'zipped.egg', 'subdir/no-such-file'))
155 def test_getmtime(self
):
156 fn
= os
.path
.join(TESTDATA
, 'test.txt')
157 self
.assertEqual(os
.path
.getmtime(fn
), zipio
.getmtime(fn
))
159 fn
= os
.path
.join(TESTDATA
, 'zipped.egg')
160 self
.assertEqual(os
.path
.getmtime(fn
), zipio
.getmtime(fn
))
162 fn
= os
.path
.join(TESTDATA
, 'zipped.egg/test.txt')
163 self
.assertIn(zipio
.getmtime(fn
), (1300193680.0, 1300222480.0))
165 fn
= os
.path
.join(TESTDATA
, 'zipped.egg/subdir')
166 self
.assertIn(zipio
.getmtime(fn
), (1300193890.0, 1300222690.0))
168 fn
= os
.path
.join(TESTDATA
, 'zipped.egg/subdir4')
169 self
.assertEqual(zipio
.getmtime(fn
), os
.path
.getmtime(os
.path
.join(TESTDATA
, 'zipped.egg')))
171 self
.assertRaises(IOError, zipio
.getmtime
, os
.path
.join(TESTDATA
, 'no-file'))
172 self
.assertRaises(IOError, zipio
.getmtime
, os
.path
.join(TESTDATA
, 'zipped.egg/no-file'))
174 def test_contextlib(self
):
176 with zipio
.open(os
.path
.join(TESTDATA
, 'test.txt'), 'r') as fp
:
180 self
.fail("file not closed")
181 except (ValueError, IOError):
184 self
.assertEqual(data
, 'This is test.txt\n')
186 if sys
.version_info
[0] == 3:
187 with zipio
.open(os
.path
.join(TESTDATA
, 'test.txt'), 'rb') as fp
:
191 self
.fail("file not closed")
192 except (ValueError, IOError):
195 self
.assertEqual(data
, b
'This is test.txt\n')
197 # 2. File inside zipfile
198 with zipio
.open(os
.path
.join(TESTDATA
, 'zipped.egg', 'test.txt'), 'r') as fp
:
202 self
.fail("file not closed")
203 except (ValueError, IOError):
205 self
.assertEqual(data
, 'Zipped up test.txt\n')
207 if sys
.version_info
[0] == 3:
208 with zipio
.open(os
.path
.join(TESTDATA
, 'zipped.egg', 'test.txt'), 'rb') as fp
:
212 self
.fail("file not closed")
213 except (IOError, ValueError):
215 self
.assertEqual(data
, b
'Zipped up test.txt\n')
217 if __name__
== "__main__":