88
83
"""Check that we error when trying to upgrade a read lock to write."""
89
84
branch = self.make_branch('branch')
90
85
tree = MemoryTree.create_on_branch(branch)
92
self.assertRaises(errors.ReadOnlyError, tree.lock_write)
86
with tree.lock_read():
87
self.assertRaises(errors.ReadOnlyError, tree.lock_write)
95
89
def test_add_with_kind(self):
96
90
branch = self.make_branch('branch')
97
91
tree = MemoryTree.create_on_branch(branch)
99
tree.add(['', 'afile', 'adir'], None,
100
['directory', 'file', 'directory'])
101
self.assertEqual('afile', tree.id2path(tree.path2id('afile')))
102
self.assertEqual('adir', tree.id2path(tree.path2id('adir')))
103
self.assertFalse(tree.has_filename('afile'))
104
self.assertFalse(tree.has_filename('adir'))
92
with tree.lock_write():
93
tree.add(['', 'afile', 'adir'], None,
94
['directory', 'file', 'directory'])
95
self.assertEqual('afile', tree.id2path(tree.path2id('afile')))
96
self.assertEqual('adir', tree.id2path(tree.path2id('adir')))
97
self.assertFalse(tree.has_filename('afile'))
98
self.assertFalse(tree.has_filename('adir'))
107
100
def test_put_new_file(self):
108
101
branch = self.make_branch('branch')
109
102
tree = MemoryTree.create_on_branch(branch)
111
tree.add(['', 'foo'], ids=['root-id', 'foo-id'],
112
kinds=['directory', 'file'])
113
tree.put_file_bytes_non_atomic('foo', 'barshoom')
114
self.assertEqual('barshoom', tree.get_file('foo').read())
103
with tree.lock_write():
104
tree.add(['', 'foo'], ids=[b'root-id', b'foo-id'],
105
kinds=['directory', 'file'])
106
tree.put_file_bytes_non_atomic('foo', b'barshoom')
107
with tree.get_file('foo') as f:
108
self.assertEqual(b'barshoom', f.read())
117
110
def test_put_existing_file(self):
118
111
branch = self.make_branch('branch')
119
112
tree = MemoryTree.create_on_branch(branch)
121
tree.add(['', 'foo'], ids=['root-id', 'foo-id'],
122
kinds=['directory', 'file'])
123
tree.put_file_bytes_non_atomic('foo', 'first-content')
124
tree.put_file_bytes_non_atomic('foo', 'barshoom')
125
self.assertEqual('barshoom', tree.get_file('foo').read())
113
with tree.lock_write():
114
tree.add(['', 'foo'], ids=[b'root-id', b'foo-id'],
115
kinds=['directory', 'file'])
116
tree.put_file_bytes_non_atomic('foo', b'first-content')
117
tree.put_file_bytes_non_atomic('foo', b'barshoom')
118
self.assertEqual(b'barshoom', tree.get_file('foo').read())
128
120
def test_add_in_subdir(self):
129
121
branch = self.make_branch('branch')
130
122
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('adir/afile', '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')
135
def test_add_symlink(self):
136
branch = self.make_branch('branch')
137
tree = MemoryTree.create_on_branch(branch)
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'))
144
144
def test_commit_trivial(self):
145
145
"""Smoke test for commit on a MemoryTree.
150
150
branch = self.make_branch('branch')
151
151
tree = MemoryTree.create_on_branch(branch)
153
tree.add(['', 'foo'], ids=['root-id', 'foo-id'],
154
kinds=['directory', 'file'])
155
tree.put_file_bytes_non_atomic('foo', '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())
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())
160
159
# and we should have a revision that is accessible outside the tree lock
161
160
revtree = tree.branch.repository.revision_tree(revision_id)
163
self.addCleanup(revtree.unlock)
164
self.assertEqual('barshoom', revtree.get_file('foo').read())
161
with revtree.lock_read(), revtree.get_file('foo') as f:
162
self.assertEqual(b'barshoom', f.read())
166
164
def test_unversion(self):
167
165
"""Some test for unversion of a memory tree."""
168
166
branch = self.make_branch('branch')
169
167
tree = MemoryTree.create_on_branch(branch)
171
tree.add(['', 'foo'], ids=['root-id', 'foo-id'],
172
kinds=['directory', 'file'])
173
tree.unversion(['foo'])
174
self.assertFalse(tree.has_id('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.assertRaises(errors.NoSuchId, tree.id2path, b'foo-id')
177
175
def test_last_revision(self):
178
176
"""There should be a last revision method we can call."""
179
177
tree = self.make_branch_and_memory_tree('branch')
182
rev_id = tree.commit('first post')
178
with tree.lock_write():
180
rev_id = tree.commit('first post')
184
181
self.assertEqual(rev_id, tree.last_revision())
186
183
def test_rename_file(self):
187
184
tree = self.make_branch_and_memory_tree('branch')
188
185
tree.lock_write()
189
186
self.addCleanup(tree.unlock)
190
tree.add(['', 'foo'], ['root-id', 'foo-id'], ['directory', 'file'])
191
tree.put_file_bytes_non_atomic('foo', 'content\n')
192
tree.commit('one', rev_id='rev-one')
187
tree.add(['', 'foo'], [b'root-id', b'foo-id'], ['directory', 'file'])
188
tree.put_file_bytes_non_atomic('foo', b'content\n')
189
tree.commit('one', rev_id=b'rev-one')
193
190
tree.rename_one('foo', 'bar')
194
self.assertEqual('bar', tree.id2path('foo-id'))
195
self.assertEqual('content\n', tree._file_transport.get_bytes('bar'))
191
self.assertEqual('bar', tree.id2path(b'foo-id'))
192
self.assertEqual(b'content\n', tree._file_transport.get_bytes('bar'))
196
193
self.assertRaises(errors.NoSuchFile,
197
194
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'))
195
tree.commit('two', rev_id=b'rev-two')
196
self.assertEqual(b'content\n', tree._file_transport.get_bytes('bar'))
200
197
self.assertRaises(errors.NoSuchFile,
201
198
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('bar'))
200
rev_tree2 = tree.branch.repository.revision_tree(b'rev-two')
201
self.assertEqual('bar', rev_tree2.id2path(b'foo-id'))
202
self.assertEqual(b'content\n', rev_tree2.get_file_text('bar'))
207
204
def test_rename_file_to_subdir(self):
208
205
tree = self.make_branch_and_memory_tree('branch')
209
206
tree.lock_write()
210
207
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', 'content\n')
215
tree.commit('one', rev_id='rev-one')
209
tree.mkdir('subdir', b'subdir-id')
210
tree.add('foo', b'foo-id', 'file')
211
tree.put_file_bytes_non_atomic('foo', b'content\n')
212
tree.commit('one', rev_id=b'rev-one')
217
214
tree.rename_one('foo', 'subdir/bar')
218
self.assertEqual('subdir/bar', tree.id2path('foo-id'))
219
self.assertEqual('content\n',
215
self.assertEqual('subdir/bar', tree.id2path(b'foo-id'))
216
self.assertEqual(b'content\n',
220
217
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'))
218
tree.commit('two', rev_id=b'rev-two')
219
rev_tree2 = tree.branch.repository.revision_tree(b'rev-two')
220
self.assertEqual('subdir/bar', rev_tree2.id2path(b'foo-id'))