blob: bd11226e243a0ba60e3fd2a1880892c904fbe952 [file] [log] [blame]
Mark Hammond7995eb22002-10-03 23:14:10 +00001# -*- 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
4import os, unittest
5from test.test_support import TESTFN, TestSkipped, TestFailed, run_suite
6try:
7 from nt import _getfullpathname
8except ImportError:
Tim Peters1ee401f2002-10-05 17:54:56 +00009 raise TestSkipped, "test works only on NT+"
Mark Hammond7995eb22002-10-03 23:14:10 +000010
11filenames = [
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
Tim Peters1ee401f2002-10-05 17:54:56 +000023# Destroy directory dirname and all files under it, to one level.
24def deltree(dirname):
25 # Don't hide legitimate errors: if one of these suckers exists, it's
26 # an error if we can't remove it.
27 if os.path.exists(dirname):
28 for fname in os.listdir(dirname):
29 os.unlink(os.path.join(dirname, fname))
30 os.rmdir(dirname)
31
Mark Hammond7995eb22002-10-03 23:14:10 +000032class UnicodeFileTests(unittest.TestCase):
Tim Peters1ee401f2002-10-05 17:54:56 +000033 files = [os.path.join(TESTFN, f) for f in filenames]
34
Mark Hammond7995eb22002-10-03 23:14:10 +000035 def setUp(self):
Mark Hammond7995eb22002-10-03 23:14:10 +000036 try:
37 os.mkdir(TESTFN)
38 except OSError:
39 pass
40 for name in self.files:
41 f = open(name, 'w')
42 f.write((name+'\n').encode("utf-8"))
43 f.close()
44 os.stat(name)
45
46 def tearDown(self):
Tim Peters1ee401f2002-10-05 17:54:56 +000047 deltree(TESTFN)
Mark Hammond7995eb22002-10-03 23:14:10 +000048
49 def _apply_failure(self, fn, filename, expected_exception,
50 check_fn_in_exception = True):
51 try:
52 fn(filename)
53 raise TestFailed("Expected to fail calling '%s(%r)'"
54 % (fn.__name__, filename))
55 except expected_exception, details:
56 if check_fn_in_exception and details.filename != filename:
57 raise TestFailed("Function '%s(%r) failed with "
58 "bad filename in the exception: %r"
59 % (fn.__name__, filename,
60 details.filename))
61
62 def test_failures(self):
63 # Pass non-existing Unicode filenames all over the place.
64 for name in self.files:
65 name = "not_" + name
66 self._apply_failure(open, name, IOError)
67 self._apply_failure(os.stat, name, OSError)
68 self._apply_failure(os.chdir, name, OSError)
69 self._apply_failure(os.rmdir, name, OSError)
70 self._apply_failure(os.remove, name, OSError)
71 # listdir may append a wildcard to the filename, so dont check
72 self._apply_failure(os.listdir, name, OSError, False)
73
74 def test_open(self):
75 for name in self.files:
76 f = open(name, 'w')
77 f.write((name+'\n').encode("utf-8"))
78 f.close()
79 os.stat(name)
80
81 def test_listdir(self):
82 f1 = os.listdir(TESTFN)
83 f1.sort()
84 f2 = os.listdir(unicode(TESTFN,"mbcs"))
85 f2.sort()
86 print f1
87 print f2
88
89 def test_rename(self):
90 for name in self.files:
91 os.rename(name,"tmp")
92 os.rename("tmp",name)
93
94 def test_directory(self):
95 dirname = unicode(os.path.join(TESTFN,"Grüß-曨曩曫"),"utf-8")
96 filename = unicode("ß-曨曩曫","utf-8")
97 oldwd = os.getcwd()
98 os.mkdir(dirname)
99 os.chdir(dirname)
100 f = open(filename, 'w')
101 f.write((filename + '\n').encode("utf-8"))
102 f.close()
103 print repr(_getfullpathname(filename))
104 os.remove(filename)
105 os.chdir(oldwd)
106 os.rmdir(dirname)
107
108def test_main():
109 suite = unittest.TestSuite()
110 suite.addTest(unittest.makeSuite(UnicodeFileTests))
Tim Peters1ee401f2002-10-05 17:54:56 +0000111 try:
112 run_suite(suite)
113 finally:
114 deltree(TESTFN)
Mark Hammond7995eb22002-10-03 23:14:10 +0000115
116if __name__ == "__main__":
117 test_main()