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, fancy_rename
27 from dulwich.tests import TestSkipped
30 class FancyRenameTests(unittest.TestCase):
33 self._tempdir = tempfile.mkdtemp()
34 self.foo = self.path('foo')
35 self.bar = self.path('bar')
36 self.create(self.foo, 'foo contents')
39 shutil.rmtree(self._tempdir)
41 def path(self, filename):
42 return os.path.join(self._tempdir, filename)
44 def create(self, path, contents):
49 def test_no_dest_exists(self):
50 self.assertFalse(os.path.exists(self.bar))
51 fancy_rename(self.foo, self.bar)
52 self.assertFalse(os.path.exists(self.foo))
54 new_f = open(self.bar, 'rb')
55 self.assertEquals('foo contents', new_f.read())
58 def test_dest_exists(self):
59 self.create(self.bar, 'bar contents')
60 fancy_rename(self.foo, self.bar)
61 self.assertFalse(os.path.exists(self.foo))
63 new_f = open(self.bar, 'rb')
64 self.assertEquals('foo contents', new_f.read())
67 def test_dest_opened(self):
68 if sys.platform != "win32":
69 raise TestSkipped("platform allows overwriting open files")
70 self.create(self.bar, 'bar contents')
71 dest_f = open(self.bar, 'rb')
72 self.assertRaises(OSError, fancy_rename, self.foo, self.bar)
74 self.assertTrue(os.path.exists(self.path('foo')))
76 new_f = open(self.foo, 'rb')
77 self.assertEquals('foo contents', new_f.read())
80 new_f = open(self.bar, 'rb')
81 self.assertEquals('bar contents', new_f.read())
85 class GitFileTests(unittest.TestCase):
88 self._tempdir = tempfile.mkdtemp()
89 f = open(self.path('foo'), 'wb')
90 f.write('foo contents')
94 shutil.rmtree(self._tempdir)
96 def path(self, filename):
97 return os.path.join(self._tempdir, filename)
99 def test_invalid(self):
100 foo = self.path('foo')
101 self.assertRaises(IOError, GitFile, foo, mode='r')
102 self.assertRaises(IOError, GitFile, foo, mode='ab')
103 self.assertRaises(IOError, GitFile, foo, mode='r+b')
104 self.assertRaises(IOError, GitFile, foo, mode='w+b')
105 self.assertRaises(IOError, GitFile, foo, mode='a+bU')
107 def test_readonly(self):
108 f = GitFile(self.path('foo'), 'rb')
109 self.assertTrue(isinstance(f, file))
110 self.assertEquals('foo contents', f.read())
111 self.assertEquals('', f.read())
113 self.assertEquals('contents', f.read())
116 def test_write(self):
117 foo = self.path('foo')
118 foo_lock = '%s.lock' % foo
120 orig_f = open(foo, 'rb')
121 self.assertEquals(orig_f.read(), 'foo contents')
124 self.assertFalse(os.path.exists(foo_lock))
125 f = GitFile(foo, 'wb')
126 self.assertFalse(f.closed)
127 self.assertRaises(AttributeError, getattr, f, 'not_a_file_property')
129 self.assertTrue(os.path.exists(foo_lock))
134 self.assertFalse(os.path.exists(foo_lock))
136 new_f = open(foo, 'rb')
137 self.assertEquals('new contents', new_f.read())
140 def test_open_twice(self):
141 foo = self.path('foo')
142 f1 = GitFile(foo, 'wb')
145 f2 = GitFile(foo, 'wb')
148 self.assertEquals(errno.EEXIST, e.errno)
149 f1.write(' contents')
152 # Ensure trying to open twice doesn't affect original.
154 self.assertEquals('new contents', f.read())
157 def test_abort(self):
158 foo = self.path('foo')
159 foo_lock = '%s.lock' % foo
161 orig_f = open(foo, 'rb')
162 self.assertEquals(orig_f.read(), 'foo contents')
165 f = GitFile(foo, 'wb')
166 f.write('new contents')
168 self.assertTrue(f.closed)
169 self.assertFalse(os.path.exists(foo_lock))
171 new_orig_f = open(foo, 'rb')
172 self.assertEquals(new_orig_f.read(), 'foo contents')
175 def test_abort_close(self):
176 foo = self.path('foo')
177 f = GitFile(foo, 'wb')
181 except (IOError, OSError):
184 f = GitFile(foo, 'wb')
188 except (IOError, OSError):
191 def test_abort_close_removed(self):
192 foo = self.path('foo')
193 f = GitFile(foo, 'wb')
194 os.remove(foo+".lock")
196 self.assertTrue(f._closed)