/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: Breezy landing bot
  • Author(s): Vincent Ladeuil
  • Date: 2019-03-06 14:58:32 UTC
  • mfrom: (7294.1.2 trunk)
  • Revision ID: breezy.the.bot@gmail.com-20190306145832-ghgjxu1recloriwh
Open trunk again as 3.1.0dev1

Merged from https://code.launchpad.net/~vila/brz/trunk/+merge/364041

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')
57
56
        tree = MemoryTree.create_on_branch(branch)
58
 
        tree.lock_write()
59
 
        try:
 
57
        with tree.lock_write():
60
58
            tree.add([''])
61
59
            self.assertIsNot(None, tree.get_root_id())
62
 
        finally:
63
 
            tree.unlock()
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=[b'root-id', b'foo-id'],
112
 
                 kinds=['directory', 'file'])
113
 
        tree.put_file_bytes_non_atomic('foo', b'barshoom')
114
 
        self.assertEqual(b'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=[b'root-id', b'foo-id'],
122
 
                 kinds=['directory', 'file'])
123
 
        tree.put_file_bytes_non_atomic('foo', b'first-content')
124
 
        tree.put_file_bytes_non_atomic('foo', b'barshoom')
125
 
        self.assertEqual(b'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([''], [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', b'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', 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')
 
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=[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())
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
 
        with revtree.get_file('foo') as f:
 
161
        with revtree.lock_read(), revtree.get_file('foo') as f:
165
162
            self.assertEqual(b'barshoom', f.read())
166
163
 
167
164
    def test_unversion(self):
168
165
        """Some test for unversion of a memory tree."""
169
166
        branch = self.make_branch('branch')
170
167
        tree = MemoryTree.create_on_branch(branch)
171
 
        tree.lock_write()
172
 
        tree.add(['', 'foo'], ids=[b'root-id', b'foo-id'],
173
 
                 kinds=['directory', 'file'])
174
 
        tree.unversion(['foo'])
175
 
        self.assertFalse(tree.is_versioned('foo'))
176
 
        self.assertFalse(tree.has_id(b'foo-id'))
177
 
        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'))
178
174
 
179
175
    def test_last_revision(self):
180
176
        """There should be a last revision method we can call."""
181
177
        tree = self.make_branch_and_memory_tree('branch')
182
 
        tree.lock_write()
183
 
        tree.add('')
184
 
        rev_id = tree.commit('first post')
185
 
        tree.unlock()
 
178
        with tree.lock_write():
 
179
            tree.add('')
 
180
            rev_id = tree.commit('first post')
186
181
        self.assertEqual(rev_id, tree.last_revision())
187
182
 
188
183
    def test_rename_file(self):