/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
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
4183.7.1 by Sabin Iacob
update FSF mailing address
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
17
18
"""Tests for the MemoryTree class."""
19
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
20
from .. import errors
21
from ..memorytree import MemoryTree
22
from . import TestCaseWithTransport
23
from ..treebuilder import TreeBuilder
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
24
25
26
class TestMemoryTree(TestCaseWithTransport):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
27
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
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)
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
32
        self.assertEqual(branch.controldir, tree.controldir)
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
33
        self.assertEqual(branch, tree.branch)
34
        self.assertEqual([], tree.get_parent_ids())
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
35
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
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
        tree.lock_read()
50
        self.assertEqual([rev_id], tree.get_parent_ids())
7045.4.21 by Jelmer Vernooij
Fix some msgeditor tests.
51
        with tree.get_file('foo') as f:
52
            self.assertEqual(b'contents of foo\n', f.read())
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
53
        tree.unlock()
54
2946.3.5 by John Arbash Meinel
MemoryTree is not tested under the tree_implementations tests.
55
    def test_get_root_id(self):
56
        branch = self.make_branch('branch')
57
        tree = MemoryTree.create_on_branch(branch)
58
        tree.lock_write()
59
        try:
60
            tree.add([''])
61
            self.assertIsNot(None, tree.get_root_id())
62
        finally:
63
            tree.unlock()
64
1986.1.8 by Robert Collins
Update to bzr.dev, which involves adding lock_tree_write to MutableTree and MemoryTree.
65
    def test_lock_tree_write(self):
66
        """Check we can lock_tree_write and unlock MemoryTrees."""
67
        branch = self.make_branch('branch')
68
        tree = MemoryTree.create_on_branch(branch)
69
        tree.lock_tree_write()
70
        tree.unlock()
71
72
    def test_lock_tree_write_after_read_fails(self):
73
        """Check that we error when trying to upgrade a read lock to write."""
74
        branch = self.make_branch('branch')
75
        tree = MemoryTree.create_on_branch(branch)
76
        tree.lock_read()
77
        self.assertRaises(errors.ReadOnlyError, tree.lock_tree_write)
78
        tree.unlock()
79
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
80
    def test_lock_write(self):
81
        """Check we can lock_write and unlock MemoryTrees."""
82
        branch = self.make_branch('branch')
83
        tree = MemoryTree.create_on_branch(branch)
84
        tree.lock_write()
85
        tree.unlock()
86
87
    def test_lock_write_after_read_fails(self):
88
        """Check that we error when trying to upgrade a read lock to write."""
89
        branch = self.make_branch('branch')
90
        tree = MemoryTree.create_on_branch(branch)
91
        tree.lock_read()
92
        self.assertRaises(errors.ReadOnlyError, tree.lock_write)
93
        tree.unlock()
94
95
    def test_add_with_kind(self):
96
        branch = self.make_branch('branch')
97
        tree = MemoryTree.create_on_branch(branch)
98
        tree.lock_write()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
99
        tree.add(['', 'afile', 'adir'], None,
1731.1.50 by Aaron Bentley
Merge bzr.dev
100
                 ['directory', 'file', 'directory'])
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
101
        self.assertEqual('afile', tree.id2path(tree.path2id('afile')))
102
        self.assertEqual('adir', tree.id2path(tree.path2id('adir')))
103
        self.assertFalse(tree.has_filename('afile'))
3567.4.7 by John Arbash Meinel
Revert back to using MemoryTree.mkdir() rather than creating the directory during add().
104
        self.assertFalse(tree.has_filename('adir'))
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
105
        tree.unlock()
106
107
    def test_put_new_file(self):
108
        branch = self.make_branch('branch')
109
        tree = MemoryTree.create_on_branch(branch)
110
        tree.lock_write()
6855.4.1 by Jelmer Vernooij
Yet more bees.
111
        tree.add(['', 'foo'], ids=[b'root-id', b'foo-id'],
7143.15.2 by Jelmer Vernooij
Run autopep8.
112
                 kinds=['directory', 'file'])
6973.6.3 by Jelmer Vernooij
More fixes.
113
        tree.put_file_bytes_non_atomic('foo', b'barshoom')
114
        self.assertEqual(b'barshoom', tree.get_file('foo').read())
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
115
        tree.unlock()
116
117
    def test_put_existing_file(self):
118
        branch = self.make_branch('branch')
119
        tree = MemoryTree.create_on_branch(branch)
120
        tree.lock_write()
6855.4.1 by Jelmer Vernooij
Yet more bees.
121
        tree.add(['', 'foo'], ids=[b'root-id', b'foo-id'],
1731.1.50 by Aaron Bentley
Merge bzr.dev
122
                 kinds=['directory', 'file'])
6973.6.3 by Jelmer Vernooij
More fixes.
123
        tree.put_file_bytes_non_atomic('foo', b'first-content')
124
        tree.put_file_bytes_non_atomic('foo', b'barshoom')
125
        self.assertEqual(b'barshoom', tree.get_file('foo').read())
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
126
        tree.unlock()
127
3567.4.5 by John Arbash Meinel
MemoryTree.add(directory) will now create a directory node in the Transport
128
    def test_add_in_subdir(self):
129
        branch = self.make_branch('branch')
130
        tree = MemoryTree.create_on_branch(branch)
131
        tree.lock_write()
132
        self.addCleanup(tree.unlock)
6855.4.1 by Jelmer Vernooij
Yet more bees.
133
        tree.add([''], [b'root-id'], ['directory'])
3567.4.7 by John Arbash Meinel
Revert back to using MemoryTree.mkdir() rather than creating the directory during add().
134
        # Unfortunately, the only way to 'mkdir' is to call 'tree.mkdir', but
135
        # that *always* adds the directory as well. So if you want to create a
136
        # file in a subdirectory, you have to split out the 'mkdir()' calls
137
        # from the add and put_file_bytes_non_atomic calls. :(
6973.6.3 by Jelmer Vernooij
More fixes.
138
        tree.mkdir('adir', b'dir-id')
6855.4.1 by Jelmer Vernooij
Yet more bees.
139
        tree.add(['adir/afile'], [b'file-id'], ['file'])
140
        self.assertEqual('adir/afile', tree.id2path(b'file-id'))
141
        self.assertEqual('adir', tree.id2path(b'dir-id'))
6973.6.3 by Jelmer Vernooij
More fixes.
142
        tree.put_file_bytes_non_atomic('adir/afile', b'barshoom')
3567.4.5 by John Arbash Meinel
MemoryTree.add(directory) will now create a directory node in the Transport
143
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
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
        tree.lock_write()
6855.4.1 by Jelmer Vernooij
Yet more bees.
153
        tree.add(['', 'foo'], ids=[b'root-id', b'foo-id'],
1731.1.50 by Aaron Bentley
Merge bzr.dev
154
                 kinds=['directory', 'file'])
6973.6.3 by Jelmer Vernooij
More fixes.
155
        tree.put_file_bytes_non_atomic('foo', b'barshoom')
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
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
        tree.unlock()
160
        # and we should have a revision that is accessible outside the tree lock
161
        revtree = tree.branch.repository.revision_tree(revision_id)
3010.1.7 by Robert Collins
Locking in test_memorytree.
162
        revtree.lock_read()
163
        self.addCleanup(revtree.unlock)
6973.6.3 by Jelmer Vernooij
More fixes.
164
        with revtree.get_file('foo') as f:
165
            self.assertEqual(b'barshoom', f.read())
1986.1.3 by Robert Collins
Merge bzr.dev.
166
167
    def test_unversion(self):
168
        """Some test for unversion of a memory tree."""
169
        branch = self.make_branch('branch')
170
        tree = MemoryTree.create_on_branch(branch)
171
        tree.lock_write()
6855.4.1 by Jelmer Vernooij
Yet more bees.
172
        tree.add(['', 'foo'], ids=[b'root-id', b'foo-id'],
1731.1.50 by Aaron Bentley
Merge bzr.dev
173
                 kinds=['directory', 'file'])
6809.4.25 by Jelmer Vernooij
Add paths argument to .unversion.
174
        tree.unversion(['foo'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
175
        self.assertFalse(tree.is_versioned('foo'))
176
        self.assertFalse(tree.has_id(b'foo-id'))
1986.1.3 by Robert Collins
Merge bzr.dev.
177
        tree.unlock()
1986.4.1 by Robert Collins
Merge MemoryTree branch.
178
179
    def test_last_revision(self):
180
        """There should be a last revision method we can call."""
181
        tree = self.make_branch_and_memory_tree('branch')
1731.1.64 by Aaron Bentley
Fix up more memorytree tests
182
        tree.lock_write()
183
        tree.add('')
1986.4.1 by Robert Collins
Merge MemoryTree branch.
184
        rev_id = tree.commit('first post')
1731.1.64 by Aaron Bentley
Fix up more memorytree tests
185
        tree.unlock()
1986.4.1 by Robert Collins
Merge MemoryTree branch.
186
        self.assertEqual(rev_id, tree.last_revision())
3567.5.1 by John Arbash Meinel
Implement rename_one on MemoryTree, and expose that in the Branch Builder
187
188
    def test_rename_file(self):
189
        tree = self.make_branch_and_memory_tree('branch')
190
        tree.lock_write()
191
        self.addCleanup(tree.unlock)
6855.4.1 by Jelmer Vernooij
Yet more bees.
192
        tree.add(['', 'foo'], [b'root-id', b'foo-id'], ['directory', 'file'])
6973.6.3 by Jelmer Vernooij
More fixes.
193
        tree.put_file_bytes_non_atomic('foo', b'content\n')
6855.4.1 by Jelmer Vernooij
Yet more bees.
194
        tree.commit('one', rev_id=b'rev-one')
3567.5.1 by John Arbash Meinel
Implement rename_one on MemoryTree, and expose that in the Branch Builder
195
        tree.rename_one('foo', 'bar')
6855.4.1 by Jelmer Vernooij
Yet more bees.
196
        self.assertEqual('bar', tree.id2path(b'foo-id'))
7045.1.1 by Jelmer Vernooij
Fix another 300 tests.
197
        self.assertEqual(b'content\n', tree._file_transport.get_bytes('bar'))
3567.5.1 by John Arbash Meinel
Implement rename_one on MemoryTree, and expose that in the Branch Builder
198
        self.assertRaises(errors.NoSuchFile,
199
                          tree._file_transport.get_bytes, 'foo')
6855.4.1 by Jelmer Vernooij
Yet more bees.
200
        tree.commit('two', rev_id=b'rev-two')
6973.6.3 by Jelmer Vernooij
More fixes.
201
        self.assertEqual(b'content\n', tree._file_transport.get_bytes('bar'))
3567.5.1 by John Arbash Meinel
Implement rename_one on MemoryTree, and expose that in the Branch Builder
202
        self.assertRaises(errors.NoSuchFile,
203
                          tree._file_transport.get_bytes, 'foo')
204
6855.4.1 by Jelmer Vernooij
Yet more bees.
205
        rev_tree2 = tree.branch.repository.revision_tree(b'rev-two')
206
        self.assertEqual('bar', rev_tree2.id2path(b'foo-id'))
6973.6.3 by Jelmer Vernooij
More fixes.
207
        self.assertEqual(b'content\n', rev_tree2.get_file_text('bar'))
3567.5.1 by John Arbash Meinel
Implement rename_one on MemoryTree, and expose that in the Branch Builder
208
209
    def test_rename_file_to_subdir(self):
210
        tree = self.make_branch_and_memory_tree('branch')
211
        tree.lock_write()
212
        self.addCleanup(tree.unlock)
213
        tree.add('')
6855.4.1 by Jelmer Vernooij
Yet more bees.
214
        tree.mkdir('subdir', b'subdir-id')
215
        tree.add('foo', b'foo-id', 'file')
6973.6.3 by Jelmer Vernooij
More fixes.
216
        tree.put_file_bytes_non_atomic('foo', b'content\n')
6855.4.1 by Jelmer Vernooij
Yet more bees.
217
        tree.commit('one', rev_id=b'rev-one')
3567.5.1 by John Arbash Meinel
Implement rename_one on MemoryTree, and expose that in the Branch Builder
218
219
        tree.rename_one('foo', 'subdir/bar')
6855.4.1 by Jelmer Vernooij
Yet more bees.
220
        self.assertEqual('subdir/bar', tree.id2path(b'foo-id'))
7045.1.1 by Jelmer Vernooij
Fix another 300 tests.
221
        self.assertEqual(b'content\n',
3567.5.1 by John Arbash Meinel
Implement rename_one on MemoryTree, and expose that in the Branch Builder
222
                         tree._file_transport.get_bytes('subdir/bar'))
6855.4.1 by Jelmer Vernooij
Yet more bees.
223
        tree.commit('two', rev_id=b'rev-two')
224
        rev_tree2 = tree.branch.repository.revision_tree(b'rev-two')
225
        self.assertEqual('subdir/bar', rev_tree2.id2path(b'foo-id'))