1 # test_file.py -- Test for git files
2 # Copyright (C) 2010 Google, Inc.
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.
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.
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,
26 from dulwich.file import GitFile
28 class GitFileTests(unittest.TestCase):
30 self._tempdir = tempfile.mkdtemp()
31 f = open(self.path('foo'), 'wb')
32 f.write('foo contents')
36 shutil.rmtree(self._tempdir)
38 def path(self, filename):
39 return os.path.join(self._tempdir, filename)
41 def test_invalid(self):
42 foo = self.path('foo')
43 self.assertRaises(IOError, GitFile, foo, mode='r')
44 self.assertRaises(IOError, GitFile, foo, mode='ab')
45 self.assertRaises(IOError, GitFile, foo, mode='r+b')
46 self.assertRaises(IOError, GitFile, foo, mode='w+b')
47 self.assertRaises(IOError, GitFile, foo, mode='a+bU')
49 def test_readonly(self):
50 f = GitFile(self.path('foo'), 'rb')
51 self.assertTrue(isinstance(f, file))
52 self.assertEquals('foo contents', f.read())
53 self.assertEquals('', f.read())
55 self.assertEquals('contents', f.read())
59 foo = self.path('foo')
60 foo_lock = '%s.lock' % foo
62 orig_f = open(foo, 'rb')
63 self.assertEquals(orig_f.read(), 'foo contents')
66 self.assertFalse(os.path.exists(foo_lock))
67 f = GitFile(foo, 'wb')
68 self.assertFalse(f.closed)
69 self.assertRaises(AttributeError, getattr, f, 'not_a_file_property')
71 self.assertTrue(os.path.exists(foo_lock))
76 self.assertFalse(os.path.exists(foo_lock))
78 new_f = open(foo, 'rb')
79 self.assertEquals('new contents', new_f.read())
82 def test_open_twice(self):
83 foo = self.path('foo')
84 f1 = GitFile(foo, 'wb')
87 f2 = GitFile(foo, 'wb')
90 self.assertEquals(errno.EEXIST, e.errno)
94 # Ensure trying to open twice doesn't affect original.
96 self.assertEquals('new contents', f.read())
100 foo = self.path('foo')
101 foo_lock = '%s.lock' % foo
103 orig_f = open(foo, 'rb')
104 self.assertEquals(orig_f.read(), 'foo contents')
107 f = GitFile(foo, 'wb')
108 f.write('new contents')
110 self.assertTrue(f.closed)
111 self.assertFalse(os.path.exists(foo_lock))
113 new_orig_f = open(foo, 'rb')
114 self.assertEquals(new_orig_f.read(), 'foo contents')
117 def test_abort_close(self):
118 foo = self.path('foo')
119 f = GitFile(foo, 'wb')
123 except (IOError, OSError):
126 f = GitFile(foo, 'wb')
130 except (IOError, OSError):