/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_memorytree.py

[merge] robertc's integration, updated tests to check for retcode=3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
2
 
# Authors:  Robert Collins <robert.collins@canonical.com>
3
 
#
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.
8
 
#
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.
13
 
#
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
 
 
18
 
"""Tests for the MemoryTree class."""
19
 
 
20
 
from .. import errors
21
 
from ..memorytree import MemoryTree
22
 
from . import TestCaseWithTransport
23
 
from ..treebuilder import TreeBuilder
24
 
 
25
 
 
26
 
class TestMemoryTree(TestCaseWithTransport):
27
 
 
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.controldir, tree.controldir)
33
 
        self.assertEqual(branch, tree.branch)
34
 
        self.assertEqual([], tree.get_parent_ids())
35
 
 
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)
40
 
        # build some content
41
 
        tree.lock_write()
42
 
        builder = TreeBuilder()
43
 
        builder.start_tree(tree)
44
 
        builder.build(['foo'])
45
 
        builder.finish_tree()
46
 
        rev_id = tree.commit('first post')
47
 
        tree.unlock()
48
 
        tree = MemoryTree.create_on_branch(branch)
49
 
        with tree.lock_read():
50
 
            self.assertEqual([rev_id], tree.get_parent_ids())
51
 
            with tree.get_file('foo') as f:
52
 
                self.assertEqual(b'contents of foo\n', f.read())
53
 
 
54
 
    def test_get_root_id(self):
55
 
        branch = self.make_branch('branch')
56
 
        tree = MemoryTree.create_on_branch(branch)
57
 
        with tree.lock_write():
58
 
            tree.add([''])
59
 
            self.assertIsNot(None, tree.path2id(''))
60
 
 
61
 
    def test_lock_tree_write(self):
62
 
        """Check we can lock_tree_write and unlock MemoryTrees."""
63
 
        branch = self.make_branch('branch')
64
 
        tree = MemoryTree.create_on_branch(branch)
65
 
        tree.lock_tree_write()
66
 
        tree.unlock()
67
 
 
68
 
    def test_lock_tree_write_after_read_fails(self):
69
 
        """Check that we error when trying to upgrade a read lock to write."""
70
 
        branch = self.make_branch('branch')
71
 
        tree = MemoryTree.create_on_branch(branch)
72
 
        with tree.lock_read():
73
 
            self.assertRaises(errors.ReadOnlyError, tree.lock_tree_write)
74
 
 
75
 
    def test_lock_write(self):
76
 
        """Check we can lock_write and unlock MemoryTrees."""
77
 
        branch = self.make_branch('branch')
78
 
        tree = MemoryTree.create_on_branch(branch)
79
 
        tree.lock_write()
80
 
        tree.unlock()
81
 
 
82
 
    def test_lock_write_after_read_fails(self):
83
 
        """Check that we error when trying to upgrade a read lock to write."""
84
 
        branch = self.make_branch('branch')
85
 
        tree = MemoryTree.create_on_branch(branch)
86
 
        with tree.lock_read():
87
 
            self.assertRaises(errors.ReadOnlyError, tree.lock_write)
88
 
 
89
 
    def test_add_with_kind(self):
90
 
        branch = self.make_branch('branch')
91
 
        tree = MemoryTree.create_on_branch(branch)
92
 
        with tree.lock_write():
93
 
            tree.add(['', 'afile', 'adir'], None,
94
 
                     ['directory', 'file', 'directory'])
95
 
            self.assertEqual('afile', tree.id2path(tree.path2id('afile')))
96
 
            self.assertEqual('adir', tree.id2path(tree.path2id('adir')))
97
 
            self.assertFalse(tree.has_filename('afile'))
98
 
            self.assertFalse(tree.has_filename('adir'))
99
 
 
100
 
    def test_put_new_file(self):
101
 
        branch = self.make_branch('branch')
102
 
        tree = MemoryTree.create_on_branch(branch)
103
 
        with tree.lock_write():
104
 
            tree.add(['', 'foo'], ids=[b'root-id', b'foo-id'],
105
 
                     kinds=['directory', 'file'])
106
 
            tree.put_file_bytes_non_atomic('foo', b'barshoom')
107
 
            with tree.get_file('foo') as f:
108
 
                self.assertEqual(b'barshoom', f.read())
109
 
 
110
 
    def test_put_existing_file(self):
111
 
        branch = self.make_branch('branch')
112
 
        tree = MemoryTree.create_on_branch(branch)
113
 
        with tree.lock_write():
114
 
            tree.add(['', 'foo'], ids=[b'root-id', b'foo-id'],
115
 
                     kinds=['directory', 'file'])
116
 
            tree.put_file_bytes_non_atomic('foo', b'first-content')
117
 
            tree.put_file_bytes_non_atomic('foo', b'barshoom')
118
 
            self.assertEqual(b'barshoom', tree.get_file('foo').read())
119
 
 
120
 
    def test_add_in_subdir(self):
121
 
        branch = self.make_branch('branch')
122
 
        tree = MemoryTree.create_on_branch(branch)
123
 
        with tree.lock_write():
124
 
            tree.add([''], [b'root-id'], ['directory'])
125
 
            # Unfortunately, the only way to 'mkdir' is to call 'tree.mkdir', but
126
 
            # that *always* adds the directory as well. So if you want to create a
127
 
            # file in a subdirectory, you have to split out the 'mkdir()' calls
128
 
            # from the add and put_file_bytes_non_atomic calls. :(
129
 
            tree.mkdir('adir', b'dir-id')
130
 
            tree.add(['adir/afile'], [b'file-id'], ['file'])
131
 
            self.assertEqual('adir/afile', tree.id2path(b'file-id'))
132
 
            self.assertEqual('adir', tree.id2path(b'dir-id'))
133
 
            tree.put_file_bytes_non_atomic('adir/afile', b'barshoom')
134
 
 
135
 
    def test_add_symlink(self):
136
 
        branch = self.make_branch('branch')
137
 
        tree = MemoryTree.create_on_branch(branch)
138
 
        with tree.lock_write():
139
 
            tree._file_transport.symlink('bar', 'foo')
140
 
            tree.add(['', 'foo'])
141
 
            self.assertEqual('symlink', tree.kind('foo'))
142
 
            self.assertEqual('bar', tree.get_symlink_target('foo'))
143
 
 
144
 
    def test_commit_trivial(self):
145
 
        """Smoke test for commit on a MemoryTree.
146
 
 
147
 
        Becamse of commits design and layering, if this works, all commit
148
 
        logic should work quite reliably.
149
 
        """
150
 
        branch = self.make_branch('branch')
151
 
        tree = MemoryTree.create_on_branch(branch)
152
 
        with tree.lock_write():
153
 
            tree.add(['', 'foo'], ids=[b'root-id', b'foo-id'],
154
 
                     kinds=['directory', 'file'])
155
 
            tree.put_file_bytes_non_atomic('foo', b'barshoom')
156
 
            revision_id = tree.commit('message baby')
157
 
            # the parents list for the tree should have changed.
158
 
            self.assertEqual([revision_id], tree.get_parent_ids())
159
 
        # and we should have a revision that is accessible outside the tree lock
160
 
        revtree = tree.branch.repository.revision_tree(revision_id)
161
 
        with revtree.lock_read(), revtree.get_file('foo') as f:
162
 
            self.assertEqual(b'barshoom', f.read())
163
 
 
164
 
    def test_unversion(self):
165
 
        """Some test for unversion of a memory tree."""
166
 
        branch = self.make_branch('branch')
167
 
        tree = MemoryTree.create_on_branch(branch)
168
 
        with tree.lock_write():
169
 
            tree.add(['', 'foo'], ids=[b'root-id', b'foo-id'],
170
 
                     kinds=['directory', 'file'])
171
 
            tree.unversion(['foo'])
172
 
            self.assertFalse(tree.is_versioned('foo'))
173
 
            self.assertRaises(errors.NoSuchId, tree.id2path, b'foo-id')
174
 
 
175
 
    def test_last_revision(self):
176
 
        """There should be a last revision method we can call."""
177
 
        tree = self.make_branch_and_memory_tree('branch')
178
 
        with tree.lock_write():
179
 
            tree.add('')
180
 
            rev_id = tree.commit('first post')
181
 
        self.assertEqual(rev_id, tree.last_revision())
182
 
 
183
 
    def test_rename_file(self):
184
 
        tree = self.make_branch_and_memory_tree('branch')
185
 
        tree.lock_write()
186
 
        self.addCleanup(tree.unlock)
187
 
        tree.add(['', 'foo'], [b'root-id', b'foo-id'], ['directory', 'file'])
188
 
        tree.put_file_bytes_non_atomic('foo', b'content\n')
189
 
        tree.commit('one', rev_id=b'rev-one')
190
 
        tree.rename_one('foo', 'bar')
191
 
        self.assertEqual('bar', tree.id2path(b'foo-id'))
192
 
        self.assertEqual(b'content\n', tree._file_transport.get_bytes('bar'))
193
 
        self.assertRaises(errors.NoSuchFile,
194
 
                          tree._file_transport.get_bytes, 'foo')
195
 
        tree.commit('two', rev_id=b'rev-two')
196
 
        self.assertEqual(b'content\n', tree._file_transport.get_bytes('bar'))
197
 
        self.assertRaises(errors.NoSuchFile,
198
 
                          tree._file_transport.get_bytes, 'foo')
199
 
 
200
 
        rev_tree2 = tree.branch.repository.revision_tree(b'rev-two')
201
 
        self.assertEqual('bar', rev_tree2.id2path(b'foo-id'))
202
 
        self.assertEqual(b'content\n', rev_tree2.get_file_text('bar'))
203
 
 
204
 
    def test_rename_file_to_subdir(self):
205
 
        tree = self.make_branch_and_memory_tree('branch')
206
 
        tree.lock_write()
207
 
        self.addCleanup(tree.unlock)
208
 
        tree.add('')
209
 
        tree.mkdir('subdir', b'subdir-id')
210
 
        tree.add('foo', b'foo-id', 'file')
211
 
        tree.put_file_bytes_non_atomic('foo', b'content\n')
212
 
        tree.commit('one', rev_id=b'rev-one')
213
 
 
214
 
        tree.rename_one('foo', 'subdir/bar')
215
 
        self.assertEqual('subdir/bar', tree.id2path(b'foo-id'))
216
 
        self.assertEqual(b'content\n',
217
 
                         tree._file_transport.get_bytes('subdir/bar'))
218
 
        tree.commit('two', rev_id=b'rev-two')
219
 
        rev_tree2 = tree.branch.repository.revision_tree(b'rev-two')
220
 
        self.assertEqual('subdir/bar', rev_tree2.id2path(b'foo-id'))