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,
25 from dulwich.file import GitFile, fancy_rename
26 from dulwich.tests import (
32 class FancyRenameTests(TestCase):
35 super(FancyRenameTests, self).setUp()
36 self._tempdir = tempfile.mkdtemp()
37 self.foo = self.path('foo')
38 self.bar = self.path('bar')
39 self.create(self.foo, 'foo contents')
42 shutil.rmtree(self._tempdir)
43 super(FancyRenameTests, self).tearDown()
45 def path(self, filename):
46 return os.path.join(self._tempdir, filename)
48 def create(self, path, contents):
53 def test_no_dest_exists(self):
54 self.assertFalse(os.path.exists(self.bar))
55 fancy_rename(self.foo, self.bar)
56 self.assertFalse(os.path.exists(self.foo))
58 new_f = open(self.bar, 'rb')
59 self.assertEquals('foo contents', new_f.read())
62 def test_dest_exists(self):
63 self.create(self.bar, 'bar contents')
64 fancy_rename(self.foo, self.bar)
65 self.assertFalse(os.path.exists(self.foo))
67 new_f = open(self.bar, 'rb')
68 self.assertEquals('foo contents', new_f.read())
71 def test_dest_opened(self):
72 if sys.platform != "win32":
73 raise TestSkipped("platform allows overwriting open files")
74 self.create(self.bar, 'bar contents')
75 dest_f = open(self.bar, 'rb')
76 self.assertRaises(OSError, fancy_rename, self.foo, self.bar)
78 self.assertTrue(os.path.exists(self.path('foo')))
80 new_f = open(self.foo, 'rb')
81 self.assertEquals('foo contents', new_f.read())
84 new_f = open(self.bar, 'rb')
85 self.assertEquals('bar contents', new_f.read())
89 class GitFileTests(TestCase):
92 super(GitFileTests, self).setUp()
93 self._tempdir = tempfile.mkdtemp()
94 f = open(self.path('foo'), 'wb')
95 f.write('foo contents')
99 shutil.rmtree(self._tempdir)
100 super(GitFileTests, self).tearDown()
102 def path(self, filename):
103 return os.path.join(self._tempdir, filename)
105 def test_invalid(self):
106 foo = self.path('foo')
107 self.assertRaises(IOError, GitFile, foo, mode='r')
108 self.assertRaises(IOError, GitFile, foo, mode='ab')
109 self.assertRaises(IOError, GitFile, foo, mode='r+b')
110 self.assertRaises(IOError, GitFile, foo, mode='w+b')
111 self.assertRaises(IOError, GitFile, foo, mode='a+bU')
113 def test_readonly(self):
114 f = GitFile(self.path('foo'), 'rb')
115 self.assertTrue(isinstance(f, file))
116 self.assertEquals('foo contents', f.read())
117 self.assertEquals('', f.read())
119 self.assertEquals('contents', f.read())
122 def test_default_mode(self):
123 f = GitFile(self.path('foo'))
124 self.assertEquals('foo contents', f.read())
127 def test_write(self):
128 foo = self.path('foo')
129 foo_lock = '%s.lock' % foo
131 orig_f = open(foo, 'rb')
132 self.assertEquals(orig_f.read(), 'foo contents')
135 self.assertFalse(os.path.exists(foo_lock))
136 f = GitFile(foo, 'wb')
137 self.assertFalse(f.closed)
138 self.assertRaises(AttributeError, getattr, f, 'not_a_file_property')
140 self.assertTrue(os.path.exists(foo_lock))
145 self.assertFalse(os.path.exists(foo_lock))
147 new_f = open(foo, 'rb')
148 self.assertEquals('new contents', new_f.read())
151 def test_open_twice(self):
152 foo = self.path('foo')
153 f1 = GitFile(foo, 'wb')
156 f2 = GitFile(foo, 'wb')
159 self.assertEquals(errno.EEXIST, e.errno)
160 f1.write(' contents')
163 # Ensure trying to open twice doesn't affect original.
165 self.assertEquals('new contents', f.read())
168 def test_abort(self):
169 foo = self.path('foo')
170 foo_lock = '%s.lock' % foo
172 orig_f = open(foo, 'rb')
173 self.assertEquals(orig_f.read(), 'foo contents')
176 f = GitFile(foo, 'wb')
177 f.write('new contents')
179 self.assertTrue(f.closed)
180 self.assertFalse(os.path.exists(foo_lock))
182 new_orig_f = open(foo, 'rb')
183 self.assertEquals(new_orig_f.read(), 'foo contents')
186 def test_abort_close(self):
187 foo = self.path('foo')
188 f = GitFile(foo, 'wb')
192 except (IOError, OSError):
195 f = GitFile(foo, 'wb')
199 except (IOError, OSError):
202 def test_abort_close_removed(self):
203 foo = self.path('foo')
204 f = GitFile(foo, 'wb')
207 os.remove(foo+".lock")
210 self.assertTrue(f._closed)