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()
49
self.assertEqual([rev_id], tree.get_parent_ids())
50
self.assertEqual('contents of sometree/foo\n',
51
tree.get_file('foo').read())
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()
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()
66
self.assertRaises(errors.ReadOnlyError, tree.lock_tree_write)
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()
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()
81
self.assertRaises(errors.ReadOnlyError, tree.lock_write)
84
def test_add_with_kind(self):
85
branch = self.make_branch('branch')
86
tree = branch.create_memorytree()
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'))
96
def test_put_new_file(self):
97
branch = self.make_branch('branch')
98
tree = branch.create_memorytree()
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())
105
def test_put_existing_file(self):
106
branch = self.make_branch('branch')
107
tree = branch.create_memorytree()
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())
115
def test_add_in_subdir(self):
116
branch = self.make_branch('branch')
117
tree = branch.create_memorytree()
119
self.addCleanup(tree.unlock)
120
tree.add([''], None, ['directory'])
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'))
127
def test_commit_trivial(self):
128
"""Smoke test for commit on a MemoryTree.
130
Becamse of commits design and layering, if this works, all commit
131
logic should work quite reliably.
133
branch = self.make_branch('branch')
134
tree = branch.create_memorytree()
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())
142
# and we should have a revision that is accessible outside the tree lock
143
revtree = tree.branch.repository.revision_tree(revision_id)
145
self.addCleanup(revtree.unlock)
146
self.assertEqual('barshoom', revtree.get_file('foo').read())
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()
153
tree.add(['', 'foo'], kinds=['directory', 'file'])
154
tree.unversion(['foo'])
155
self.assertFalse(tree.has_id('foo-id'))
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')
163
rev_id = tree.commit('first post')
165
self.assertEqual(rev_id, tree.last_revision())
167
def test_rename_file(self):
168
tree = self.make_branch_and_memory_tree('branch')
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')
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'))
188
def test_rename_file_to_subdir(self):
189
tree = self.make_branch_and_memory_tree('branch')
191
self.addCleanup(tree.unlock)
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')
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'))