1 from modulegraph import zipio 2 import os 3 import time 4 import sys 5 6 if sys.version_info[:2] <= (2,6): 7 import unittest2 as unittest 8 9 else: 10 import unittest 11 12 TESTDATA=os.path.join( 13 os.path.dirname(os.path.abspath(__file__)), 14 'testdata') 15 16 class TestModuleGraph (unittest.TestCase): 17 def test_locating(self): 18 # Private function 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') 25 26 def test_open(self): 27 # 1. Regular file 28 fp = zipio.open(os.path.join(TESTDATA, 'test.txt'), 'r') 29 data = fp.read() 30 fp.close() 31 self.assertEqual(data, 'This is test.txt\n') 32 33 if sys.version_info[0] == 3: 34 fp = zipio.open(os.path.join(TESTDATA, 'test.txt'), 'rb') 35 data = fp.read() 36 fp.close() 37 self.assertEqual(data, b'This is test.txt\n') 38 39 # 2. File inside zipfile 40 fp = zipio.open(os.path.join(TESTDATA, 'zipped.egg', 'test.txt'), 'r') 41 data = fp.read() 42 fp.close() 43 self.assertEqual(data, 'Zipped up test.txt\n') 44 45 if sys.version_info[0] == 3: 46 fp = zipio.open(os.path.join(TESTDATA, 'zipped.egg', 'test.txt'), 'rb') 47 data = fp.read() 48 fp.close() 49 self.assertEqual(data, b'Zipped up test.txt\n') 50 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')) 58 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')) 62 63 # 5. EXC: No such regular file 64 self.assertRaises(IOError, zipio.open, os.path.join(TESTDATA, 'no-such-file.txt')) 65 66 # 6. EXC: Open r/w 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+') 72 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'])) 76 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'])) 80 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'])) 85 86 # 4. Zipfile with entry for directory, no files 87 self.assertEqual(set(zipio.listdir(os.path.join(TESTDATA, 'zipped.egg', 'subdir3'))), set([])) 88 89 # 5. EXC: Zipfile without directory 90 self.assertRaises(IOError, zipio.listdir, os.path.join(TESTDATA, 'zipped.egg', 'subdir10')) 91 92 # 6. EXC: Regular directory doesn't exist 93 self.assertRaises(IOError, zipio.listdir, os.path.join(TESTDATA, 'subdir10')) 94 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'))) 105 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')) 117 118 def test_islink(self): 119 fn = os.path.join(TESTDATA, 'symlink') 120 os.symlink('test.txt', fn) 121 try: 122 self.assertTrue(zipio.islink(fn)) 123 124 finally: 125 os.unlink(fn) 126 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'))) 134 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')) 137 138 139 def test_readlink(self): 140 fn = os.path.join(TESTDATA, 'symlink') 141 os.symlink('test.txt', fn) 142 try: 143 self.assertEqual(zipio.readlink(fn), 'test.txt') 144 145 finally: 146 os.unlink(fn) 147 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')) 154 155 def test_getmtime(self): 156 fn = os.path.join(TESTDATA, 'test.txt') 157 self.assertEqual(os.path.getmtime(fn), zipio.getmtime(fn)) 158 159 fn = os.path.join(TESTDATA, 'zipped.egg') 160 self.assertEqual(os.path.getmtime(fn), zipio.getmtime(fn)) 161 162 fn = os.path.join(TESTDATA, 'zipped.egg/test.txt') 163 self.assertIn(zipio.getmtime(fn), (1300193680.0, 1300222480.0)) 164 165 fn = os.path.join(TESTDATA, 'zipped.egg/subdir') 166 self.assertIn(zipio.getmtime(fn), (1300193890.0, 1300222690.0)) 167 168 fn = os.path.join(TESTDATA, 'zipped.egg/subdir4') 169 self.assertEqual(zipio.getmtime(fn), os.path.getmtime(os.path.join(TESTDATA, 'zipped.egg'))) 170 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')) 173 174 def test_contextlib(self): 175 # 1. Regular file 176 with zipio.open(os.path.join(TESTDATA, 'test.txt'), 'r') as fp: 177 data = fp.read() 178 try: 179 fp.read() 180 self.fail("file not closed") 181 except (ValueError, IOError): 182 pass 183 184 self.assertEqual(data, 'This is test.txt\n') 185 186 if sys.version_info[0] == 3: 187 with zipio.open(os.path.join(TESTDATA, 'test.txt'), 'rb') as fp: 188 data = fp.read() 189 try: 190 fp.read() 191 self.fail("file not closed") 192 except (ValueError, IOError): 193 pass 194 195 self.assertEqual(data, b'This is test.txt\n') 196 197 # 2. File inside zipfile 198 with zipio.open(os.path.join(TESTDATA, 'zipped.egg', 'test.txt'), 'r') as fp: 199 data = fp.read() 200 try: 201 fp.read() 202 self.fail("file not closed") 203 except (ValueError, IOError): 204 pass 205 self.assertEqual(data, 'Zipped up test.txt\n') 206 207 if sys.version_info[0] == 3: 208 with zipio.open(os.path.join(TESTDATA, 'zipped.egg', 'test.txt'), 'rb') as fp: 209 data = fp.read() 210 try: 211 fp.read() 212 self.fail("file not closed") 213 except (IOError, ValueError): 214 pass 215 self.assertEqual(data, b'Zipped up test.txt\n') 216 217 if __name__ == "__main__": 218 unittest.main() 219