1
# Copyright (C) 2006 Canonical Ltd
2
# Authors: Robert Collins <robert.collins@canonical.com>
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
"""Tests for the MemoryTree class."""
20
from bzrlib import errors
21
from bzrlib.memorytree import MemoryTree
22
from bzrlib.tests import TestCaseWithTransport
23
from bzrlib.treebuilder import TreeBuilder
26
class TestMemoryTree(TestCaseWithTransport):
28
def test_create_on_branch(self):
29
"""Creating a mutable tree on a trivial branch works."""
30
branch = self.make_branch('branch')
31
tree = MemoryTree.create_on_branch(branch)
32
self.assertEqual(branch.bzrdir, tree.bzrdir)
33
self.assertEqual(branch, tree.branch)
34
self.assertEqual([], tree.get_parent_ids())
36
def test_create_on_branch_with_content(self):
37
"""Creating a mutable tree on a non-trivial branch works."""
38
branch = self.make_branch('branch')
39
tree = MemoryTree.create_on_branch(branch)
42
builder = TreeBuilder()
43
builder.start_tree(tree)
44
builder.build(['foo'])
46
rev_id = tree.commit('first post')
48
tree = MemoryTree.create_on_branch(branch)
50
self.assertEqual([rev_id], tree.get_parent_ids())
51
self.assertEqual('contents of foo\n',
52
tree.get_file(tree.path2id('foo')).read())
55
def test_lock_tree_write(self):
56
"""Check we can lock_tree_write and unlock MemoryTrees."""
57
branch = self.make_branch('branch')
58
tree = MemoryTree.create_on_branch(branch)
59
tree.lock_tree_write()
62
def test_lock_tree_write_after_read_fails(self):
63
"""Check that we error when trying to upgrade a read lock to write."""
64
branch = self.make_branch('branch')
65
tree = MemoryTree.create_on_branch(branch)
67
self.assertRaises(errors.ReadOnlyError, tree.lock_tree_write)
70
def test_lock_write(self):
71
"""Check we can lock_write and unlock MemoryTrees."""
72
branch = self.make_branch('branch')
73
tree = MemoryTree.create_on_branch(branch)
77
def test_lock_write_after_read_fails(self):
78
"""Check that we error when trying to upgrade a read lock to write."""
79
branch = self.make_branch('branch')
80
tree = MemoryTree.create_on_branch(branch)
82
self.assertRaises(errors.ReadOnlyError, tree.lock_write)
85
def test_add_with_kind(self):
86
branch = self.make_branch('branch')
87
tree = MemoryTree.create_on_branch(branch)
89
tree.add(['', 'afile', 'adir'], None,
90
['directory', 'file', 'directory'])
91
self.assertEqual('afile', tree.id2path(tree.path2id('afile')))
92
self.assertEqual('adir', tree.id2path(tree.path2id('adir')))
93
self.assertFalse(tree.has_filename('afile'))
94
self.assertFalse(tree.has_filename('adir'))
97
def test_put_new_file(self):
98
branch = self.make_branch('branch')
99
tree = MemoryTree.create_on_branch(branch)
101
tree.add(['', 'foo'], ids=['root-id', 'foo-id'],
102
kinds=['directory', 'file'])
103
tree.put_file_bytes_non_atomic('foo-id', 'barshoom')
104
self.assertEqual('barshoom', tree.get_file('foo-id').read())
107
def test_put_existing_file(self):
108
branch = self.make_branch('branch')
109
tree = MemoryTree.create_on_branch(branch)
111
tree.add(['', 'foo'], ids=['root-id', 'foo-id'],
112
kinds=['directory', 'file'])
113
tree.put_file_bytes_non_atomic('foo-id', 'first-content')
114
tree.put_file_bytes_non_atomic('foo-id', 'barshoom')
115
self.assertEqual('barshoom', tree.get_file('foo-id').read())
118
def test_commit_trivial(self):
119
"""Smoke test for commit on a MemoryTree.
121
Becamse of commits design and layering, if this works, all commit
122
logic should work quite reliably.
124
branch = self.make_branch('branch')
125
tree = MemoryTree.create_on_branch(branch)
127
tree.add(['', 'foo'], ids=['root-id', 'foo-id'],
128
kinds=['directory', 'file'])
129
tree.put_file_bytes_non_atomic('foo-id', 'barshoom')
130
revision_id = tree.commit('message baby')
131
# the parents list for the tree should have changed.
132
self.assertEqual([revision_id], tree.get_parent_ids())
134
# and we should have a revision that is accessible outside the tree lock
135
revtree = tree.branch.repository.revision_tree(revision_id)
136
self.assertEqual('barshoom', revtree.get_file('foo-id').read())
138
def test_unversion(self):
139
"""Some test for unversion of a memory tree."""
140
branch = self.make_branch('branch')
141
tree = MemoryTree.create_on_branch(branch)
143
tree.add(['', 'foo'], ids=['root-id', 'foo-id'],
144
kinds=['directory', 'file'])
145
tree.unversion(['foo-id'])
146
self.assertFalse(tree.has_id('foo-id'))
149
def test_last_revision(self):
150
"""There should be a last revision method we can call."""
151
tree = self.make_branch_and_memory_tree('branch')
154
rev_id = tree.commit('first post')
156
self.assertEqual(rev_id, tree.last_revision())