summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_pep277.py
blob: f543a5a616c8e720edf3f60ef071c21cd11b2479 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# -*- coding: utf-8 -*-
# Test the Unicode versions of normal file functions
# open, os.open, os.stat. os.listdir, os.rename, os.remove, os.mkdir, os.chdir, os.rmdir
import os, unittest
from test.test_support import TESTFN, TestSkipped, TestFailed, run_suite
try:
    from nt import _getfullpathname
except ImportError:
    raise TestSkipped, "test works only on NT"

filenames = [
    "abc",
    unicode("ascii","utf-8"),
    unicode("Grüß-Gott","utf-8"),
    unicode("Γειά-σας","utf-8"),
    unicode("Здравствуйте","utf-8"),
    unicode("にぽん","utf-8"),
    unicode("השקצץס","utf-8"),
    unicode("曨曩曫","utf-8"),
    unicode("曨שんдΓß","utf-8"),
    ]

class UnicodeFileTests(unittest.TestCase):
    def setUp(self):
        self.files = [os.path.join(TESTFN, f) for f in filenames]
        try:
            os.mkdir(TESTFN)
        except OSError:
            pass
        for name in self.files:
            f = open(name, 'w')
            f.write((name+'\n').encode("utf-8"))
            f.close()
            os.stat(name)

    def tearDown(self):
        for name in self.files:
            os.unlink(name)
        os.rmdir(TESTFN)

    def _apply_failure(self, fn, filename, expected_exception,
                       check_fn_in_exception = True):
        try:
            fn(filename)
            raise TestFailed("Expected to fail calling '%s(%r)'"
                             % (fn.__name__, filename))
        except expected_exception, details:
            if check_fn_in_exception and details.filename != filename:
                    raise TestFailed("Function '%s(%r) failed with "
                                     "bad filename in the exception: %r"
                                     % (fn.__name__, filename,
                                        details.filename))

    def test_failures(self):
        # Pass non-existing Unicode filenames all over the place.
        for name in self.files:
            name = "not_" + name
            self._apply_failure(open, name, IOError)
            self._apply_failure(os.stat, name, OSError)
            self._apply_failure(os.chdir, name, OSError)
            self._apply_failure(os.rmdir, name, OSError)
            self._apply_failure(os.remove, name, OSError)
            # listdir may append a wildcard to the filename, so dont check
            self._apply_failure(os.listdir, name, OSError, False)

    def test_open(self):
        for name in self.files:
            f = open(name, 'w')
            f.write((name+'\n').encode("utf-8"))
            f.close()
            os.stat(name)

    def test_listdir(self):
        f1 = os.listdir(TESTFN)
        f1.sort()
        f2 = os.listdir(unicode(TESTFN,"mbcs"))
        f2.sort()
        print f1
        print f2

    def test_rename(self):
        for name in self.files:
            os.rename(name,"tmp")
            os.rename("tmp",name)

    def test_directory(self):
        dirname = unicode(os.path.join(TESTFN,"Grüß-曨曩曫"),"utf-8")
        filename = unicode("ß-曨曩曫","utf-8")
        oldwd = os.getcwd()
        os.mkdir(dirname)
        os.chdir(dirname)
        f = open(filename, 'w')
        f.write((filename + '\n').encode("utf-8"))
        f.close()
        print repr(_getfullpathname(filename))
        os.remove(filename)
        os.chdir(oldwd)
        os.rmdir(dirname)

def test_main():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(UnicodeFileTests))
    run_suite(suite)

if __name__ == "__main__":
    test_main()