1
# Copyright (C) 2006 Canonical Ltd
2
# Authors: Robert Collins <robert.collins@canonical.com>
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.
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.
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
18
"""Tests for the GitMemoryTree class."""
20
from ... import errors
21
from ...treebuilder import TreeBuilder
22
from . import TestCaseWithTransport
25
class TestMemoryTree(TestCaseWithTransport):
27
def make_branch(self, path, format='git'):
28
return super(TestMemoryTree, self).make_branch(path, format=format)
30
def make_branch_and_tree(self, path, format='git'):
31
return super(TestMemoryTree, self).make_branch_and_tree(path, format=format)
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())
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'])
46
rev_id = wt.commit('first post')
47
tree = wt.branch.create_memorytree()
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())
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()
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()
65
self.assertRaises(errors.ReadOnlyError, tree.lock_tree_write)
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()
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()
80
self.assertRaises(errors.ReadOnlyError, tree.lock_write)
83
def test_add_with_kind(self):
84
branch = self.make_branch('branch')
85
tree = branch.create_memorytree()
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'))
95
def test_put_new_file(self):
96
branch = self.make_branch('branch')
97
tree = branch.create_memorytree()
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())
103
def test_put_existing_file(self):
104
branch = self.make_branch('branch')
105
tree = branch.create_memorytree()
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())
112
def test_add_in_subdir(self):
113
branch = self.make_branch('branch')
114
tree = branch.create_memorytree()
115
with tree.lock_write():
116
tree.add([''], None, ['directory'])
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'))
123
def test_commit_trivial(self):
124
"""Smoke test for commit on a MemoryTree.
126
Becamse of commits design and layering, if this works, all commit
127
logic should work quite reliably.
129
branch = self.make_branch('branch')
130
tree = branch.create_memorytree()
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())
137
# and we should have a revision that is accessible outside the tree lock
138
revtree = tree.branch.repository.revision_tree(revision_id)
139
with revtree.lock_read():
140
self.assertEqual(b'barshoom', revtree.get_file('foo').read())
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()
147
tree.add(['', 'foo'], kinds=['directory', 'file'])
148
tree.unversion(['foo'])
149
self.assertFalse(tree.has_id(b'foo-id'))
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')
157
rev_id = tree.commit('first post')
159
self.assertEqual(rev_id, tree.last_revision())
161
def test_rename_file(self):
162
tree = self.make_branch_and_memory_tree('branch')
164
self.addCleanup(tree.unlock)
165
tree.add(['', 'foo'], [b'root-id', b'foo-id'], ['directory', 'file'])
166
tree.put_file_bytes_non_atomic('foo', b'content\n')
167
tree.commit('one', rev_id=b'rev-one')
168
tree.rename_one('foo', 'bar')
169
self.assertEqual('bar', tree.id2path(b'foo-id'))
170
self.assertEqual(b'content\n', tree._file_transport.get_bytes('bar'))
171
self.assertRaises(errors.NoSuchFile,
172
tree._file_transport.get_bytes, 'foo')
173
tree.commit('two', rev_id=b'rev-two')
174
self.assertEqual(b'content\n', tree._file_transport.get_bytes('bar'))
175
self.assertRaises(errors.NoSuchFile,
176
tree._file_transport.get_bytes, 'foo')
178
rev_tree2 = tree.branch.repository.revision_tree(b'rev-two')
179
self.assertEqual('bar', rev_tree2.id2path(b'foo-id'))
180
self.assertEqual(b'content\n', rev_tree2.get_file_text('bar'))
182
def test_rename_file_to_subdir(self):
183
tree = self.make_branch_and_memory_tree('branch')
185
self.addCleanup(tree.unlock)
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')
192
tree.rename_one('foo', 'subdir/bar')
193
self.assertEqual('subdir/bar', tree.id2path(b'foo-id'))
194
self.assertEqual(b'content\n',
195
tree._file_transport.get_bytes('subdir/bar'))
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'))