/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 breezy/tests/test_memorytree.py

Fix tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
        rev_id = tree.commit('first post')
47
47
        tree.unlock()
48
48
        tree = MemoryTree.create_on_branch(branch)
49
 
        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())
53
 
        tree.unlock()
 
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())
54
53
 
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)
73
 
        tree.lock_read()
74
 
        self.assertRaises(errors.ReadOnlyError, tree.lock_tree_write)
75
 
        tree.unlock()
 
72
        with tree.lock_read():
 
73
            self.assertRaises(errors.ReadOnlyError, tree.lock_tree_write)
76
74
 
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)
88
 
        tree.lock_read()
89
 
        self.assertRaises(errors.ReadOnlyError, tree.lock_write)
90
 
        tree.unlock()
 
86
        with tree.lock_read():
 
87
            self.assertRaises(errors.ReadOnlyError, tree.lock_write)
91
88
 
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)
126
 
        tree.lock_write()
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')
138
134
 
139
135
    def test_add_symlink(self):
140
136
        branch = self.make_branch('branch')
141
137
        tree = MemoryTree.create_on_branch(branch)
142
 
        tree.lock_write()
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'))
147
143
 
148
144
    def test_commit_trivial(self):
149
145
        """Smoke test for commit on a MemoryTree.
153
149
        """
154
150
        branch = self.make_branch('branch')
155
151
        tree = MemoryTree.create_on_branch(branch)
156
 
        tree.lock_write()
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())
163
 
        tree.unlock()
 
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)
166
 
        revtree.lock_read()
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())
170
163
 
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)
175
 
        tree.lock_write()
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'))
181
 
        tree.unlock()
 
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'))
182
174
 
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')
186
 
        tree.lock_write()
187
 
        tree.add('')
188
 
        rev_id = tree.commit('first post')
189
 
        tree.unlock()
 
178
        with tree.lock_write():
 
179
            tree.add('')
 
180
            rev_id = tree.commit('first post')
190
181
        self.assertEqual(rev_id, tree.last_revision())
191
182
 
192
183
    def test_rename_file(self):