home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 June / PCWorld_2005-06_cd.bin / software / vyzkuste / firewally / firewally.exe / framework-2.3.exe / test_pep277.py < prev    next >
Text File  |  2003-12-30  |  4KB  |  115 lines

  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
  3. import sys, os, unittest
  4. from test import test_support
  5. if not os.path.supports_unicode_filenames:
  6.     raise test_support.TestSkipped, "test works only on NT+"
  7.  
  8. filenames = [
  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.
  21. def 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.  
  30. class 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.         f1.sort()
  82.         f2 = os.listdir(unicode(test_support.TESTFN,
  83.                                 sys.getfilesystemencoding()))
  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):
  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.         print repr(filename)
  103.         os.remove(filename)
  104.         os.chdir(oldwd)
  105.         os.rmdir(dirname)
  106.  
  107. def test_main():
  108.     try:
  109.         test_support.run_unittest(UnicodeFileTests)
  110.     finally:
  111.         deltree(test_support.TESTFN)
  112.  
  113. if __name__ == "__main__":
  114.     test_main()
  115.