test_file.py 5.7 KB

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