test_file.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 os
  20. import shutil
  21. import tempfile
  22. import unittest
  23. from dulwich.file import GitFile, fancy_rename
  24. class FancyRenameTests(unittest.TestCase):
  25. def setUp(self):
  26. self._tempdir = tempfile.mkdtemp()
  27. self.foo = self.path('foo')
  28. self.bar = self.path('bar')
  29. self.create(self.foo, 'foo contents')
  30. def tearDown(self):
  31. shutil.rmtree(self._tempdir)
  32. def path(self, filename):
  33. return os.path.join(self._tempdir, filename)
  34. def create(self, path, contents):
  35. f = open(path, 'wb')
  36. f.write(contents)
  37. f.close()
  38. def test_no_dest_exists(self):
  39. self.assertFalse(os.path.exists(self.bar))
  40. fancy_rename(self.foo, self.bar)
  41. self.assertFalse(os.path.exists(self.foo))
  42. new_f = open(self.bar, 'rb')
  43. self.assertEquals('foo contents', new_f.read())
  44. new_f.close()
  45. def test_dest_exists(self):
  46. self.create(self.bar, 'bar contents')
  47. fancy_rename(self.foo, self.bar)
  48. self.assertFalse(os.path.exists(self.foo))
  49. new_f = open(self.bar, 'rb')
  50. self.assertEquals('foo contents', new_f.read())
  51. new_f.close()
  52. def test_dest_opened(self):
  53. self.create(self.bar, 'bar contents')
  54. dest_f = open(self.bar, 'rb')
  55. self.assertRaises(OSError, fancy_rename, self.foo, self.bar)
  56. dest_f.close()
  57. self.assertTrue(os.path.exists(self.path('foo')))
  58. new_f = open(self.foo, 'rb')
  59. self.assertEquals('foo contents', new_f.read())
  60. new_f.close()
  61. new_f = open(self.bar, 'rb')
  62. self.assertEquals('bar contents', new_f.read())
  63. new_f.close()
  64. class GitFileTests(unittest.TestCase):
  65. def setUp(self):
  66. self._tempdir = tempfile.mkdtemp()
  67. f = open(self.path('foo'), 'wb')
  68. f.write('foo contents')
  69. f.close()
  70. def tearDown(self):
  71. shutil.rmtree(self._tempdir)
  72. def path(self, filename):
  73. return os.path.join(self._tempdir, filename)
  74. def test_invalid(self):
  75. foo = self.path('foo')
  76. self.assertRaises(IOError, GitFile, foo, mode='r')
  77. self.assertRaises(IOError, GitFile, foo, mode='ab')
  78. self.assertRaises(IOError, GitFile, foo, mode='r+b')
  79. self.assertRaises(IOError, GitFile, foo, mode='w+b')
  80. self.assertRaises(IOError, GitFile, foo, mode='a+bU')
  81. def test_readonly(self):
  82. f = GitFile(self.path('foo'), 'rb')
  83. self.assertTrue(isinstance(f, file))
  84. self.assertEquals('foo contents', f.read())
  85. self.assertEquals('', f.read())
  86. f.seek(4)
  87. self.assertEquals('contents', f.read())
  88. f.close()
  89. def test_write(self):
  90. foo = self.path('foo')
  91. foo_lock = '%s.lock' % foo
  92. orig_f = open(foo, 'rb')
  93. self.assertEquals(orig_f.read(), 'foo contents')
  94. orig_f.close()
  95. self.assertFalse(os.path.exists(foo_lock))
  96. f = GitFile(foo, 'wb')
  97. self.assertFalse(f.closed)
  98. self.assertRaises(AttributeError, getattr, f, 'not_a_file_property')
  99. self.assertTrue(os.path.exists(foo_lock))
  100. f.write('new stuff')
  101. f.seek(4)
  102. f.write('contents')
  103. f.close()
  104. self.assertFalse(os.path.exists(foo_lock))
  105. new_f = open(foo, 'rb')
  106. self.assertEquals('new contents', new_f.read())
  107. new_f.close()
  108. def test_open_twice(self):
  109. foo = self.path('foo')
  110. f1 = GitFile(foo, 'wb')
  111. f1.write('new')
  112. try:
  113. f2 = GitFile(foo, 'wb')
  114. fail()
  115. except OSError, e:
  116. self.assertEquals(errno.EEXIST, e.errno)
  117. f1.write(' contents')
  118. f1.close()
  119. # Ensure trying to open twice doesn't affect original.
  120. f = open(foo, 'rb')
  121. self.assertEquals('new contents', f.read())
  122. f.close()
  123. def test_abort(self):
  124. foo = self.path('foo')
  125. foo_lock = '%s.lock' % foo
  126. orig_f = open(foo, 'rb')
  127. self.assertEquals(orig_f.read(), 'foo contents')
  128. orig_f.close()
  129. f = GitFile(foo, 'wb')
  130. f.write('new contents')
  131. f.abort()
  132. self.assertTrue(f.closed)
  133. self.assertFalse(os.path.exists(foo_lock))
  134. new_orig_f = open(foo, 'rb')
  135. self.assertEquals(new_orig_f.read(), 'foo contents')
  136. new_orig_f.close()
  137. def test_abort_close(self):
  138. foo = self.path('foo')
  139. f = GitFile(foo, 'wb')
  140. f.abort()
  141. try:
  142. f.close()
  143. except (IOError, OSError):
  144. self.fail()
  145. f = GitFile(foo, 'wb')
  146. f.close()
  147. try:
  148. f.abort()
  149. except (IOError, OSError):
  150. self.fail()