Mark Hammond | 7995eb2 | 2002-10-03 23:14:10 +0000 | [diff] [blame^] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Test the Unicode versions of normal file functions |
| 3 | # open, os.open, os.stat. os.listdir, os.rename, os.remove, os.mkdir, os.chdir, os.rmdir |
| 4 | import os, unittest |
| 5 | from test.test_support import TESTFN, TestSkipped, TestFailed, run_suite |
| 6 | try: |
| 7 | from nt import _getfullpathname |
| 8 | except ImportError: |
| 9 | raise TestSkipped, "test works only on NT" |
| 10 | |
| 11 | filenames = [ |
| 12 | "abc", |
| 13 | unicode("ascii","utf-8"), |
| 14 | unicode("Grüß-Gott","utf-8"), |
| 15 | unicode("Γειά-σας","utf-8"), |
| 16 | unicode("Здравствуйте","utf-8"), |
| 17 | unicode("にぽん","utf-8"), |
| 18 | unicode("השקצץס","utf-8"), |
| 19 | unicode("曨曩曫","utf-8"), |
| 20 | unicode("曨שんдΓß","utf-8"), |
| 21 | ] |
| 22 | |
| 23 | class UnicodeFileTests(unittest.TestCase): |
| 24 | def setUp(self): |
| 25 | self.files = [os.path.join(TESTFN, f) for f in filenames] |
| 26 | try: |
| 27 | os.mkdir(TESTFN) |
| 28 | except OSError: |
| 29 | pass |
| 30 | for name in self.files: |
| 31 | f = open(name, 'w') |
| 32 | f.write((name+'\n').encode("utf-8")) |
| 33 | f.close() |
| 34 | os.stat(name) |
| 35 | |
| 36 | def tearDown(self): |
| 37 | for name in self.files: |
| 38 | os.unlink(name) |
| 39 | os.rmdir(TESTFN) |
| 40 | |
| 41 | def _apply_failure(self, fn, filename, expected_exception, |
| 42 | check_fn_in_exception = True): |
| 43 | try: |
| 44 | fn(filename) |
| 45 | raise TestFailed("Expected to fail calling '%s(%r)'" |
| 46 | % (fn.__name__, filename)) |
| 47 | except expected_exception, details: |
| 48 | if check_fn_in_exception and details.filename != filename: |
| 49 | raise TestFailed("Function '%s(%r) failed with " |
| 50 | "bad filename in the exception: %r" |
| 51 | % (fn.__name__, filename, |
| 52 | details.filename)) |
| 53 | |
| 54 | def test_failures(self): |
| 55 | # Pass non-existing Unicode filenames all over the place. |
| 56 | for name in self.files: |
| 57 | name = "not_" + name |
| 58 | self._apply_failure(open, name, IOError) |
| 59 | self._apply_failure(os.stat, name, OSError) |
| 60 | self._apply_failure(os.chdir, name, OSError) |
| 61 | self._apply_failure(os.rmdir, name, OSError) |
| 62 | self._apply_failure(os.remove, name, OSError) |
| 63 | # listdir may append a wildcard to the filename, so dont check |
| 64 | self._apply_failure(os.listdir, name, OSError, False) |
| 65 | |
| 66 | def test_open(self): |
| 67 | for name in self.files: |
| 68 | f = open(name, 'w') |
| 69 | f.write((name+'\n').encode("utf-8")) |
| 70 | f.close() |
| 71 | os.stat(name) |
| 72 | |
| 73 | def test_listdir(self): |
| 74 | f1 = os.listdir(TESTFN) |
| 75 | f1.sort() |
| 76 | f2 = os.listdir(unicode(TESTFN,"mbcs")) |
| 77 | f2.sort() |
| 78 | print f1 |
| 79 | print f2 |
| 80 | |
| 81 | def test_rename(self): |
| 82 | for name in self.files: |
| 83 | os.rename(name,"tmp") |
| 84 | os.rename("tmp",name) |
| 85 | |
| 86 | def test_directory(self): |
| 87 | dirname = unicode(os.path.join(TESTFN,"Grüß-曨曩曫"),"utf-8") |
| 88 | filename = unicode("ß-曨曩曫","utf-8") |
| 89 | oldwd = os.getcwd() |
| 90 | os.mkdir(dirname) |
| 91 | os.chdir(dirname) |
| 92 | f = open(filename, 'w') |
| 93 | f.write((filename + '\n').encode("utf-8")) |
| 94 | f.close() |
| 95 | print repr(_getfullpathname(filename)) |
| 96 | os.remove(filename) |
| 97 | os.chdir(oldwd) |
| 98 | os.rmdir(dirname) |
| 99 | |
| 100 | def test_main(): |
| 101 | suite = unittest.TestSuite() |
| 102 | suite.addTest(unittest.makeSuite(UnicodeFileTests)) |
| 103 | run_suite(suite) |
| 104 | |
| 105 | if __name__ == "__main__": |
| 106 | test_main() |