PageRenderTime 30ms CodeModel.GetById 15ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 1ms

/Lib/test/test_pep277.py

http://unladen-swallow.googlecode.com/
Python | 114 lines | 97 code | 9 blank | 8 comment | 15 complexity | d6397660f489a1480d8c4c8121b7da20 MD5 | raw file
  1# 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 sys, os, unittest
  4from test import test_support
  5if not os.path.supports_unicode_filenames:
  6    raise test_support.TestSkipped, "test works only on NT+"
  7
  8filenames = [
  9    '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',
 18    ]
 19
 20# 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):
 25        # must pass unicode to os.listdir() so we get back unicode results.
 26        for fname in os.listdir(unicode(dirname)):
 27            os.unlink(os.path.join(dirname, fname))
 28        os.rmdir(dirname)
 29
 30class UnicodeFileTests(unittest.TestCase):
 31    files = [os.path.join(test_support.TESTFN, f) for f in filenames]
 32
 33    def setUp(self):
 34        try:
 35            os.mkdir(test_support.TESTFN)
 36        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):
 45        deltree(test_support.TESTFN)
 46
 47    def _apply_failure(self, fn, filename, expected_exception,
 48                       check_fn_in_exception = True):
 49        try:
 50            fn(filename)
 51            raise test_support.TestFailed("Expected to fail calling '%s(%r)'"
 52                             % (fn.__name__, filename))
 53        except expected_exception, details:
 54            if check_fn_in_exception and details.filename != filename:
 55                raise test_support.TestFailed("Function '%s(%r) failed with "
 56                                 "bad filename in the exception: %r"
 57                                 % (fn.__name__, filename,
 58                                    details.filename))
 59
 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):
 80        f1 = os.listdir(test_support.TESTFN)
 81        f2 = os.listdir(unicode(test_support.TESTFN,
 82                                sys.getfilesystemencoding()))
 83        sf2 = set(u"\\".join((unicode(test_support.TESTFN), f))
 84                  for f in f2)
 85        self.failUnlessEqual(len(f1), len(self.files))
 86        self.failUnlessEqual(sf2, set(self.files))
 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):
 94        dirname = os.path.join(test_support.TESTFN,u'Gr\xfc\xdf-\u66e8\u66e9\u66eb')
 95        filename = u'\xdf-\u66e8\u66e9\u66eb'
 96        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        os.access(filename,os.R_OK)
103        os.remove(filename)
104        os.chdir(oldwd)
105        os.rmdir(dirname)
106
107def test_main():
108    try:
109        test_support.run_unittest(UnicodeFileTests)
110    finally:
111        deltree(test_support.TESTFN)
112
113if __name__ == "__main__":
114    test_main()