/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
20
from .... import errors
21
from ....treebuilder import TreeBuilder
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()
48
        tree.lock_read()
49
        self.assertEqual([rev_id], tree.get_parent_ids())
50
        self.assertEqual('contents of sometree/foo\n',
51
            tree.get_file('foo').read())
52
        tree.unlock()
53
54
    def test_lock_tree_write(self):
55
        """Check we can lock_tree_write and unlock MemoryTrees."""
56
        branch = self.make_branch('branch')
57
        tree = branch.create_memorytree()
58
        tree.lock_tree_write()
59
        tree.unlock()
60
61
    def test_lock_tree_write_after_read_fails(self):
62
        """Check that we error when trying to upgrade a read lock to write."""
63
        branch = self.make_branch('branch')
64
        tree = branch.create_memorytree()
65
        tree.lock_read()
66
        self.assertRaises(errors.ReadOnlyError, tree.lock_tree_write)
67
        tree.unlock()
68
69
    def test_lock_write(self):
70
        """Check we can lock_write and unlock MemoryTrees."""
71
        branch = self.make_branch('branch')
72
        tree = branch.create_memorytree()
73
        tree.lock_write()
74
        tree.unlock()
75
76
    def test_lock_write_after_read_fails(self):
77
        """Check that we error when trying to upgrade a read lock to write."""
78
        branch = self.make_branch('branch')
79
        tree = branch.create_memorytree()
80
        tree.lock_read()
81
        self.assertRaises(errors.ReadOnlyError, tree.lock_write)
82
        tree.unlock()
83
84
    def test_add_with_kind(self):
85
        branch = self.make_branch('branch')
86
        tree = branch.create_memorytree()
87
        tree.lock_write()
88
        tree.add(['', 'afile', 'adir'], None,
89
                 ['directory', 'file', 'directory'])
90
        self.assertTrue(tree.is_versioned('afile'))
91
        self.assertFalse(tree.is_versioned('adir'))
92
        self.assertFalse(tree.has_filename('afile'))
93
        self.assertFalse(tree.has_filename('adir'))
94
        tree.unlock()
95
96
    def test_put_new_file(self):
97
        branch = self.make_branch('branch')
98
        tree = branch.create_memorytree()
99
        tree.lock_write()
100
        tree.add(['', 'foo'], kinds=['directory', 'file'])
101
        tree.put_file_bytes_non_atomic('foo', 'barshoom')
102
        self.assertEqual('barshoom', tree.get_file('foo').read())
103
        tree.unlock()
104
105
    def test_put_existing_file(self):
106
        branch = self.make_branch('branch')
107
        tree = branch.create_memorytree()
108
        tree.lock_write()
109
        tree.add(['', 'foo'], kinds=['directory', 'file'])
110
        tree.put_file_bytes_non_atomic('foo', 'first-content')
111
        tree.put_file_bytes_non_atomic('foo', 'barshoom')
112
        self.assertEqual('barshoom', tree.get_file('foo').read())
113
        tree.unlock()
114
115
    def test_add_in_subdir(self):
116
        branch = self.make_branch('branch')
117
        tree = branch.create_memorytree()
118
        tree.lock_write()
119
        self.addCleanup(tree.unlock)
120
        tree.add([''], None, ['directory'])
121
        tree.mkdir('adir')
122
        tree.put_file_bytes_non_atomic('adir/afile', 'barshoom')
123
        tree.add(['adir/afile'], None, ['file'])
124
        self.assertTrue(tree.is_versioned('adir/afile'))
125
        self.assertTrue(tree.is_versioned('adir'))
126
127
    def test_commit_trivial(self):
128
        """Smoke test for commit on a MemoryTree.
129
130
        Becamse of commits design and layering, if this works, all commit
131
        logic should work quite reliably.
132
        """
133
        branch = self.make_branch('branch')
134
        tree = branch.create_memorytree()
135
        tree.lock_write()
136
        tree.add(['', 'foo'], kinds=['directory', 'file'])
137
        tree.put_file_bytes_non_atomic('foo', 'barshoom')
138
        revision_id = tree.commit('message baby')
139
        # the parents list for the tree should have changed.
140
        self.assertEqual([revision_id], tree.get_parent_ids())
141
        tree.unlock()
142
        # and we should have a revision that is accessible outside the tree lock
143
        revtree = tree.branch.repository.revision_tree(revision_id)
144
        revtree.lock_read()
145
        self.addCleanup(revtree.unlock)
146
        self.assertEqual('barshoom', revtree.get_file('foo').read())
147
148
    def test_unversion(self):
149
        """Some test for unversion of a memory tree."""
150
        branch = self.make_branch('branch')
151
        tree = branch.create_memorytree()
152
        tree.lock_write()
153
        tree.add(['', 'foo'], kinds=['directory', 'file'])
154
        tree.unversion(['foo'])
155
        self.assertFalse(tree.has_id('foo-id'))
156
        tree.unlock()
157
158
    def test_last_revision(self):
159
        """There should be a last revision method we can call."""
160
        tree = self.make_branch_and_memory_tree('branch')
161
        tree.lock_write()
162
        tree.add('')
163
        rev_id = tree.commit('first post')
164
        tree.unlock()
165
        self.assertEqual(rev_id, tree.last_revision())
166
167
    def test_rename_file(self):
168
        tree = self.make_branch_and_memory_tree('branch')
169
        tree.lock_write()
170
        self.addCleanup(tree.unlock)
171
        tree.add(['', 'foo'], ['root-id', 'foo-id'], ['directory', 'file'])
172
        tree.put_file_bytes_non_atomic('foo', 'content\n')
173
        tree.commit('one', rev_id='rev-one')
174
        tree.rename_one('foo', 'bar')
175
        self.assertEqual('bar', tree.id2path('foo-id'))
176
        self.assertEqual('content\n', tree._file_transport.get_bytes('bar'))
177
        self.assertRaises(errors.NoSuchFile,
178
                          tree._file_transport.get_bytes, 'foo')
179
        tree.commit('two', rev_id='rev-two')
180
        self.assertEqual('content\n', tree._file_transport.get_bytes('bar'))
181
        self.assertRaises(errors.NoSuchFile,
182
                          tree._file_transport.get_bytes, 'foo')
183
184
        rev_tree2 = tree.branch.repository.revision_tree('rev-two')
185
        self.assertEqual('bar', rev_tree2.id2path('foo-id'))
186
        self.assertEqual('content\n', rev_tree2.get_file_text('bar'))
187
188
    def test_rename_file_to_subdir(self):
189
        tree = self.make_branch_and_memory_tree('branch')
190
        tree.lock_write()
191
        self.addCleanup(tree.unlock)
192
        tree.add('')
193
        tree.mkdir('subdir', 'subdir-id')
194
        tree.add('foo', 'foo-id', 'file')
195
        tree.put_file_bytes_non_atomic('foo', 'content\n')
196
        tree.commit('one', rev_id='rev-one')
197
198
        tree.rename_one('foo', 'subdir/bar')
199
        self.assertEqual('subdir/bar', tree.id2path('foo-id'))
200
        self.assertEqual('content\n',
201
                         tree._file_transport.get_bytes('subdir/bar'))
202
        tree.commit('two', rev_id='rev-two')
203
        rev_tree2 = tree.branch.repository.revision_tree('rev-two')
204
        self.assertEqual('subdir/bar', rev_tree2.id2path('foo-id'))