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