/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

  • Committer: Jelmer Vernooij
  • Date: 2018-05-06 11:48:54 UTC
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@jelmer.uk-20180506114854-h4qd9ojaqy8wxjsd
Move .mailmap to root.

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
 
        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())
 
49
        tree.lock_read()
 
50
        self.assertEqual([rev_id], tree.get_parent_ids())
 
51
        self.assertEqual('contents of foo\n',
 
52
            tree.get_file('foo').read())
 
53
        tree.unlock()
53
54
 
54
55
    def test_get_root_id(self):
55
56
        branch = self.make_branch('branch')
56
57
        tree = MemoryTree.create_on_branch(branch)
57
 
        with tree.lock_write():
 
58
        tree.lock_write()
 
59
        try:
58
60
            tree.add([''])
59
 
            self.assertIsNot(None, tree.path2id(''))
 
61
            self.assertIsNot(None, tree.get_root_id())
 
62
        finally:
 
63
            tree.unlock()
60
64
 
61
65
    def test_lock_tree_write(self):
62
66
        """Check we can lock_tree_write and unlock MemoryTrees."""
69
73
        """Check that we error when trying to upgrade a read lock to write."""
70
74
        branch = self.make_branch('branch')
71
75
        tree = MemoryTree.create_on_branch(branch)
72
 
        with tree.lock_read():
73
 
            self.assertRaises(errors.ReadOnlyError, tree.lock_tree_write)
 
76
        tree.lock_read()
 
77
        self.assertRaises(errors.ReadOnlyError, tree.lock_tree_write)
 
78
        tree.unlock()
74
79
 
75
80
    def test_lock_write(self):
76
81
        """Check we can lock_write and unlock MemoryTrees."""
83
88
        """Check that we error when trying to upgrade a read lock to write."""
84
89
        branch = self.make_branch('branch')
85
90
        tree = MemoryTree.create_on_branch(branch)
86
 
        with tree.lock_read():
87
 
            self.assertRaises(errors.ReadOnlyError, tree.lock_write)
 
91
        tree.lock_read()
 
92
        self.assertRaises(errors.ReadOnlyError, tree.lock_write)
 
93
        tree.unlock()
88
94
 
89
95
    def test_add_with_kind(self):
90
96
        branch = self.make_branch('branch')
91
97
        tree = MemoryTree.create_on_branch(branch)
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'))
 
98
        tree.lock_write()
 
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'))
 
105
        tree.unlock()
99
106
 
100
107
    def test_put_new_file(self):
101
108
        branch = self.make_branch('branch')
102
109
        tree = MemoryTree.create_on_branch(branch)
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())
 
110
        tree.lock_write()
 
111
        tree.add(['', 'foo'], ids=[b'root-id', b'foo-id'],
 
112
                  kinds=['directory', 'file'])
 
113
        tree.put_file_bytes_non_atomic('foo', 'barshoom')
 
114
        self.assertEqual('barshoom', tree.get_file('foo').read())
 
115
        tree.unlock()
109
116
 
110
117
    def test_put_existing_file(self):
111
118
        branch = self.make_branch('branch')
112
119
        tree = MemoryTree.create_on_branch(branch)
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())
 
120
        tree.lock_write()
 
121
        tree.add(['', 'foo'], ids=[b'root-id', b'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())
 
126
        tree.unlock()
119
127
 
120
128
    def test_add_in_subdir(self):
121
129
        branch = self.make_branch('branch')
122
130
        tree = MemoryTree.create_on_branch(branch)
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')
134
 
 
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'))
 
131
        tree.lock_write()
 
132
        self.addCleanup(tree.unlock)
 
133
        tree.add([''], [b'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'], [b'file-id'], ['file'])
 
140
        self.assertEqual('adir/afile', tree.id2path(b'file-id'))
 
141
        self.assertEqual('adir', tree.id2path(b'dir-id'))
 
142
        tree.put_file_bytes_non_atomic('adir/afile', 'barshoom')
143
143
 
144
144
    def test_commit_trivial(self):
145
145
        """Smoke test for commit on a MemoryTree.
149
149
        """
150
150
        branch = self.make_branch('branch')
151
151
        tree = MemoryTree.create_on_branch(branch)
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())
 
152
        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', '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())
 
159
        tree.unlock()
159
160
        # and we should have a revision that is accessible outside the tree lock
160
161
        revtree = tree.branch.repository.revision_tree(revision_id)
161
 
        with revtree.lock_read(), revtree.get_file('foo') as f:
162
 
            self.assertEqual(b'barshoom', f.read())
 
162
        revtree.lock_read()
 
163
        self.addCleanup(revtree.unlock)
 
164
        self.assertEqual('barshoom', revtree.get_file('foo').read())
163
165
 
164
166
    def test_unversion(self):
165
167
        """Some test for unversion of a memory tree."""
166
168
        branch = self.make_branch('branch')
167
169
        tree = MemoryTree.create_on_branch(branch)
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')
 
170
        tree.lock_write()
 
171
        tree.add(['', 'foo'], ids=[b'root-id', b'foo-id'],
 
172
                 kinds=['directory', 'file'])
 
173
        tree.unversion(['foo'])
 
174
        self.assertFalse(tree.is_versioned('foo'))
 
175
        self.assertFalse(tree.has_id(b'foo-id'))
 
176
        tree.unlock()
174
177
 
175
178
    def test_last_revision(self):
176
179
        """There should be a last revision method we can call."""
177
180
        tree = self.make_branch_and_memory_tree('branch')
178
 
        with tree.lock_write():
179
 
            tree.add('')
180
 
            rev_id = tree.commit('first post')
 
181
        tree.lock_write()
 
182
        tree.add('')
 
183
        rev_id = tree.commit('first post')
 
184
        tree.unlock()
181
185
        self.assertEqual(rev_id, tree.last_revision())
182
186
 
183
187
    def test_rename_file(self):
185
189
        tree.lock_write()
186
190
        self.addCleanup(tree.unlock)
187
191
        tree.add(['', 'foo'], [b'root-id', b'foo-id'], ['directory', 'file'])
188
 
        tree.put_file_bytes_non_atomic('foo', b'content\n')
 
192
        tree.put_file_bytes_non_atomic('foo', 'content\n')
189
193
        tree.commit('one', rev_id=b'rev-one')
190
194
        tree.rename_one('foo', 'bar')
191
195
        self.assertEqual('bar', tree.id2path(b'foo-id'))
192
 
        self.assertEqual(b'content\n', tree._file_transport.get_bytes('bar'))
 
196
        self.assertEqual('content\n', tree._file_transport.get_bytes('bar'))
193
197
        self.assertRaises(errors.NoSuchFile,
194
198
                          tree._file_transport.get_bytes, 'foo')
195
199
        tree.commit('two', rev_id=b'rev-two')
196
 
        self.assertEqual(b'content\n', tree._file_transport.get_bytes('bar'))
 
200
        self.assertEqual('content\n', tree._file_transport.get_bytes('bar'))
197
201
        self.assertRaises(errors.NoSuchFile,
198
202
                          tree._file_transport.get_bytes, 'foo')
199
203
 
200
204
        rev_tree2 = tree.branch.repository.revision_tree(b'rev-two')
201
205
        self.assertEqual('bar', rev_tree2.id2path(b'foo-id'))
202
 
        self.assertEqual(b'content\n', rev_tree2.get_file_text('bar'))
 
206
        self.assertEqual('content\n', rev_tree2.get_file_text('bar'))
203
207
 
204
208
    def test_rename_file_to_subdir(self):
205
209
        tree = self.make_branch_and_memory_tree('branch')
208
212
        tree.add('')
209
213
        tree.mkdir('subdir', b'subdir-id')
210
214
        tree.add('foo', b'foo-id', 'file')
211
 
        tree.put_file_bytes_non_atomic('foo', b'content\n')
 
215
        tree.put_file_bytes_non_atomic('foo', 'content\n')
212
216
        tree.commit('one', rev_id=b'rev-one')
213
217
 
214
218
        tree.rename_one('foo', 'subdir/bar')
215
219
        self.assertEqual('subdir/bar', tree.id2path(b'foo-id'))
216
 
        self.assertEqual(b'content\n',
 
220
        self.assertEqual('content\n',
217
221
                         tree._file_transport.get_bytes('subdir/bar'))
218
222
        tree.commit('two', rev_id=b'rev-two')
219
223
        rev_tree2 = tree.branch.repository.revision_tree(b'rev-two')