/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()
7397.4.4 by Jelmer Vernooij
Allow has_id to be unsupported.
145
        with tree.lock_write():
146
            tree.add(['', 'foo'], kinds=['directory', 'file'])
147
            tree.unversion(['foo'])
148
            self.assertFalse(tree.is_versioned('foo'))
149
            self.assertFalse(tree.has_filename('foo'))
0.360.7 by Jelmer Vernooij
Add tests for memorytree.
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')
7397.4.4 by Jelmer Vernooij
Allow has_id to be unsupported.
154
        with tree.lock_write():
155
            tree.add('')
156
            rev_id = tree.commit('first post')
0.360.7 by Jelmer Vernooij
Add tests for memorytree.
157
        self.assertEqual(rev_id, tree.last_revision())
158
159
    def test_rename_file(self):
160
        tree = self.make_branch_and_memory_tree('branch')
161
        tree.lock_write()
162
        self.addCleanup(tree.unlock)
6973.6.3 by Jelmer Vernooij
More fixes.
163
        tree.add(['', 'foo'], [b'root-id', b'foo-id'], ['directory', 'file'])
164
        tree.put_file_bytes_non_atomic('foo', b'content\n')
6973.5.2 by Jelmer Vernooij
Add more bees.
165
        tree.commit('one', rev_id=b'rev-one')
0.360.7 by Jelmer Vernooij
Add tests for memorytree.
166
        tree.rename_one('foo', 'bar')
6973.6.3 by Jelmer Vernooij
More fixes.
167
        self.assertEqual('bar', tree.id2path(b'foo-id'))
6973.13.2 by Jelmer Vernooij
Fix some more tests.
168
        self.assertEqual(b'content\n', tree._file_transport.get_bytes('bar'))
0.360.7 by Jelmer Vernooij
Add tests for memorytree.
169
        self.assertRaises(errors.NoSuchFile,
170
                          tree._file_transport.get_bytes, 'foo')
6973.5.2 by Jelmer Vernooij
Add more bees.
171
        tree.commit('two', rev_id=b'rev-two')
6973.6.3 by Jelmer Vernooij
More fixes.
172
        self.assertEqual(b'content\n', tree._file_transport.get_bytes('bar'))
0.360.7 by Jelmer Vernooij
Add tests for memorytree.
173
        self.assertRaises(errors.NoSuchFile,
174
                          tree._file_transport.get_bytes, 'foo')
175
6973.5.2 by Jelmer Vernooij
Add more bees.
176
        rev_tree2 = tree.branch.repository.revision_tree(b'rev-two')
6973.6.3 by Jelmer Vernooij
More fixes.
177
        self.assertEqual('bar', rev_tree2.id2path(b'foo-id'))
178
        self.assertEqual(b'content\n', rev_tree2.get_file_text('bar'))
0.360.7 by Jelmer Vernooij
Add tests for memorytree.
179
180
    def test_rename_file_to_subdir(self):
181
        tree = self.make_branch_and_memory_tree('branch')
182
        tree.lock_write()
183
        self.addCleanup(tree.unlock)
184
        tree.add('')
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
185
        tree.mkdir('subdir', b'subdir-id')
186
        tree.add('foo', b'foo-id', 'file')
187
        tree.put_file_bytes_non_atomic('foo', b'content\n')
188
        tree.commit('one', rev_id=b'rev-one')
0.360.7 by Jelmer Vernooij
Add tests for memorytree.
189
190
        tree.rename_one('foo', 'subdir/bar')
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
191
        self.assertEqual('subdir/bar', tree.id2path(b'foo-id'))
192
        self.assertEqual(b'content\n',
0.360.7 by Jelmer Vernooij
Add tests for memorytree.
193
                         tree._file_transport.get_bytes('subdir/bar'))
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
194
        tree.commit('two', rev_id=b'rev-two')
195
        rev_tree2 = tree.branch.repository.revision_tree(b'rev-two')
196
        self.assertEqual('subdir/bar', rev_tree2.id2path(b'foo-id'))