test_file.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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
  24. class GitFileTests(unittest.TestCase):
  25. def setUp(self):
  26. self._tempdir = tempfile.mkdtemp()
  27. f = open(self.path('foo'), 'wb')
  28. f.write('foo contents')
  29. f.close()
  30. def tearDown(self):
  31. shutil.rmtree(self._tempdir)
  32. def path(self, filename):
  33. return os.path.join(self._tempdir, filename)
  34. def test_invalid(self):
  35. foo = self.path('foo')
  36. self.assertRaises(IOError, GitFile, foo, mode='r')
  37. self.assertRaises(IOError, GitFile, foo, mode='ab')
  38. self.assertRaises(IOError, GitFile, foo, mode='r+b')
  39. self.assertRaises(IOError, GitFile, foo, mode='w+b')
  40. self.assertRaises(IOError, GitFile, foo, mode='a+bU')
  41. def test_readonly(self):
  42. f = GitFile(self.path('foo'), 'rb')
  43. self.assertTrue(isinstance(f, file))
  44. self.assertEquals('foo contents', f.read())
  45. self.assertEquals('', f.read())
  46. f.seek(4)
  47. self.assertEquals('contents', f.read())
  48. f.close()
  49. def test_write(self):
  50. foo = self.path('foo')
  51. foo_lock = '%s.lock' % foo
  52. orig_f = open(foo, 'rb')
  53. self.assertEquals(orig_f.read(), 'foo contents')
  54. orig_f.close()
  55. self.assertFalse(os.path.exists(foo_lock))
  56. f = GitFile(foo, 'wb')
  57. self.assertFalse(f.closed)
  58. self.assertRaises(AttributeError, getattr, f, 'not_a_file_property')
  59. self.assertTrue(os.path.exists(foo_lock))
  60. f.write('new stuff')
  61. f.seek(4)
  62. f.write('contents')
  63. f.close()
  64. self.assertFalse(os.path.exists(foo_lock))
  65. new_f = open(foo, 'rb')
  66. self.assertEquals('new contents', new_f.read())
  67. new_f.close()
  68. def test_open_twice(self):
  69. foo = self.path('foo')
  70. f1 = GitFile(foo, 'wb')
  71. f1.write('new')
  72. try:
  73. f2 = GitFile(foo, 'wb')
  74. fail()
  75. except OSError, e:
  76. self.assertEquals(errno.EEXIST, e.errno)
  77. f1.write(' contents')
  78. f1.close()
  79. # Ensure trying to open twice doesn't affect original.
  80. f = open(foo, 'rb')
  81. self.assertEquals('new contents', f.read())
  82. f.close()
  83. def test_abort(self):
  84. foo = self.path('foo')
  85. foo_lock = '%s.lock' % foo
  86. orig_f = open(foo, 'rb')
  87. self.assertEquals(orig_f.read(), 'foo contents')
  88. orig_f.close()
  89. f = GitFile(foo, 'wb')
  90. f.write('new contents')
  91. f.abort()
  92. self.assertTrue(f.closed)
  93. self.assertFalse(os.path.exists(foo_lock))
  94. new_orig_f = open(foo, 'rb')
  95. self.assertEquals(new_orig_f.read(), 'foo contents')
  96. new_orig_f.close()
  97. def test_abort_close(self):
  98. foo = self.path('foo')
  99. f = GitFile(foo, 'wb')
  100. f.abort()
  101. try:
  102. f.close()
  103. except (IOError, OSError):
  104. self.fail()
  105. f = GitFile(foo, 'wb')
  106. f.close()
  107. try:
  108. f.abort()
  109. except (IOError, OSError):
  110. self.fail()