PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_Main/External.LCA_RESTRICTED/Languages/IronPython/26/Lib/test/test_unicode_file.py

#
Python | 208 lines | 142 code | 22 blank | 44 comment | 35 complexity | 3566ee31eb2711290a78da59e8742893 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. # Test some Unicode file name semantics
  2. # We dont test many operations on files other than
  3. # that their names can be used with Unicode characters.
  4. from test import test_support
  5. import os, glob, time, shutil
  6. if not test_support.due_to_ironpython_bug("http://tkbgitvstfat01:8080/WorkItemTracking/WorkItem.aspx?artifactMoniker=303481"):
  7. import unicodedata
  8. import unittest
  9. from test.test_support import run_unittest, TestSkipped, TESTFN_UNICODE
  10. from test.test_support import TESTFN_ENCODING, TESTFN_UNICODE_UNENCODEABLE
  11. try:
  12. TESTFN_ENCODED = TESTFN_UNICODE.encode(TESTFN_ENCODING)
  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 TestSkipped("No Unicode filesystem semantics on this platform.")
  17. if TESTFN_ENCODED.decode(TESTFN_ENCODING) != TESTFN_UNICODE:
  18. # The file system encoding does not support Latin-1
  19. # (which test_support assumes), so try the file system
  20. # encoding instead.
  21. import sys
  22. try:
  23. TESTFN_UNICODE = unicode("@test-\xe0\xf2", sys.getfilesystemencoding())
  24. TESTFN_ENCODED = TESTFN_UNICODE.encode(TESTFN_ENCODING)
  25. if '?' in TESTFN_ENCODED:
  26. # MBCS will not report the error properly
  27. raise UnicodeError, "mbcs encoding problem"
  28. except (UnicodeError, TypeError):
  29. raise TestSkipped("Cannot find a suiteable filename.")
  30. if TESTFN_ENCODED.decode(TESTFN_ENCODING) != TESTFN_UNICODE:
  31. raise TestSkipped("Cannot find a suitable filename.")
  32. def remove_if_exists(filename):
  33. if os.path.exists(filename):
  34. os.unlink(filename)
  35. class TestUnicodeFiles(unittest.TestCase):
  36. # The 'do_' functions are the actual tests. They generally assume the
  37. # file already exists etc.
  38. # Do all the tests we can given only a single filename. The file should
  39. # exist.
  40. def _do_single(self, filename):
  41. self.failUnless(os.path.exists(filename))
  42. self.failUnless(os.path.isfile(filename))
  43. self.failUnless(os.access(filename, os.R_OK))
  44. self.failUnless(os.path.exists(os.path.abspath(filename)))
  45. self.failUnless(os.path.isfile(os.path.abspath(filename)))
  46. self.failUnless(os.access(os.path.abspath(filename), os.R_OK))
  47. if not test_support.due_to_ironpython_bug("http://tkbgitvstfat01:8080/WorkItemTracking/WorkItem.aspx?artifactMoniker=363042"):
  48. os.chmod(filename, 0777)
  49. os.utime(filename, None)
  50. os.utime(filename, (time.time(), time.time()))
  51. # Copy/rename etc tests using the same filename
  52. self._do_copyish(filename, filename)
  53. # Filename should appear in glob output
  54. self.failUnless(
  55. os.path.abspath(filename)==os.path.abspath(glob.glob(filename)[0]))
  56. # basename should appear in listdir.
  57. path, base = os.path.split(os.path.abspath(filename))
  58. if isinstance(base, str):
  59. base = base.decode(TESTFN_ENCODING)
  60. file_list = os.listdir(path)
  61. # listdir() with a unicode arg may or may not return Unicode
  62. # objects, depending on the platform.
  63. if file_list and isinstance(file_list[0], str):
  64. file_list = [f.decode(TESTFN_ENCODING) for f in file_list]
  65. # Normalize the unicode strings, as round-tripping the name via the OS
  66. # may return a different (but equivalent) value.
  67. if not test_support.due_to_ironpython_bug("http://tkbgitvstfat01:8080/WorkItemTracking/WorkItem.aspx?artifactMoniker=303481"):
  68. base = unicodedata.normalize("NFD", base)
  69. file_list = [unicodedata.normalize("NFD", f) for f in file_list]
  70. self.failUnless(base in file_list)
  71. # Do as many "equivalancy' tests as we can - ie, check that although we
  72. # have different types for the filename, they refer to the same file.
  73. def _do_equivalent(self, filename1, filename2):
  74. # Note we only check "filename1 against filename2" - we don't bother
  75. # checking "filename2 against 1", as we assume we are called again with
  76. # the args reversed.
  77. if not test_support.due_to_ironpython_bug("http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=7279"):
  78. self.failUnless(type(filename1)!=type(filename2),
  79. "No point checking equivalent filenames of the same type")
  80. # stat and lstat should return the same results.
  81. self.failUnlessEqual(os.stat(filename1),
  82. os.stat(filename2))
  83. self.failUnlessEqual(os.lstat(filename1),
  84. os.lstat(filename2))
  85. # Copy/rename etc tests using equivalent filename
  86. self._do_copyish(filename1, filename2)
  87. # Tests that copy, move, etc one file to another.
  88. def _do_copyish(self, filename1, filename2):
  89. # Should be able to rename the file using either name.
  90. self.failUnless(os.path.isfile(filename1)) # must exist.
  91. os.rename(filename1, filename2 + ".new")
  92. self.failUnless(os.path.isfile(filename1+".new"))
  93. os.rename(filename1 + ".new", filename2)
  94. self.failUnless(os.path.isfile(filename2))
  95. shutil.copy(filename1, filename2 + ".new")
  96. os.unlink(filename1 + ".new") # remove using equiv name.
  97. # And a couple of moves, one using each name.
  98. shutil.move(filename1, filename2 + ".new")
  99. self.failUnless(not os.path.exists(filename2))
  100. shutil.move(filename1 + ".new", filename2)
  101. self.failUnless(os.path.exists(filename1))
  102. # Note - due to the implementation of shutil.move,
  103. # it tries a rename first. This only fails on Windows when on
  104. # different file systems - and this test can't ensure that.
  105. # So we test the shutil.copy2 function, which is the thing most
  106. # likely to fail.
  107. shutil.copy2(filename1, filename2 + ".new")
  108. os.unlink(filename1 + ".new")
  109. def _do_directory(self, make_name, chdir_name, encoded):
  110. cwd = os.getcwd()
  111. if os.path.isdir(make_name):
  112. os.rmdir(make_name)
  113. os.mkdir(make_name)
  114. try:
  115. os.chdir(chdir_name)
  116. try:
  117. if not encoded:
  118. cwd_result = os.getcwdu()
  119. name_result = make_name
  120. else:
  121. cwd_result = os.getcwd().decode(TESTFN_ENCODING)
  122. name_result = make_name.decode(TESTFN_ENCODING)
  123. if not test_support.due_to_ironpython_bug("http://tkbgitvstfat01:8080/WorkItemTracking/WorkItem.aspx?artifactMoniker=303481"):
  124. cwd_result = unicodedata.normalize("NFD", cwd_result)
  125. name_result = unicodedata.normalize("NFD", name_result)
  126. self.failUnlessEqual(os.path.basename(cwd_result),name_result)
  127. finally:
  128. os.chdir(cwd)
  129. finally:
  130. os.rmdir(make_name)
  131. # The '_test' functions 'entry points with params' - ie, what the
  132. # top-level 'test' functions would be if they could take params
  133. def _test_single(self, filename):
  134. remove_if_exists(filename)
  135. f = file(filename, "w")
  136. f.close()
  137. try:
  138. self._do_single(filename)
  139. finally:
  140. os.unlink(filename)
  141. self.failUnless(not os.path.exists(filename))
  142. # and again with os.open.
  143. f = os.open(filename, os.O_CREAT)
  144. os.close(f)
  145. try:
  146. self._do_single(filename)
  147. finally:
  148. os.unlink(filename)
  149. def _test_equivalent(self, filename1, filename2):
  150. remove_if_exists(filename1)
  151. self.failUnless(not os.path.exists(filename2))
  152. f = file(filename1, "w")
  153. f.close()
  154. try:
  155. self._do_equivalent(filename1, filename2)
  156. finally:
  157. os.unlink(filename1)
  158. # The 'test' functions are unittest entry points, and simply call our
  159. # _test functions with each of the filename combinations we wish to test
  160. def test_single_files(self):
  161. self._test_single(TESTFN_ENCODED)
  162. self._test_single(TESTFN_UNICODE)
  163. if TESTFN_UNICODE_UNENCODEABLE is not None:
  164. self._test_single(TESTFN_UNICODE_UNENCODEABLE)
  165. def test_equivalent_files(self):
  166. self._test_equivalent(TESTFN_ENCODED, TESTFN_UNICODE)
  167. self._test_equivalent(TESTFN_UNICODE, TESTFN_ENCODED)
  168. def test_directories(self):
  169. # For all 'equivalent' combinations:
  170. # Make dir with encoded, chdir with unicode, checkdir with encoded
  171. # (or unicode/encoded/unicode, etc
  172. ext = ".dir"
  173. self._do_directory(TESTFN_ENCODED+ext, TESTFN_ENCODED+ext, True)
  174. self._do_directory(TESTFN_ENCODED+ext, TESTFN_UNICODE+ext, True)
  175. self._do_directory(TESTFN_UNICODE+ext, TESTFN_ENCODED+ext, False)
  176. self._do_directory(TESTFN_UNICODE+ext, TESTFN_UNICODE+ext, False)
  177. # Our directory name that can't use a non-unicode name.
  178. if TESTFN_UNICODE_UNENCODEABLE is not None:
  179. self._do_directory(TESTFN_UNICODE_UNENCODEABLE+ext,
  180. TESTFN_UNICODE_UNENCODEABLE+ext,
  181. False)
  182. def test_main():
  183. if not test_support.due_to_ironpython_bug("http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=7279"):
  184. run_unittest(__name__)
  185. if __name__ == "__main__":
  186. test_main()