46
46
rev_id = tree.commit('first post')
48
48
tree = MemoryTree.create_on_branch(branch)
50
self.assertEqual([rev_id], tree.get_parent_ids())
51
with tree.get_file('foo') as f:
52
self.assertEqual(b'contents of foo\n', f.read())
49
with tree.lock_read():
50
self.assertEqual([rev_id], tree.get_parent_ids())
51
with tree.get_file('foo') as f:
52
self.assertEqual(b'contents of foo\n', f.read())
55
54
def test_get_root_id(self):
56
55
branch = self.make_branch('branch')
70
69
"""Check that we error when trying to upgrade a read lock to write."""
71
70
branch = self.make_branch('branch')
72
71
tree = MemoryTree.create_on_branch(branch)
74
self.assertRaises(errors.ReadOnlyError, tree.lock_tree_write)
72
with tree.lock_read():
73
self.assertRaises(errors.ReadOnlyError, tree.lock_tree_write)
77
75
def test_lock_write(self):
78
76
"""Check we can lock_write and unlock MemoryTrees."""
85
83
"""Check that we error when trying to upgrade a read lock to write."""
86
84
branch = self.make_branch('branch')
87
85
tree = MemoryTree.create_on_branch(branch)
89
self.assertRaises(errors.ReadOnlyError, tree.lock_write)
86
with tree.lock_read():
87
self.assertRaises(errors.ReadOnlyError, tree.lock_write)
92
89
def test_add_with_kind(self):
93
90
branch = self.make_branch('branch')
123
120
def test_add_in_subdir(self):
124
121
branch = self.make_branch('branch')
125
122
tree = MemoryTree.create_on_branch(branch)
127
self.addCleanup(tree.unlock)
128
tree.add([''], [b'root-id'], ['directory'])
129
# Unfortunately, the only way to 'mkdir' is to call 'tree.mkdir', but
130
# that *always* adds the directory as well. So if you want to create a
131
# file in a subdirectory, you have to split out the 'mkdir()' calls
132
# from the add and put_file_bytes_non_atomic calls. :(
133
tree.mkdir('adir', b'dir-id')
134
tree.add(['adir/afile'], [b'file-id'], ['file'])
135
self.assertEqual('adir/afile', tree.id2path(b'file-id'))
136
self.assertEqual('adir', tree.id2path(b'dir-id'))
137
tree.put_file_bytes_non_atomic('adir/afile', b'barshoom')
123
with tree.lock_write():
124
tree.add([''], [b'root-id'], ['directory'])
125
# Unfortunately, the only way to 'mkdir' is to call 'tree.mkdir', but
126
# that *always* adds the directory as well. So if you want to create a
127
# file in a subdirectory, you have to split out the 'mkdir()' calls
128
# from the add and put_file_bytes_non_atomic calls. :(
129
tree.mkdir('adir', b'dir-id')
130
tree.add(['adir/afile'], [b'file-id'], ['file'])
131
self.assertEqual('adir/afile', tree.id2path(b'file-id'))
132
self.assertEqual('adir', tree.id2path(b'dir-id'))
133
tree.put_file_bytes_non_atomic('adir/afile', b'barshoom')
139
135
def test_add_symlink(self):
140
136
branch = self.make_branch('branch')
141
137
tree = MemoryTree.create_on_branch(branch)
143
tree._file_transport.symlink('bar', 'foo')
144
tree.add(['', 'foo'])
145
self.assertEqual('symlink', tree.kind('foo'))
146
self.assertEqual('bar', tree.get_symlink_target('foo'))
138
with tree.lock_write():
139
tree._file_transport.symlink('bar', 'foo')
140
tree.add(['', 'foo'])
141
self.assertEqual('symlink', tree.kind('foo'))
142
self.assertEqual('bar', tree.get_symlink_target('foo'))
148
144
def test_commit_trivial(self):
149
145
"""Smoke test for commit on a MemoryTree.
154
150
branch = self.make_branch('branch')
155
151
tree = MemoryTree.create_on_branch(branch)
157
tree.add(['', 'foo'], ids=[b'root-id', b'foo-id'],
158
kinds=['directory', 'file'])
159
tree.put_file_bytes_non_atomic('foo', b'barshoom')
160
revision_id = tree.commit('message baby')
161
# the parents list for the tree should have changed.
162
self.assertEqual([revision_id], tree.get_parent_ids())
152
with tree.lock_write():
153
tree.add(['', 'foo'], ids=[b'root-id', b'foo-id'],
154
kinds=['directory', 'file'])
155
tree.put_file_bytes_non_atomic('foo', b'barshoom')
156
revision_id = tree.commit('message baby')
157
# the parents list for the tree should have changed.
158
self.assertEqual([revision_id], tree.get_parent_ids())
164
159
# and we should have a revision that is accessible outside the tree lock
165
160
revtree = tree.branch.repository.revision_tree(revision_id)
167
self.addCleanup(revtree.unlock)
168
with revtree.get_file('foo') as f:
161
with revtree.lock_read(), revtree.get_file('foo') as f:
169
162
self.assertEqual(b'barshoom', f.read())
171
164
def test_unversion(self):
172
165
"""Some test for unversion of a memory tree."""
173
166
branch = self.make_branch('branch')
174
167
tree = MemoryTree.create_on_branch(branch)
176
tree.add(['', 'foo'], ids=[b'root-id', b'foo-id'],
177
kinds=['directory', 'file'])
178
tree.unversion(['foo'])
179
self.assertFalse(tree.is_versioned('foo'))
180
self.assertFalse(tree.has_id(b'foo-id'))
168
with tree.lock_write():
169
tree.add(['', 'foo'], ids=[b'root-id', b'foo-id'],
170
kinds=['directory', 'file'])
171
tree.unversion(['foo'])
172
self.assertFalse(tree.is_versioned('foo'))
173
self.assertFalse(tree.has_id(b'foo-id'))
183
175
def test_last_revision(self):
184
176
"""There should be a last revision method we can call."""
185
177
tree = self.make_branch_and_memory_tree('branch')
188
rev_id = tree.commit('first post')
178
with tree.lock_write():
180
rev_id = tree.commit('first post')
190
181
self.assertEqual(rev_id, tree.last_revision())
192
183
def test_rename_file(self):