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
|
# Copyright (C) 2006 Canonical Ltd
# Authors: Robert Collins <robert.collins@canonical.com>
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""Tests for the MemoryTree class."""
from bzrlib import errors
from bzrlib.memorytree import MemoryTree
from bzrlib.tests import TestCaseWithTransport
from bzrlib.treebuilder import TreeBuilder
class TestMemoryTree(TestCaseWithTransport):
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_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_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()
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_add_with_kind(self):
branch = self.make_branch('branch')
tree = MemoryTree.create_on_branch(branch)
tree.lock_write()
tree.add(['afile', 'adir'], None, ['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_put_new_file(self):
branch = self.make_branch('branch')
tree = MemoryTree.create_on_branch(branch)
tree.lock_write()
tree.add(['foo'], ids=['foo-id'], kinds=['file'])
tree.put_file_bytes_non_atomic('foo-id', 'barshoom')
self.assertEqual('barshoom', tree.get_file('foo-id').read())
tree.unlock()
def test_put_existing_file(self):
branch = self.make_branch('branch')
tree = MemoryTree.create_on_branch(branch)
tree.lock_write()
tree.add(['foo'], ids=['foo-id'], kinds=['file'])
tree.put_file_bytes_non_atomic('foo-id', 'first-content')
tree.put_file_bytes_non_atomic('foo-id', 'barshoom')
self.assertEqual('barshoom', tree.get_file('foo-id').read())
tree.unlock()
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=['foo-id'], kinds=['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)
self.assertEqual('barshoom', revtree.get_file('foo-id').read())
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=['foo-id'], kinds=['file'])
tree.unversion(['foo-id'])
self.assertFalse(tree.has_id('foo-id'))
tree.unlock()
|