test_file.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. # test_file.py -- Test for git files
  2. # Copyright (C) 2010 Google, Inc.
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # of the License or (at your option) a later version of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. import errno
  19. import io
  20. import os
  21. import shutil
  22. import sys
  23. import tempfile
  24. from dulwich.file import GitFile, fancy_rename
  25. from dulwich.tests import (
  26. SkipTest,
  27. TestCase,
  28. )
  29. class FancyRenameTests(TestCase):
  30. def setUp(self):
  31. super(FancyRenameTests, self).setUp()
  32. self._tempdir = tempfile.mkdtemp()
  33. self.foo = self.path('foo')
  34. self.bar = self.path('bar')
  35. self.create(self.foo, b'foo contents')
  36. def tearDown(self):
  37. shutil.rmtree(self._tempdir)
  38. super(FancyRenameTests, self).tearDown()
  39. def path(self, filename):
  40. return os.path.join(self._tempdir, filename)
  41. def create(self, path, contents):
  42. f = open(path, 'wb')
  43. f.write(contents)
  44. f.close()
  45. def test_no_dest_exists(self):
  46. self.assertFalse(os.path.exists(self.bar))
  47. fancy_rename(self.foo, self.bar)
  48. self.assertFalse(os.path.exists(self.foo))
  49. new_f = open(self.bar, 'rb')
  50. self.assertEqual(b'foo contents', new_f.read())
  51. new_f.close()
  52. def test_dest_exists(self):
  53. self.create(self.bar, b'bar contents')
  54. fancy_rename(self.foo, self.bar)
  55. self.assertFalse(os.path.exists(self.foo))
  56. new_f = open(self.bar, 'rb')
  57. self.assertEqual(b'foo contents', new_f.read())
  58. new_f.close()
  59. def test_dest_opened(self):
  60. if sys.platform != "win32":
  61. raise SkipTest("platform allows overwriting open files")
  62. self.create(self.bar, b'bar contents')
  63. dest_f = open(self.bar, 'rb')
  64. self.assertRaises(OSError, fancy_rename, self.foo, self.bar)
  65. dest_f.close()
  66. self.assertTrue(os.path.exists(self.path('foo')))
  67. new_f = open(self.foo, 'rb')
  68. self.assertEqual(b'foo contents', new_f.read())
  69. new_f.close()
  70. new_f = open(self.bar, 'rb')
  71. self.assertEqual(b'bar contents', new_f.read())
  72. new_f.close()
  73. class GitFileTests(TestCase):
  74. def setUp(self):
  75. super(GitFileTests, self).setUp()
  76. self._tempdir = tempfile.mkdtemp()
  77. if not isinstance(self._tempdir, bytes):
  78. self._tempdir = self._tempdir.encode(sys.getfilesystemencoding())
  79. f = open(self.path(b'foo'), 'wb')
  80. f.write(b'foo contents')
  81. f.close()
  82. def tearDown(self):
  83. shutil.rmtree(self._tempdir)
  84. super(GitFileTests, self).tearDown()
  85. def path(self, filename):
  86. return os.path.join(self._tempdir, filename)
  87. def test_invalid(self):
  88. foo = self.path(b'foo')
  89. self.assertRaises(IOError, GitFile, foo, mode='r')
  90. self.assertRaises(IOError, GitFile, foo, mode='ab')
  91. self.assertRaises(IOError, GitFile, foo, mode='r+b')
  92. self.assertRaises(IOError, GitFile, foo, mode='w+b')
  93. self.assertRaises(IOError, GitFile, foo, mode='a+bU')
  94. def test_readonly(self):
  95. f = GitFile(self.path(b'foo'), 'rb')
  96. self.assertTrue(isinstance(f, io.IOBase))
  97. self.assertEqual(b'foo contents', f.read())
  98. self.assertEqual(b'', f.read())
  99. f.seek(4)
  100. self.assertEqual(b'contents', f.read())
  101. f.close()
  102. def test_default_mode(self):
  103. f = GitFile(self.path(b'foo'))
  104. self.assertEqual(b'foo contents', f.read())
  105. f.close()
  106. def test_write(self):
  107. foo = self.path(b'foo')
  108. foo_lock = foo + b'.lock'
  109. orig_f = open(foo, 'rb')
  110. self.assertEqual(orig_f.read(), b'foo contents')
  111. orig_f.close()
  112. self.assertFalse(os.path.exists(foo_lock))
  113. f = GitFile(foo, 'wb')
  114. self.assertFalse(f.closed)
  115. self.assertRaises(AttributeError, getattr, f, 'not_a_file_property')
  116. self.assertTrue(os.path.exists(foo_lock))
  117. f.write(b'new stuff')
  118. f.seek(4)
  119. f.write(b'contents')
  120. f.close()
  121. self.assertFalse(os.path.exists(foo_lock))
  122. new_f = open(foo, 'rb')
  123. self.assertEqual(b'new contents', new_f.read())
  124. new_f.close()
  125. def test_open_twice(self):
  126. foo = self.path(b'foo')
  127. f1 = GitFile(foo, 'wb')
  128. f1.write(b'new')
  129. try:
  130. f2 = GitFile(foo, 'wb')
  131. self.fail()
  132. except OSError as e:
  133. self.assertEqual(errno.EEXIST, e.errno)
  134. else:
  135. f2.close()
  136. f1.write(b' contents')
  137. f1.close()
  138. # Ensure trying to open twice doesn't affect original.
  139. f = open(foo, 'rb')
  140. self.assertEqual(b'new contents', f.read())
  141. f.close()
  142. def test_abort(self):
  143. foo = self.path(b'foo')
  144. foo_lock = foo + b'.lock'
  145. orig_f = open(foo, 'rb')
  146. self.assertEqual(orig_f.read(), b'foo contents')
  147. orig_f.close()
  148. f = GitFile(foo, 'wb')
  149. f.write(b'new contents')
  150. f.abort()
  151. self.assertTrue(f.closed)
  152. self.assertFalse(os.path.exists(foo_lock))
  153. new_orig_f = open(foo, 'rb')
  154. self.assertEqual(new_orig_f.read(), b'foo contents')
  155. new_orig_f.close()
  156. def test_abort_close(self):
  157. foo = self.path(b'foo')
  158. f = GitFile(foo, 'wb')
  159. f.abort()
  160. try:
  161. f.close()
  162. except (IOError, OSError):
  163. self.fail()
  164. f = GitFile(foo, 'wb')
  165. f.close()
  166. try:
  167. f.abort()
  168. except (IOError, OSError):
  169. self.fail()
  170. def test_abort_close_removed(self):
  171. foo = self.path(b'foo')
  172. f = GitFile(foo, 'wb')
  173. f._file.close()
  174. os.remove(foo + b'.lock')
  175. f.abort()
  176. self.assertTrue(f._closed)