blob: 480fe0096e16336c159395d991c2fd06946b67dc [file] [log] [blame]
Mark Hammond7995eb22002-10-03 23:14:10 +00001# 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
3import os, unittest
4from test.test_support import TESTFN, TestSkipped, TestFailed, run_suite
5try:
6 from nt import _getfullpathname
7except ImportError:
Tim Peters1ee401f2002-10-05 17:54:56 +00008 raise TestSkipped, "test works only on NT+"
Mark Hammond7995eb22002-10-03 23:14:10 +00009
10filenames = [
Martin v. Löwis45bb87b2002-10-07 17:27:15 +000011 'abc',
12 u'ascii',
13 u'Gr\xfc\xdf-Gott',
14 u'\u0393\u03b5\u03b9\u03ac-\u03c3\u03b1\u03c2',
15 u'\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435',
16 u'\u306b\u307d\u3093',
17 u'\u05d4\u05e9\u05e7\u05e6\u05e5\u05e1',
18 u'\u66e8\u66e9\u66eb',
19 u'\u66e8\u05e9\u3093\u0434\u0393\xdf',
Mark Hammond7995eb22002-10-03 23:14:10 +000020 ]
21
Tim Peters1ee401f2002-10-05 17:54:56 +000022# Destroy directory dirname and all files under it, to one level.
23def deltree(dirname):
24 # Don't hide legitimate errors: if one of these suckers exists, it's
25 # an error if we can't remove it.
26 if os.path.exists(dirname):
27 for fname in os.listdir(dirname):
28 os.unlink(os.path.join(dirname, fname))
29 os.rmdir(dirname)
30
Mark Hammond7995eb22002-10-03 23:14:10 +000031class UnicodeFileTests(unittest.TestCase):
Tim Peters1ee401f2002-10-05 17:54:56 +000032 files = [os.path.join(TESTFN, f) for f in filenames]
33
Mark Hammond7995eb22002-10-03 23:14:10 +000034 def setUp(self):
Mark Hammond7995eb22002-10-03 23:14:10 +000035 try:
36 os.mkdir(TESTFN)
37 except OSError:
38 pass
39 for name in self.files:
40 f = open(name, 'w')
41 f.write((name+'\n').encode("utf-8"))
42 f.close()
43 os.stat(name)
44
45 def tearDown(self):
Tim Peters1ee401f2002-10-05 17:54:56 +000046 deltree(TESTFN)
Mark Hammond7995eb22002-10-03 23:14:10 +000047
48 def _apply_failure(self, fn, filename, expected_exception,
49 check_fn_in_exception = True):
50 try:
51 fn(filename)
52 raise TestFailed("Expected to fail calling '%s(%r)'"
53 % (fn.__name__, filename))
54 except expected_exception, details:
55 if check_fn_in_exception and details.filename != filename:
56 raise TestFailed("Function '%s(%r) failed with "
57 "bad filename in the exception: %r"
58 % (fn.__name__, filename,
59 details.filename))
60
61 def test_failures(self):
62 # Pass non-existing Unicode filenames all over the place.
63 for name in self.files:
64 name = "not_" + name
65 self._apply_failure(open, name, IOError)
66 self._apply_failure(os.stat, name, OSError)
67 self._apply_failure(os.chdir, name, OSError)
68 self._apply_failure(os.rmdir, name, OSError)
69 self._apply_failure(os.remove, name, OSError)
70 # listdir may append a wildcard to the filename, so dont check
71 self._apply_failure(os.listdir, name, OSError, False)
72
73 def test_open(self):
74 for name in self.files:
75 f = open(name, 'w')
76 f.write((name+'\n').encode("utf-8"))
77 f.close()
78 os.stat(name)
79
80 def test_listdir(self):
81 f1 = os.listdir(TESTFN)
82 f1.sort()
83 f2 = os.listdir(unicode(TESTFN,"mbcs"))
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):
Martin v. Löwis45bb87b2002-10-07 17:27:15 +000094 dirname = os.path.join(TESTFN,u'Gr\xfc\xdf-\u66e8\u66e9\u66eb')
95 filename = u'\xdf-\u66e8\u66e9\u66eb'
Mark Hammond7995eb22002-10-03 23:14:10 +000096 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()
102 print repr(_getfullpathname(filename))
103 os.remove(filename)
104 os.chdir(oldwd)
105 os.rmdir(dirname)
106
107def test_main():
108 suite = unittest.TestSuite()
109 suite.addTest(unittest.makeSuite(UnicodeFileTests))
Tim Peters1ee401f2002-10-05 17:54:56 +0000110 try:
111 run_suite(suite)
112 finally:
113 deltree(TESTFN)
Mark Hammond7995eb22002-10-03 23:14:10 +0000114
115if __name__ == "__main__":
116 test_main()