3 # Copyright (C) 2012, Ansgar Burchardt <ansgar@debian.org>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 from base_test import DakTestCase
20 from daklib.fstransactions import FilesystemTransaction
22 from unittest import main
29 class TemporaryDirectory:
34 def filename(self, suffix):
35 return os.path.join(self.directory, suffix)
37 self.directory = tempfile.mkdtemp()
39 def __exit__(self, *args):
40 if self.directory is not None:
41 shutil.rmtree(self.directory)
45 class FilesystemTransactionTestCase(DakTestCase):
46 def _copy_a_b(self, tmp, fs, **kwargs):
47 fs.copy(tmp.filename('a'), tmp.filename('b'), **kwargs)
49 def _write_to_a(self, tmp):
50 with open(tmp.filename('a'), 'w') as fh:
53 def test_copy_non_existing(self):
55 with TemporaryDirectory() as t:
56 with FilesystemTransaction() as fs:
59 self.assertRaises(IOError, copy)
61 def test_copy_existing_and_commit(self):
62 with TemporaryDirectory() as t:
65 with FilesystemTransaction() as fs:
67 self.assert_(os.path.exists(t.filename('a')))
68 self.assert_(os.path.exists(t.filename('b')))
70 self.assert_(os.path.exists(t.filename('a')))
71 self.assert_(os.path.exists(t.filename('b')))
73 def test_copy_existing_and_rollback(self):
74 with TemporaryDirectory() as t:
77 class TestException(Exception):
80 with FilesystemTransaction() as fs:
82 self.assert_(os.path.exists(t.filename('a')))
83 self.assert_(os.path.exists(t.filename('b')))
88 self.assert_(os.path.exists(t.filename('a')))
89 self.assert_(not os.path.exists(t.filename('b')))
91 def test_unlink_and_commit(self):
92 with TemporaryDirectory() as t:
95 with FilesystemTransaction() as fs:
96 self.assert_(os.path.exists(a))
98 self.assert_(not os.path.exists(a))
99 self.assert_(not os.path.exists(a))
101 def test_unlink_and_rollback(self):
102 with TemporaryDirectory() as t:
105 class TestException(Exception):
109 with FilesystemTransaction() as fs:
110 self.assert_(os.path.exists(a))
112 self.assert_(not os.path.exists(a))
113 raise TestException()
114 except TestException:
116 self.assert_(os.path.exists(a))
118 if __name__ == '__main__':