14
14
# You should have received a copy of the GNU General Public License
15
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
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
18
"""Tests for the MemoryTree class."""
52
52
tree.get_file(tree.path2id('foo')).read())
55
def test_get_root_id(self):
56
branch = self.make_branch('branch')
57
tree = MemoryTree.create_on_branch(branch)
61
self.assertIsNot(None, tree.get_root_id())
65
def test_lock_tree_write(self):
66
"""Check we can lock_tree_write and unlock MemoryTrees."""
67
branch = self.make_branch('branch')
68
tree = MemoryTree.create_on_branch(branch)
69
tree.lock_tree_write()
72
def test_lock_tree_write_after_read_fails(self):
73
"""Check that we error when trying to upgrade a read lock to write."""
74
branch = self.make_branch('branch')
75
tree = MemoryTree.create_on_branch(branch)
77
self.assertRaises(errors.ReadOnlyError, tree.lock_tree_write)
80
55
def test_lock_write(self):
81
56
"""Check we can lock_write and unlock MemoryTrees."""
82
57
branch = self.make_branch('branch')
96
71
branch = self.make_branch('branch')
97
72
tree = MemoryTree.create_on_branch(branch)
99
tree.add(['', 'afile', 'adir'], None,
100
['directory', 'file', 'directory'])
74
tree.add(['afile', 'adir'], None, ['file', 'directory'])
101
75
self.assertEqual('afile', tree.id2path(tree.path2id('afile')))
102
76
self.assertEqual('adir', tree.id2path(tree.path2id('adir')))
103
77
self.assertFalse(tree.has_filename('afile'))
108
82
branch = self.make_branch('branch')
109
83
tree = MemoryTree.create_on_branch(branch)
111
tree.add(['', 'foo'], ids=['root-id', 'foo-id'],
112
kinds=['directory', 'file'])
85
tree.add(['foo'], ids=['foo-id'], kinds=['file'])
113
86
tree.put_file_bytes_non_atomic('foo-id', 'barshoom')
114
87
self.assertEqual('barshoom', tree.get_file('foo-id').read())
118
91
branch = self.make_branch('branch')
119
92
tree = MemoryTree.create_on_branch(branch)
121
tree.add(['', 'foo'], ids=['root-id', 'foo-id'],
122
kinds=['directory', 'file'])
94
tree.add(['foo'], ids=['foo-id'], kinds=['file'])
123
95
tree.put_file_bytes_non_atomic('foo-id', 'first-content')
124
96
tree.put_file_bytes_non_atomic('foo-id', 'barshoom')
125
97
self.assertEqual('barshoom', tree.get_file('foo-id').read())
128
def test_add_in_subdir(self):
129
branch = self.make_branch('branch')
130
tree = MemoryTree.create_on_branch(branch)
132
self.addCleanup(tree.unlock)
133
tree.add([''], ['root-id'], ['directory'])
134
# Unfortunately, the only way to 'mkdir' is to call 'tree.mkdir', but
135
# that *always* adds the directory as well. So if you want to create a
136
# file in a subdirectory, you have to split out the 'mkdir()' calls
137
# from the add and put_file_bytes_non_atomic calls. :(
138
tree.mkdir('adir', 'dir-id')
139
tree.add(['adir/afile'], ['file-id'], ['file'])
140
self.assertEqual('adir/afile', tree.id2path('file-id'))
141
self.assertEqual('adir', tree.id2path('dir-id'))
142
tree.put_file_bytes_non_atomic('file-id', 'barshoom')
144
100
def test_commit_trivial(self):
145
101
"""Smoke test for commit on a MemoryTree.
150
106
branch = self.make_branch('branch')
151
107
tree = MemoryTree.create_on_branch(branch)
152
108
tree.lock_write()
153
tree.add(['', 'foo'], ids=['root-id', 'foo-id'],
154
kinds=['directory', 'file'])
109
tree.add(['foo'], ids=['foo-id'], kinds=['file'])
155
110
tree.put_file_bytes_non_atomic('foo-id', 'barshoom')
156
111
revision_id = tree.commit('message baby')
157
112
# the parents list for the tree should have changed.
160
115
# and we should have a revision that is accessible outside the tree lock
161
116
revtree = tree.branch.repository.revision_tree(revision_id)
163
self.addCleanup(revtree.unlock)
164
117
self.assertEqual('barshoom', revtree.get_file('foo-id').read())
166
def test_unversion(self):
167
"""Some test for unversion of a memory tree."""
168
branch = self.make_branch('branch')
169
tree = MemoryTree.create_on_branch(branch)
171
tree.add(['', 'foo'], ids=['root-id', 'foo-id'],
172
kinds=['directory', 'file'])
173
tree.unversion(['foo-id'])
174
self.assertFalse(tree.has_id('foo-id'))
177
def test_last_revision(self):
178
"""There should be a last revision method we can call."""
179
tree = self.make_branch_and_memory_tree('branch')
182
rev_id = tree.commit('first post')
184
self.assertEqual(rev_id, tree.last_revision())
186
def test_rename_file(self):
187
tree = self.make_branch_and_memory_tree('branch')
189
self.addCleanup(tree.unlock)
190
tree.add(['', 'foo'], ['root-id', 'foo-id'], ['directory', 'file'])
191
tree.put_file_bytes_non_atomic('foo-id', 'content\n')
192
tree.commit('one', rev_id='rev-one')
193
tree.rename_one('foo', 'bar')
194
self.assertEqual('bar', tree.id2path('foo-id'))
195
self.assertEqual('content\n', tree._file_transport.get_bytes('bar'))
196
self.assertRaises(errors.NoSuchFile,
197
tree._file_transport.get_bytes, 'foo')
198
tree.commit('two', rev_id='rev-two')
199
self.assertEqual('content\n', tree._file_transport.get_bytes('bar'))
200
self.assertRaises(errors.NoSuchFile,
201
tree._file_transport.get_bytes, 'foo')
203
rev_tree2 = tree.branch.repository.revision_tree('rev-two')
204
self.assertEqual('bar', rev_tree2.id2path('foo-id'))
205
self.assertEqual('content\n', rev_tree2.get_file_text('foo-id'))
207
def test_rename_file_to_subdir(self):
208
tree = self.make_branch_and_memory_tree('branch')
210
self.addCleanup(tree.unlock)
212
tree.mkdir('subdir', 'subdir-id')
213
tree.add('foo', 'foo-id', 'file')
214
tree.put_file_bytes_non_atomic('foo-id', 'content\n')
215
tree.commit('one', rev_id='rev-one')
217
tree.rename_one('foo', 'subdir/bar')
218
self.assertEqual('subdir/bar', tree.id2path('foo-id'))
219
self.assertEqual('content\n',
220
tree._file_transport.get_bytes('subdir/bar'))
221
tree.commit('two', rev_id='rev-two')
222
rev_tree2 = tree.branch.repository.revision_tree('rev-two')
223
self.assertEqual('subdir/bar', rev_tree2.id2path('foo-id'))