/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_memorytree.py

  • Committer: Martin Pool
  • Date: 2007-10-03 08:06:44 UTC
  • mto: This revision was merged to the branch mainline in revision 2901.
  • Revision ID: mbp@sourcefrog.net-20071003080644-oivy0gkg98sex0ed
Avoid internal error tracebacks on failure to lock on readonly transport (#129701).

Add new LockFailed, which doesn't imply that we failed to get it because of
contention.  Raise this if we fail to create the pending or lock directories
because of Transport errors.

UnlockableTransport is not an internal error.

ReadOnlyLockError has a message which didn't match its name or usage; it's now
deprecated and callers are updated to use LockFailed which is more appropriate.

Add zero_ninetytwo deprecation symbol.

Unify assertMatchesRe with TestCase.assertContainsRe.

When the constructor is deprecated, just say that the class is deprecated, not
the __init__ method - this works better with applyDeprecated in tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
#
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
17
17
 
18
18
"""Tests for the MemoryTree class."""
19
19
 
24
24
 
25
25
 
26
26
class TestMemoryTree(TestCaseWithTransport):
27
 
 
 
27
    
28
28
    def test_create_on_branch(self):
29
29
        """Creating a mutable tree on a trivial branch works."""
30
30
        branch = self.make_branch('branch')
32
32
        self.assertEqual(branch.bzrdir, tree.bzrdir)
33
33
        self.assertEqual(branch, tree.branch)
34
34
        self.assertEqual([], tree.get_parent_ids())
35
 
 
 
35
    
36
36
    def test_create_on_branch_with_content(self):
37
37
        """Creating a mutable tree on a non-trivial branch works."""
38
38
        branch = self.make_branch('branch')
52
52
            tree.get_file(tree.path2id('foo')).read())
53
53
        tree.unlock()
54
54
 
55
 
    def test_get_root_id(self):
56
 
        branch = self.make_branch('branch')
57
 
        tree = MemoryTree.create_on_branch(branch)
58
 
        tree.lock_write()
59
 
        try:
60
 
            tree.add([''])
61
 
            self.assertIsNot(None, tree.get_root_id())
62
 
        finally:
63
 
            tree.unlock()
64
 
 
65
55
    def test_lock_tree_write(self):
66
56
        """Check we can lock_tree_write and unlock MemoryTrees."""
67
57
        branch = self.make_branch('branch')
96
86
        branch = self.make_branch('branch')
97
87
        tree = MemoryTree.create_on_branch(branch)
98
88
        tree.lock_write()
99
 
        tree.add(['', 'afile', 'adir'], None,
 
89
        tree.add(['', 'afile', 'adir'], None, 
100
90
                 ['directory', 'file', 'directory'])
101
91
        self.assertEqual('afile', tree.id2path(tree.path2id('afile')))
102
92
        self.assertEqual('adir', tree.id2path(tree.path2id('adir')))
108
98
        branch = self.make_branch('branch')
109
99
        tree = MemoryTree.create_on_branch(branch)
110
100
        tree.lock_write()
111
 
        tree.add(['', 'foo'], ids=['root-id', 'foo-id'],
 
101
        tree.add(['', 'foo'], ids=['root-id', 'foo-id'], 
112
102
                  kinds=['directory', 'file'])
113
103
        tree.put_file_bytes_non_atomic('foo-id', 'barshoom')
114
104
        self.assertEqual('barshoom', tree.get_file('foo-id').read())
118
108
        branch = self.make_branch('branch')
119
109
        tree = MemoryTree.create_on_branch(branch)
120
110
        tree.lock_write()
121
 
        tree.add(['', 'foo'], ids=['root-id', 'foo-id'],
 
111
        tree.add(['', 'foo'], ids=['root-id', 'foo-id'], 
122
112
                 kinds=['directory', 'file'])
123
113
        tree.put_file_bytes_non_atomic('foo-id', 'first-content')
124
114
        tree.put_file_bytes_non_atomic('foo-id', 'barshoom')
125
115
        self.assertEqual('barshoom', tree.get_file('foo-id').read())
126
116
        tree.unlock()
127
117
 
128
 
    def test_add_in_subdir(self):
129
 
        branch = self.make_branch('branch')
130
 
        tree = MemoryTree.create_on_branch(branch)
131
 
        tree.lock_write()
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')
143
 
 
144
118
    def test_commit_trivial(self):
145
119
        """Smoke test for commit on a MemoryTree.
146
120
 
150
124
        branch = self.make_branch('branch')
151
125
        tree = MemoryTree.create_on_branch(branch)
152
126
        tree.lock_write()
153
 
        tree.add(['', 'foo'], ids=['root-id', 'foo-id'],
 
127
        tree.add(['', 'foo'], ids=['root-id', 'foo-id'], 
154
128
                 kinds=['directory', 'file'])
155
129
        tree.put_file_bytes_non_atomic('foo-id', 'barshoom')
156
130
        revision_id = tree.commit('message baby')
159
133
        tree.unlock()
160
134
        # and we should have a revision that is accessible outside the tree lock
161
135
        revtree = tree.branch.repository.revision_tree(revision_id)
162
 
        revtree.lock_read()
163
 
        self.addCleanup(revtree.unlock)
164
136
        self.assertEqual('barshoom', revtree.get_file('foo-id').read())
165
137
 
166
138
    def test_unversion(self):
168
140
        branch = self.make_branch('branch')
169
141
        tree = MemoryTree.create_on_branch(branch)
170
142
        tree.lock_write()
171
 
        tree.add(['', 'foo'], ids=['root-id', 'foo-id'],
 
143
        tree.add(['', 'foo'], ids=['root-id', 'foo-id'], 
172
144
                 kinds=['directory', 'file'])
173
145
        tree.unversion(['foo-id'])
174
146
        self.assertFalse(tree.has_id('foo-id'))
182
154
        rev_id = tree.commit('first post')
183
155
        tree.unlock()
184
156
        self.assertEqual(rev_id, tree.last_revision())
185
 
 
186
 
    def test_rename_file(self):
187
 
        tree = self.make_branch_and_memory_tree('branch')
188
 
        tree.lock_write()
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')
202
 
 
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'))
206
 
 
207
 
    def test_rename_file_to_subdir(self):
208
 
        tree = self.make_branch_and_memory_tree('branch')
209
 
        tree.lock_write()
210
 
        self.addCleanup(tree.unlock)
211
 
        tree.add('')
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')
216
 
 
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'))