Improve ref handling.
[jelmer/dulwich-libgit2.git] / dulwich / tests / test_file.py
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
19
20 import errno
21 import os
22 import shutil
23 import tempfile
24 import unittest
25
26 from dulwich.file import GitFile
27
28 class GitFileTests(unittest.TestCase):
29     def setUp(self):
30         self._tempdir = tempfile.mkdtemp()
31         f = open(self.path('foo'), 'wb')
32         f.write('foo contents')
33         f.close()
34
35     def tearDown(self):
36         shutil.rmtree(self._tempdir)
37
38     def path(self, filename):
39         return os.path.join(self._tempdir, filename)
40
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')
48
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())
54         f.seek(4)
55         self.assertEquals('contents', f.read())
56         f.close()
57
58     def test_write(self):
59         foo = self.path('foo')
60         foo_lock = '%s.lock' % foo
61
62         orig_f = open(foo, 'rb')
63         self.assertEquals(orig_f.read(), 'foo contents')
64         orig_f.close()
65
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')
70
71         self.assertTrue(os.path.exists(foo_lock))
72         f.write('new stuff')
73         f.seek(4)
74         f.write('contents')
75         f.close()
76         self.assertFalse(os.path.exists(foo_lock))
77
78         new_f = open(foo, 'rb')
79         self.assertEquals('new contents', new_f.read())
80         new_f.close()
81
82     def test_open_twice(self):
83         foo = self.path('foo')
84         f1 = GitFile(foo, 'wb')
85         f1.write('new')
86         try:
87             f2 = GitFile(foo, 'wb')
88             fail()
89         except OSError, e:
90             self.assertEquals(errno.EEXIST, e.errno)
91         f1.write(' contents')
92         f1.close()
93
94         # Ensure trying to open twice doesn't affect original.
95         f = open(foo, 'rb')
96         self.assertEquals('new contents', f.read())
97         f.close()
98
99     def test_abort(self):
100         foo = self.path('foo')
101         foo_lock = '%s.lock' % foo
102
103         orig_f = open(foo, 'rb')
104         self.assertEquals(orig_f.read(), 'foo contents')
105         orig_f.close()
106
107         f = GitFile(foo, 'wb')
108         f.write('new contents')
109         f.abort()
110         self.assertTrue(f.closed)
111         self.assertFalse(os.path.exists(foo_lock))
112
113         new_orig_f = open(foo, 'rb')
114         self.assertEquals(new_orig_f.read(), 'foo contents')
115         new_orig_f.close()
116
117     def test_abort_close(self):
118         foo = self.path('foo')
119         f = GitFile(foo, 'wb')
120         f.abort()
121         try:
122             f.close()
123         except (IOError, OSError):
124             self.fail()
125
126         f = GitFile(foo, 'wb')
127         f.close()
128         try:
129             f.abort()
130         except (IOError, OSError):
131             self.fail()