/usr/share/pyshared/larch/journal_tests.py is in python-larch 1.20131130-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | # Copyright 2012 Lars Wirzenius
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import shutil
import tempfile
import unittest
import larch
class JournalTests(unittest.TestCase):
def setUp(self):
self.tempdir = tempfile.mkdtemp()
self.fs = larch.LocalFS()
self.j = larch.Journal(True, self.fs, self.tempdir)
def tearDown(self):
shutil.rmtree(self.tempdir)
def join(self, *args):
return os.path.join(self.tempdir, *args)
def test_constructs_new_filename(self):
self.assertEqual(self.j._new(self.join('foo')),
self.join('new', 'foo'))
def test_constructs_deleted_filename(self):
self.assertEqual(self.j._deleted(self.join('foo')),
self.join('delete', 'foo'))
def test_does_not_know_random_directory_initially(self):
self.assertFalse(self.j.exists(self.join('foo')))
def test_creates_directory(self):
dirname = self.join('foo/bar')
self.j.makedirs(dirname)
self.assertTrue(self.j.exists(dirname))
def test_rollback_undoes_directory_creation(self):
dirname = self.join('foo/bar')
self.j.makedirs(dirname)
self.j.rollback()
self.assertFalse(self.j.exists(dirname))
def test_rollback_keeps_committed_directory(self):
dirname = self.join('foo/bar')
self.j.makedirs(dirname)
self.j.commit()
self.j.rollback()
self.assertTrue(self.j.exists(dirname))
def test_rollback_works_without_changes(self):
self.assertEqual(self.j.rollback(), None)
def test_creates_new_file(self):
filename = self.join('foo/bar')
self.j.overwrite_file(filename, 'bar')
self.assertEqual(self.j.cat(filename), 'bar')
def test_rollback_undoes_new_file(self):
filename = self.join('foo/bar')
self.j.overwrite_file(filename, 'bar')
self.j.rollback()
self.assertFalse(self.j.exists(filename))
def test_commits_new_file(self):
filename = self.join('foo/bar')
self.j.overwrite_file(filename, 'bar')
self.j.commit()
self.j.rollback()
self.assertEqual(self.j.cat(filename), 'bar')
def test_creates_new_file_after_commit(self):
filename = self.join('foo/bar')
self.j.overwrite_file(filename, 'bar')
self.j.commit()
self.j.overwrite_file(filename, 'yo')
self.assertEqual(self.j.cat(filename), 'yo')
def test_cat_does_not_find_deleted_file(self):
filename = self.join('foo/bar')
self.j.overwrite_file(filename, 'bar')
self.j.commit()
self.j.remove(filename)
self.assertRaises(OSError, self.j.cat, filename)
def test_rollback_brings_back_old_file(self):
filename = self.join('foo/bar')
self.j.overwrite_file(filename, 'bar')
self.j.commit()
self.j.overwrite_file(filename, 'yo')
self.j.rollback()
self.assertEqual(self.j.cat(filename), 'bar')
def test_removes_uncommitted_file(self):
filename = self.join('foo/bar')
self.j.overwrite_file(filename, 'bar')
self.j.remove(filename)
self.assertFalse(self.j.exists(filename))
def test_rollback_undoes_removal_of_uncommitted_file(self):
filename = self.join('foo/bar')
self.j.overwrite_file(filename, 'bar')
self.j.remove(filename)
self.j.rollback()
self.assertFalse(self.j.exists(filename))
def test_commits_file_removal(self):
filename = self.join('foo/bar')
self.j.overwrite_file(filename, 'bar')
self.j.remove(filename)
self.j.commit()
self.j.rollback()
self.assertFalse(self.j.exists(filename))
def test_removes_committed_file(self):
filename = self.join('foo/bar')
self.j.overwrite_file(filename, 'bar')
self.j.commit()
self.j.remove(filename)
self.assertFalse(self.j.exists(filename))
def test_removing_committed_file_twice_causes_oserror(self):
filename = self.join('foo/bar')
self.j.overwrite_file(filename, 'bar')
self.j.commit()
self.j.remove(filename)
self.assertRaises(OSError, self.j.remove, filename)
def test_rollback_brings_back_committed_file(self):
filename = self.join('foo/bar')
self.j.overwrite_file(filename, 'bar')
self.j.commit()
self.j.remove(filename)
self.j.rollback()
self.assertEqual(self.j.cat(filename), 'bar')
def test_commits_removal_of_committed_file(self):
filename = self.join('foo/bar')
self.j.overwrite_file(filename, 'bar')
self.j.commit()
self.j.remove(filename)
self.j.commit()
self.j.rollback()
self.assertFalse(self.j.exists(filename))
def test_commits_metadata(self):
metadata = self.join('metadata')
self.j.overwrite_file(metadata, 'yuck')
self.j.commit()
self.assertEqual(self.fs.cat(self.join(metadata)), 'yuck')
def test_unflagged_commit_means_new_instance_rollbacks(self):
filename = self.join('foo/bar')
self.j.overwrite_file(filename, 'bar')
j2 = larch.Journal(True, self.fs, self.tempdir)
self.assertFalse(j2.exists(filename))
def test_partial_commit_finished_by_new_instance(self):
filename = self.join('foo/bar')
metadata = self.join('metadata')
self.j.overwrite_file(filename, 'bar')
self.j.overwrite_file(metadata, '')
self.j.commit(skip=[filename])
j2 = larch.Journal(True, self.fs, self.tempdir)
self.assertTrue(j2.exists(filename))
class ReadOnlyJournalTests(unittest.TestCase):
def setUp(self):
self.tempdir = tempfile.mkdtemp()
self.fs = larch.LocalFS()
self.rw = larch.Journal(True, self.fs, self.tempdir)
self.ro = larch.Journal(False, self.fs, self.tempdir)
def tearDown(self):
shutil.rmtree(self.tempdir)
def join(self, *args):
return os.path.join(self.tempdir, *args)
def test_does_not_know_random_directory_initially(self):
self.assertFalse(self.ro.exists(self.join('foo')))
def test_creating_directory_raises_error(self):
self.assertRaises(larch.ReadOnlyMode, self.ro.makedirs, 'foo')
def test_calling_rollback_raises_error(self):
self.assertRaises(larch.ReadOnlyMode, self.ro.rollback)
def test_readonly_mode_does_not_check_for_directory_creation(self):
dirname = self.join('foo/bar')
self.rw.makedirs(dirname)
self.assertFalse(self.ro.exists(dirname))
def test_write_file_raisees_error(self):
self.assertRaises(larch.ReadOnlyMode,
self.ro.overwrite_file, 'foo', 'bar')
def test_readonly_mode_does_not_check_for_new_file(self):
filename = self.join('foo')
self.rw.overwrite_file(filename, 'bar')
self.assertFalse(self.ro.exists(filename))
def test_readonly_mode_does_not_check_for_modified_file(self):
filename = self.join('foo')
self.rw.overwrite_file(filename, 'first')
self.rw.commit()
self.assertEqual(self.ro.cat(filename), 'first')
self.rw.overwrite_file(filename, 'second')
self.assertEqual(self.ro.cat(filename), 'first')
def test_readonly_mode_does_not_know_file_is_deleted_in_journal(self):
filename = self.join('foo/bar')
self.rw.overwrite_file(filename, 'bar')
self.rw.commit()
self.rw.remove(filename)
self.assertEqual(self.ro.cat(filename), 'bar')
def tests_lists_no_files_initially(self):
dirname = self.join('foo')
self.assertEqual(list(self.ro.list_files(dirname)), [])
def test_lists_files_correctly_when_no_changes(self):
dirname = self.join('foo')
filename = self.join('foo/bar')
self.rw.overwrite_file(filename, 'bar')
self.rw.commit()
self.assertEqual(list(self.ro.list_files(dirname)), [filename])
def test_lists_added_file_correctly(self):
dirname = self.join('foo')
filename = self.join('foo/bar')
self.rw.overwrite_file(filename, 'bar')
self.assertEqual(list(self.rw.list_files(dirname)), [filename])
self.assertEqual(list(self.ro.list_files(dirname)), [])
def test_lists_added_file_correctly_when_dir_existed_already(self):
dirname = self.join('foo')
filename = self.join('foo/bar')
filename2 = self.join('foo/foobar')
self.rw.overwrite_file(filename, 'bar')
self.rw.commit()
self.rw.overwrite_file(filename2, 'yoyo')
self.assertEqual(sorted(list(self.rw.list_files(dirname))),
sorted([filename, filename2]))
self.assertEqual(list(self.ro.list_files(dirname)), [filename])
def test_lists_removed_file_correctly(self):
dirname = self.join('foo')
filename = self.join('foo/bar')
self.rw.overwrite_file(filename, 'bar')
self.rw.commit()
self.rw.remove(filename)
self.assertEqual(list(self.rw.list_files(dirname)), [])
self.assertEqual(list(self.ro.list_files(dirname)), [filename])
|