/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.360.7 by Jelmer Vernooij
Add tests for memorytree.
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 GitMemoryTree class."""
19
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
20
from ... import errors
21
from ...treebuilder import TreeBuilder
0.360.7 by Jelmer Vernooij
Add tests for memorytree.
22
from . import TestCaseWithTransport
23
24
25
class TestMemoryTree(TestCaseWithTransport):
26
27
    def make_branch(self, path, format='git'):
28
        return super(TestMemoryTree, self).make_branch(path, format=format)
29
30
    def make_branch_and_tree(self, path, format='git'):
31
        return super(TestMemoryTree, self).make_branch_and_tree(path, format=format)
32
33
    def test_create_on_branch(self):
34
        """Creating a mutable tree on a trivial branch works."""
35
        branch = self.make_branch('branch')
36
        tree = branch.create_memorytree()
37
        self.assertEqual(branch.controldir, tree.controldir)
38
        self.assertEqual(branch, tree.branch)
39
        self.assertEqual([], tree.get_parent_ids())
40
41
    def test_create_on_branch_with_content(self):
42
        """Creating a mutable tree on a non-trivial branch works."""
43
        wt = self.make_branch_and_tree('sometree')
44
        self.build_tree(['sometree/foo'])
45
        wt.add(['foo'])
46
        rev_id = wt.commit('first post')
47
        tree = wt.branch.create_memorytree()
7045.4.1 by Jelmer Vernooij
Some brz-git fixes.
48
        with tree.lock_read():
49
            self.assertEqual([rev_id], tree.get_parent_ids())
50
            self.assertEqual(b'contents of sometree/foo\n',
51
                tree.get_file('foo').read())
0.360.7 by Jelmer Vernooij
Add tests for memorytree.
52
53
    def test_lock_tree_write(self):
54
        """Check we can lock_tree_write and unlock MemoryTrees."""
55
        branch = self.make_branch('branch')
56
        tree = branch.create_memorytree()
57
        tree.lock_tree_write()
58
        tree.unlock()
59
60
    def test_lock_tree_write_after_read_fails(self):
61
        """Check that we error when trying to upgrade a read lock to write."""
62
        branch = self.make_branch('branch')
63
        tree = branch.create_memorytree()
64
        tree.lock_read()
65
        self.assertRaises(errors.ReadOnlyError, tree.lock_tree_write)
66
        tree.unlock()
67
68
    def test_lock_write(self):
69
        """Check we can lock_write and unlock MemoryTrees."""
70
        branch = self.make_branch('branch')
71
        tree = branch.create_memorytree()
72
        tree.lock_write()
73
        tree.unlock()
74
75
    def test_lock_write_after_read_fails(self):
76
        """Check that we error when trying to upgrade a read lock to write."""
77
        branch = self.make_branch('branch')
78
        tree = branch.create_memorytree()
79
        tree.lock_read()
80
        self.assertRaises(errors.ReadOnlyError, tree.lock_write)
81
        tree.unlock()
82
83
    def test_add_with_kind(self):
84
        branch = self.make_branch('branch')
85
        tree = branch.create_memorytree()
86
        tree.lock_write()
87
        tree.add(['', 'afile', 'adir'], None,
88
                 ['directory', 'file', 'directory'])
89
        self.assertTrue(tree.is_versioned('afile'))
90
        self.assertFalse(tree.is_versioned('adir'))
91
        self.assertFalse(tree.has_filename('afile'))
92
        self.assertFalse(tree.has_filename('adir'))
93
        tree.unlock()
94
95
    def test_put_new_file(self):
96
        branch = self.make_branch('branch')
97
        tree = branch.create_memorytree()
6973.6.3 by Jelmer Vernooij
More fixes.
98
        with tree.lock_write():
99
            tree.add(['', 'foo'], kinds=['directory', 'file'])
100
            tree.put_file_bytes_non_atomic('foo', b'barshoom')
101
            self.assertEqual(b'barshoom', tree.get_file('foo').read())
0.360.7 by Jelmer Vernooij
Add tests for memorytree.
102
103
    def test_put_existing_file(self):
104
        branch = self.make_branch('branch')
105
        tree = branch.create_memorytree()
6973.6.3 by Jelmer Vernooij
More fixes.
106
        with tree.lock_write():
107
            tree.add(['', 'foo'], kinds=['directory', 'file'])
108
            tree.put_file_bytes_non_atomic('foo', b'first-content')
109
            tree.put_file_bytes_non_atomic('foo', b'barshoom')
110
            self.assertEqual(b'barshoom', tree.get_file('foo').read())
0.360.7 by Jelmer Vernooij
Add tests for memorytree.
111
112
    def test_add_in_subdir(self):
113
        branch = self.make_branch('branch')
114
        tree = branch.create_memorytree()
6973.6.3 by Jelmer Vernooij
More fixes.
115
        with tree.lock_write():
116
            tree.add([''], None, ['directory'])
117
            tree.mkdir('adir')
118
            tree.put_file_bytes_non_atomic('adir/afile', b'barshoom')
119
            tree.add(['adir/afile'], None, ['file'])
120
            self.assertTrue(tree.is_versioned('adir/afile'))
121
            self.assertTrue(tree.is_versioned('adir'))
0.360.7 by Jelmer Vernooij
Add tests for memorytree.
122
123
    def test_commit_trivial(self):
124
        """Smoke test for commit on a MemoryTree.
125
126
        Becamse of commits design and layering, if this works, all commit
127
        logic should work quite reliably.
128
        """
129
        branch = self.make_branch('branch')
130
        tree = branch.create_memorytree()
6973.6.3 by Jelmer Vernooij
More fixes.
131
        with tree.lock_write():
132
            tree.add(['', 'foo'], kinds=['directory', 'file'])
133
            tree.put_file_bytes_non_atomic('foo', b'barshoom')
134
            revision_id = tree.commit('message baby')
135
            # the parents list for the tree should have changed.
136
            self.assertEqual([revision_id], tree.get_parent_ids())
0.360.7 by Jelmer Vernooij
Add tests for memorytree.
137
        # and we should have a revision that is accessible outside the tree lock
138
        revtree = tree.branch.repository.revision_tree(revision_id)
6973.6.3 by Jelmer Vernooij
More fixes.
139
        with revtree.lock_read():
140
            self.assertEqual(b'barshoom', revtree.get_file('foo').read())
0.360.7 by Jelmer Vernooij
Add tests for memorytree.
141
142
    def test_unversion(self):
143
        """Some test for unversion of a memory tree."""
144
        branch = self.make_branch('branch')
145
        tree = branch.create_memorytree()
146
        tree.lock_write()
147
        tree.add(['', 'foo'], kinds=['directory', 'file'])
148
        tree.unversion(['foo'])
6973.7.5 by Jelmer Vernooij
s/file/open.
149
        self.assertFalse(tree.has_id(b'foo-id'))
0.360.7 by Jelmer Vernooij
Add tests for memorytree.
150
        tree.unlock()
151
152
    def test_last_revision(self):
153
        """There should be a last revision method we can call."""
154
        tree = self.make_branch_and_memory_tree('branch')
155
        tree.lock_write()
156
        tree.add('')
157
        rev_id = tree.commit('first post')
158
        tree.unlock()
159
        self.assertEqual(rev_id, tree.last_revision())
160
161
    def test_rename_file(self):
162
        tree = self.make_branch_and_memory_tree('branch')
163
        tree.lock_write()
164
        self.addCleanup(tree.unlock)
6973.6.3 by Jelmer Vernooij
More fixes.
165
        tree.add(['', 'foo'], [b'root-id', b'foo-id'], ['directory', 'file'])
166
        tree.put_file_bytes_non_atomic('foo', b'content\n')
6973.5.2 by Jelmer Vernooij
Add more bees.
167
        tree.commit('one', rev_id=b'rev-one')
0.360.7 by Jelmer Vernooij
Add tests for memorytree.
168
        tree.rename_one('foo', 'bar')
6973.6.3 by Jelmer Vernooij
More fixes.
169
        self.assertEqual('bar', tree.id2path(b'foo-id'))
6973.13.2 by Jelmer Vernooij
Fix some more tests.
170
        self.assertEqual(b'content\n', tree._file_transport.get_bytes('bar'))
0.360.7 by Jelmer Vernooij
Add tests for memorytree.
171
        self.assertRaises(errors.NoSuchFile,
172
                          tree._file_transport.get_bytes, 'foo')
6973.5.2 by Jelmer Vernooij
Add more bees.
173
        tree.commit('two', rev_id=b'rev-two')
6973.6.3 by Jelmer Vernooij
More fixes.
174
        self.assertEqual(b'content\n', tree._file_transport.get_bytes('bar'))
0.360.7 by Jelmer Vernooij
Add tests for memorytree.
175
        self.assertRaises(errors.NoSuchFile,
176
                          tree._file_transport.get_bytes, 'foo')
177
6973.5.2 by Jelmer Vernooij
Add more bees.
178
        rev_tree2 = tree.branch.repository.revision_tree(b'rev-two')
6973.6.3 by Jelmer Vernooij
More fixes.
179
        self.assertEqual('bar', rev_tree2.id2path(b'foo-id'))
180
        self.assertEqual(b'content\n', rev_tree2.get_file_text('bar'))
0.360.7 by Jelmer Vernooij
Add tests for memorytree.
181
182
    def test_rename_file_to_subdir(self):
183
        tree = self.make_branch_and_memory_tree('branch')
184
        tree.lock_write()
185
        self.addCleanup(tree.unlock)
186
        tree.add('')
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
187
        tree.mkdir('subdir', b'subdir-id')
188
        tree.add('foo', b'foo-id', 'file')
189
        tree.put_file_bytes_non_atomic('foo', b'content\n')
190
        tree.commit('one', rev_id=b'rev-one')
0.360.7 by Jelmer Vernooij
Add tests for memorytree.
191
192
        tree.rename_one('foo', 'subdir/bar')
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
193
        self.assertEqual('subdir/bar', tree.id2path(b'foo-id'))
194
        self.assertEqual(b'content\n',
0.360.7 by Jelmer Vernooij
Add tests for memorytree.
195
                         tree._file_transport.get_bytes('subdir/bar'))
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
196
        tree.commit('two', rev_id=b'rev-two')
197
        rev_tree2 = tree.branch.repository.revision_tree(b'rev-two')
198
        self.assertEqual('subdir/bar', rev_tree2.id2path(b'foo-id'))