def test_lock_write_after_read_fails(self): """Check that we error when trying to upgrade a read lock to write.""" branch = self.make_branch('branch') tree = MemoryTree.create_on_branch(branch) tree.lock_read() self.assertRaises(errors.ReadOnlyError, tree.lock_write) tree.unlock()
def test_create_on_branch(self): """Creating a mutable tree on a trivial branch works.""" branch = self.make_branch('branch') tree = MemoryTree.create_on_branch(branch) self.assertEqual(branch.bzrdir, tree.bzrdir) self.assertEqual(branch, tree.branch) self.assertEqual([], tree.get_parent_ids())
def test_get_root_id(self): branch = self.make_branch('branch') tree = MemoryTree.create_on_branch(branch) tree.lock_write() try: tree.add(['']) self.assertIsNot(None, tree.get_root_id()) finally: tree.unlock()
def test_create_on_branch_with_content(self): """Creating a mutable tree on a non-trivial branch works.""" branch = self.make_branch('branch') tree = MemoryTree.create_on_branch(branch) # build some content tree.lock_write() builder = TreeBuilder() builder.start_tree(tree) builder.build(['foo']) builder.finish_tree() rev_id = tree.commit('first post') tree.unlock() tree = MemoryTree.create_on_branch(branch) tree.lock_read() self.assertEqual([rev_id], tree.get_parent_ids()) self.assertEqual('contents of foo\n', tree.get_file(tree.path2id('foo')).read()) tree.unlock()
def test_put_new_file(self): branch = self.make_branch('branch') tree = MemoryTree.create_on_branch(branch) tree.lock_write() tree.add(['', 'foo'], ids=['root-id', 'foo-id'], kinds=['directory', 'file']) tree.put_file_bytes_non_atomic('foo-id', 'barshoom') self.assertEqual('barshoom', tree.get_file('foo-id').read()) tree.unlock()
def test_unversion(self): """Some test for unversion of a memory tree.""" branch = self.make_branch('branch') tree = MemoryTree.create_on_branch(branch) tree.lock_write() tree.add(['', 'foo'], ids=['root-id', 'foo-id'], kinds=['directory', 'file']) tree.unversion(['foo-id']) self.assertFalse(tree.has_id('foo-id')) tree.unlock()
def test_add_with_kind(self): branch = self.make_branch('branch') tree = MemoryTree.create_on_branch(branch) tree.lock_write() tree.add(['', 'afile', 'adir'], None, ['directory', 'file', 'directory']) self.assertEqual('afile', tree.id2path(tree.path2id('afile'))) self.assertEqual('adir', tree.id2path(tree.path2id('adir'))) self.assertFalse(tree.has_filename('afile')) self.assertFalse(tree.has_filename('adir')) tree.unlock()
def test_build_tree(self): """Test building works using a MemoryTree.""" branch = self.make_branch('branch') tree = MemoryTree.create_on_branch(branch) builder = TreeBuilder() builder.start_tree(tree) builder.build(['foo', "bar/", "bar/file"]) self.assertEqual('contents of foo\n', tree.get_file(tree.path2id('foo')).read()) self.assertEqual('contents of bar/file\n', tree.get_file(tree.path2id('bar/file')).read()) builder.finish_tree()
def test_add_in_subdir(self): branch = self.make_branch('branch') tree = MemoryTree.create_on_branch(branch) tree.lock_write() self.addCleanup(tree.unlock) tree.add([''], ['root-id'], ['directory']) # Unfortunately, the only way to 'mkdir' is to call 'tree.mkdir', but # that *always* adds the directory as well. So if you want to create a # file in a subdirectory, you have to split out the 'mkdir()' calls # from the add and put_file_bytes_non_atomic calls. :( tree.mkdir('adir', 'dir-id') tree.add(['adir/afile'], ['file-id'], ['file']) self.assertEqual('adir/afile', tree.id2path('file-id')) self.assertEqual('adir', tree.id2path('dir-id')) tree.put_file_bytes_non_atomic('file-id', 'barshoom')
def test_post_push_nonempty_history(self): target = self.make_branch_and_memory_tree('target') target.lock_write() target.add('') rev1 = target.commit('rev 1') target.unlock() sourcedir = target.bzrdir.clone(self.get_url('source')) source = MemoryTree.create_on_branch(sourcedir.open_branch()) rev2 = source.commit('rev 2') Branch.hooks.install_named_hook('post_push', self.capture_post_push_hook, None) source.branch.push(target.branch) # with nothing there we should still get a notification, and # have both branches locked at the notification time. self.assertEqual([ ('post_push', source.branch, None, target.branch.base, 1, rev1, 2, rev2, True, None, True) ], self.hook_calls)
def test_commit_trivial(self): """Smoke test for commit on a MemoryTree. Becamse of commits design and layering, if this works, all commit logic should work quite reliably. """ branch = self.make_branch('branch') tree = MemoryTree.create_on_branch(branch) tree.lock_write() tree.add(['', 'foo'], ids=['root-id', 'foo-id'], kinds=['directory', 'file']) tree.put_file_bytes_non_atomic('foo-id', 'barshoom') revision_id = tree.commit('message baby') # the parents list for the tree should have changed. self.assertEqual([revision_id], tree.get_parent_ids()) tree.unlock() # and we should have a revision that is accessible outside the tree lock revtree = tree.branch.repository.revision_tree(revision_id) revtree.lock_read() self.addCleanup(revtree.unlock) self.assertEqual('barshoom', revtree.get_file('foo-id').read())
def test_lock_write(self): """Check we can lock_write and unlock MemoryTrees.""" branch = self.make_branch('branch') tree = MemoryTree.create_on_branch(branch) tree.lock_write() tree.unlock()