Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | from test import support |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3 | import os |
| 4 | import genericpath |
| 5 | |
| 6 | class AllCommonTest(unittest.TestCase): |
| 7 | |
| 8 | def assertIs(self, a, b): |
| 9 | self.assert_(a is b) |
| 10 | |
| 11 | def test_commonprefix(self): |
| 12 | self.assertEqual( |
| 13 | genericpath.commonprefix([]), |
| 14 | "" |
| 15 | ) |
| 16 | self.assertEqual( |
| 17 | genericpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"]), |
| 18 | "/home/swen" |
| 19 | ) |
| 20 | self.assertEqual( |
| 21 | genericpath.commonprefix(["/home/swen/spam", "/home/swen/eggs"]), |
| 22 | "/home/swen/" |
| 23 | ) |
| 24 | self.assertEqual( |
| 25 | genericpath.commonprefix(["/home/swen/spam", "/home/swen/spam"]), |
| 26 | "/home/swen/spam" |
| 27 | ) |
| 28 | |
| 29 | def test_getsize(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 30 | f = open(support.TESTFN, "wb") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 31 | try: |
Guido van Rossum | 199fc75 | 2007-08-27 23:38:12 +0000 | [diff] [blame] | 32 | f.write(b"foo") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 33 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 34 | self.assertEqual(genericpath.getsize(support.TESTFN), 3) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 35 | finally: |
| 36 | if not f.closed: |
| 37 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 38 | os.remove(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 39 | |
| 40 | def test_time(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 41 | f = open(support.TESTFN, "wb") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 42 | try: |
Guido van Rossum | 199fc75 | 2007-08-27 23:38:12 +0000 | [diff] [blame] | 43 | f.write(b"foo") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 44 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 45 | f = open(support.TESTFN, "ab") |
Guido van Rossum | 199fc75 | 2007-08-27 23:38:12 +0000 | [diff] [blame] | 46 | f.write(b"bar") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 47 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 48 | f = open(support.TESTFN, "rb") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 49 | d = f.read() |
| 50 | f.close() |
Guido van Rossum | dc12288 | 2007-07-10 12:09:13 +0000 | [diff] [blame] | 51 | self.assertEqual(d, b"foobar") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 52 | |
| 53 | self.assert_( |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 54 | genericpath.getctime(support.TESTFN) <= |
| 55 | genericpath.getmtime(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 56 | ) |
| 57 | finally: |
| 58 | if not f.closed: |
| 59 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 60 | os.remove(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 61 | |
| 62 | def test_exists(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 63 | self.assertIs(genericpath.exists(support.TESTFN), False) |
| 64 | f = open(support.TESTFN, "wb") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 65 | try: |
Guido van Rossum | 199fc75 | 2007-08-27 23:38:12 +0000 | [diff] [blame] | 66 | f.write(b"foo") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 67 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 68 | self.assertIs(genericpath.exists(support.TESTFN), True) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 69 | finally: |
| 70 | if not f.close(): |
| 71 | f.close() |
| 72 | try: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 73 | os.remove(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 74 | except os.error: |
| 75 | pass |
| 76 | |
| 77 | self.assertRaises(TypeError, genericpath.exists) |
| 78 | |
| 79 | def test_isdir(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 80 | self.assertIs(genericpath.isdir(support.TESTFN), False) |
| 81 | f = open(support.TESTFN, "wb") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 82 | try: |
Guido van Rossum | 199fc75 | 2007-08-27 23:38:12 +0000 | [diff] [blame] | 83 | f.write(b"foo") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 84 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 85 | self.assertIs(genericpath.isdir(support.TESTFN), False) |
| 86 | os.remove(support.TESTFN) |
| 87 | os.mkdir(support.TESTFN) |
| 88 | self.assertIs(genericpath.isdir(support.TESTFN), True) |
| 89 | os.rmdir(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 90 | finally: |
| 91 | if not f.close(): |
| 92 | f.close() |
| 93 | try: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 94 | os.remove(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 95 | except os.error: |
| 96 | pass |
| 97 | try: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 98 | os.rmdir(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 99 | except os.error: |
| 100 | pass |
| 101 | |
| 102 | self.assertRaises(TypeError, genericpath.isdir) |
| 103 | |
| 104 | def test_isfile(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 105 | self.assertIs(genericpath.isfile(support.TESTFN), False) |
| 106 | f = open(support.TESTFN, "wb") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 107 | try: |
Guido van Rossum | 199fc75 | 2007-08-27 23:38:12 +0000 | [diff] [blame] | 108 | f.write(b"foo") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 109 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 110 | self.assertIs(genericpath.isfile(support.TESTFN), True) |
| 111 | os.remove(support.TESTFN) |
| 112 | os.mkdir(support.TESTFN) |
| 113 | self.assertIs(genericpath.isfile(support.TESTFN), False) |
| 114 | os.rmdir(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 115 | finally: |
| 116 | if not f.close(): |
| 117 | f.close() |
| 118 | try: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 119 | os.remove(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 120 | except os.error: |
| 121 | pass |
| 122 | try: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 123 | os.rmdir(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 124 | except os.error: |
| 125 | pass |
| 126 | |
| 127 | self.assertRaises(TypeError, genericpath.isdir) |
| 128 | |
| 129 | def test_samefile(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 130 | f = open(support.TESTFN + "1", "wb") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 131 | try: |
Guido van Rossum | 199fc75 | 2007-08-27 23:38:12 +0000 | [diff] [blame] | 132 | f.write(b"foo") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 133 | f.close() |
| 134 | self.assertIs( |
| 135 | genericpath.samefile( |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 136 | support.TESTFN + "1", |
| 137 | support.TESTFN + "1" |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 138 | ), |
| 139 | True |
| 140 | ) |
| 141 | # If we don't have links, assume that os.stat doesn't return resonable |
| 142 | # inode information and thus, that samefile() doesn't work |
| 143 | if hasattr(os, "symlink"): |
| 144 | os.symlink( |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 145 | support.TESTFN + "1", |
| 146 | support.TESTFN + "2" |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 147 | ) |
| 148 | self.assertIs( |
| 149 | genericpath.samefile( |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 150 | support.TESTFN + "1", |
| 151 | support.TESTFN + "2" |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 152 | ), |
| 153 | True |
| 154 | ) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 155 | os.remove(support.TESTFN + "2") |
| 156 | f = open(support.TESTFN + "2", "wb") |
Guido van Rossum | 199fc75 | 2007-08-27 23:38:12 +0000 | [diff] [blame] | 157 | f.write(b"bar") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 158 | f.close() |
| 159 | self.assertIs( |
| 160 | genericpath.samefile( |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 161 | support.TESTFN + "1", |
| 162 | support.TESTFN + "2" |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 163 | ), |
| 164 | False |
| 165 | ) |
| 166 | finally: |
| 167 | if not f.close(): |
| 168 | f.close() |
| 169 | try: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 170 | os.remove(support.TESTFN + "1") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 171 | except os.error: |
| 172 | pass |
| 173 | try: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 174 | os.remove(support.TESTFN + "2") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 175 | except os.error: |
| 176 | pass |
| 177 | |
| 178 | self.assertRaises(TypeError, genericpath.samefile) |
| 179 | |
| 180 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 181 | support.run_unittest(AllCommonTest) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 182 | |
| 183 | if __name__=="__main__": |
| 184 | test_main() |