test_file.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. # test_file.py -- Test for git files
  2. # Copyright (C) 2010 Google, Inc.
  3. #
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. import errno
  21. import io
  22. import os
  23. import shutil
  24. import sys
  25. import tempfile
  26. from dulwich.file import GitFile, _fancy_rename
  27. from dulwich.tests import (
  28. SkipTest,
  29. TestCase,
  30. )
  31. class FancyRenameTests(TestCase):
  32. def setUp(self):
  33. super(FancyRenameTests, self).setUp()
  34. self._tempdir = tempfile.mkdtemp()
  35. self.foo = self.path('foo')
  36. self.bar = self.path('bar')
  37. self.create(self.foo, b'foo contents')
  38. def tearDown(self):
  39. shutil.rmtree(self._tempdir)
  40. super(FancyRenameTests, self).tearDown()
  41. def path(self, filename):
  42. return os.path.join(self._tempdir, filename)
  43. def create(self, path, contents):
  44. f = open(path, 'wb')
  45. f.write(contents)
  46. f.close()
  47. def test_no_dest_exists(self):
  48. self.assertFalse(os.path.exists(self.bar))
  49. _fancy_rename(self.foo, self.bar)
  50. self.assertFalse(os.path.exists(self.foo))
  51. new_f = open(self.bar, 'rb')
  52. self.assertEqual(b'foo contents', new_f.read())
  53. new_f.close()
  54. def test_dest_exists(self):
  55. self.create(self.bar, b'bar contents')
  56. _fancy_rename(self.foo, self.bar)
  57. self.assertFalse(os.path.exists(self.foo))
  58. new_f = open(self.bar, 'rb')
  59. self.assertEqual(b'foo contents', new_f.read())
  60. new_f.close()
  61. def test_dest_opened(self):
  62. if sys.platform != "win32":
  63. raise SkipTest("platform allows overwriting open files")
  64. self.create(self.bar, b'bar contents')
  65. dest_f = open(self.bar, 'rb')
  66. self.assertRaises(OSError, _fancy_rename, self.foo, self.bar)
  67. dest_f.close()
  68. self.assertTrue(os.path.exists(self.path('foo')))
  69. new_f = open(self.foo, 'rb')
  70. self.assertEqual(b'foo contents', new_f.read())
  71. new_f.close()
  72. new_f = open(self.bar, 'rb')
  73. self.assertEqual(b'bar contents', new_f.read())
  74. new_f.close()
  75. class GitFileTests(TestCase):
  76. def setUp(self):
  77. super(GitFileTests, self).setUp()
  78. self._tempdir = tempfile.mkdtemp()
  79. f = open(self.path('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('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('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('foo'))
  104. self.assertEqual(b'foo contents', f.read())
  105. f.close()
  106. def test_write(self):
  107. foo = self.path('foo')
  108. foo_lock = '%s.lock' % foo
  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('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('foo')
  144. foo_lock = '%s.lock' % foo
  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('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('foo')
  172. f = GitFile(foo, 'wb')
  173. f._file.close()
  174. os.remove(foo+".lock")
  175. f.abort()
  176. self.assertTrue(f._closed)