/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: 2020-03-22 01:35:14 UTC
  • mfrom: (7490.7.6 work)
  • mto: This revision was merged to the branch mainline in revision 7499.
  • Revision ID: jelmer@jelmer.uk-20200322013514-7vw1ntwho04rcuj3
merge lp:brz/3.1.

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
 
        self.assertEqual('contents of foo\n',
52
 
            tree.get_file('foo').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')
57
56
        tree = MemoryTree.create_on_branch(branch)
58
 
        tree.lock_write()
59
 
        try:
 
57
        with tree.lock_write():
60
58
            tree.add([''])
61
 
            self.assertIsNot(None, tree.get_root_id())
62
 
        finally:
63
 
            tree.unlock()
 
59
            self.assertIsNot(None, tree.path2id(''))
64
60
 
65
61
    def test_lock_tree_write(self):
66
62
        """Check we can lock_tree_write and unlock MemoryTrees."""
73
69
        """Check that we error when trying to upgrade a read lock to write."""
74
70
        branch = self.make_branch('branch')
75
71
        tree = MemoryTree.create_on_branch(branch)
76
 
        tree.lock_read()
77
 
        self.assertRaises(errors.ReadOnlyError, tree.lock_tree_write)
78
 
        tree.unlock()
 
72
        with tree.lock_read():
 
73
            self.assertRaises(errors.ReadOnlyError, tree.lock_tree_write)
79
74
 
80
75
    def test_lock_write(self):
81
76
        """Check we can lock_write and unlock MemoryTrees."""
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)
91
 
        tree.lock_read()
92
 
        self.assertRaises(errors.ReadOnlyError, tree.lock_write)
93
 
        tree.unlock()
 
86
        with tree.lock_read():
 
87
            self.assertRaises(errors.ReadOnlyError, tree.lock_write)
94
88
 
95
89
    def test_add_with_kind(self):
96
90
        branch = self.make_branch('branch')
97
91
        tree = MemoryTree.create_on_branch(branch)
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()
 
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'))
106
99
 
107
100
    def test_put_new_file(self):
108
101
        branch = self.make_branch('branch')
109
102
        tree = MemoryTree.create_on_branch(branch)
110
 
        tree.lock_write()
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())
115
 
        tree.unlock()
 
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())
116
109
 
117
110
    def test_put_existing_file(self):
118
111
        branch = self.make_branch('branch')
119
112
        tree = MemoryTree.create_on_branch(branch)
120
 
        tree.lock_write()
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())
126
 
        tree.unlock()
 
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())
127
119
 
128
120
    def test_add_in_subdir(self):
129
121
        branch = self.make_branch('branch')
130
122
        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('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')
 
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'))
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
 
        tree.lock_write()
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())
159
 
        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())
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)
162
 
        revtree.lock_read()
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())
165
163
 
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)
170
 
        tree.lock_write()
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'))
175
 
        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.assertRaises(errors.NoSuchId, tree.id2path, b'foo-id')
176
174
 
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')
180
 
        tree.lock_write()
181
 
        tree.add('')
182
 
        rev_id = tree.commit('first post')
183
 
        tree.unlock()
 
178
        with tree.lock_write():
 
179
            tree.add('')
 
180
            rev_id = tree.commit('first post')
184
181
        self.assertEqual(rev_id, tree.last_revision())
185
182
 
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')
202
199
 
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'))
206
203
 
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)
211
208
        tree.add('')
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')
216
213
 
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'))