Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 1 | # Test the Unicode versions of normal file functions |
| 2 | # open, os.open, os.stat. os.listdir, os.rename, os.remove, os.mkdir, os.chdir, os.rmdir |
Mark Hammond | bb4a47c | 2003-07-16 03:46:38 +0000 | [diff] [blame] | 3 | import sys, os, unittest |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 4 | from test import test_support |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 5 | if not os.path.supports_unicode_filenames: |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 6 | raise test_support.TestSkipped, "test works only on NT+" |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 7 | |
| 8 | filenames = [ |
Martin v. Löwis | 45bb87b | 2002-10-07 17:27:15 +0000 | [diff] [blame] | 9 | 'abc', |
| 10 | u'ascii', |
| 11 | u'Gr\xfc\xdf-Gott', |
| 12 | u'\u0393\u03b5\u03b9\u03ac-\u03c3\u03b1\u03c2', |
| 13 | u'\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435', |
| 14 | u'\u306b\u307d\u3093', |
| 15 | u'\u05d4\u05e9\u05e7\u05e6\u05e5\u05e1', |
| 16 | u'\u66e8\u66e9\u66eb', |
| 17 | u'\u66e8\u05e9\u3093\u0434\u0393\xdf', |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 18 | ] |
| 19 | |
Tim Peters | 1ee401f | 2002-10-05 17:54:56 +0000 | [diff] [blame] | 20 | # Destroy directory dirname and all files under it, to one level. |
| 21 | def deltree(dirname): |
| 22 | # Don't hide legitimate errors: if one of these suckers exists, it's |
| 23 | # an error if we can't remove it. |
| 24 | if os.path.exists(dirname): |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 25 | # must pass unicode to os.listdir() so we get back unicode results. |
| 26 | for fname in os.listdir(unicode(dirname)): |
Tim Peters | 1ee401f | 2002-10-05 17:54:56 +0000 | [diff] [blame] | 27 | os.unlink(os.path.join(dirname, fname)) |
| 28 | os.rmdir(dirname) |
| 29 | |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 30 | class UnicodeFileTests(unittest.TestCase): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 31 | files = [os.path.join(test_support.TESTFN, f) for f in filenames] |
Tim Peters | 1ee401f | 2002-10-05 17:54:56 +0000 | [diff] [blame] | 32 | |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 33 | def setUp(self): |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 34 | try: |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 35 | os.mkdir(test_support.TESTFN) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 36 | except OSError: |
| 37 | pass |
| 38 | for name in self.files: |
| 39 | f = open(name, 'w') |
| 40 | f.write((name+'\n').encode("utf-8")) |
| 41 | f.close() |
| 42 | os.stat(name) |
| 43 | |
| 44 | def tearDown(self): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 45 | deltree(test_support.TESTFN) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 46 | |
| 47 | def _apply_failure(self, fn, filename, expected_exception, |
| 48 | check_fn_in_exception = True): |
| 49 | try: |
| 50 | fn(filename) |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 51 | raise test_support.TestFailed("Expected to fail calling '%s(%r)'" |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 52 | % (fn.__name__, filename)) |
| 53 | except expected_exception, details: |
| 54 | if check_fn_in_exception and details.filename != filename: |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 55 | raise test_support.TestFailed("Function '%s(%r) failed with " |
Tim Peters | 3de7526 | 2002-11-09 05:26:15 +0000 | [diff] [blame] | 56 | "bad filename in the exception: %r" |
| 57 | % (fn.__name__, filename, |
| 58 | details.filename)) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 59 | |
| 60 | def test_failures(self): |
| 61 | # Pass non-existing Unicode filenames all over the place. |
| 62 | for name in self.files: |
| 63 | name = "not_" + name |
| 64 | self._apply_failure(open, name, IOError) |
| 65 | self._apply_failure(os.stat, name, OSError) |
| 66 | self._apply_failure(os.chdir, name, OSError) |
| 67 | self._apply_failure(os.rmdir, name, OSError) |
| 68 | self._apply_failure(os.remove, name, OSError) |
| 69 | # listdir may append a wildcard to the filename, so dont check |
| 70 | self._apply_failure(os.listdir, name, OSError, False) |
| 71 | |
| 72 | def test_open(self): |
| 73 | for name in self.files: |
| 74 | f = open(name, 'w') |
| 75 | f.write((name+'\n').encode("utf-8")) |
| 76 | f.close() |
| 77 | os.stat(name) |
| 78 | |
| 79 | def test_listdir(self): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 80 | f1 = os.listdir(test_support.TESTFN) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 81 | f1.sort() |
Mark Hammond | bb4a47c | 2003-07-16 03:46:38 +0000 | [diff] [blame] | 82 | f2 = os.listdir(unicode(test_support.TESTFN, |
| 83 | sys.getfilesystemencoding())) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 84 | f2.sort() |
| 85 | print f1 |
| 86 | print f2 |
| 87 | |
| 88 | def test_rename(self): |
| 89 | for name in self.files: |
| 90 | os.rename(name,"tmp") |
| 91 | os.rename("tmp",name) |
| 92 | |
| 93 | def test_directory(self): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 94 | dirname = os.path.join(test_support.TESTFN,u'Gr\xfc\xdf-\u66e8\u66e9\u66eb') |
Martin v. Löwis | 45bb87b | 2002-10-07 17:27:15 +0000 | [diff] [blame] | 95 | filename = u'\xdf-\u66e8\u66e9\u66eb' |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 96 | oldwd = os.getcwd() |
| 97 | os.mkdir(dirname) |
| 98 | os.chdir(dirname) |
| 99 | f = open(filename, 'w') |
| 100 | f.write((filename + '\n').encode("utf-8")) |
| 101 | f.close() |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 102 | print repr(filename) |
Martin v. Löwis | 1b699a5 | 2003-09-12 16:25:38 +0000 | [diff] [blame] | 103 | os.access(filename,os.R_OK) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 104 | os.remove(filename) |
| 105 | os.chdir(oldwd) |
| 106 | os.rmdir(dirname) |
| 107 | |
| 108 | def test_main(): |
Tim Peters | 1ee401f | 2002-10-05 17:54:56 +0000 | [diff] [blame] | 109 | try: |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 110 | test_support.run_unittest(UnicodeFileTests) |
Tim Peters | 1ee401f | 2002-10-05 17:54:56 +0000 | [diff] [blame] | 111 | finally: |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 112 | deltree(test_support.TESTFN) |
Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame] | 113 | |
| 114 | if __name__ == "__main__": |
| 115 | test_main() |