blob: b48317f6d6cb04bbbb073978391d8d1d44b70884 [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
Mark Hammondbb4a47c2003-07-16 03:46:38 +00003import sys, os, unittest
Walter Dörwald21d3a322003-05-01 17:45:56 +00004from test import test_support
Mark Hammond8696ebc2002-10-08 02:44:31 +00005if not os.path.supports_unicode_filenames:
Walter Dörwald21d3a322003-05-01 17:45:56 +00006 raise test_support.TestSkipped, "test works only on NT+"
Mark Hammond7995eb22002-10-03 23:14:10 +00007
8filenames = [
Martin v. Löwis45bb87b2002-10-07 17:27:15 +00009 '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 Hammond7995eb22002-10-03 23:14:10 +000018 ]
19
Tim Peters1ee401f2002-10-05 17:54:56 +000020# Destroy directory dirname and all files under it, to one level.
21def 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 Hammond8696ebc2002-10-08 02:44:31 +000025 # must pass unicode to os.listdir() so we get back unicode results.
26 for fname in os.listdir(unicode(dirname)):
Tim Peters1ee401f2002-10-05 17:54:56 +000027 os.unlink(os.path.join(dirname, fname))
28 os.rmdir(dirname)
29
Mark Hammond7995eb22002-10-03 23:14:10 +000030class UnicodeFileTests(unittest.TestCase):
Walter Dörwald21d3a322003-05-01 17:45:56 +000031 files = [os.path.join(test_support.TESTFN, f) for f in filenames]
Tim Peters1ee401f2002-10-05 17:54:56 +000032
Mark Hammond7995eb22002-10-03 23:14:10 +000033 def setUp(self):
Mark Hammond7995eb22002-10-03 23:14:10 +000034 try:
Walter Dörwald21d3a322003-05-01 17:45:56 +000035 os.mkdir(test_support.TESTFN)
Mark Hammond7995eb22002-10-03 23:14:10 +000036 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örwald21d3a322003-05-01 17:45:56 +000045 deltree(test_support.TESTFN)
Mark Hammond7995eb22002-10-03 23:14:10 +000046
47 def _apply_failure(self, fn, filename, expected_exception,
48 check_fn_in_exception = True):
49 try:
50 fn(filename)
Walter Dörwald21d3a322003-05-01 17:45:56 +000051 raise test_support.TestFailed("Expected to fail calling '%s(%r)'"
Mark Hammond7995eb22002-10-03 23:14:10 +000052 % (fn.__name__, filename))
53 except expected_exception, details:
54 if check_fn_in_exception and details.filename != filename:
Walter Dörwald21d3a322003-05-01 17:45:56 +000055 raise test_support.TestFailed("Function '%s(%r) failed with "
Tim Peters3de75262002-11-09 05:26:15 +000056 "bad filename in the exception: %r"
57 % (fn.__name__, filename,
58 details.filename))
Mark Hammond7995eb22002-10-03 23:14:10 +000059
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örwald21d3a322003-05-01 17:45:56 +000080 f1 = os.listdir(test_support.TESTFN)
Mark Hammond7995eb22002-10-03 23:14:10 +000081 f1.sort()
Mark Hammondbb4a47c2003-07-16 03:46:38 +000082 f2 = os.listdir(unicode(test_support.TESTFN,
83 sys.getfilesystemencoding()))
Mark Hammond7995eb22002-10-03 23:14:10 +000084 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örwald21d3a322003-05-01 17:45:56 +000094 dirname = os.path.join(test_support.TESTFN,u'Gr\xfc\xdf-\u66e8\u66e9\u66eb')
Martin v. Löwis45bb87b2002-10-07 17:27:15 +000095 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()
Mark Hammond8696ebc2002-10-08 02:44:31 +0000102 print repr(filename)
Martin v. Löwis1b699a52003-09-12 16:25:38 +0000103 os.access(filename,os.R_OK)
Mark Hammond7995eb22002-10-03 23:14:10 +0000104 os.remove(filename)
105 os.chdir(oldwd)
106 os.rmdir(dirname)
107
108def test_main():
Tim Peters1ee401f2002-10-05 17:54:56 +0000109 try:
Walter Dörwald21d3a322003-05-01 17:45:56 +0000110 test_support.run_unittest(UnicodeFileTests)
Tim Peters1ee401f2002-10-05 17:54:56 +0000111 finally:
Walter Dörwald21d3a322003-05-01 17:45:56 +0000112 deltree(test_support.TESTFN)
Mark Hammond7995eb22002-10-03 23:14:10 +0000113
114if __name__ == "__main__":
115 test_main()