test_unicode_file.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # Test some Unicode file name semantics
  2. # We don't test many operations on files other than
  3. # that their names can be used with Unicode characters.
  4. import os, glob, time, shutil
  5. import sys
  6. import unicodedata
  7. import unittest
  8. from test.support.os_helper import (rmtree, change_cwd, TESTFN_UNICODE,
  9. TESTFN_UNENCODABLE, create_empty_file)
  10. if not os.path.supports_unicode_filenames:
  11. try:
  12. TESTFN_UNICODE.encode(sys.getfilesystemencoding())
  13. except (UnicodeError, TypeError):
  14. # Either the file system encoding is None, or the file name
  15. # cannot be encoded in the file system encoding.
  16. raise unittest.SkipTest("No Unicode filesystem semantics on this platform.")
  17. def remove_if_exists(filename):
  18. if os.path.exists(filename):
  19. os.unlink(filename)
  20. class TestUnicodeFiles(unittest.TestCase):
  21. # The 'do_' functions are the actual tests. They generally assume the
  22. # file already exists etc.
  23. # Do all the tests we can given only a single filename. The file should
  24. # exist.
  25. def _do_single(self, filename):
  26. self.assertTrue(os.path.exists(filename))
  27. self.assertTrue(os.path.isfile(filename))
  28. self.assertTrue(os.access(filename, os.R_OK))
  29. self.assertTrue(os.path.exists(os.path.abspath(filename)))
  30. self.assertTrue(os.path.isfile(os.path.abspath(filename)))
  31. self.assertTrue(os.access(os.path.abspath(filename), os.R_OK))
  32. os.chmod(filename, 0o777)
  33. os.utime(filename, None)
  34. os.utime(filename, (time.time(), time.time()))
  35. # Copy/rename etc tests using the same filename
  36. self._do_copyish(filename, filename)
  37. # Filename should appear in glob output
  38. self.assertTrue(
  39. os.path.abspath(filename)==os.path.abspath(glob.glob(glob.escape(filename))[0]))
  40. # basename should appear in listdir.
  41. path, base = os.path.split(os.path.abspath(filename))
  42. file_list = os.listdir(path)
  43. # Normalize the unicode strings, as round-tripping the name via the OS
  44. # may return a different (but equivalent) value.
  45. base = unicodedata.normalize("NFD", base)
  46. file_list = [unicodedata.normalize("NFD", f) for f in file_list]
  47. self.assertIn(base, file_list)
  48. # Tests that copy, move, etc one file to another.
  49. def _do_copyish(self, filename1, filename2):
  50. # Should be able to rename the file using either name.
  51. self.assertTrue(os.path.isfile(filename1)) # must exist.
  52. os.rename(filename1, filename2 + ".new")
  53. self.assertFalse(os.path.isfile(filename2))
  54. self.assertTrue(os.path.isfile(filename1 + '.new'))
  55. os.rename(filename1 + ".new", filename2)
  56. self.assertFalse(os.path.isfile(filename1 + '.new'))
  57. self.assertTrue(os.path.isfile(filename2))
  58. shutil.copy(filename1, filename2 + ".new")
  59. os.unlink(filename1 + ".new") # remove using equiv name.
  60. # And a couple of moves, one using each name.
  61. shutil.move(filename1, filename2 + ".new")
  62. self.assertFalse(os.path.exists(filename2))
  63. self.assertTrue(os.path.exists(filename1 + '.new'))
  64. shutil.move(filename1 + ".new", filename2)
  65. self.assertFalse(os.path.exists(filename2 + '.new'))
  66. self.assertTrue(os.path.exists(filename1))
  67. # Note - due to the implementation of shutil.move,
  68. # it tries a rename first. This only fails on Windows when on
  69. # different file systems - and this test can't ensure that.
  70. # So we test the shutil.copy2 function, which is the thing most
  71. # likely to fail.
  72. shutil.copy2(filename1, filename2 + ".new")
  73. self.assertTrue(os.path.isfile(filename1 + '.new'))
  74. os.unlink(filename1 + ".new")
  75. self.assertFalse(os.path.exists(filename2 + '.new'))
  76. def _do_directory(self, make_name, chdir_name):
  77. if os.path.isdir(make_name):
  78. rmtree(make_name)
  79. os.mkdir(make_name)
  80. try:
  81. with change_cwd(chdir_name):
  82. cwd_result = os.getcwd()
  83. name_result = make_name
  84. cwd_result = unicodedata.normalize("NFD", cwd_result)
  85. name_result = unicodedata.normalize("NFD", name_result)
  86. self.assertEqual(os.path.basename(cwd_result),name_result)
  87. finally:
  88. os.rmdir(make_name)
  89. # The '_test' functions 'entry points with params' - ie, what the
  90. # top-level 'test' functions would be if they could take params
  91. def _test_single(self, filename):
  92. remove_if_exists(filename)
  93. create_empty_file(filename)
  94. try:
  95. self._do_single(filename)
  96. finally:
  97. os.unlink(filename)
  98. self.assertTrue(not os.path.exists(filename))
  99. # and again with os.open.
  100. f = os.open(filename, os.O_CREAT | os.O_WRONLY)
  101. os.close(f)
  102. try:
  103. self._do_single(filename)
  104. finally:
  105. os.unlink(filename)
  106. # The 'test' functions are unittest entry points, and simply call our
  107. # _test functions with each of the filename combinations we wish to test
  108. def test_single_files(self):
  109. self._test_single(TESTFN_UNICODE)
  110. if TESTFN_UNENCODABLE is not None:
  111. self._test_single(TESTFN_UNENCODABLE)
  112. def test_directories(self):
  113. # For all 'equivalent' combinations:
  114. # Make dir with encoded, chdir with unicode, checkdir with encoded
  115. # (or unicode/encoded/unicode, etc
  116. ext = ".dir"
  117. self._do_directory(TESTFN_UNICODE+ext, TESTFN_UNICODE+ext)
  118. # Our directory name that can't use a non-unicode name.
  119. if TESTFN_UNENCODABLE is not None:
  120. self._do_directory(TESTFN_UNENCODABLE+ext,
  121. TESTFN_UNENCODABLE+ext)
  122. if __name__ == "__main__":
  123. unittest.main()