PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/qt/qtwebkit/Tools/Scripts/webkitpy/common/system/filesystem_unittest.py

https://gitlab.com/x33n/phantomjs
Python | 264 lines | 195 code | 32 blank | 37 comment | 17 complexity | e6048241a2c1dadf3432462c8ddbdf82 MD5 | raw file
  1. # vim: set fileencoding=utf-8 :
  2. # Copyright (C) 2010 Google Inc. All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are
  6. # met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above
  11. # copyright notice, this list of conditions and the following disclaimer
  12. # in the documentation and/or other materials provided with the
  13. # distribution.
  14. # * Neither the name of Google Inc. nor the names of its
  15. # contributors may be used to endorse or promote products derived from
  16. # this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. # NOTE: The fileencoding comment on the first line of the file is
  30. # important; without it, Python will choke while trying to parse the file,
  31. # since it includes non-ASCII characters.
  32. import os
  33. import stat
  34. import sys
  35. import tempfile
  36. import unittest2 as unittest
  37. from filesystem import FileSystem
  38. class GenericFileSystemTests(object):
  39. """Tests that should pass on either a real or mock filesystem."""
  40. def setup_generic_test_dir(self):
  41. fs = self.fs
  42. self.generic_test_dir = str(self.fs.mkdtemp())
  43. self.orig_cwd = fs.getcwd()
  44. fs.chdir(self.generic_test_dir)
  45. fs.write_text_file('foo.txt', 'foo')
  46. fs.write_text_file('foobar', 'foobar')
  47. fs.maybe_make_directory('foodir')
  48. fs.write_text_file(fs.join('foodir', 'baz'), 'baz')
  49. fs.chdir(self.orig_cwd)
  50. def teardown_generic_test_dir(self):
  51. self.fs.rmtree(self.generic_test_dir)
  52. self.fs.chdir(self.orig_cwd)
  53. self.generic_test_dir = None
  54. def test_glob__trailing_asterisk(self):
  55. self.fs.chdir(self.generic_test_dir)
  56. self.assertEqual(set(self.fs.glob('fo*')), set(['foo.txt', 'foobar', 'foodir']))
  57. def test_glob__leading_asterisk(self):
  58. self.fs.chdir(self.generic_test_dir)
  59. self.assertEqual(set(self.fs.glob('*xt')), set(['foo.txt']))
  60. def test_glob__middle_asterisk(self):
  61. self.fs.chdir(self.generic_test_dir)
  62. self.assertEqual(set(self.fs.glob('f*r')), set(['foobar', 'foodir']))
  63. def test_glob__period_is_escaped(self):
  64. self.fs.chdir(self.generic_test_dir)
  65. self.assertEqual(set(self.fs.glob('foo.*')), set(['foo.txt']))
  66. class RealFileSystemTest(unittest.TestCase, GenericFileSystemTests):
  67. def setUp(self):
  68. self.fs = FileSystem()
  69. self.setup_generic_test_dir()
  70. self._this_dir = os.path.dirname(os.path.abspath(__file__))
  71. self._missing_file = os.path.join(self._this_dir, 'missing_file.py')
  72. self._this_file = os.path.join(self._this_dir, 'filesystem_unittest.py')
  73. def tearDown(self):
  74. self.teardown_generic_test_dir()
  75. self.fs = None
  76. def test_chdir(self):
  77. fs = FileSystem()
  78. cwd = fs.getcwd()
  79. newdir = '/'
  80. if sys.platform == 'win32':
  81. newdir = 'c:\\'
  82. fs.chdir(newdir)
  83. self.assertEqual(fs.getcwd(), newdir)
  84. fs.chdir(cwd)
  85. def test_chdir__notexists(self):
  86. fs = FileSystem()
  87. newdir = '/dirdoesnotexist'
  88. if sys.platform == 'win32':
  89. newdir = 'c:\\dirdoesnotexist'
  90. self.assertRaises(OSError, fs.chdir, newdir)
  91. def test_exists__true(self):
  92. fs = FileSystem()
  93. self.assertTrue(fs.exists(self._this_file))
  94. def test_exists__false(self):
  95. fs = FileSystem()
  96. self.assertFalse(fs.exists(self._missing_file))
  97. def test_getcwd(self):
  98. fs = FileSystem()
  99. self.assertTrue(fs.exists(fs.getcwd()))
  100. def test_isdir__true(self):
  101. fs = FileSystem()
  102. self.assertTrue(fs.isdir(self._this_dir))
  103. def test_isdir__false(self):
  104. fs = FileSystem()
  105. self.assertFalse(fs.isdir(self._this_file))
  106. def test_join(self):
  107. fs = FileSystem()
  108. self.assertEqual(fs.join('foo', 'bar'),
  109. os.path.join('foo', 'bar'))
  110. def test_listdir(self):
  111. fs = FileSystem()
  112. with fs.mkdtemp(prefix='filesystem_unittest_') as d:
  113. self.assertEqual(fs.listdir(d), [])
  114. new_file = os.path.join(d, 'foo')
  115. fs.write_text_file(new_file, u'foo')
  116. self.assertEqual(fs.listdir(d), ['foo'])
  117. os.remove(new_file)
  118. def test_maybe_make_directory__success(self):
  119. fs = FileSystem()
  120. with fs.mkdtemp(prefix='filesystem_unittest_') as base_path:
  121. sub_path = os.path.join(base_path, "newdir")
  122. self.assertFalse(os.path.exists(sub_path))
  123. self.assertFalse(fs.isdir(sub_path))
  124. fs.maybe_make_directory(sub_path)
  125. self.assertTrue(os.path.exists(sub_path))
  126. self.assertTrue(fs.isdir(sub_path))
  127. # Make sure we can re-create it.
  128. fs.maybe_make_directory(sub_path)
  129. self.assertTrue(os.path.exists(sub_path))
  130. self.assertTrue(fs.isdir(sub_path))
  131. # Clean up.
  132. os.rmdir(sub_path)
  133. self.assertFalse(os.path.exists(base_path))
  134. self.assertFalse(fs.isdir(base_path))
  135. def test_maybe_make_directory__failure(self):
  136. # FIXME: os.chmod() doesn't work on Windows to set directories
  137. # as readonly, so we skip this test for now.
  138. if sys.platform in ('win32', 'cygwin'):
  139. return
  140. fs = FileSystem()
  141. with fs.mkdtemp(prefix='filesystem_unittest_') as d:
  142. # Remove write permissions on the parent directory.
  143. os.chmod(d, stat.S_IRUSR)
  144. # Now try to create a sub directory - should fail.
  145. sub_dir = fs.join(d, 'subdir')
  146. self.assertRaises(OSError, fs.maybe_make_directory, sub_dir)
  147. # Clean up in case the test failed and we did create the
  148. # directory.
  149. if os.path.exists(sub_dir):
  150. os.rmdir(sub_dir)
  151. def test_read_and_write_text_file(self):
  152. fs = FileSystem()
  153. text_path = None
  154. unicode_text_string = u'\u016An\u012Dc\u014Dde\u033D'
  155. hex_equivalent = '\xC5\xAA\x6E\xC4\xAD\x63\xC5\x8D\x64\x65\xCC\xBD'
  156. try:
  157. text_path = tempfile.mktemp(prefix='tree_unittest_')
  158. file = fs.open_text_file_for_writing(text_path)
  159. file.write(unicode_text_string)
  160. file.close()
  161. file = fs.open_text_file_for_reading(text_path)
  162. read_text = file.read()
  163. file.close()
  164. self.assertEqual(read_text, unicode_text_string)
  165. finally:
  166. if text_path and fs.isfile(text_path):
  167. os.remove(text_path)
  168. def test_read_and_write_file(self):
  169. fs = FileSystem()
  170. text_path = None
  171. binary_path = None
  172. unicode_text_string = u'\u016An\u012Dc\u014Dde\u033D'
  173. hex_equivalent = '\xC5\xAA\x6E\xC4\xAD\x63\xC5\x8D\x64\x65\xCC\xBD'
  174. malformed_text_hex = '\x4D\x69\x63\x72\x6F\x73\x6F\x66\x74\xAE\x20\x56\x69\x73\x75\x61\x6C\x20\x53\x74\x75\x64\x69\x6F\xAE\x20\x32\x30\x31\x30\x0D\x0A'
  175. malformed_ignored_text_hex = '\x4D\x69\x63\x72\x6F\x73\x6F\x66\x74\x20\x56\x69\x73\x75\x61\x6C\x20\x53\x74\x75\x64\x69\x6F\x20\x32\x30\x31\x30\x0D\x0A'
  176. try:
  177. text_path = tempfile.mktemp(prefix='tree_unittest_')
  178. binary_path = tempfile.mktemp(prefix='tree_unittest_')
  179. fs.write_text_file(text_path, unicode_text_string)
  180. contents = fs.read_binary_file(text_path)
  181. self.assertEqual(contents, hex_equivalent)
  182. fs.write_binary_file(binary_path, hex_equivalent)
  183. text_contents = fs.read_text_file(binary_path)
  184. self.assertEqual(text_contents, unicode_text_string)
  185. self.assertRaises(ValueError, fs.write_text_file, binary_path, malformed_text_hex)
  186. fs.write_binary_file(binary_path, malformed_text_hex)
  187. self.assertRaises(ValueError, fs.read_text_file, binary_path)
  188. text_contents = fs.read_binary_file(binary_path).decode('utf8', 'ignore')
  189. self.assertEquals(text_contents, malformed_ignored_text_hex)
  190. finally:
  191. if text_path and fs.isfile(text_path):
  192. os.remove(text_path)
  193. if binary_path and fs.isfile(binary_path):
  194. os.remove(binary_path)
  195. def test_read_binary_file__missing(self):
  196. fs = FileSystem()
  197. self.assertRaises(IOError, fs.read_binary_file, self._missing_file)
  198. def test_read_text_file__missing(self):
  199. fs = FileSystem()
  200. self.assertRaises(IOError, fs.read_text_file, self._missing_file)
  201. def test_remove_file_with_retry(self):
  202. RealFileSystemTest._remove_failures = 2
  203. def remove_with_exception(filename):
  204. RealFileSystemTest._remove_failures -= 1
  205. if RealFileSystemTest._remove_failures >= 0:
  206. try:
  207. raise WindowsError
  208. except NameError:
  209. raise FileSystem._WindowsError
  210. fs = FileSystem()
  211. self.assertTrue(fs.remove('filename', remove_with_exception))
  212. self.assertEqual(-1, RealFileSystemTest._remove_failures)
  213. def test_sep(self):
  214. fs = FileSystem()
  215. self.assertEqual(fs.sep, os.sep)
  216. self.assertEqual(fs.join("foo", "bar"),
  217. os.path.join("foo", "bar"))