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() |
|
|
6973.6.3
by Jelmer Vernooij
More fixes. |
99 |
with tree.lock_write(): |
100 |
tree.add(['', 'foo'], kinds=['directory', 'file']) |
|
101 |
tree.put_file_bytes_non_atomic('foo', b'barshoom') |
|
102 |
self.assertEqual(b'barshoom', tree.get_file('foo').read()) |
|
|
0.360.7
by Jelmer Vernooij
Add tests for memorytree. |
103 |
|
104 |
def test_put_existing_file(self): |
|
105 |
branch = self.make_branch('branch') |
|
106 |
tree = branch.create_memorytree() |
|
|
6973.6.3
by Jelmer Vernooij
More fixes. |
107 |
with tree.lock_write(): |
108 |
tree.add(['', 'foo'], kinds=['directory', 'file']) |
|
109 |
tree.put_file_bytes_non_atomic('foo', b'first-content') |
|
110 |
tree.put_file_bytes_non_atomic('foo', b'barshoom') |
|
111 |
self.assertEqual(b'barshoom', tree.get_file('foo').read()) |
|
|
0.360.7
by Jelmer Vernooij
Add tests for memorytree. |
112 |
|
113 |
def test_add_in_subdir(self): |
|
114 |
branch = self.make_branch('branch') |
|
115 |
tree = branch.create_memorytree() |
|
|
6973.6.3
by Jelmer Vernooij
More fixes. |
116 |
with tree.lock_write(): |
117 |
tree.add([''], None, ['directory']) |
|
118 |
tree.mkdir('adir') |
|
119 |
tree.put_file_bytes_non_atomic('adir/afile', b'barshoom') |
|
120 |
tree.add(['adir/afile'], None, ['file']) |
|
121 |
self.assertTrue(tree.is_versioned('adir/afile')) |
|
122 |
self.assertTrue(tree.is_versioned('adir')) |
|
|
0.360.7
by Jelmer Vernooij
Add tests for memorytree. |
123 |
|
124 |
def test_commit_trivial(self): |
|
125 |
"""Smoke test for commit on a MemoryTree. |
|
126 |
||
127 |
Becamse of commits design and layering, if this works, all commit
|
|
128 |
logic should work quite reliably.
|
|
129 |
"""
|
|
130 |
branch = self.make_branch('branch') |
|
131 |
tree = branch.create_memorytree() |
|
|
6973.6.3
by Jelmer Vernooij
More fixes. |
132 |
with tree.lock_write(): |
133 |
tree.add(['', 'foo'], kinds=['directory', 'file']) |
|
134 |
tree.put_file_bytes_non_atomic('foo', b'barshoom') |
|
135 |
revision_id = tree.commit('message baby') |
|
136 |
# the parents list for the tree should have changed.
|
|
137 |
self.assertEqual([revision_id], tree.get_parent_ids()) |
|
|
0.360.7
by Jelmer Vernooij
Add tests for memorytree. |
138 |
# and we should have a revision that is accessible outside the tree lock
|
139 |
revtree = tree.branch.repository.revision_tree(revision_id) |
|
|
6973.6.3
by Jelmer Vernooij
More fixes. |
140 |
with revtree.lock_read(): |
141 |
self.assertEqual(b'barshoom', revtree.get_file('foo').read()) |
|
|
0.360.7
by Jelmer Vernooij
Add tests for memorytree. |
142 |
|
143 |
def test_unversion(self): |
|
144 |
"""Some test for unversion of a memory tree.""" |
|
145 |
branch = self.make_branch('branch') |
|
146 |
tree = branch.create_memorytree() |
|
147 |
tree.lock_write() |
|
148 |
tree.add(['', 'foo'], kinds=['directory', 'file']) |
|
149 |
tree.unversion(['foo']) |
|
|
6973.7.5
by Jelmer Vernooij
s/file/open. |
150 |
self.assertFalse(tree.has_id(b'foo-id')) |
|
0.360.7
by Jelmer Vernooij
Add tests for memorytree. |
151 |
tree.unlock() |
152 |
||
153 |
def test_last_revision(self): |
|
154 |
"""There should be a last revision method we can call.""" |
|
155 |
tree = self.make_branch_and_memory_tree('branch') |
|
156 |
tree.lock_write() |
|
157 |
tree.add('') |
|
158 |
rev_id = tree.commit('first post') |
|
159 |
tree.unlock() |
|
160 |
self.assertEqual(rev_id, tree.last_revision()) |
|
161 |
||
162 |
def test_rename_file(self): |
|
163 |
tree = self.make_branch_and_memory_tree('branch') |
|
164 |
tree.lock_write() |
|
165 |
self.addCleanup(tree.unlock) |
|
|
6973.6.3
by Jelmer Vernooij
More fixes. |
166 |
tree.add(['', 'foo'], [b'root-id', b'foo-id'], ['directory', 'file']) |
167 |
tree.put_file_bytes_non_atomic('foo', b'content\n') |
|
|
6973.5.2
by Jelmer Vernooij
Add more bees. |
168 |
tree.commit('one', rev_id=b'rev-one') |
|
0.360.7
by Jelmer Vernooij
Add tests for memorytree. |
169 |
tree.rename_one('foo', 'bar') |
|
6973.6.3
by Jelmer Vernooij
More fixes. |
170 |
self.assertEqual('bar', tree.id2path(b'foo-id')) |
|
6973.13.2
by Jelmer Vernooij
Fix some more tests. |
171 |
self.assertEqual(b'content\n', tree._file_transport.get_bytes('bar')) |
|
0.360.7
by Jelmer Vernooij
Add tests for memorytree. |
172 |
self.assertRaises(errors.NoSuchFile, |
173 |
tree._file_transport.get_bytes, 'foo') |
|
|
6973.5.2
by Jelmer Vernooij
Add more bees. |
174 |
tree.commit('two', rev_id=b'rev-two') |
|
6973.6.3
by Jelmer Vernooij
More fixes. |
175 |
self.assertEqual(b'content\n', tree._file_transport.get_bytes('bar')) |
|
0.360.7
by Jelmer Vernooij
Add tests for memorytree. |
176 |
self.assertRaises(errors.NoSuchFile, |
177 |
tree._file_transport.get_bytes, 'foo') |
|
178 |
||
|
6973.5.2
by Jelmer Vernooij
Add more bees. |
179 |
rev_tree2 = tree.branch.repository.revision_tree(b'rev-two') |
|
6973.6.3
by Jelmer Vernooij
More fixes. |
180 |
self.assertEqual('bar', rev_tree2.id2path(b'foo-id')) |
181 |
self.assertEqual(b'content\n', rev_tree2.get_file_text('bar')) |
|
|
0.360.7
by Jelmer Vernooij
Add tests for memorytree. |
182 |
|
183 |
def test_rename_file_to_subdir(self): |
|
184 |
tree = self.make_branch_and_memory_tree('branch') |
|
185 |
tree.lock_write() |
|
186 |
self.addCleanup(tree.unlock) |
|
187 |
tree.add('') |
|
|
6964.2.1
by Jelmer Vernooij
Initial work to support brz-git on python3. |
188 |
tree.mkdir('subdir', b'subdir-id') |
189 |
tree.add('foo', b'foo-id', 'file') |
|
190 |
tree.put_file_bytes_non_atomic('foo', b'content\n') |
|
191 |
tree.commit('one', rev_id=b'rev-one') |
|
|
0.360.7
by Jelmer Vernooij
Add tests for memorytree. |
192 |
|
193 |
tree.rename_one('foo', 'subdir/bar') |
|
|
6964.2.1
by Jelmer Vernooij
Initial work to support brz-git on python3. |
194 |
self.assertEqual('subdir/bar', tree.id2path(b'foo-id')) |
195 |
self.assertEqual(b'content\n', |
|
|
0.360.7
by Jelmer Vernooij
Add tests for memorytree. |
196 |
tree._file_transport.get_bytes('subdir/bar')) |
|
6964.2.1
by Jelmer Vernooij
Initial work to support brz-git on python3. |
197 |
tree.commit('two', rev_id=b'rev-two') |
198 |
rev_tree2 = tree.branch.repository.revision_tree(b'rev-two') |
|
199 |
self.assertEqual('subdir/bar', rev_tree2.id2path(b'foo-id')) |