/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2255.2.27 by John Arbash Meinel
Fix a copyright statement to let 'source' tests pass
1
# Copyright (C) 2006, 2007 Canonical Ltd
1852.13.6 by Robert Collins
start hooking in the prototype dirstate serialiser.
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1852.13.6 by Robert Collins
start hooking in the prototype dirstate serialiser.
16
17
"""Tests of the dirstate functionality being built for WorkingTreeFormat4."""
18
2255.8.2 by John Arbash Meinel
Add a helper function, which allows us to store keys as plain paths,
19
import bisect
1852.13.20 by Robert Collins
Steps toward an object model.
20
import os
21
2255.2.125 by John Arbash Meinel
Initial effort at adding a basic _bisect function to DirState.
22
from bzrlib import (
23
    dirstate,
24
    errors,
3477.2.1 by John Arbash Meinel
Assert that we properly encode inv_entry.symlink_target, (bug #135320)
25
    inventory,
4241.14.10 by Vincent Ladeuil
Add DirReader parametrized tests.
26
    memorytree,
2255.2.125 by John Arbash Meinel
Initial effort at adding a basic _bisect function to DirState.
27
    osutils,
3668.5.1 by Jelmer Vernooij
Use NULL_REVISION rather than None for Repository.revision_tree().
28
    revision as _mod_revision,
4241.14.10 by Vincent Ladeuil
Add DirReader parametrized tests.
29
    tests,
2255.2.125 by John Arbash Meinel
Initial effort at adding a basic _bisect function to DirState.
30
    )
4241.14.10 by Vincent Ladeuil
Add DirReader parametrized tests.
31
from bzrlib.tests import test_osutils
1852.13.6 by Robert Collins
start hooking in the prototype dirstate serialiser.
32
33
34
# TODO:
2255.2.4 by Robert Collins
Snapshot dirstate development
35
# TESTS to write:
2255.2.15 by Robert Collins
Dirstate - truncate state file fixing bug in saving a smaller file, get more tree_implementation tests passing.
36
# general checks for NOT_IN_MEMORY error conditions.
37
# set_path_id on a NOT_IN_MEMORY dirstate
2255.2.4 by Robert Collins
Snapshot dirstate development
38
# set_path_id  unicode support
39
# set_path_id  setting id of a path not root
40
# set_path_id  setting id when there are parents without the id in the parents
41
# set_path_id  setting id when there are parents with the id in the parents
42
# set_path_id  setting id when state is not in memory
43
# set_path_id  setting id when state is in memory unmodified
44
# set_path_id  setting id when state is in memory modified
1852.13.6 by Robert Collins
start hooking in the prototype dirstate serialiser.
45
2255.2.236 by Martin Pool
Review cleanups: mostly updating or removing todo comments.
46
4241.14.10 by Vincent Ladeuil
Add DirReader parametrized tests.
47
def load_tests(basic_tests, module, loader):
48
    suite = loader.suiteClass()
49
    dir_reader_tests, remaining_tests = tests.split_suite_by_condition(
50
        basic_tests, tests.condition_isinstance(TestCaseWithDirState))
51
    tests.multiply_tests(dir_reader_tests,
52
                         test_osutils.dir_reader_scenarios(), suite)
53
    suite.addTest(remaining_tests)
54
    return suite
55
56
57
class TestCaseWithDirState(tests.TestCaseWithTransport):
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
58
    """Helper functions for creating DirState objects with various content."""
59
4241.14.10 by Vincent Ladeuil
Add DirReader parametrized tests.
60
    # Set by load_tests
61
    _dir_reader_class = None
62
    _native_to_unicode = None # Not used yet
63
64
    def setUp(self):
65
        tests.TestCaseWithTransport.setUp(self)
66
67
        # Save platform specific info and reset it
68
        cur_dir_reader = osutils._selected_dir_reader
69
70
        def restore():
71
            osutils._selected_dir_reader = cur_dir_reader
72
        self.addCleanup(restore)
73
74
        osutils._selected_dir_reader = self._dir_reader_class()
75
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
76
    def create_empty_dirstate(self):
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
77
        """Return a locked but empty dirstate"""
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
78
        state = dirstate.DirState.initialize('dirstate')
79
        return state
80
81
    def create_dirstate_with_root(self):
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
82
        """Return a write-locked state with a single root entry."""
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
83
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
84
        root_entry_direntry = ('', '', 'a-root-value'), [
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
85
            ('d', '', 0, False, packed_stat),
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
86
            ]
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
87
        dirblocks = []
88
        dirblocks.append(('', [root_entry_direntry]))
89
        dirblocks.append(('', []))
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
90
        state = self.create_empty_dirstate()
91
        try:
92
            state._set_data([], dirblocks)
2255.7.78 by Martin Pool
Add DirState._validate and call from the tests
93
            state._validate()
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
94
        except:
95
            state.unlock()
96
            raise
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
97
        return state
98
99
    def create_dirstate_with_root_and_subdir(self):
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
100
        """Return a locked DirState with a root and a subdir"""
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
101
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
102
        subdir_entry = ('', 'subdir', 'subdir-id'), [
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
103
            ('d', '', 0, False, packed_stat),
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
104
            ]
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
105
        state = self.create_dirstate_with_root()
106
        try:
107
            dirblocks = list(state._dirblocks)
108
            dirblocks[1][1].append(subdir_entry)
109
            state._set_data([], dirblocks)
110
        except:
111
            state.unlock()
112
            raise
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
113
        return state
114
115
    def create_complex_dirstate(self):
116
        """This dirstate contains multiple files and directories.
117
118
         /        a-root-value
119
         a/       a-dir
120
         b/       b-dir
121
         c        c-file
122
         d        d-file
123
         a/e/     e-dir
124
         a/f      f-file
125
         b/g      g-file
126
         b/h\xc3\xa5  h-\xc3\xa5-file  #This is u'\xe5' encoded into utf-8
127
2323.5.4 by Martin Pool
Move some dirstate test setup methods into the base class
128
        Notice that a/e is an empty directory.
129
130
        :return: The dirstate, still write-locked.
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
131
        """
132
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
133
        null_sha = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
134
        root_entry = ('', '', 'a-root-value'), [
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
135
            ('d', '', 0, False, packed_stat),
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
136
            ]
137
        a_entry = ('', 'a', 'a-dir'), [
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
138
            ('d', '', 0, False, packed_stat),
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
139
            ]
140
        b_entry = ('', 'b', 'b-dir'), [
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
141
            ('d', '', 0, False, packed_stat),
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
142
            ]
143
        c_entry = ('', 'c', 'c-file'), [
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
144
            ('f', null_sha, 10, False, packed_stat),
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
145
            ]
146
        d_entry = ('', 'd', 'd-file'), [
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
147
            ('f', null_sha, 20, False, packed_stat),
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
148
            ]
149
        e_entry = ('a', 'e', 'e-dir'), [
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
150
            ('d', '', 0, False, packed_stat),
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
151
            ]
152
        f_entry = ('a', 'f', 'f-file'), [
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
153
            ('f', null_sha, 30, False, packed_stat),
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
154
            ]
155
        g_entry = ('b', 'g', 'g-file'), [
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
156
            ('f', null_sha, 30, False, packed_stat),
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
157
            ]
158
        h_entry = ('b', 'h\xc3\xa5', 'h-\xc3\xa5-file'), [
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
159
            ('f', null_sha, 40, False, packed_stat),
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
160
            ]
161
        dirblocks = []
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
162
        dirblocks.append(('', [root_entry]))
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
163
        dirblocks.append(('', [a_entry, b_entry, c_entry, d_entry]))
164
        dirblocks.append(('a', [e_entry, f_entry]))
165
        dirblocks.append(('b', [g_entry, h_entry]))
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
166
        state = dirstate.DirState.initialize('dirstate')
2255.7.78 by Martin Pool
Add DirState._validate and call from the tests
167
        state._validate()
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
168
        try:
169
            state._set_data([], dirblocks)
170
        except:
171
            state.unlock()
172
            raise
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
173
        return state
174
175
    def check_state_with_reopen(self, expected_result, state):
176
        """Check that state has current state expected_result.
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
177
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
178
        This will check the current state, open the file anew and check it
179
        again.
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
180
        This function expects the current state to be locked for writing, and
181
        will unlock it before re-opening.
182
        This is required because we can't open a lock_read() while something
183
        else has a lock_write().
184
            write => mutually exclusive lock
185
            read => shared lock
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
186
        """
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
187
        # The state should already be write locked, since we just had to do
188
        # some operation to get here.
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
189
        self.assertTrue(state._lock_token is not None)
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
190
        try:
191
            self.assertEqual(expected_result[0],  state.get_parent_ids())
192
            # there should be no ghosts in this tree.
193
            self.assertEqual([], state.get_ghosts())
194
            # there should be one fileid in this tree - the root of the tree.
195
            self.assertEqual(expected_result[1], list(state._iter_entries()))
196
            state.save()
197
        finally:
198
            state.unlock()
2425.3.1 by John Arbash Meinel
Change the DirState.test_initialize test so that we don't try to read a locked file.
199
        del state
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
200
        state = dirstate.DirState.on_file('dirstate')
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
201
        state.lock_read()
202
        try:
203
            self.assertEqual(expected_result[1], list(state._iter_entries()))
204
        finally:
205
            state.unlock()
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
206
2323.5.4 by Martin Pool
Move some dirstate test setup methods into the base class
207
    def create_basic_dirstate(self):
208
        """Create a dirstate with a few files and directories.
209
210
            a
211
            b/
212
              c
213
              d/
214
                e
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
215
            b-c
2323.5.4 by Martin Pool
Move some dirstate test setup methods into the base class
216
            f
217
        """
218
        tree = self.make_branch_and_tree('tree')
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
219
        paths = ['a', 'b/', 'b/c', 'b/d/', 'b/d/e', 'b-c', 'f']
220
        file_ids = ['a-id', 'b-id', 'c-id', 'd-id', 'e-id', 'b-c-id', 'f-id']
2323.5.4 by Martin Pool
Move some dirstate test setup methods into the base class
221
        self.build_tree(['tree/' + p for p in paths])
222
        tree.set_root_id('TREE_ROOT')
223
        tree.add([p.rstrip('/') for p in paths], file_ids)
224
        tree.commit('initial', rev_id='rev-1')
225
        revision_id = 'rev-1'
226
        # a_packed_stat = dirstate.pack_stat(os.stat('tree/a'))
2520.3.1 by Vincent Ladeuil
Fix 110448 by adding a relpath parameter to get_transport.
227
        t = self.get_transport('tree')
2323.5.4 by Martin Pool
Move some dirstate test setup methods into the base class
228
        a_text = t.get_bytes('a')
229
        a_sha = osutils.sha_string(a_text)
230
        a_len = len(a_text)
231
        # b_packed_stat = dirstate.pack_stat(os.stat('tree/b'))
232
        # c_packed_stat = dirstate.pack_stat(os.stat('tree/b/c'))
233
        c_text = t.get_bytes('b/c')
234
        c_sha = osutils.sha_string(c_text)
235
        c_len = len(c_text)
236
        # d_packed_stat = dirstate.pack_stat(os.stat('tree/b/d'))
237
        # e_packed_stat = dirstate.pack_stat(os.stat('tree/b/d/e'))
238
        e_text = t.get_bytes('b/d/e')
239
        e_sha = osutils.sha_string(e_text)
240
        e_len = len(e_text)
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
241
        b_c_text = t.get_bytes('b-c')
242
        b_c_sha = osutils.sha_string(b_c_text)
243
        b_c_len = len(b_c_text)
2323.5.4 by Martin Pool
Move some dirstate test setup methods into the base class
244
        # f_packed_stat = dirstate.pack_stat(os.stat('tree/f'))
245
        f_text = t.get_bytes('f')
246
        f_sha = osutils.sha_string(f_text)
247
        f_len = len(f_text)
248
        null_stat = dirstate.DirState.NULLSTAT
249
        expected = {
250
            '':(('', '', 'TREE_ROOT'), [
251
                  ('d', '', 0, False, null_stat),
252
                  ('d', '', 0, False, revision_id),
253
                ]),
254
            'a':(('', 'a', 'a-id'), [
255
                   ('f', '', 0, False, null_stat),
256
                   ('f', a_sha, a_len, False, revision_id),
257
                 ]),
258
            'b':(('', 'b', 'b-id'), [
259
                  ('d', '', 0, False, null_stat),
260
                  ('d', '', 0, False, revision_id),
261
                 ]),
262
            'b/c':(('b', 'c', 'c-id'), [
263
                    ('f', '', 0, False, null_stat),
264
                    ('f', c_sha, c_len, False, revision_id),
265
                   ]),
266
            'b/d':(('b', 'd', 'd-id'), [
267
                    ('d', '', 0, False, null_stat),
268
                    ('d', '', 0, False, revision_id),
269
                   ]),
270
            'b/d/e':(('b/d', 'e', 'e-id'), [
271
                      ('f', '', 0, False, null_stat),
272
                      ('f', e_sha, e_len, False, revision_id),
273
                     ]),
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
274
            'b-c':(('', 'b-c', 'b-c-id'), [
275
                      ('f', '', 0, False, null_stat),
276
                      ('f', b_c_sha, b_c_len, False, revision_id),
277
                     ]),
2323.5.4 by Martin Pool
Move some dirstate test setup methods into the base class
278
            'f':(('', 'f', 'f-id'), [
279
                  ('f', '', 0, False, null_stat),
280
                  ('f', f_sha, f_len, False, revision_id),
281
                 ]),
282
        }
283
        state = dirstate.DirState.from_tree(tree, 'dirstate')
284
        try:
285
            state.save()
286
        finally:
287
            state.unlock()
288
        # Use a different object, to make sure nothing is pre-cached in memory.
289
        state = dirstate.DirState.on_file('dirstate')
290
        state.lock_read()
291
        self.addCleanup(state.unlock)
292
        self.assertEqual(dirstate.DirState.NOT_IN_MEMORY,
293
                         state._dirblock_state)
294
        # This is code is only really tested if we actually have to make more
295
        # than one read, so set the page size to something smaller.
296
        # We want it to contain about 2.2 records, so that we have a couple
297
        # records that we can read per attempt
298
        state._bisect_page_size = 200
299
        return tree, state, expected
300
301
    def create_duplicated_dirstate(self):
302
        """Create a dirstate with a deleted and added entries.
303
304
        This grabs a basic_dirstate, and then removes and re adds every entry
305
        with a new file id.
306
        """
307
        tree, state, expected = self.create_basic_dirstate()
308
        # Now we will just remove and add every file so we get an extra entry
309
        # per entry. Unversion in reverse order so we handle subdirs
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
310
        tree.unversion(['f-id', 'b-c-id', 'e-id', 'd-id', 'c-id', 'b-id', 'a-id'])
311
        tree.add(['a', 'b', 'b/c', 'b/d', 'b/d/e', 'b-c', 'f'],
312
                 ['a-id2', 'b-id2', 'c-id2', 'd-id2', 'e-id2', 'b-c-id2', 'f-id2'])
2323.5.4 by Martin Pool
Move some dirstate test setup methods into the base class
313
314
        # Update the expected dictionary.
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
315
        for path in ['a', 'b', 'b/c', 'b/d', 'b/d/e', 'b-c', 'f']:
2323.5.4 by Martin Pool
Move some dirstate test setup methods into the base class
316
            orig = expected[path]
317
            path2 = path + '2'
318
            # This record was deleted in the current tree
319
            expected[path] = (orig[0], [dirstate.DirState.NULL_PARENT_DETAILS,
320
                                        orig[1][1]])
321
            new_key = (orig[0][0], orig[0][1], orig[0][2]+'2')
322
            # And didn't exist in the basis tree
323
            expected[path2] = (new_key, [orig[1][0],
324
                                         dirstate.DirState.NULL_PARENT_DETAILS])
325
326
        # We will replace the 'dirstate' file underneath 'state', but that is
327
        # okay as lock as we unlock 'state' first.
328
        state.unlock()
329
        try:
330
            new_state = dirstate.DirState.from_tree(tree, 'dirstate')
331
            try:
332
                new_state.save()
333
            finally:
334
                new_state.unlock()
335
        finally:
336
            # But we need to leave state in a read-lock because we already have
337
            # a cleanup scheduled
338
            state.lock_read()
339
        return tree, state, expected
340
341
    def create_renamed_dirstate(self):
342
        """Create a dirstate with a few internal renames.
343
344
        This takes the basic dirstate, and moves the paths around.
345
        """
346
        tree, state, expected = self.create_basic_dirstate()
347
        # Rename a file
348
        tree.rename_one('a', 'b/g')
349
        # And a directory
350
        tree.rename_one('b/d', 'h')
351
352
        old_a = expected['a']
353
        expected['a'] = (old_a[0], [('r', 'b/g', 0, False, ''), old_a[1][1]])
354
        expected['b/g'] = (('b', 'g', 'a-id'), [old_a[1][0],
355
                                                ('r', 'a', 0, False, '')])
356
        old_d = expected['b/d']
357
        expected['b/d'] = (old_d[0], [('r', 'h', 0, False, ''), old_d[1][1]])
358
        expected['h'] = (('', 'h', 'd-id'), [old_d[1][0],
359
                                             ('r', 'b/d', 0, False, '')])
360
361
        old_e = expected['b/d/e']
362
        expected['b/d/e'] = (old_e[0], [('r', 'h/e', 0, False, ''),
363
                             old_e[1][1]])
364
        expected['h/e'] = (('h', 'e', 'e-id'), [old_e[1][0],
365
                                                ('r', 'b/d/e', 0, False, '')])
366
367
        state.unlock()
368
        try:
369
            new_state = dirstate.DirState.from_tree(tree, 'dirstate')
370
            try:
371
                new_state.save()
372
            finally:
373
                new_state.unlock()
374
        finally:
375
            state.lock_read()
376
        return tree, state, expected
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
377
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
378
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
379
class TestTreeToDirState(TestCaseWithDirState):
1852.13.6 by Robert Collins
start hooking in the prototype dirstate serialiser.
380
381
    def test_empty_to_dirstate(self):
382
        """We should be able to create a dirstate for an empty tree."""
383
        # There are no files on disk and no parents
384
        tree = self.make_branch_and_tree('tree')
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
385
        expected_result = ([], [
2946.3.3 by John Arbash Meinel
Prefer tree.get_root_id() as more explicit than tree.path2id('')
386
            (('', '', tree.get_root_id()), # common details
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
387
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
388
             ])])
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
389
        state = dirstate.DirState.from_tree(tree, 'dirstate')
2255.7.78 by Martin Pool
Add DirState._validate and call from the tests
390
        state._validate()
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
391
        self.check_state_with_reopen(expected_result, state)
1852.13.6 by Robert Collins
start hooking in the prototype dirstate serialiser.
392
393
    def test_1_parents_empty_to_dirstate(self):
394
        # create a parent by doing a commit
395
        tree = self.make_branch_and_tree('tree')
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
396
        rev_id = tree.commit('first post').encode('utf8')
397
        root_stat_pack = dirstate.pack_stat(os.stat(tree.basedir))
398
        expected_result = ([rev_id], [
2946.3.3 by John Arbash Meinel
Prefer tree.get_root_id() as more explicit than tree.path2id('')
399
            (('', '', tree.get_root_id()), # common details
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
400
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
401
              ('d', '', 0, False, rev_id), # first parent details
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
402
             ])])
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
403
        state = dirstate.DirState.from_tree(tree, 'dirstate')
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
404
        self.check_state_with_reopen(expected_result, state)
2323.6.13 by Martin Pool
Fix some tests that need to lock dirstate before validating
405
        state.lock_read()
406
        try:
407
            state._validate()
408
        finally:
409
            state.unlock()
1852.13.6 by Robert Collins
start hooking in the prototype dirstate serialiser.
410
411
    def test_2_parents_empty_to_dirstate(self):
412
        # create a parent by doing a commit
413
        tree = self.make_branch_and_tree('tree')
414
        rev_id = tree.commit('first post')
415
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
1852.13.24 by Robert Collins
Get back to the broken-pending-revision-tree-from-dirstate state of development, changing dirstate from_tree to use _set_data rather than generating lines itself.
416
        rev_id2 = tree2.commit('second post', allow_pointless=True)
1852.13.19 by Robert Collins
Get DirState objects roundtripping an add of a ghost tree.
417
        tree.merge_from_branch(tree2.branch)
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
418
        expected_result = ([rev_id, rev_id2], [
2946.3.3 by John Arbash Meinel
Prefer tree.get_root_id() as more explicit than tree.path2id('')
419
            (('', '', tree.get_root_id()), # common details
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
420
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
421
              ('d', '', 0, False, rev_id), # first parent details
422
              ('d', '', 0, False, rev_id2), # second parent details
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
423
             ])])
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
424
        state = dirstate.DirState.from_tree(tree, 'dirstate')
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
425
        self.check_state_with_reopen(expected_result, state)
2323.6.13 by Martin Pool
Fix some tests that need to lock dirstate before validating
426
        state.lock_read()
427
        try:
428
            state._validate()
429
        finally:
430
            state.unlock()
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
431
1852.13.6 by Robert Collins
start hooking in the prototype dirstate serialiser.
432
    def test_empty_unknowns_are_ignored_to_dirstate(self):
433
        """We should be able to create a dirstate for an empty tree."""
434
        # There are no files on disk and no parents
435
        tree = self.make_branch_and_tree('tree')
1852.13.10 by Robert Collins
Use just the tree api to generate dirstate information.
436
        self.build_tree(['tree/unknown'])
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
437
        expected_result = ([], [
2946.3.3 by John Arbash Meinel
Prefer tree.get_root_id() as more explicit than tree.path2id('')
438
            (('', '', tree.get_root_id()), # common details
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
439
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
440
             ])])
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
441
        state = dirstate.DirState.from_tree(tree, 'dirstate')
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
442
        self.check_state_with_reopen(expected_result, state)
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
443
1852.13.12 by Robert Collins
get actual parent info for the first parent.
444
    def get_tree_with_a_file(self):
445
        tree = self.make_branch_and_tree('tree')
446
        self.build_tree(['tree/a file'])
3010.1.2 by Robert Collins
Use valid file-ids for dirstate tests.
447
        tree.add('a file', 'a-file-id')
1852.13.12 by Robert Collins
get actual parent info for the first parent.
448
        return tree
449
1852.13.6 by Robert Collins
start hooking in the prototype dirstate serialiser.
450
    def test_non_empty_no_parents_to_dirstate(self):
451
        """We should be able to create a dirstate for an empty tree."""
1852.13.11 by Robert Collins
Get one content containing test passing.
452
        # There are files on disk and no parents
1852.13.12 by Robert Collins
get actual parent info for the first parent.
453
        tree = self.get_tree_with_a_file()
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
454
        expected_result = ([], [
2946.3.3 by John Arbash Meinel
Prefer tree.get_root_id() as more explicit than tree.path2id('')
455
            (('', '', tree.get_root_id()), # common details
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
456
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
457
             ]),
3010.1.2 by Robert Collins
Use valid file-ids for dirstate tests.
458
            (('', 'a file', 'a-file-id'), # common
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
459
             [('f', '', 0, False, dirstate.DirState.NULLSTAT), # current
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
460
             ]),
461
            ])
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
462
        state = dirstate.DirState.from_tree(tree, 'dirstate')
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
463
        self.check_state_with_reopen(expected_result, state)
1852.13.6 by Robert Collins
start hooking in the prototype dirstate serialiser.
464
465
    def test_1_parents_not_empty_to_dirstate(self):
466
        # create a parent by doing a commit
1852.13.12 by Robert Collins
get actual parent info for the first parent.
467
        tree = self.get_tree_with_a_file()
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
468
        rev_id = tree.commit('first post').encode('utf8')
1852.13.12 by Robert Collins
get actual parent info for the first parent.
469
        # change the current content to be different this will alter stat, sha
470
        # and length:
471
        self.build_tree_contents([('tree/a file', 'new content\n')])
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
472
        expected_result = ([rev_id], [
2946.3.3 by John Arbash Meinel
Prefer tree.get_root_id() as more explicit than tree.path2id('')
473
            (('', '', tree.get_root_id()), # common details
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
474
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
475
              ('d', '', 0, False, rev_id), # first parent details
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
476
             ]),
3010.1.2 by Robert Collins
Use valid file-ids for dirstate tests.
477
            (('', 'a file', 'a-file-id'), # common
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
478
             [('f', '', 0, False, dirstate.DirState.NULLSTAT), # current
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
479
              ('f', 'c3ed76e4bfd45ff1763ca206055bca8e9fc28aa8', 24, False,
480
               rev_id), # first parent
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
481
             ]),
482
            ])
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
483
        state = dirstate.DirState.from_tree(tree, 'dirstate')
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
484
        self.check_state_with_reopen(expected_result, state)
1852.13.6 by Robert Collins
start hooking in the prototype dirstate serialiser.
485
486
    def test_2_parents_not_empty_to_dirstate(self):
487
        # create a parent by doing a commit
1852.13.13 by Robert Collins
2-parent case working.
488
        tree = self.get_tree_with_a_file()
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
489
        rev_id = tree.commit('first post').encode('utf8')
1852.13.6 by Robert Collins
start hooking in the prototype dirstate serialiser.
490
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
1852.13.13 by Robert Collins
2-parent case working.
491
        # change the current content to be different this will alter stat, sha
492
        # and length:
493
        self.build_tree_contents([('tree2/a file', 'merge content\n')])
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
494
        rev_id2 = tree2.commit('second post').encode('utf8')
1852.13.19 by Robert Collins
Get DirState objects roundtripping an add of a ghost tree.
495
        tree.merge_from_branch(tree2.branch)
1852.13.13 by Robert Collins
2-parent case working.
496
        # change the current content to be different this will alter stat, sha
497
        # and length again, giving us three distinct values:
498
        self.build_tree_contents([('tree/a file', 'new content\n')])
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
499
        expected_result = ([rev_id, rev_id2], [
2946.3.3 by John Arbash Meinel
Prefer tree.get_root_id() as more explicit than tree.path2id('')
500
            (('', '', tree.get_root_id()), # common details
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
501
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
502
              ('d', '', 0, False, rev_id), # first parent details
503
              ('d', '', 0, False, rev_id2), # second parent details
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
504
             ]),
3010.1.2 by Robert Collins
Use valid file-ids for dirstate tests.
505
            (('', 'a file', 'a-file-id'), # common
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
506
             [('f', '', 0, False, dirstate.DirState.NULLSTAT), # current
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
507
              ('f', 'c3ed76e4bfd45ff1763ca206055bca8e9fc28aa8', 24, False,
508
               rev_id), # first parent
509
              ('f', '314d796174c9412647c3ce07dfb5d36a94e72958', 14, False,
510
               rev_id2), # second parent
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
511
             ]),
512
            ])
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
513
        state = dirstate.DirState.from_tree(tree, 'dirstate')
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
514
        self.check_state_with_reopen(expected_result, state)
515
2255.7.94 by Martin Pool
Fix dirstate sorting bug and refine the _validate() assertions:
516
    def test_colliding_fileids(self):
517
        # test insertion of parents creating several entries at the same path.
518
        # we used to have a bug where they could cause the dirstate to break
519
        # its ordering invariants.
520
        # create some trees to test from
521
        parents = []
522
        for i in range(7):
523
            tree = self.make_branch_and_tree('tree%d' % i)
524
            self.build_tree(['tree%d/name' % i,])
525
            tree.add(['name'], ['file-id%d' % i])
526
            revision_id = 'revid-%d' % i
527
            tree.commit('message', rev_id=revision_id)
528
            parents.append((revision_id,
529
                tree.branch.repository.revision_tree(revision_id)))
530
        # now fold these trees into a dirstate
531
        state = dirstate.DirState.initialize('dirstate')
532
        try:
533
            state.set_parent_trees(parents, [])
534
            state._validate()
535
        finally:
536
            state.unlock()
537
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
538
539
class TestDirStateOnFile(TestCaseWithDirState):
1852.13.15 by Robert Collins
Ensure Format4 working trees start with a dirstate.
540
541
    def test_construct_with_path(self):
542
        tree = self.make_branch_and_tree('tree')
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
543
        state = dirstate.DirState.from_tree(tree, 'dirstate.from_tree')
1852.13.15 by Robert Collins
Ensure Format4 working trees start with a dirstate.
544
        # we want to be able to get the lines of the dirstate that we will
545
        # write to disk.
546
        lines = state.get_lines()
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
547
        state.unlock()
1852.13.15 by Robert Collins
Ensure Format4 working trees start with a dirstate.
548
        self.build_tree_contents([('dirstate', ''.join(lines))])
549
        # get a state object
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
550
        # no parents, default tree content
551
        expected_result = ([], [
2946.3.3 by John Arbash Meinel
Prefer tree.get_root_id() as more explicit than tree.path2id('')
552
            (('', '', tree.get_root_id()), # common details
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
553
             # current tree details, but new from_tree skips statting, it
554
             # uses set_state_from_inventory, and thus depends on the
555
             # inventory state.
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
556
             [('d', '', 0, False, dirstate.DirState.NULLSTAT),
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
557
             ])
558
            ])
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
559
        state = dirstate.DirState.on_file('dirstate')
560
        state.lock_write() # check_state_with_reopen will save() and unlock it
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
561
        self.check_state_with_reopen(expected_result, state)
562
563
    def test_can_save_clean_on_file(self):
564
        tree = self.make_branch_and_tree('tree')
565
        state = dirstate.DirState.from_tree(tree, 'dirstate')
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
566
        try:
567
            # doing a save should work here as there have been no changes.
568
            state.save()
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
569
            # TODO: stat it and check it hasn't changed; may require waiting
570
            # for the state accuracy window.
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
571
        finally:
572
            state.unlock()
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
573
2353.4.5 by John Arbash Meinel
Update DirState to use the new 'temporary_write_lock', and add tests that it works.
574
    def test_can_save_in_read_lock(self):
575
        self.build_tree(['a-file'])
576
        state = dirstate.DirState.initialize('dirstate')
577
        try:
578
            # No stat and no sha1 sum.
579
            state.add('a-file', 'a-file-id', 'file', None, '')
580
            state.save()
581
        finally:
582
            state.unlock()
583
584
        # Now open in readonly mode
585
        state = dirstate.DirState.on_file('dirstate')
586
        state.lock_read()
587
        try:
588
            entry = state._get_entry(0, path_utf8='a-file')
3709.4.1 by Robert Collins
Sha files for the stat cache more lazily rather than on first-examination, allowing less overall sha calculations to occur.
589
            # The current size should be 0 (default)
590
            self.assertEqual(0, entry[1][0][2])
2353.4.5 by John Arbash Meinel
Update DirState to use the new 'temporary_write_lock', and add tests that it works.
591
            # We should have a real entry.
592
            self.assertNotEqual((None, None), entry)
2485.3.14 by John Arbash Meinel
Update the code so that symlinks aren't cached at incorrect times
593
            # Make sure everything is old enough
594
            state._sha_cutoff_time()
595
            state._cutoff_time += 10
3709.4.1 by Robert Collins
Sha files for the stat cache more lazily rather than on first-examination, allowing less overall sha calculations to occur.
596
            # Change the file length
597
            self.build_tree_contents([('a-file', 'shorter')])
3696.5.2 by Robert Collins
Integrate less aggressive sha logic with C iter-changes.
598
            sha1sum = dirstate.update_entry(state, entry, 'a-file',
599
                os.lstat('a-file'))
3709.4.1 by Robert Collins
Sha files for the stat cache more lazily rather than on first-examination, allowing less overall sha calculations to occur.
600
            # new file, no cached sha:
601
            self.assertEqual(None, sha1sum)
2353.4.5 by John Arbash Meinel
Update DirState to use the new 'temporary_write_lock', and add tests that it works.
602
603
            # The dirblock has been updated
3709.4.1 by Robert Collins
Sha files for the stat cache more lazily rather than on first-examination, allowing less overall sha calculations to occur.
604
            self.assertEqual(7, entry[1][0][2])
2353.4.5 by John Arbash Meinel
Update DirState to use the new 'temporary_write_lock', and add tests that it works.
605
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
606
                             state._dirblock_state)
607
608
            del entry
609
            # Now, since we are the only one holding a lock, we should be able
610
            # to save and have it written to disk
611
            state.save()
612
        finally:
613
            state.unlock()
614
615
        # Re-open the file, and ensure that the state has been updated.
616
        state = dirstate.DirState.on_file('dirstate')
617
        state.lock_read()
618
        try:
619
            entry = state._get_entry(0, path_utf8='a-file')
3709.4.1 by Robert Collins
Sha files for the stat cache more lazily rather than on first-examination, allowing less overall sha calculations to occur.
620
            self.assertEqual(7, entry[1][0][2])
2353.4.5 by John Arbash Meinel
Update DirState to use the new 'temporary_write_lock', and add tests that it works.
621
        finally:
622
            state.unlock()
623
624
    def test_save_fails_quietly_if_locked(self):
625
        """If dirstate is locked, save will fail without complaining."""
626
        self.build_tree(['a-file'])
627
        state = dirstate.DirState.initialize('dirstate')
628
        try:
629
            # No stat and no sha1 sum.
630
            state.add('a-file', 'a-file-id', 'file', None, '')
631
            state.save()
632
        finally:
633
            state.unlock()
634
635
        state = dirstate.DirState.on_file('dirstate')
636
        state.lock_read()
637
        try:
638
            entry = state._get_entry(0, path_utf8='a-file')
3696.5.2 by Robert Collins
Integrate less aggressive sha logic with C iter-changes.
639
            sha1sum = dirstate.update_entry(state, entry, 'a-file',
640
                os.lstat('a-file'))
3709.4.1 by Robert Collins
Sha files for the stat cache more lazily rather than on first-examination, allowing less overall sha calculations to occur.
641
            # No sha - too new
642
            self.assertEqual(None, sha1sum)
2353.4.5 by John Arbash Meinel
Update DirState to use the new 'temporary_write_lock', and add tests that it works.
643
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
644
                             state._dirblock_state)
645
646
            # Now, before we try to save, grab another dirstate, and take out a
647
            # read lock.
648
            # TODO: jam 20070315 Ideally this would be locked by another
649
            #       process. To make sure the file is really OS locked.
650
            state2 = dirstate.DirState.on_file('dirstate')
651
            state2.lock_read()
652
            try:
653
                # This won't actually write anything, because it couldn't grab
654
                # a write lock. But it shouldn't raise an error, either.
655
                # TODO: jam 20070315 We should probably distinguish between
656
                #       being dirty because of 'update_entry'. And dirty
657
                #       because of real modification. So that save() *does*
658
                #       raise a real error if it fails when we have real
659
                #       modifications.
660
                state.save()
661
            finally:
662
                state2.unlock()
663
        finally:
664
            state.unlock()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
665
2353.4.5 by John Arbash Meinel
Update DirState to use the new 'temporary_write_lock', and add tests that it works.
666
        # The file on disk should not be modified.
667
        state = dirstate.DirState.on_file('dirstate')
668
        state.lock_read()
669
        try:
670
            entry = state._get_entry(0, path_utf8='a-file')
671
            self.assertEqual('', entry[1][0][1])
672
        finally:
673
            state.unlock()
674
3221.1.7 by Martin Pool
Update and rename test for Dirstate._changes_aborted
675
    def test_save_refuses_if_changes_aborted(self):
3207.2.2 by John Arbash Meinel
Fix bug #187169, when an invalid delta is supplied to update_basis_by_delta
676
        self.build_tree(['a-file', 'a-dir/'])
677
        state = dirstate.DirState.initialize('dirstate')
678
        try:
679
            # No stat and no sha1 sum.
680
            state.add('a-file', 'a-file-id', 'file', None, '')
681
            state.save()
682
        finally:
683
            state.unlock()
684
685
        # The dirstate should include TREE_ROOT and 'a-file' and nothing else
686
        expected_blocks = [
687
            ('', [(('', '', 'TREE_ROOT'),
688
                   [('d', '', 0, False, dirstate.DirState.NULLSTAT)])]),
689
            ('', [(('', 'a-file', 'a-file-id'),
690
                   [('f', '', 0, False, dirstate.DirState.NULLSTAT)])]),
691
        ]
692
693
        state = dirstate.DirState.on_file('dirstate')
694
        state.lock_write()
695
        try:
696
            state._read_dirblocks_if_needed()
697
            self.assertEqual(expected_blocks, state._dirblocks)
698
699
            # Now modify the state, but mark it as inconsistent
700
            state.add('a-dir', 'a-dir-id', 'directory', None, '')
3221.1.7 by Martin Pool
Update and rename test for Dirstate._changes_aborted
701
            state._changes_aborted = True
3207.2.2 by John Arbash Meinel
Fix bug #187169, when an invalid delta is supplied to update_basis_by_delta
702
            state.save()
703
        finally:
704
            state.unlock()
705
706
        state = dirstate.DirState.on_file('dirstate')
707
        state.lock_read()
708
        try:
709
            state._read_dirblocks_if_needed()
710
            self.assertEqual(expected_blocks, state._dirblocks)
711
        finally:
712
            state.unlock()
713
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
714
715
class TestDirStateInitialize(TestCaseWithDirState):
1852.13.15 by Robert Collins
Ensure Format4 working trees start with a dirstate.
716
717
    def test_initialize(self):
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
718
        expected_result = ([], [
719
            (('', '', 'TREE_ROOT'), # common details
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
720
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
721
             ])
722
            ])
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
723
        state = dirstate.DirState.initialize('dirstate')
724
        try:
725
            self.assertIsInstance(state, dirstate.DirState)
726
            lines = state.get_lines()
2425.3.1 by John Arbash Meinel
Change the DirState.test_initialize test so that we don't try to read a locked file.
727
        finally:
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
728
            state.unlock()
2425.3.1 by John Arbash Meinel
Change the DirState.test_initialize test so that we don't try to read a locked file.
729
        # On win32 you can't read from a locked file, even within the same
730
        # process. So we have to unlock and release before we check the file
731
        # contents.
732
        self.assertFileEqual(''.join(lines), 'dirstate')
733
        state.lock_read() # check_state_with_reopen will unlock
734
        self.check_state_with_reopen(expected_result, state)
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
735
736
737
class TestDirStateManipulations(TestCaseWithDirState):
1852.13.19 by Robert Collins
Get DirState objects roundtripping an add of a ghost tree.
738
2255.2.16 by Robert Collins
Implement WorkingTreeFormat4._write_inventory for better compatability with existing code, letting more test_test_trees pass, now up to test_tree_with_subdirs_and_all_content_types.
739
    def test_set_state_from_inventory_no_content_no_parents(self):
740
        # setting the current inventory is a slow but important api to support.
741
        tree1 = self.make_branch_and_memory_tree('tree1')
742
        tree1.lock_write()
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
743
        try:
744
            tree1.add('')
745
            revid1 = tree1.commit('foo').encode('utf8')
2946.3.3 by John Arbash Meinel
Prefer tree.get_root_id() as more explicit than tree.path2id('')
746
            root_id = tree1.get_root_id()
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
747
            inv = tree1.inventory
748
        finally:
749
            tree1.unlock()
2255.2.87 by Robert Collins
core dirstate tests passing with new structure.
750
        expected_result = [], [
751
            (('', '', root_id), [
2255.2.124 by John Arbash Meinel
Remove direct access to Dirstate prefering dirstate.Dirstate
752
             ('d', '', 0, False, dirstate.DirState.NULLSTAT)])]
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
753
        state = dirstate.DirState.initialize('dirstate')
754
        try:
755
            state.set_state_from_inventory(inv)
2255.2.124 by John Arbash Meinel
Remove direct access to Dirstate prefering dirstate.Dirstate
756
            self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
757
                             state._header_state)
2255.2.124 by John Arbash Meinel
Remove direct access to Dirstate prefering dirstate.Dirstate
758
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
759
                             state._dirblock_state)
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
760
        except:
761
            state.unlock()
762
            raise
763
        else:
764
            # This will unlock it
765
            self.check_state_with_reopen(expected_result, state)
2255.2.16 by Robert Collins
Implement WorkingTreeFormat4._write_inventory for better compatability with existing code, letting more test_test_trees pass, now up to test_tree_with_subdirs_and_all_content_types.
766
2872.4.3 by Martin Pool
Fix comparison for merge sort in Dirstate.set_state_from_inventory
767
    def test_set_state_from_inventory_preserves_hashcache(self):
2872.4.11 by Martin Pool
Review documentation cleanups
768
        # https://bugs.launchpad.net/bzr/+bug/146176
2872.4.1 by Martin Pool
Add xfail test for #146176
769
        # set_state_from_inventory should preserve the stat and hash value for
770
        # workingtree files that are not changed by the inventory.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
771
2872.4.1 by Martin Pool
Add xfail test for #146176
772
        tree = self.make_branch_and_tree('.')
773
        # depends on the default format using dirstate...
774
        tree.lock_write()
775
        try:
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
776
            # make a dirstate with some valid hashcache data
2872.4.1 by Martin Pool
Add xfail test for #146176
777
            # file on disk, but that's not needed for this test
778
            foo_contents = 'contents of foo'
779
            self.build_tree_contents([('foo', foo_contents)])
780
            tree.add('foo', 'foo-id')
781
782
            foo_stat = os.stat('foo')
783
            foo_packed = dirstate.pack_stat(foo_stat)
784
            foo_sha = osutils.sha_string(foo_contents)
785
            foo_size = len(foo_contents)
786
787
            # should not be cached yet, because the file's too fresh
2872.4.8 by Martin Pool
Clear up test code
788
            self.assertEqual(
789
                (('', 'foo', 'foo-id',),
790
                 [('f', '', 0, False, dirstate.DirState.NULLSTAT)]),
791
                tree._dirstate._get_entry(0, 'foo-id'))
2872.4.1 by Martin Pool
Add xfail test for #146176
792
            # poke in some hashcache information - it wouldn't normally be
793
            # stored because it's too fresh
794
            tree._dirstate.update_minimal(
795
                ('', 'foo', 'foo-id'),
796
                'f', False, foo_sha, foo_packed, foo_size, 'foo')
797
            # now should be cached
2872.4.8 by Martin Pool
Clear up test code
798
            self.assertEqual(
799
                (('', 'foo', 'foo-id',),
800
                 [('f', foo_sha, foo_size, False, foo_packed)]),
801
                tree._dirstate._get_entry(0, 'foo-id'))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
802
2872.4.1 by Martin Pool
Add xfail test for #146176
803
            # extract the inventory, and add something to it
804
            inv = tree._get_inventory()
2872.4.3 by Martin Pool
Fix comparison for merge sort in Dirstate.set_state_from_inventory
805
            # should see the file we poked in...
806
            self.assertTrue(inv.has_id('foo-id'))
807
            self.assertTrue(inv.has_filename('foo'))
2872.4.1 by Martin Pool
Add xfail test for #146176
808
            inv.add_path('bar', 'file', 'bar-id')
2872.4.13 by Martin Pool
Validate dirstate during tests
809
            tree._dirstate._validate()
2872.4.11 by Martin Pool
Review documentation cleanups
810
            # this used to cause it to lose its hashcache
2872.4.1 by Martin Pool
Add xfail test for #146176
811
            tree._dirstate.set_state_from_inventory(inv)
2872.4.13 by Martin Pool
Validate dirstate during tests
812
            tree._dirstate._validate()
2872.4.1 by Martin Pool
Add xfail test for #146176
813
        finally:
814
            tree.unlock()
815
816
        tree.lock_read()
817
        try:
818
            # now check that the state still has the original hashcache value
819
            state = tree._dirstate
2872.4.13 by Martin Pool
Validate dirstate during tests
820
            state._validate()
2872.4.1 by Martin Pool
Add xfail test for #146176
821
            foo_tuple = state._get_entry(0, path_utf8='foo')
2872.4.3 by Martin Pool
Fix comparison for merge sort in Dirstate.set_state_from_inventory
822
            self.assertEqual(
2872.4.1 by Martin Pool
Add xfail test for #146176
823
                (('', 'foo', 'foo-id',),
824
                 [('f', foo_sha, len(foo_contents), False,
825
                   dirstate.pack_stat(foo_stat))]),
826
                foo_tuple)
827
        finally:
828
            tree.unlock()
829
830
2487.1.1 by John Arbash Meinel
Adding a (broken) test that set_state_from_inventory works
831
    def test_set_state_from_inventory_mixed_paths(self):
832
        tree1 = self.make_branch_and_tree('tree1')
833
        self.build_tree(['tree1/a/', 'tree1/a/b/', 'tree1/a-b/',
834
                         'tree1/a/b/foo', 'tree1/a-b/bar'])
835
        tree1.lock_write()
836
        try:
837
            tree1.add(['a', 'a/b', 'a-b', 'a/b/foo', 'a-b/bar'],
838
                      ['a-id', 'b-id', 'a-b-id', 'foo-id', 'bar-id'])
839
            tree1.commit('rev1', rev_id='rev1')
840
            root_id = tree1.get_root_id()
841
            inv = tree1.inventory
842
        finally:
843
            tree1.unlock()
844
        expected_result1 = [('', '', root_id, 'd'),
845
                            ('', 'a', 'a-id', 'd'),
846
                            ('', 'a-b', 'a-b-id', 'd'),
847
                            ('a', 'b', 'b-id', 'd'),
848
                            ('a/b', 'foo', 'foo-id', 'f'),
849
                            ('a-b', 'bar', 'bar-id', 'f'),
850
                           ]
851
        expected_result2 = [('', '', root_id, 'd'),
852
                            ('', 'a', 'a-id', 'd'),
853
                            ('', 'a-b', 'a-b-id', 'd'),
854
                            ('a-b', 'bar', 'bar-id', 'f'),
855
                           ]
856
        state = dirstate.DirState.initialize('dirstate')
857
        try:
858
            state.set_state_from_inventory(inv)
859
            values = []
860
            for entry in state._iter_entries():
861
                values.append(entry[0] + entry[1][0][:1])
862
            self.assertEqual(expected_result1, values)
863
            del inv['b-id']
864
            state.set_state_from_inventory(inv)
865
            values = []
866
            for entry in state._iter_entries():
867
                values.append(entry[0] + entry[1][0][:1])
868
            self.assertEqual(expected_result2, values)
869
        finally:
870
            state.unlock()
871
2255.2.4 by Robert Collins
Snapshot dirstate development
872
    def test_set_path_id_no_parents(self):
873
        """The id of a path can be changed trivally with no parents."""
874
        state = dirstate.DirState.initialize('dirstate')
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
875
        try:
876
            # check precondition to be sure the state does change appropriately.
877
            self.assertEqual(
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
878
                [(('', '', 'TREE_ROOT'), [('d', '', 0, False,
879
                   'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')])],
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
880
                list(state._iter_entries()))
881
            state.set_path_id('', 'foobarbaz')
882
            expected_rows = [
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
883
                (('', '', 'foobarbaz'), [('d', '', 0, False,
884
                   'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')])]
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
885
            self.assertEqual(expected_rows, list(state._iter_entries()))
886
            # should work across save too
887
            state.save()
888
        finally:
889
            state.unlock()
2255.2.15 by Robert Collins
Dirstate - truncate state file fixing bug in saving a smaller file, get more tree_implementation tests passing.
890
        state = dirstate.DirState.on_file('dirstate')
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
891
        state.lock_read()
892
        try:
2323.6.13 by Martin Pool
Fix some tests that need to lock dirstate before validating
893
            state._validate()
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
894
            self.assertEqual(expected_rows, list(state._iter_entries()))
895
        finally:
896
            state.unlock()
2255.2.4 by Robert Collins
Snapshot dirstate development
897
2255.7.68 by Martin Pool
Add a test for setting the root id in a dirstate with parent trees
898
    def test_set_path_id_with_parents(self):
899
        """Set the root file id in a dirstate with parents"""
900
        mt = self.make_branch_and_tree('mt')
2255.2.178 by Martin Pool
test_set_path_id_with_parents shouldn't depend on tree default root id
901
        # in case the default tree format uses a different root id
902
        mt.set_root_id('TREE_ROOT')
2255.7.68 by Martin Pool
Add a test for setting the root id in a dirstate with parent trees
903
        mt.commit('foo', rev_id='parent-revid')
904
        rt = mt.branch.repository.revision_tree('parent-revid')
905
        state = dirstate.DirState.initialize('dirstate')
2255.7.78 by Martin Pool
Add DirState._validate and call from the tests
906
        state._validate()
2255.7.68 by Martin Pool
Add a test for setting the root id in a dirstate with parent trees
907
        try:
908
            state.set_parent_trees([('parent-revid', rt)], ghosts=[])
909
            state.set_path_id('', 'foobarbaz')
2255.2.177 by Martin Pool
merge dirstate sorting fix, add more validation tests
910
            state._validate()
2255.7.68 by Martin Pool
Add a test for setting the root id in a dirstate with parent trees
911
            # now see that it is what we expected
912
            expected_rows = [
913
                (('', '', 'TREE_ROOT'),
914
                    [('a', '', 0, False, ''),
915
                     ('d', '', 0, False, 'parent-revid'),
916
                     ]),
917
                (('', '', 'foobarbaz'),
918
                    [('d', '', 0, False, ''),
919
                     ('a', '', 0, False, ''),
920
                     ]),
921
                ]
2255.11.2 by Martin Pool
Add more dirstate root-id-changing tests
922
            state._validate()
2255.7.68 by Martin Pool
Add a test for setting the root id in a dirstate with parent trees
923
            self.assertEqual(expected_rows, list(state._iter_entries()))
924
            # should work across save too
925
            state.save()
926
        finally:
927
            state.unlock()
928
        # now flush & check we get the same
929
        state = dirstate.DirState.on_file('dirstate')
930
        state.lock_read()
931
        try:
2255.11.2 by Martin Pool
Add more dirstate root-id-changing tests
932
            state._validate()
2255.7.68 by Martin Pool
Add a test for setting the root id in a dirstate with parent trees
933
            self.assertEqual(expected_rows, list(state._iter_entries()))
934
        finally:
935
            state.unlock()
2255.11.2 by Martin Pool
Add more dirstate root-id-changing tests
936
        # now change within an existing file-backed state
937
        state.lock_write()
938
        try:
939
            state._validate()
940
            state.set_path_id('', 'tree-root-2')
941
            state._validate()
942
        finally:
943
            state.unlock()
944
2255.7.68 by Martin Pool
Add a test for setting the root id in a dirstate with parent trees
945
1852.13.24 by Robert Collins
Get back to the broken-pending-revision-tree-from-dirstate state of development, changing dirstate from_tree to use _set_data rather than generating lines itself.
946
    def test_set_parent_trees_no_content(self):
947
        # set_parent_trees is a slow but important api to support.
948
        tree1 = self.make_branch_and_memory_tree('tree1')
2255.2.2 by Robert Collins
Partial updates for API changes in trunk.
949
        tree1.lock_write()
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
950
        try:
951
            tree1.add('')
952
            revid1 = tree1.commit('foo')
953
        finally:
954
            tree1.unlock()
2255.2.4 by Robert Collins
Snapshot dirstate development
955
        branch2 = tree1.branch.bzrdir.clone('tree2').open_branch()
4241.14.10 by Vincent Ladeuil
Add DirReader parametrized tests.
956
        tree2 = memorytree.MemoryTree.create_on_branch(branch2)
2255.2.2 by Robert Collins
Partial updates for API changes in trunk.
957
        tree2.lock_write()
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
958
        try:
959
            revid2 = tree2.commit('foo')
2946.3.3 by John Arbash Meinel
Prefer tree.get_root_id() as more explicit than tree.path2id('')
960
            root_id = tree2.get_root_id()
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
961
        finally:
962
            tree2.unlock()
963
        state = dirstate.DirState.initialize('dirstate')
964
        try:
965
            state.set_path_id('', root_id)
966
            state.set_parent_trees(
967
                ((revid1, tree1.branch.repository.revision_tree(revid1)),
968
                 (revid2, tree2.branch.repository.revision_tree(revid2)),
969
                 ('ghost-rev', None)),
970
                ['ghost-rev'])
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
971
            # check we can reopen and use the dirstate after setting parent
972
            # trees.
2255.7.78 by Martin Pool
Add DirState._validate and call from the tests
973
            state._validate()
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
974
            state.save()
2255.7.78 by Martin Pool
Add DirState._validate and call from the tests
975
            state._validate()
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
976
        finally:
977
            state.unlock()
2255.2.3 by Robert Collins
Split out working tree format 4 to its own file, create stub dirstate revision object, start working on dirstate.set_parent_trees - a key failure point.
978
        state = dirstate.DirState.on_file('dirstate')
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
979
        state.lock_write()
980
        try:
981
            self.assertEqual([revid1, revid2, 'ghost-rev'],
982
                             state.get_parent_ids())
983
            # iterating the entire state ensures that the state is parsable.
984
            list(state._iter_entries())
985
            # be sure that it sets not appends - change it
986
            state.set_parent_trees(
987
                ((revid1, tree1.branch.repository.revision_tree(revid1)),
988
                 ('ghost-rev', None)),
989
                ['ghost-rev'])
990
            # and now put it back.
991
            state.set_parent_trees(
992
                ((revid1, tree1.branch.repository.revision_tree(revid1)),
993
                 (revid2, tree2.branch.repository.revision_tree(revid2)),
3668.5.1 by Jelmer Vernooij
Use NULL_REVISION rather than None for Repository.revision_tree().
994
                 ('ghost-rev', tree2.branch.repository.revision_tree(
995
                                   _mod_revision.NULL_REVISION))),
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
996
                ['ghost-rev'])
997
            self.assertEqual([revid1, revid2, 'ghost-rev'],
998
                             state.get_parent_ids())
999
            # the ghost should be recorded as such by set_parent_trees.
1000
            self.assertEqual(['ghost-rev'], state.get_ghosts())
1001
            self.assertEqual(
1002
                [(('', '', root_id), [
2255.2.124 by John Arbash Meinel
Remove direct access to Dirstate prefering dirstate.Dirstate
1003
                  ('d', '', 0, False, dirstate.DirState.NULLSTAT),
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1004
                  ('d', '', 0, False, revid1),
1005
                  ('d', '', 0, False, revid2)
1006
                  ])],
1007
                list(state._iter_entries()))
1008
        finally:
1009
            state.unlock()
1852.13.19 by Robert Collins
Get DirState objects roundtripping an add of a ghost tree.
1010
2255.2.9 by Robert Collins
Dirstate: Fix setting of parent trees to record data about entries not in
1011
    def test_set_parent_trees_file_missing_from_tree(self):
1012
        # Adding a parent tree may reference files not in the current state.
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
1013
        # they should get listed just once by id, even if they are in two
2255.2.9 by Robert Collins
Dirstate: Fix setting of parent trees to record data about entries not in
1014
        # separate trees.
1015
        # set_parent_trees is a slow but important api to support.
1016
        tree1 = self.make_branch_and_memory_tree('tree1')
1017
        tree1.lock_write()
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1018
        try:
1019
            tree1.add('')
1020
            tree1.add(['a file'], ['file-id'], ['file'])
1021
            tree1.put_file_bytes_non_atomic('file-id', 'file-content')
1022
            revid1 = tree1.commit('foo')
1023
        finally:
1024
            tree1.unlock()
2255.2.9 by Robert Collins
Dirstate: Fix setting of parent trees to record data about entries not in
1025
        branch2 = tree1.branch.bzrdir.clone('tree2').open_branch()
4241.14.10 by Vincent Ladeuil
Add DirReader parametrized tests.
1026
        tree2 = memorytree.MemoryTree.create_on_branch(branch2)
2255.2.9 by Robert Collins
Dirstate: Fix setting of parent trees to record data about entries not in
1027
        tree2.lock_write()
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1028
        try:
1029
            tree2.put_file_bytes_non_atomic('file-id', 'new file-content')
1030
            revid2 = tree2.commit('foo')
2946.3.3 by John Arbash Meinel
Prefer tree.get_root_id() as more explicit than tree.path2id('')
1031
            root_id = tree2.get_root_id()
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1032
        finally:
1033
            tree2.unlock()
2255.2.9 by Robert Collins
Dirstate: Fix setting of parent trees to record data about entries not in
1034
        # check the layout in memory
2255.2.87 by Robert Collins
core dirstate tests passing with new structure.
1035
        expected_result = [revid1.encode('utf8'), revid2.encode('utf8')], [
1036
            (('', '', root_id), [
2255.2.124 by John Arbash Meinel
Remove direct access to Dirstate prefering dirstate.Dirstate
1037
             ('d', '', 0, False, dirstate.DirState.NULLSTAT),
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
1038
             ('d', '', 0, False, revid1.encode('utf8')),
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
1039
             ('d', '', 0, False, revid2.encode('utf8'))
1040
             ]),
2255.2.87 by Robert Collins
core dirstate tests passing with new structure.
1041
            (('', 'a file', 'file-id'), [
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
1042
             ('a', '', 0, False, ''),
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
1043
             ('f', '2439573625385400f2a669657a7db6ae7515d371', 12, False,
1044
              revid1.encode('utf8')),
1045
             ('f', '542e57dc1cda4af37cb8e55ec07ce60364bb3c7d', 16, False,
1046
              revid2.encode('utf8'))
1047
             ])
2255.2.9 by Robert Collins
Dirstate: Fix setting of parent trees to record data about entries not in
1048
            ]
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1049
        state = dirstate.DirState.initialize('dirstate')
1050
        try:
1051
            state.set_path_id('', root_id)
1052
            state.set_parent_trees(
1053
                ((revid1, tree1.branch.repository.revision_tree(revid1)),
1054
                 (revid2, tree2.branch.repository.revision_tree(revid2)),
1055
                 ), [])
1056
        except:
1057
            state.unlock()
1058
            raise
1059
        else:
1060
            # check_state_with_reopen will unlock
1061
            self.check_state_with_reopen(expected_result, state)
2255.2.9 by Robert Collins
Dirstate: Fix setting of parent trees to record data about entries not in
1062
1852.13.20 by Robert Collins
Steps toward an object model.
1063
    ### add a path via _set_data - so we dont need delta work, just
1064
    # raw data in, and ensure that it comes out via get_lines happily.
1852.13.19 by Robert Collins
Get DirState objects roundtripping an add of a ghost tree.
1065
1852.13.25 by Robert Collins
Snapshot state
1066
    def test_add_path_to_root_no_parents_all_data(self):
1067
        # The most trivial addition of a path is when there are no parents and
1068
        # its in the root and all data about the file is supplied
1069
        self.build_tree(['a file'])
1070
        stat = os.lstat('a file')
1071
        # the 1*20 is the sha1 pretend value.
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1072
        state = dirstate.DirState.initialize('dirstate')
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1073
        expected_entries = [
1074
            (('', '', 'TREE_ROOT'), [
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
1075
             ('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1076
             ]),
3010.1.2 by Robert Collins
Use valid file-ids for dirstate tests.
1077
            (('', 'a file', 'a-file-id'), [
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
1078
             ('f', '1'*20, 19, False, dirstate.pack_stat(stat)), # current tree
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1079
             ]),
2255.2.13 by Robert Collins
Test adding of directories to the root of a dirstate.
1080
            ]
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1081
        try:
3010.1.2 by Robert Collins
Use valid file-ids for dirstate tests.
1082
            state.add('a file', 'a-file-id', 'file', stat, '1'*20)
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1083
            # having added it, it should be in the output of iter_entries.
1084
            self.assertEqual(expected_entries, list(state._iter_entries()))
1085
            # saving and reloading should not affect this.
1086
            state.save()
1087
        finally:
1088
            state.unlock()
2255.2.13 by Robert Collins
Test adding of directories to the root of a dirstate.
1089
        state = dirstate.DirState.on_file('dirstate')
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1090
        state.lock_read()
4241.14.10 by Vincent Ladeuil
Add DirReader parametrized tests.
1091
        self.addCleanup(state.unlock)
1092
        self.assertEqual(expected_entries, list(state._iter_entries()))
2255.2.13 by Robert Collins
Test adding of directories to the root of a dirstate.
1093
1094
    def test_add_path_to_unversioned_directory(self):
2255.2.29 by Robert Collins
Change the error raised from Dirstate.add for an unversioned parent path to match the WorkingTree interface.
1095
        """Adding a path to an unversioned directory should error.
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1096
1097
        This is a duplicate of TestWorkingTree.test_add_in_unversioned,
2255.2.29 by Robert Collins
Change the error raised from Dirstate.add for an unversioned parent path to match the WorkingTree interface.
1098
        once dirstate is stable and if it is merged with WorkingTree3, consider
1099
        removing this copy of the test.
1100
        """
2255.2.13 by Robert Collins
Test adding of directories to the root of a dirstate.
1101
        self.build_tree(['unversioned/', 'unversioned/a file'])
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1102
        state = dirstate.DirState.initialize('dirstate')
4241.14.10 by Vincent Ladeuil
Add DirReader parametrized tests.
1103
        self.addCleanup(state.unlock)
1104
        self.assertRaises(errors.NotVersionedError, state.add,
1105
                          'unversioned/a file', 'a-file-id', 'file', None, None)
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1106
2255.2.13 by Robert Collins
Test adding of directories to the root of a dirstate.
1107
    def test_add_directory_to_root_no_parents_all_data(self):
1108
        # The most trivial addition of a dir is when there are no parents and
1109
        # its in the root and all data about the file is supplied
1110
        self.build_tree(['a dir/'])
1111
        stat = os.lstat('a dir')
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1112
        expected_entries = [
1113
            (('', '', 'TREE_ROOT'), [
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
1114
             ('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1115
             ]),
1116
            (('', 'a dir', 'a dir id'), [
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
1117
             ('d', '', 0, False, dirstate.pack_stat(stat)), # current tree
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1118
             ]),
2255.2.13 by Robert Collins
Test adding of directories to the root of a dirstate.
1119
            ]
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1120
        state = dirstate.DirState.initialize('dirstate')
1121
        try:
1122
            state.add('a dir', 'a dir id', 'directory', stat, None)
1123
            # having added it, it should be in the output of iter_entries.
1124
            self.assertEqual(expected_entries, list(state._iter_entries()))
1125
            # saving and reloading should not affect this.
1126
            state.save()
1127
        finally:
1128
            state.unlock()
2255.2.13 by Robert Collins
Test adding of directories to the root of a dirstate.
1129
        state = dirstate.DirState.on_file('dirstate')
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1130
        state.lock_read()
4241.14.10 by Vincent Ladeuil
Add DirReader parametrized tests.
1131
        self.addCleanup(state.unlock)
2255.7.78 by Martin Pool
Add DirState._validate and call from the tests
1132
        state._validate()
4241.14.10 by Vincent Ladeuil
Add DirReader parametrized tests.
1133
        self.assertEqual(expected_entries, list(state._iter_entries()))
1852.13.25 by Robert Collins
Snapshot state
1134
4241.14.10 by Vincent Ladeuil
Add DirReader parametrized tests.
1135
    def _test_add_symlink_to_root_no_parents_all_data(self, link_name, target):
2255.2.14 by Robert Collins
Dirstate: fix adding of directories to setup the next directories block, and test representation of symlinks. Also fix iter_rows to not reset the dirty bit.
1136
        # The most trivial addition of a symlink when there are no parents and
1137
        # its in the root and all data about the file is supplied
2321.3.8 by Alexander Belchenko
Cleanup patch after John's review
1138
        # bzr doesn't support fake symlinks on windows, yet.
4241.14.10 by Vincent Ladeuil
Add DirReader parametrized tests.
1139
        self.requireFeature(tests.SymlinkFeature)
1140
        os.symlink(target, link_name)
1141
        stat = os.lstat(link_name)
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1142
        expected_entries = [
1143
            (('', '', 'TREE_ROOT'), [
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
1144
             ('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1145
             ]),
4241.14.11 by Vincent Ladeuil
Fix wrong encoding.
1146
            (('', link_name.encode('UTF-8'), 'a link id'), [
1147
             ('l', target.encode('UTF-8'), stat[6],
4241.14.10 by Vincent Ladeuil
Add DirReader parametrized tests.
1148
              False, dirstate.pack_stat(stat)), # current tree
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1149
             ]),
2255.2.14 by Robert Collins
Dirstate: fix adding of directories to setup the next directories block, and test representation of symlinks. Also fix iter_rows to not reset the dirty bit.
1150
            ]
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1151
        state = dirstate.DirState.initialize('dirstate')
1152
        try:
4241.14.10 by Vincent Ladeuil
Add DirReader parametrized tests.
1153
            state.add(link_name, 'a link id', 'symlink', stat,
4241.14.11 by Vincent Ladeuil
Fix wrong encoding.
1154
                      target.encode('UTF-8'))
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1155
            # having added it, it should be in the output of iter_entries.
1156
            self.assertEqual(expected_entries, list(state._iter_entries()))
1157
            # saving and reloading should not affect this.
1158
            state.save()
1159
        finally:
1160
            state.unlock()
2255.2.14 by Robert Collins
Dirstate: fix adding of directories to setup the next directories block, and test representation of symlinks. Also fix iter_rows to not reset the dirty bit.
1161
        state = dirstate.DirState.on_file('dirstate')
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1162
        state.lock_read()
4241.14.10 by Vincent Ladeuil
Add DirReader parametrized tests.
1163
        self.addCleanup(state.unlock)
1164
        self.assertEqual(expected_entries, list(state._iter_entries()))
1165
1166
    def test_add_symlink_to_root_no_parents_all_data(self):
1167
        self._test_add_symlink_to_root_no_parents_all_data('a link', 'target')
1168
1169
    def test_add_symlink_unicode_to_root_no_parents_all_data(self):
1170
        self.requireFeature(tests.UnicodeFilenameFeature)
1171
        self._test_add_symlink_to_root_no_parents_all_data(
1172
            u'\N{Euro Sign}link', u'targ\N{Euro Sign}et')
2255.2.14 by Robert Collins
Dirstate: fix adding of directories to setup the next directories block, and test representation of symlinks. Also fix iter_rows to not reset the dirty bit.
1173
1174
    def test_add_directory_and_child_no_parents_all_data(self):
1175
        # after adding a directory, we should be able to add children to it.
1176
        self.build_tree(['a dir/', 'a dir/a file'])
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1177
        dirstat = os.lstat('a dir')
2255.2.14 by Robert Collins
Dirstate: fix adding of directories to setup the next directories block, and test representation of symlinks. Also fix iter_rows to not reset the dirty bit.
1178
        filestat = os.lstat('a dir/a file')
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1179
        expected_entries = [
1180
            (('', '', 'TREE_ROOT'), [
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
1181
             ('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1182
             ]),
1183
            (('', 'a dir', 'a dir id'), [
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
1184
             ('d', '', 0, False, dirstate.pack_stat(dirstat)), # current tree
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1185
             ]),
3010.1.2 by Robert Collins
Use valid file-ids for dirstate tests.
1186
            (('a dir', 'a file', 'a-file-id'), [
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
1187
             ('f', '1'*20, 25, False,
1188
              dirstate.pack_stat(filestat)), # current tree details
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1189
             ]),
2255.2.14 by Robert Collins
Dirstate: fix adding of directories to setup the next directories block, and test representation of symlinks. Also fix iter_rows to not reset the dirty bit.
1190
            ]
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1191
        state = dirstate.DirState.initialize('dirstate')
1192
        try:
1193
            state.add('a dir', 'a dir id', 'directory', dirstat, None)
3010.1.2 by Robert Collins
Use valid file-ids for dirstate tests.
1194
            state.add('a dir/a file', 'a-file-id', 'file', filestat, '1'*20)
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1195
            # added it, it should be in the output of iter_entries.
1196
            self.assertEqual(expected_entries, list(state._iter_entries()))
1197
            # saving and reloading should not affect this.
1198
            state.save()
1199
        finally:
1200
            state.unlock()
2255.2.14 by Robert Collins
Dirstate: fix adding of directories to setup the next directories block, and test representation of symlinks. Also fix iter_rows to not reset the dirty bit.
1201
        state = dirstate.DirState.on_file('dirstate')
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1202
        state.lock_read()
4241.14.10 by Vincent Ladeuil
Add DirReader parametrized tests.
1203
        self.addCleanup(state.unlock)
1204
        self.assertEqual(expected_entries, list(state._iter_entries()))
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1205
2255.7.93 by Martin Pool
Add support for tree-references in dirstate
1206
    def test_add_tree_reference(self):
1207
        # make a dirstate and add a tree reference
1208
        state = dirstate.DirState.initialize('dirstate')
1209
        expected_entry = (
1210
            ('', 'subdir', 'subdir-id'),
1211
            [('t', 'subtree-123123', 0, False,
1212
              'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')],
1213
            )
1214
        try:
1215
            state.add('subdir', 'subdir-id', 'tree-reference', None, 'subtree-123123')
1216
            entry = state._get_entry(0, 'subdir-id', 'subdir')
1217
            self.assertEqual(entry, expected_entry)
1218
            state._validate()
1219
            state.save()
1220
        finally:
1221
            state.unlock()
1222
        # now check we can read it back
1223
        state.lock_read()
4241.14.10 by Vincent Ladeuil
Add DirReader parametrized tests.
1224
        self.addCleanup(state.unlock)
2255.7.93 by Martin Pool
Add support for tree-references in dirstate
1225
        state._validate()
4241.14.10 by Vincent Ladeuil
Add DirReader parametrized tests.
1226
        entry2 = state._get_entry(0, 'subdir-id', 'subdir')
1227
        self.assertEqual(entry, entry2)
1228
        self.assertEqual(entry, expected_entry)
1229
        # and lookup by id should work too
1230
        entry2 = state._get_entry(0, fileid_utf8='subdir-id')
1231
        self.assertEqual(entry, expected_entry)
2255.7.93 by Martin Pool
Add support for tree-references in dirstate
1232
2255.2.225 by Martin Pool
Prohibit dirstate from getting entries called ..
1233
    def test_add_forbidden_names(self):
1234
        state = dirstate.DirState.initialize('dirstate')
2255.2.233 by John Arbash Meinel
DirState.initialize returns a locked state, unlock as part of cleanup.
1235
        self.addCleanup(state.unlock)
2255.2.225 by Martin Pool
Prohibit dirstate from getting entries called ..
1236
        self.assertRaises(errors.BzrError,
1237
            state.add, '.', 'ass-id', 'directory', None, None)
1238
        self.assertRaises(errors.BzrError,
1239
            state.add, '..', 'ass-id', 'directory', None, None)
1240
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1241
1242
class TestGetLines(TestCaseWithDirState):
1852.13.19 by Robert Collins
Get DirState objects roundtripping an add of a ghost tree.
1243
1852.13.24 by Robert Collins
Get back to the broken-pending-revision-tree-from-dirstate state of development, changing dirstate from_tree to use _set_data rather than generating lines itself.
1244
    def test_get_line_with_2_rows(self):
2255.2.66 by John Arbash Meinel
Move _get_row and _get_block_row_index into Dirstate itself.
1245
        state = self.create_dirstate_with_root_and_subdir()
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1246
        try:
2255.7.20 by John Arbash Meinel
update test for format 3, and enable caching of path split while lock is held.
1247
            self.assertEqual(['#bazaar dirstate flat format 3\n',
2255.2.239 by Robert Collins
Change from adler to crc checksums, as adler32 in python is not stable from 32 to 64 bit systems.
1248
                'crc32: 41262208\n',
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1249
                'num_entries: 2\n',
1250
                '0\x00\n\x00'
1251
                '0\x00\n\x00'
1252
                '\x00\x00a-root-value\x00'
1253
                'd\x00\x000\x00n\x00AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk\x00\n\x00'
1254
                '\x00subdir\x00subdir-id\x00'
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
1255
                'd\x00\x000\x00n\x00AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk\x00\n\x00'
1256
                ], state.get_lines())
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1257
        finally:
1258
            state.unlock()
1852.13.24 by Robert Collins
Get back to the broken-pending-revision-tree-from-dirstate state of development, changing dirstate from_tree to use _set_data rather than generating lines itself.
1259
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1260
    def test_entry_to_line(self):
2255.2.66 by John Arbash Meinel
Move _get_row and _get_block_row_index into Dirstate itself.
1261
        state = self.create_dirstate_with_root()
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1262
        try:
1263
            self.assertEqual(
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
1264
                '\x00\x00a-root-value\x00d\x00\x000\x00n'
1265
                '\x00AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk',
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1266
                state._entry_to_line(state._dirblocks[0][1][0]))
1267
        finally:
1268
            state.unlock()
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1269
1270
    def test_entry_to_line_with_parent(self):
1271
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
1272
        root_entry = ('', '', 'a-root-value'), [
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
1273
            ('d', '', 0, False, packed_stat), # current tree details
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
1274
             # first: a pointer to the current location
1275
            ('a', 'dirname/basename', 0, False, ''),
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1276
            ]
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1277
        state = dirstate.DirState.initialize('dirstate')
1278
        try:
1279
            self.assertEqual(
1280
                '\x00\x00a-root-value\x00'
1281
                'd\x00\x000\x00n\x00AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk\x00'
1282
                'a\x00dirname/basename\x000\x00n\x00',
1283
                state._entry_to_line(root_entry))
1284
        finally:
1285
            state.unlock()
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1286
1287
    def test_entry_to_line_with_two_parents_at_different_paths(self):
1288
        # / in the tree, at / in one parent and /dirname/basename in the other.
1289
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
1290
        root_entry = ('', '', 'a-root-value'), [
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
1291
            ('d', '', 0, False, packed_stat), # current tree details
1292
            ('d', '', 0, False, 'rev_id'), # first parent details
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
1293
             # second: a pointer to the current location
1294
            ('a', 'dirname/basename', 0, False, ''),
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1295
            ]
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1296
        state = dirstate.DirState.initialize('dirstate')
1297
        try:
1298
            self.assertEqual(
1299
                '\x00\x00a-root-value\x00'
1300
                'd\x00\x000\x00n\x00AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk\x00'
1301
                'd\x00\x000\x00n\x00rev_id\x00'
1302
                'a\x00dirname/basename\x000\x00n\x00',
1303
                state._entry_to_line(root_entry))
1304
        finally:
1305
            state.unlock()
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1306
1307
    def test_iter_entries(self):
1308
        # we should be able to iterate the dirstate entries from end to end
1852.13.24 by Robert Collins
Get back to the broken-pending-revision-tree-from-dirstate state of development, changing dirstate from_tree to use _set_data rather than generating lines itself.
1309
        # this is for get_lines to be easy to read.
1310
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
1311
        dirblocks = []
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1312
        root_entries = [(('', '', 'a-root-value'), [
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
1313
            ('d', '', 0, False, packed_stat), # current tree details
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1314
            ])]
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
1315
        dirblocks.append(('', root_entries))
1852.13.24 by Robert Collins
Get back to the broken-pending-revision-tree-from-dirstate state of development, changing dirstate from_tree to use _set_data rather than generating lines itself.
1316
        # add two files in the root
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1317
        subdir_entry = ('', 'subdir', 'subdir-id'), [
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
1318
            ('d', '', 0, False, packed_stat), # current tree details
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1319
            ]
1320
        afile_entry = ('', 'afile', 'afile-id'), [
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
1321
            ('f', 'sha1value', 34, False, packed_stat), # current tree details
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1322
            ]
1323
        dirblocks.append(('', [subdir_entry, afile_entry]))
1852.13.24 by Robert Collins
Get back to the broken-pending-revision-tree-from-dirstate state of development, changing dirstate from_tree to use _set_data rather than generating lines itself.
1324
        # and one in subdir
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1325
        file_entry2 = ('subdir', '2file', '2file-id'), [
2255.2.113 by John Arbash Meinel
545ms, 600ms: Switch memory model from storing kind to using minikind
1326
            ('f', 'sha1value', 23, False, packed_stat), # current tree details
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1327
            ]
1328
        dirblocks.append(('subdir', [file_entry2]))
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1329
        state = dirstate.DirState.initialize('dirstate')
1330
        try:
1331
            state._set_data([], dirblocks)
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
1332
            expected_entries = [root_entries[0], subdir_entry, afile_entry,
1333
                                file_entry2]
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1334
            self.assertEqual(expected_entries, list(state._iter_entries()))
1335
        finally:
1336
            state.unlock()
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1337
1338
1339
class TestGetBlockRowIndex(TestCaseWithDirState):
2255.2.66 by John Arbash Meinel
Move _get_row and _get_block_row_index into Dirstate itself.
1340
1341
    def assertBlockRowIndexEqual(self, block_index, row_index, dir_present,
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1342
        file_present, state, dirname, basename, tree_index):
2255.2.66 by John Arbash Meinel
Move _get_row and _get_block_row_index into Dirstate itself.
1343
        self.assertEqual((block_index, row_index, dir_present, file_present),
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1344
            state._get_block_entry_index(dirname, basename, tree_index))
2255.2.66 by John Arbash Meinel
Move _get_row and _get_block_row_index into Dirstate itself.
1345
        if dir_present:
1346
            block = state._dirblocks[block_index]
1347
            self.assertEqual(dirname, block[0])
1348
        if dir_present and file_present:
1349
            row = state._dirblocks[block_index][1][row_index]
1350
            self.assertEqual(dirname, row[0][0])
1351
            self.assertEqual(basename, row[0][1])
1352
1353
    def test_simple_structure(self):
1354
        state = self.create_dirstate_with_root_and_subdir()
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1355
        self.addCleanup(state.unlock)
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
1356
        self.assertBlockRowIndexEqual(1, 0, True, True, state, '', 'subdir', 0)
1357
        self.assertBlockRowIndexEqual(1, 0, True, False, state, '', 'bdir', 0)
1358
        self.assertBlockRowIndexEqual(1, 1, True, False, state, '', 'zdir', 0)
1359
        self.assertBlockRowIndexEqual(2, 0, False, False, state, 'a', 'foo', 0)
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
1360
        self.assertBlockRowIndexEqual(2, 0, False, False, state,
1361
                                      'subdir', 'foo', 0)
2255.2.66 by John Arbash Meinel
Move _get_row and _get_block_row_index into Dirstate itself.
1362
1363
    def test_complex_structure_exists(self):
1364
        state = self.create_complex_dirstate()
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1365
        self.addCleanup(state.unlock)
2255.2.66 by John Arbash Meinel
Move _get_row and _get_block_row_index into Dirstate itself.
1366
        # Make sure we can find everything that exists
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
1367
        self.assertBlockRowIndexEqual(0, 0, True, True, state, '', '', 0)
1368
        self.assertBlockRowIndexEqual(1, 0, True, True, state, '', 'a', 0)
1369
        self.assertBlockRowIndexEqual(1, 1, True, True, state, '', 'b', 0)
1370
        self.assertBlockRowIndexEqual(1, 2, True, True, state, '', 'c', 0)
1371
        self.assertBlockRowIndexEqual(1, 3, True, True, state, '', 'd', 0)
1372
        self.assertBlockRowIndexEqual(2, 0, True, True, state, 'a', 'e', 0)
1373
        self.assertBlockRowIndexEqual(2, 1, True, True, state, 'a', 'f', 0)
1374
        self.assertBlockRowIndexEqual(3, 0, True, True, state, 'b', 'g', 0)
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
1375
        self.assertBlockRowIndexEqual(3, 1, True, True, state,
1376
                                      'b', 'h\xc3\xa5', 0)
2255.2.66 by John Arbash Meinel
Move _get_row and _get_block_row_index into Dirstate itself.
1377
1378
    def test_complex_structure_missing(self):
1379
        state = self.create_complex_dirstate()
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1380
        self.addCleanup(state.unlock)
2255.2.66 by John Arbash Meinel
Move _get_row and _get_block_row_index into Dirstate itself.
1381
        # Make sure things would be inserted in the right locations
1382
        # '_' comes before 'a'
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
1383
        self.assertBlockRowIndexEqual(0, 0, True, True, state, '', '', 0)
1384
        self.assertBlockRowIndexEqual(1, 0, True, False, state, '', '_', 0)
1385
        self.assertBlockRowIndexEqual(1, 1, True, False, state, '', 'aa', 0)
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
1386
        self.assertBlockRowIndexEqual(1, 4, True, False, state,
1387
                                      '', 'h\xc3\xa5', 0)
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
1388
        self.assertBlockRowIndexEqual(2, 0, False, False, state, '_', 'a', 0)
1389
        self.assertBlockRowIndexEqual(3, 0, False, False, state, 'aa', 'a', 0)
1390
        self.assertBlockRowIndexEqual(4, 0, False, False, state, 'bb', 'a', 0)
2255.2.66 by John Arbash Meinel
Move _get_row and _get_block_row_index into Dirstate itself.
1391
        # This would be inserted between a/ and b/
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
1392
        self.assertBlockRowIndexEqual(3, 0, False, False, state, 'a/e', 'a', 0)
2255.2.66 by John Arbash Meinel
Move _get_row and _get_block_row_index into Dirstate itself.
1393
        # Put at the end
2255.2.96 by Robert Collins
Restore dirstate to all tests passing condition.
1394
        self.assertBlockRowIndexEqual(4, 0, False, False, state, 'e', 'a', 0)
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1395
1396
1397
class TestGetEntry(TestCaseWithDirState):
1398
1399
    def assertEntryEqual(self, dirname, basename, file_id, state, path, index):
1400
        """Check that the right entry is returned for a request to getEntry."""
2255.2.87 by Robert Collins
core dirstate tests passing with new structure.
1401
        entry = state._get_entry(index, path_utf8=path)
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1402
        if file_id is None:
1403
            self.assertEqual((None, None), entry)
2255.2.66 by John Arbash Meinel
Move _get_row and _get_block_row_index into Dirstate itself.
1404
        else:
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1405
            cur = entry[0]
1406
            self.assertEqual((dirname, basename, file_id), cur[:3])
2255.2.66 by John Arbash Meinel
Move _get_row and _get_block_row_index into Dirstate itself.
1407
1408
    def test_simple_structure(self):
1409
        state = self.create_dirstate_with_root_and_subdir()
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1410
        self.addCleanup(state.unlock)
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1411
        self.assertEntryEqual('', '', 'a-root-value', state, '', 0)
1412
        self.assertEntryEqual('', 'subdir', 'subdir-id', state, 'subdir', 0)
1413
        self.assertEntryEqual(None, None, None, state, 'missing', 0)
1414
        self.assertEntryEqual(None, None, None, state, 'missing/foo', 0)
1415
        self.assertEntryEqual(None, None, None, state, 'subdir/foo', 0)
2255.2.66 by John Arbash Meinel
Move _get_row and _get_block_row_index into Dirstate itself.
1416
1417
    def test_complex_structure_exists(self):
1418
        state = self.create_complex_dirstate()
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1419
        self.addCleanup(state.unlock)
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1420
        self.assertEntryEqual('', '', 'a-root-value', state, '', 0)
1421
        self.assertEntryEqual('', 'a', 'a-dir', state, 'a', 0)
1422
        self.assertEntryEqual('', 'b', 'b-dir', state, 'b', 0)
1423
        self.assertEntryEqual('', 'c', 'c-file', state, 'c', 0)
1424
        self.assertEntryEqual('', 'd', 'd-file', state, 'd', 0)
1425
        self.assertEntryEqual('a', 'e', 'e-dir', state, 'a/e', 0)
1426
        self.assertEntryEqual('a', 'f', 'f-file', state, 'a/f', 0)
1427
        self.assertEntryEqual('b', 'g', 'g-file', state, 'b/g', 0)
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
1428
        self.assertEntryEqual('b', 'h\xc3\xa5', 'h-\xc3\xa5-file', state,
1429
                              'b/h\xc3\xa5', 0)
2255.2.66 by John Arbash Meinel
Move _get_row and _get_block_row_index into Dirstate itself.
1430
1431
    def test_complex_structure_missing(self):
1432
        state = self.create_complex_dirstate()
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1433
        self.addCleanup(state.unlock)
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1434
        self.assertEntryEqual(None, None, None, state, '_', 0)
1435
        self.assertEntryEqual(None, None, None, state, '_\xc3\xa5', 0)
1436
        self.assertEntryEqual(None, None, None, state, 'a/b', 0)
1437
        self.assertEntryEqual(None, None, None, state, 'c/d', 0)
2255.2.66 by John Arbash Meinel
Move _get_row and _get_block_row_index into Dirstate itself.
1438
2255.2.85 by Robert Collins
[BROKEN] Partial conversion to new dirstate structure, please continue on the tests matching dirstate from here.
1439
    def test_get_entry_uninitialized(self):
1440
        """Calling get_entry will load data if it needs to"""
2255.2.66 by John Arbash Meinel
Move _get_row and _get_block_row_index into Dirstate itself.
1441
        state = self.create_dirstate_with_root()
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1442
        try:
1443
            state.save()
1444
        finally:
1445
            state.unlock()
2255.2.66 by John Arbash Meinel
Move _get_row and _get_block_row_index into Dirstate itself.
1446
        del state
1447
        state = dirstate.DirState.on_file('dirstate')
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1448
        state.lock_read()
1449
        try:
2255.2.123 by John Arbash Meinel
Simple line wrapping cleanup in test_dirstate.py
1450
            self.assertEqual(dirstate.DirState.NOT_IN_MEMORY,
1451
                             state._header_state)
1452
            self.assertEqual(dirstate.DirState.NOT_IN_MEMORY,
1453
                             state._dirblock_state)
2255.5.1 by John Arbash Meinel
Update the dirstate tests to lock and unlock properly.
1454
            self.assertEntryEqual('', '', 'a-root-value', state, '', 0)
1455
        finally:
1456
            state.unlock()
2255.3.2 by John Arbash Meinel
(broken) some basic work on adding bisect functionality to dirstate.
1457
1458
2929.2.1 by Robert Collins
* Commit updates the state of the working tree via a delta rather than
1459
class TestIterChildEntries(TestCaseWithDirState):
1460
1461
    def create_dirstate_with_two_trees(self):
1462
        """This dirstate contains multiple files and directories.
1463
1464
         /        a-root-value
1465
         a/       a-dir
1466
         b/       b-dir
1467
         c        c-file
1468
         d        d-file
1469
         a/e/     e-dir
1470
         a/f      f-file
1471
         b/g      g-file
1472
         b/h\xc3\xa5  h-\xc3\xa5-file  #This is u'\xe5' encoded into utf-8
1473
1474
        Notice that a/e is an empty directory.
1475
1476
        There is one parent tree, which has the same shape with the following variations:
1477
        b/g in the parent is gone.
1478
        b/h in the parent has a different id
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1479
        b/i is new in the parent
2929.2.1 by Robert Collins
* Commit updates the state of the working tree via a delta rather than
1480
        c is renamed to b/j in the parent
1481
1482
        :return: The dirstate, still write-locked.
1483
        """
1484
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
1485
        null_sha = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
1486
        NULL_PARENT_DETAILS = dirstate.DirState.NULL_PARENT_DETAILS
1487
        root_entry = ('', '', 'a-root-value'), [
1488
            ('d', '', 0, False, packed_stat),
1489
            ('d', '', 0, False, 'parent-revid'),
1490
            ]
1491
        a_entry = ('', 'a', 'a-dir'), [
1492
            ('d', '', 0, False, packed_stat),
1493
            ('d', '', 0, False, 'parent-revid'),
1494
            ]
1495
        b_entry = ('', 'b', 'b-dir'), [
1496
            ('d', '', 0, False, packed_stat),
1497
            ('d', '', 0, False, 'parent-revid'),
1498
            ]
1499
        c_entry = ('', 'c', 'c-file'), [
1500
            ('f', null_sha, 10, False, packed_stat),
1501
            ('r', 'b/j', 0, False, ''),
1502
            ]
1503
        d_entry = ('', 'd', 'd-file'), [
1504
            ('f', null_sha, 20, False, packed_stat),
1505
            ('f', 'd', 20, False, 'parent-revid'),
1506
            ]
1507
        e_entry = ('a', 'e', 'e-dir'), [
1508
            ('d', '', 0, False, packed_stat),
1509
            ('d', '', 0, False, 'parent-revid'),
1510
            ]
1511
        f_entry = ('a', 'f', 'f-file'), [
1512
            ('f', null_sha, 30, False, packed_stat),
1513
            ('f', 'f', 20, False, 'parent-revid'),
1514
            ]
1515
        g_entry = ('b', 'g', 'g-file'), [
1516
            ('f', null_sha, 30, False, packed_stat),
1517
            NULL_PARENT_DETAILS,
1518
            ]
1519
        h_entry1 = ('b', 'h\xc3\xa5', 'h-\xc3\xa5-file1'), [
1520
            ('f', null_sha, 40, False, packed_stat),
1521
            NULL_PARENT_DETAILS,
1522
            ]
1523
        h_entry2 = ('b', 'h\xc3\xa5', 'h-\xc3\xa5-file2'), [
1524
            NULL_PARENT_DETAILS,
1525
            ('f', 'h', 20, False, 'parent-revid'),
1526
            ]
1527
        i_entry = ('b', 'i', 'i-file'), [
1528
            NULL_PARENT_DETAILS,
1529
            ('f', 'h', 20, False, 'parent-revid'),
1530
            ]
1531
        j_entry = ('b', 'j', 'c-file'), [
1532
            ('r', 'c', 0, False, ''),
1533
            ('f', 'j', 20, False, 'parent-revid'),
1534
            ]
1535
        dirblocks = []
1536
        dirblocks.append(('', [root_entry]))
1537
        dirblocks.append(('', [a_entry, b_entry, c_entry, d_entry]))
1538
        dirblocks.append(('a', [e_entry, f_entry]))
1539
        dirblocks.append(('b', [g_entry, h_entry1, h_entry2, i_entry, j_entry]))
1540
        state = dirstate.DirState.initialize('dirstate')
1541
        state._validate()
1542
        try:
1543
            state._set_data(['parent'], dirblocks)
1544
        except:
1545
            state.unlock()
1546
            raise
1547
        return state, dirblocks
1548
1549
    def test_iter_children_b(self):
1550
        state, dirblocks = self.create_dirstate_with_two_trees()
1551
        self.addCleanup(state.unlock)
1552
        expected_result = []
1553
        expected_result.append(dirblocks[3][1][2]) # h2
1554
        expected_result.append(dirblocks[3][1][3]) # i
1555
        expected_result.append(dirblocks[3][1][4]) # j
1556
        self.assertEqual(expected_result,
1557
            list(state._iter_child_entries(1, 'b')))
1558
2929.2.2 by Robert Collins
Review feedback on dirstate update_basis_via_delta logic.
1559
    def test_iter_child_root(self):
1560
        state, dirblocks = self.create_dirstate_with_two_trees()
1561
        self.addCleanup(state.unlock)
1562
        expected_result = []
1563
        expected_result.append(dirblocks[1][1][0]) # a
1564
        expected_result.append(dirblocks[1][1][1]) # b
1565
        expected_result.append(dirblocks[1][1][3]) # d
1566
        expected_result.append(dirblocks[2][1][0]) # e
1567
        expected_result.append(dirblocks[2][1][1]) # f
1568
        expected_result.append(dirblocks[3][1][2]) # h2
1569
        expected_result.append(dirblocks[3][1][3]) # i
1570
        expected_result.append(dirblocks[3][1][4]) # j
1571
        self.assertEqual(expected_result,
1572
            list(state._iter_child_entries(1, '')))
1573
2929.2.1 by Robert Collins
* Commit updates the state of the working tree via a delta rather than
1574
4241.14.10 by Vincent Ladeuil
Add DirReader parametrized tests.
1575
class TestDirstateSortOrder(tests.TestCaseWithTransport):
2255.8.5 by John Arbash Meinel
Add a test that dirstate adds records in the right order.
1576
    """Test that DirState adds entries in the right order."""
1577
1578
    def test_add_sorting(self):
2255.7.21 by John Arbash Meinel
Get iter_changes working again, by fixing set_parent_trees to
1579
        """Add entries in lexicographical order, we get path sorted order.
1580
1581
        This tests it to a depth of 4, to make sure we don't just get it right
1582
        at a single depth. 'a/a' should come before 'a-a', even though it
1583
        doesn't lexicographically.
1584
        """
1585
        dirs = ['a', 'a/a', 'a/a/a', 'a/a/a/a',
1586
                'a-a', 'a/a-a', 'a/a/a-a', 'a/a/a/a-a',
2255.8.5 by John Arbash Meinel
Add a test that dirstate adds records in the right order.
1587
               ]
1588
        null_sha = ''
1589
        state = dirstate.DirState.initialize('dirstate')
1590
        self.addCleanup(state.unlock)
1591
1592
        fake_stat = os.stat('dirstate')
1593
        for d in dirs:
1594
            d_id = d.replace('/', '_')+'-id'
1595
            file_path = d + '/f'
1596
            file_id = file_path.replace('/', '_')+'-id'
1597
            state.add(d, d_id, 'directory', fake_stat, null_sha)
1598
            state.add(file_path, file_id, 'file', fake_stat, null_sha)
1599
1600
        expected = ['', '', 'a',
2255.7.21 by John Arbash Meinel
Get iter_changes working again, by fixing set_parent_trees to
1601
                'a/a', 'a/a/a', 'a/a/a/a',
1602
                'a/a/a/a-a', 'a/a/a-a', 'a/a-a', 'a-a',
2255.8.5 by John Arbash Meinel
Add a test that dirstate adds records in the right order.
1603
               ]
2255.7.21 by John Arbash Meinel
Get iter_changes working again, by fixing set_parent_trees to
1604
        split = lambda p:p.split('/')
1605
        self.assertEqual(sorted(expected, key=split), expected)
1606
        dirblock_names = [d[0] for d in state._dirblocks]
1607
        self.assertEqual(expected, dirblock_names)
1608
1609
    def test_set_parent_trees_correct_order(self):
1610
        """After calling set_parent_trees() we should maintain the order."""
1611
        dirs = ['a', 'a-a', 'a/a']
1612
        null_sha = ''
1613
        state = dirstate.DirState.initialize('dirstate')
1614
        self.addCleanup(state.unlock)
1615
1616
        fake_stat = os.stat('dirstate')
1617
        for d in dirs:
1618
            d_id = d.replace('/', '_')+'-id'
1619
            file_path = d + '/f'
1620
            file_id = file_path.replace('/', '_')+'-id'
1621
            state.add(d, d_id, 'directory', fake_stat, null_sha)
1622
            state.add(file_path, file_id, 'file', fake_stat, null_sha)
1623
1624
        expected = ['', '', 'a', 'a/a', 'a-a']
1625
        dirblock_names = [d[0] for d in state._dirblocks]
1626
        self.assertEqual(expected, dirblock_names)
1627
1628
        # *really* cheesy way to just get an empty tree
1629
        repo = self.make_repository('repo')
3668.5.1 by Jelmer Vernooij
Use NULL_REVISION rather than None for Repository.revision_tree().
1630
        empty_tree = repo.revision_tree(_mod_revision.NULL_REVISION)
2255.7.21 by John Arbash Meinel
Get iter_changes working again, by fixing set_parent_trees to
1631
        state.set_parent_trees([('null:', empty_tree)], [])
1632
2255.8.5 by John Arbash Meinel
Add a test that dirstate adds records in the right order.
1633
        dirblock_names = [d[0] for d in state._dirblocks]
1634
        self.assertEqual(expected, dirblock_names)
1635
1636
2255.10.3 by John Arbash Meinel
(broken) Change get_sha1_for_entry into update_entry
1637
class InstrumentedDirState(dirstate.DirState):
2255.10.1 by John Arbash Meinel
Update WorkingTree4 so that it doesn't use a HashCache,
1638
    """An DirState with instrumented sha1 functionality."""
1639
4132.2.2 by Ian Clatworthy
make sha1_provider a mandatory param for DirState.__init__()
1640
    def __init__(self, path, sha1_provider):
1641
        super(InstrumentedDirState, self).__init__(path, sha1_provider)
2255.10.1 by John Arbash Meinel
Update WorkingTree4 so that it doesn't use a HashCache,
1642
        self._time_offset = 0
2255.10.3 by John Arbash Meinel
(broken) Change get_sha1_for_entry into update_entry
1643
        self._log = []
2872.3.3 by Martin Pool
Fix up test_update_entry to work with -Dhashcache
1644
        # member is dynamically set in DirState.__init__ to turn on trace
4132.2.2 by Ian Clatworthy
make sha1_provider a mandatory param for DirState.__init__()
1645
        self._sha1_provider = sha1_provider
2872.3.3 by Martin Pool
Fix up test_update_entry to work with -Dhashcache
1646
        self._sha1_file = self._sha1_file_and_log
2255.10.1 by John Arbash Meinel
Update WorkingTree4 so that it doesn't use a HashCache,
1647
1648
    def _sha_cutoff_time(self):
2255.10.3 by John Arbash Meinel
(broken) Change get_sha1_for_entry into update_entry
1649
        timestamp = super(InstrumentedDirState, self)._sha_cutoff_time()
2255.10.6 by John Arbash Meinel
Save approx 30-60ms (5-10%) on a LP tree by not calling time.time() for every entry.
1650
        self._cutoff_time = timestamp + self._time_offset
2255.10.1 by John Arbash Meinel
Update WorkingTree4 so that it doesn't use a HashCache,
1651
2872.3.3 by Martin Pool
Fix up test_update_entry to work with -Dhashcache
1652
    def _sha1_file_and_log(self, abspath):
2255.10.3 by John Arbash Meinel
(broken) Change get_sha1_for_entry into update_entry
1653
        self._log.append(('sha1', abspath))
4132.2.2 by Ian Clatworthy
make sha1_provider a mandatory param for DirState.__init__()
1654
        return self._sha1_provider.sha1(abspath)
2255.10.3 by John Arbash Meinel
(broken) Change get_sha1_for_entry into update_entry
1655
1656
    def _read_link(self, abspath, old_link):
1657
        self._log.append(('read_link', abspath, old_link))
1658
        return super(InstrumentedDirState, self)._read_link(abspath, old_link)
1659
1660
    def _lstat(self, abspath, entry):
1661
        self._log.append(('lstat', abspath))
1662
        return super(InstrumentedDirState, self)._lstat(abspath, entry)
1663
1664
    def _is_executable(self, mode, old_executable):
1665
        self._log.append(('is_exec', mode, old_executable))
1666
        return super(InstrumentedDirState, self)._is_executable(mode,
1667
                                                                old_executable)
2255.10.1 by John Arbash Meinel
Update WorkingTree4 so that it doesn't use a HashCache,
1668
1669
    def adjust_time(self, secs):
1670
        """Move the clock forward or back.
1671
1672
        :param secs: The amount to adjust the clock by. Positive values make it
1673
        seem as if we are in the future, negative values make it seem like we
1674
        are in the past.
1675
        """
1676
        self._time_offset += secs
2255.10.6 by John Arbash Meinel
Save approx 30-60ms (5-10%) on a LP tree by not calling time.time() for every entry.
1677
        self._cutoff_time = None
2255.10.1 by John Arbash Meinel
Update WorkingTree4 so that it doesn't use a HashCache,
1678
1679
1680
class _FakeStat(object):
1681
    """A class with the same attributes as a real stat result."""
1682
1683
    def __init__(self, size, mtime, ctime, dev, ino, mode):
1684
        self.st_size = size
1685
        self.st_mtime = mtime
1686
        self.st_ctime = ctime
1687
        self.st_dev = dev
1688
        self.st_ino = ino
1689
        self.st_mode = mode
1690
4132.2.3 by Ian Clatworthy
add test as suggested by poolie's review
1691
    @staticmethod
1692
    def from_stat(st):
1693
        return _FakeStat(st.st_size, st.st_mtime, st.st_ctime, st.st_dev,
1694
            st.st_ino, st.st_mode)
1695
2255.10.1 by John Arbash Meinel
Update WorkingTree4 so that it doesn't use a HashCache,
1696
4241.14.10 by Vincent Ladeuil
Add DirReader parametrized tests.
1697
class TestPackStat(tests.TestCaseWithTransport):
2255.10.1 by John Arbash Meinel
Update WorkingTree4 so that it doesn't use a HashCache,
1698
1699
    def assertPackStat(self, expected, stat_value):
1700
        """Check the packed and serialized form of a stat value."""
1701
        self.assertEqual(expected, dirstate.pack_stat(stat_value))
1702
1703
    def test_pack_stat_int(self):
1704
        st = _FakeStat(6859L, 1172758614, 1172758617, 777L, 6499538L, 0100644)
1705
        # Make sure that all parameters have an impact on the packed stat.
1706
        self.assertPackStat('AAAay0Xm4FZF5uBZAAADCQBjLNIAAIGk', st)
1707
        st.st_size = 7000L
1708
        #                ay0 => bWE
1709
        self.assertPackStat('AAAbWEXm4FZF5uBZAAADCQBjLNIAAIGk', st)
1710
        st.st_mtime = 1172758620
1711
        #                     4FZ => 4Fx
1712
        self.assertPackStat('AAAbWEXm4FxF5uBZAAADCQBjLNIAAIGk', st)
1713
        st.st_ctime = 1172758630
1714
        #                          uBZ => uBm
1715
        self.assertPackStat('AAAbWEXm4FxF5uBmAAADCQBjLNIAAIGk', st)
1716
        st.st_dev = 888L
1717
        #                                DCQ => DeA
1718
        self.assertPackStat('AAAbWEXm4FxF5uBmAAADeABjLNIAAIGk', st)
1719
        st.st_ino = 6499540L
1720
        #                                     LNI => LNQ
1721
        self.assertPackStat('AAAbWEXm4FxF5uBmAAADeABjLNQAAIGk', st)
1722
        st.st_mode = 0100744
1723
        #                                          IGk => IHk
1724
        self.assertPackStat('AAAbWEXm4FxF5uBmAAADeABjLNQAAIHk', st)
1725
1726
    def test_pack_stat_float(self):
1727
        """On some platforms mtime and ctime are floats.
1728
1729
        Make sure we don't get warnings or errors, and that we ignore changes <
1730
        1s
1731
        """
1732
        st = _FakeStat(7000L, 1172758614.0, 1172758617.0,
1733
                       777L, 6499538L, 0100644)
1734
        # These should all be the same as the integer counterparts
1735
        self.assertPackStat('AAAbWEXm4FZF5uBZAAADCQBjLNIAAIGk', st)
1736
        st.st_mtime = 1172758620.0
1737
        #                     FZF5 => FxF5
1738
        self.assertPackStat('AAAbWEXm4FxF5uBZAAADCQBjLNIAAIGk', st)
1739
        st.st_ctime = 1172758630.0
1740
        #                          uBZ => uBm
1741
        self.assertPackStat('AAAbWEXm4FxF5uBmAAADCQBjLNIAAIGk', st)
1742
        # fractional seconds are discarded, so no change from above
1743
        st.st_mtime = 1172758620.453
1744
        self.assertPackStat('AAAbWEXm4FxF5uBmAAADCQBjLNIAAIGk', st)
1745
        st.st_ctime = 1172758630.228
1746
        self.assertPackStat('AAAbWEXm4FxF5uBmAAADCQBjLNIAAIGk', st)
1747
1748
2323.5.4 by Martin Pool
Move some dirstate test setup methods into the base class
1749
class TestBisect(TestCaseWithDirState):
2255.3.2 by John Arbash Meinel
(broken) some basic work on adding bisect functionality to dirstate.
1750
    """Test the ability to bisect into the disk format."""
1751
2255.2.131 by John Arbash Meinel
Change the return values for bisect functions so they just return
1752
    def assertBisect(self, expected_map, map_keys, state, paths):
2255.2.125 by John Arbash Meinel
Initial effort at adding a basic _bisect function to DirState.
1753
        """Assert that bisecting for paths returns the right result.
1754
2255.2.131 by John Arbash Meinel
Change the return values for bisect functions so they just return
1755
        :param expected_map: A map from key => entry value
1756
        :param map_keys: The keys to expect for each path
2255.2.125 by John Arbash Meinel
Initial effort at adding a basic _bisect function to DirState.
1757
        :param state: The DirState object.
1758
        :param paths: A list of paths, these will automatically be split into
1759
                      (dir, name) tuples, and sorted according to how _bisect
1760
                      requires.
1761
        """
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
1762
        result = state._bisect(paths)
2255.2.126 by John Arbash Meinel
Switch the bisect code to support the fact that we can have
1763
        # For now, results are just returned in whatever order we read them.
1764
        # We could sort by (dir, name, file_id) or something like that, but in
1765
        # the end it would still be fairly arbitrary, and we don't want the
2255.2.131 by John Arbash Meinel
Change the return values for bisect functions so they just return
1766
        # extra overhead if we can avoid it. So sort everything to make sure
1767
        # equality is true
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
1768
        self.assertEqual(len(map_keys), len(paths))
2255.2.131 by John Arbash Meinel
Change the return values for bisect functions so they just return
1769
        expected = {}
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
1770
        for path, keys in zip(paths, map_keys):
2255.2.131 by John Arbash Meinel
Change the return values for bisect functions so they just return
1771
            if keys is None:
1772
                # This should not be present in the output
1773
                continue
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
1774
            expected[path] = sorted(expected_map[k] for k in keys)
2255.2.131 by John Arbash Meinel
Change the return values for bisect functions so they just return
1775
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
1776
        # The returned values are just arranged randomly based on when they
1777
        # were read, for testing, make sure it is properly sorted.
1778
        for path in result:
1779
            result[path].sort()
2255.2.131 by John Arbash Meinel
Change the return values for bisect functions so they just return
1780
1781
        self.assertEqual(expected, result)
1782
1783
    def assertBisectDirBlocks(self, expected_map, map_keys, state, paths):
2255.2.130 by John Arbash Meinel
Add a very similar function which grabs everything for a particular directory block.
1784
        """Assert that bisecting for dirbblocks returns the right result.
1785
2255.2.132 by John Arbash Meinel
Implement _bisect_recursive, which uses multiple bisect calls to
1786
        :param expected_map: A map from key => expected values
2255.2.131 by John Arbash Meinel
Change the return values for bisect functions so they just return
1787
        :param map_keys: A nested list of paths we expect to be returned.
2255.2.130 by John Arbash Meinel
Add a very similar function which grabs everything for a particular directory block.
1788
            Something like [['a', 'b', 'f'], ['b/c', 'b/d']]
1789
        :param state: The DirState object.
1790
        :param paths: A list of directories
1791
        """
1792
        result = state._bisect_dirblocks(paths)
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
1793
        self.assertEqual(len(map_keys), len(paths))
2255.2.131 by John Arbash Meinel
Change the return values for bisect functions so they just return
1794
        expected = {}
1795
        for path, keys in zip(paths, map_keys):
1796
            if keys is None:
1797
                # This should not be present in the output
1798
                continue
1799
            expected[path] = sorted(expected_map[k] for k in keys)
1800
        for path in result:
1801
            result[path].sort()
1802
1803
        self.assertEqual(expected, result)
2255.2.130 by John Arbash Meinel
Add a very similar function which grabs everything for a particular directory block.
1804
2255.2.132 by John Arbash Meinel
Implement _bisect_recursive, which uses multiple bisect calls to
1805
    def assertBisectRecursive(self, expected_map, map_keys, state, paths):
1806
        """Assert the return value of a recursive bisection.
1807
1808
        :param expected_map: A map from key => entry value
1809
        :param map_keys: A list of paths we expect to be returned.
1810
            Something like ['a', 'b', 'f', 'b/d', 'b/d2']
1811
        :param state: The DirState object.
1812
        :param paths: A list of files and directories. It will be broken up
1813
            into (dir, name) pairs and sorted before calling _bisect_recursive.
1814
        """
1815
        expected = {}
1816
        for key in map_keys:
1817
            entry = expected_map[key]
1818
            dir_name_id, trees_info = entry
1819
            expected[dir_name_id] = trees_info
1820
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
1821
        result = state._bisect_recursive(paths)
2255.2.132 by John Arbash Meinel
Implement _bisect_recursive, which uses multiple bisect calls to
1822
1823
        self.assertEqual(expected, result)
1824
2255.3.2 by John Arbash Meinel
(broken) some basic work on adding bisect functionality to dirstate.
1825
    def test_bisect_each(self):
1826
        """Find a single record using bisect."""
2255.2.125 by John Arbash Meinel
Initial effort at adding a basic _bisect function to DirState.
1827
        tree, state, expected = self.create_basic_dirstate()
2255.3.2 by John Arbash Meinel
(broken) some basic work on adding bisect functionality to dirstate.
1828
1829
        # Bisect should return the rows for the specified files.
2255.2.131 by John Arbash Meinel
Change the return values for bisect functions so they just return
1830
        self.assertBisect(expected, [['']], state, [''])
1831
        self.assertBisect(expected, [['a']], state, ['a'])
1832
        self.assertBisect(expected, [['b']], state, ['b'])
1833
        self.assertBisect(expected, [['b/c']], state, ['b/c'])
1834
        self.assertBisect(expected, [['b/d']], state, ['b/d'])
1835
        self.assertBisect(expected, [['b/d/e']], state, ['b/d/e'])
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
1836
        self.assertBisect(expected, [['b-c']], state, ['b-c'])
2255.2.131 by John Arbash Meinel
Change the return values for bisect functions so they just return
1837
        self.assertBisect(expected, [['f']], state, ['f'])
2255.2.125 by John Arbash Meinel
Initial effort at adding a basic _bisect function to DirState.
1838
1839
    def test_bisect_multi(self):
1840
        """Bisect can be used to find multiple records at the same time."""
1841
        tree, state, expected = self.create_basic_dirstate()
1842
        # Bisect should be capable of finding multiple entries at the same time
2255.2.131 by John Arbash Meinel
Change the return values for bisect functions so they just return
1843
        self.assertBisect(expected, [['a'], ['b'], ['f']],
2255.2.126 by John Arbash Meinel
Switch the bisect code to support the fact that we can have
1844
                          state, ['a', 'b', 'f'])
2255.2.131 by John Arbash Meinel
Change the return values for bisect functions so they just return
1845
        self.assertBisect(expected, [['f'], ['b/d'], ['b/d/e']],
2474.1.61 by John Arbash Meinel
Finish fixing DirState._bisect and the bisect tests
1846
                          state, ['f', 'b/d', 'b/d/e'])
1847
        self.assertBisect(expected, [['b'], ['b-c'], ['b/c']],
1848
                          state, ['b', 'b-c', 'b/c'])
2255.2.126 by John Arbash Meinel
Switch the bisect code to support the fact that we can have
1849
1850
    def test_bisect_one_page(self):
1851
        """Test bisect when there is only 1 page to read"""
1852
        tree, state, expected = self.create_basic_dirstate()
1853
        state._bisect_page_size = 5000
2255.2.131 by John Arbash Meinel
Change the return values for bisect functions so they just return
1854
        self.assertBisect(expected,[['']], state, [''])
1855
        self.assertBisect(expected,[['a']], state, ['a'])
1856
        self.assertBisect(expected,[['b']], state, ['b'])
1857
        self.assertBisect(expected,[['b/c']], state, ['b/c'])
1858
        self.assertBisect(expected,[['b/d']], state, ['b/d'])
1859
        self.assertBisect(expected,[['b/d/e']], state, ['b/d/e'])
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
1860
        self.assertBisect(expected,[['b-c']], state, ['b-c'])
2255.2.131 by John Arbash Meinel
Change the return values for bisect functions so they just return
1861
        self.assertBisect(expected,[['f']], state, ['f'])
1862
        self.assertBisect(expected,[['a'], ['b'], ['f']],
2255.2.126 by John Arbash Meinel
Switch the bisect code to support the fact that we can have
1863
                          state, ['a', 'b', 'f'])
2474.1.61 by John Arbash Meinel
Finish fixing DirState._bisect and the bisect tests
1864
        self.assertBisect(expected, [['b/d'], ['b/d/e'], ['f']],
2255.2.126 by John Arbash Meinel
Switch the bisect code to support the fact that we can have
1865
                          state, ['b/d', 'b/d/e', 'f'])
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
1866
        self.assertBisect(expected, [['b'], ['b/c'], ['b-c']],
1867
                          state, ['b', 'b/c', 'b-c'])
2255.2.126 by John Arbash Meinel
Switch the bisect code to support the fact that we can have
1868
1869
    def test_bisect_duplicate_paths(self):
1870
        """When bisecting for a path, handle multiple entries."""
1871
        tree, state, expected = self.create_duplicated_dirstate()
1872
1873
        # Now make sure that both records are properly returned.
2255.2.131 by John Arbash Meinel
Change the return values for bisect functions so they just return
1874
        self.assertBisect(expected, [['']], state, [''])
1875
        self.assertBisect(expected, [['a', 'a2']], state, ['a'])
1876
        self.assertBisect(expected, [['b', 'b2']], state, ['b'])
1877
        self.assertBisect(expected, [['b/c', 'b/c2']], state, ['b/c'])
1878
        self.assertBisect(expected, [['b/d', 'b/d2']], state, ['b/d'])
1879
        self.assertBisect(expected, [['b/d/e', 'b/d/e2']],
2255.2.129 by John Arbash Meinel
Start cleaning up the code, and fix one more edge case
1880
                          state, ['b/d/e'])
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
1881
        self.assertBisect(expected, [['b-c', 'b-c2']], state, ['b-c'])
2255.2.131 by John Arbash Meinel
Change the return values for bisect functions so they just return
1882
        self.assertBisect(expected, [['f', 'f2']], state, ['f'])
2255.2.126 by John Arbash Meinel
Switch the bisect code to support the fact that we can have
1883
1884
    def test_bisect_page_size_too_small(self):
2255.2.128 by John Arbash Meinel
Rather than falling over when the page size is to small, just increase it and try again.
1885
        """If the page size is too small, we will auto increase it."""
2255.2.126 by John Arbash Meinel
Switch the bisect code to support the fact that we can have
1886
        tree, state, expected = self.create_basic_dirstate()
1887
        state._bisect_page_size = 50
2255.2.131 by John Arbash Meinel
Change the return values for bisect functions so they just return
1888
        self.assertBisect(expected, [None], state, ['b/e'])
1889
        self.assertBisect(expected, [['a']], state, ['a'])
1890
        self.assertBisect(expected, [['b']], state, ['b'])
1891
        self.assertBisect(expected, [['b/c']], state, ['b/c'])
1892
        self.assertBisect(expected, [['b/d']], state, ['b/d'])
1893
        self.assertBisect(expected, [['b/d/e']], state, ['b/d/e'])
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
1894
        self.assertBisect(expected, [['b-c']], state, ['b-c'])
2255.2.131 by John Arbash Meinel
Change the return values for bisect functions so they just return
1895
        self.assertBisect(expected, [['f']], state, ['f'])
2255.2.126 by John Arbash Meinel
Switch the bisect code to support the fact that we can have
1896
1897
    def test_bisect_missing(self):
1898
        """Test that bisect return None if it cannot find a path."""
1899
        tree, state, expected = self.create_basic_dirstate()
2255.2.131 by John Arbash Meinel
Change the return values for bisect functions so they just return
1900
        self.assertBisect(expected, [None], state, ['foo'])
1901
        self.assertBisect(expected, [None], state, ['b/foo'])
1902
        self.assertBisect(expected, [None], state, ['bar/foo'])
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
1903
        self.assertBisect(expected, [None], state, ['b-c/foo'])
2255.2.126 by John Arbash Meinel
Switch the bisect code to support the fact that we can have
1904
2255.2.131 by John Arbash Meinel
Change the return values for bisect functions so they just return
1905
        self.assertBisect(expected, [['a'], None, ['b/d']],
2255.2.126 by John Arbash Meinel
Switch the bisect code to support the fact that we can have
1906
                          state, ['a', 'foo', 'b/d'])
2255.2.127 by John Arbash Meinel
Expand the test suite to cover more cases.
1907
1908
    def test_bisect_rename(self):
1909
        """Check that we find a renamed row."""
1910
        tree, state, expected = self.create_renamed_dirstate()
1911
1912
        # Search for the pre and post renamed entries
2255.2.131 by John Arbash Meinel
Change the return values for bisect functions so they just return
1913
        self.assertBisect(expected, [['a']], state, ['a'])
1914
        self.assertBisect(expected, [['b/g']], state, ['b/g'])
2255.2.132 by John Arbash Meinel
Implement _bisect_recursive, which uses multiple bisect calls to
1915
        self.assertBisect(expected, [['b/d']], state, ['b/d'])
1916
        self.assertBisect(expected, [['h']], state, ['h'])
1917
1918
        # What about b/d/e? shouldn't that also get 2 directory entries?
1919
        self.assertBisect(expected, [['b/d/e']], state, ['b/d/e'])
1920
        self.assertBisect(expected, [['h/e']], state, ['h/e'])
2255.2.130 by John Arbash Meinel
Add a very similar function which grabs everything for a particular directory block.
1921
1922
    def test_bisect_dirblocks(self):
1923
        tree, state, expected = self.create_duplicated_dirstate()
1924
        self.assertBisectDirBlocks(expected,
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
1925
            [['', 'a', 'a2', 'b', 'b2', 'b-c', 'b-c2', 'f', 'f2']],
1926
            state, [''])
2255.2.130 by John Arbash Meinel
Add a very similar function which grabs everything for a particular directory block.
1927
        self.assertBisectDirBlocks(expected,
1928
            [['b/c', 'b/c2', 'b/d', 'b/d2']], state, ['b'])
1929
        self.assertBisectDirBlocks(expected,
1930
            [['b/d/e', 'b/d/e2']], state, ['b/d'])
1931
        self.assertBisectDirBlocks(expected,
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
1932
            [['', 'a', 'a2', 'b', 'b2', 'b-c', 'b-c2', 'f', 'f2'],
2255.2.130 by John Arbash Meinel
Add a very similar function which grabs everything for a particular directory block.
1933
             ['b/c', 'b/c2', 'b/d', 'b/d2'],
1934
             ['b/d/e', 'b/d/e2'],
1935
            ], state, ['', 'b', 'b/d'])
1936
1937
    def test_bisect_dirblocks_missing(self):
1938
        tree, state, expected = self.create_basic_dirstate()
1939
        self.assertBisectDirBlocks(expected, [['b/d/e'], None],
1940
            state, ['b/d', 'b/e'])
1941
        # Files don't show up in this search
1942
        self.assertBisectDirBlocks(expected, [None], state, ['a'])
1943
        self.assertBisectDirBlocks(expected, [None], state, ['b/c'])
1944
        self.assertBisectDirBlocks(expected, [None], state, ['c'])
1945
        self.assertBisectDirBlocks(expected, [None], state, ['b/d/e'])
1946
        self.assertBisectDirBlocks(expected, [None], state, ['f'])
2255.2.132 by John Arbash Meinel
Implement _bisect_recursive, which uses multiple bisect calls to
1947
1948
    def test_bisect_recursive_each(self):
1949
        tree, state, expected = self.create_basic_dirstate()
1950
        self.assertBisectRecursive(expected, ['a'], state, ['a'])
1951
        self.assertBisectRecursive(expected, ['b/c'], state, ['b/c'])
1952
        self.assertBisectRecursive(expected, ['b/d/e'], state, ['b/d/e'])
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
1953
        self.assertBisectRecursive(expected, ['b-c'], state, ['b-c'])
2255.2.132 by John Arbash Meinel
Implement _bisect_recursive, which uses multiple bisect calls to
1954
        self.assertBisectRecursive(expected, ['b/d', 'b/d/e'],
1955
                                   state, ['b/d'])
1956
        self.assertBisectRecursive(expected, ['b', 'b/c', 'b/d', 'b/d/e'],
1957
                                   state, ['b'])
2474.1.61 by John Arbash Meinel
Finish fixing DirState._bisect and the bisect tests
1958
        self.assertBisectRecursive(expected, ['', 'a', 'b', 'b-c', 'f', 'b/c',
2255.2.132 by John Arbash Meinel
Implement _bisect_recursive, which uses multiple bisect calls to
1959
                                              'b/d', 'b/d/e'],
1960
                                   state, [''])
1961
1962
    def test_bisect_recursive_multiple(self):
1963
        tree, state, expected = self.create_basic_dirstate()
1964
        self.assertBisectRecursive(expected, ['a', 'b/c'], state, ['a', 'b/c'])
1965
        self.assertBisectRecursive(expected, ['b/d', 'b/d/e'],
1966
                                   state, ['b/d', 'b/d/e'])
1967
1968
    def test_bisect_recursive_missing(self):
1969
        tree, state, expected = self.create_basic_dirstate()
1970
        self.assertBisectRecursive(expected, [], state, ['d'])
1971
        self.assertBisectRecursive(expected, [], state, ['b/e'])
1972
        self.assertBisectRecursive(expected, [], state, ['g'])
1973
        self.assertBisectRecursive(expected, ['a'], state, ['a', 'g'])
1974
1975
    def test_bisect_recursive_renamed(self):
1976
        tree, state, expected = self.create_renamed_dirstate()
1977
1978
        # Looking for either renamed item should find the other
1979
        self.assertBisectRecursive(expected, ['a', 'b/g'], state, ['a'])
1980
        self.assertBisectRecursive(expected, ['a', 'b/g'], state, ['b/g'])
1981
        # Looking in the containing directory should find the rename target,
1982
        # and anything in a subdir of the renamed target.
1983
        self.assertBisectRecursive(expected, ['a', 'b', 'b/c', 'b/d',
1984
                                              'b/d/e', 'b/g', 'h', 'h/e'],
1985
                                   state, ['b'])
1986
2255.8.2 by John Arbash Meinel
Add a helper function, which allows us to store keys as plain paths,
1987
2323.5.4 by Martin Pool
Move some dirstate test setup methods into the base class
1988
class TestDirstateValidation(TestCaseWithDirState):
1989
1990
    def test_validate_correct_dirstate(self):
1991
        state = self.create_complex_dirstate()
1992
        state._validate()
1993
        state.unlock()
1994
        # and make sure we can also validate with a read lock
1995
        state.lock_read()
1996
        try:
1997
            state._validate()
1998
        finally:
1999
            state.unlock()
2323.5.6 by Martin Pool
Add some tests and better messages for DirState._validate
2000
2001
    def test_dirblock_not_sorted(self):
2002
        tree, state, expected = self.create_renamed_dirstate()
2003
        state._read_dirblocks_if_needed()
2004
        last_dirblock = state._dirblocks[-1]
2005
        # we're appending to the dirblock, but this name comes before some of
2006
        # the existing names; that's wrong
2007
        last_dirblock[1].append(
2008
            (('h', 'aaaa', 'a-id'),
2009
             [('a', '', 0, False, ''),
2010
              ('a', '', 0, False, '')]))
2011
        e = self.assertRaises(AssertionError,
2012
            state._validate)
2013
        self.assertContainsRe(str(e), 'not sorted')
2014
2015
    def test_dirblock_name_mismatch(self):
2016
        tree, state, expected = self.create_renamed_dirstate()
2017
        state._read_dirblocks_if_needed()
2018
        last_dirblock = state._dirblocks[-1]
2019
        # add an entry with the wrong directory name
2020
        last_dirblock[1].append(
2021
            (('', 'z', 'a-id'),
2022
             [('a', '', 0, False, ''),
2023
              ('a', '', 0, False, '')]))
2024
        e = self.assertRaises(AssertionError,
2025
            state._validate)
2026
        self.assertContainsRe(str(e),
2027
            "doesn't match directory name")
2028
2323.5.7 by Martin Pool
Better DirState._validate and tests for it.
2029
    def test_dirblock_missing_rename(self):
2030
        tree, state, expected = self.create_renamed_dirstate()
2031
        state._read_dirblocks_if_needed()
2032
        last_dirblock = state._dirblocks[-1]
2323.5.6 by Martin Pool
Add some tests and better messages for DirState._validate
2033
        # make another entry for a-id, without a correct 'r' pointer to
2034
        # the real occurrence in the working tree
2323.5.7 by Martin Pool
Better DirState._validate and tests for it.
2035
        last_dirblock[1].append(
2036
            (('h', 'z', 'a-id'),
2037
             [('a', '', 0, False, ''),
2038
              ('a', '', 0, False, '')]))
2039
        e = self.assertRaises(AssertionError,
2040
            state._validate)
2041
        self.assertContainsRe(str(e),
2042
            'file a-id is absent in row')
2474.1.41 by John Arbash Meinel
Change the name of cmp_dirblock_strings to cmp_by_dirs
2043
2645.2.1 by Wouter van Heyst
The DirState fingerprint for tree-references should be an empty string instead of None
2044
2045
class TestDirstateTreeReference(TestCaseWithDirState):
2046
2047
    def test_reference_revision_is_none(self):
2048
        tree = self.make_branch_and_tree('tree', format='dirstate-with-subtree')
2049
        subtree = self.make_branch_and_tree('tree/subtree',
2050
                            format='dirstate-with-subtree')
2051
        subtree.set_root_id('subtree')
2052
        tree.add_reference(subtree)
2053
        tree.add('subtree')
2054
        state = dirstate.DirState.from_tree(tree, 'dirstate')
2055
        key = ('', 'subtree', 'subtree')
2056
        expected = ('', [(key,
2057
            [('t', '', 0, False, 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')])])
2058
2059
        try:
2060
            self.assertEqual(expected, state._find_block(key))
2061
        finally:
2062
            state.unlock()
2984.1.1 by John Arbash Meinel
Fix bug #161131: Correct DirState._discard_merge_parents logic.
2063
2064
2065
class TestDiscardMergeParents(TestCaseWithDirState):
2066
2067
    def test_discard_no_parents(self):
2068
        # This should be a no-op
2069
        state = self.create_empty_dirstate()
2070
        self.addCleanup(state.unlock)
2071
        state._discard_merge_parents()
2072
        state._validate()
2073
2074
    def test_discard_one_parent(self):
2075
        # No-op
2076
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
2077
        root_entry_direntry = ('', '', 'a-root-value'), [
2078
            ('d', '', 0, False, packed_stat),
2079
            ('d', '', 0, False, packed_stat),
2080
            ]
2081
        dirblocks = []
2082
        dirblocks.append(('', [root_entry_direntry]))
2083
        dirblocks.append(('', []))
2084
2085
        state = self.create_empty_dirstate()
2086
        self.addCleanup(state.unlock)
2087
        state._set_data(['parent-id'], dirblocks[:])
2088
        state._validate()
2089
2090
        state._discard_merge_parents()
2091
        state._validate()
2092
        self.assertEqual(dirblocks, state._dirblocks)
2093
2094
    def test_discard_simple(self):
2095
        # No-op
2096
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
2097
        root_entry_direntry = ('', '', 'a-root-value'), [
2098
            ('d', '', 0, False, packed_stat),
2099
            ('d', '', 0, False, packed_stat),
2100
            ('d', '', 0, False, packed_stat),
2101
            ]
2102
        expected_root_entry_direntry = ('', '', 'a-root-value'), [
2103
            ('d', '', 0, False, packed_stat),
2104
            ('d', '', 0, False, packed_stat),
2105
            ]
2106
        dirblocks = []
2107
        dirblocks.append(('', [root_entry_direntry]))
2108
        dirblocks.append(('', []))
2109
2110
        state = self.create_empty_dirstate()
2111
        self.addCleanup(state.unlock)
2112
        state._set_data(['parent-id', 'merged-id'], dirblocks[:])
2113
        state._validate()
2114
2115
        # This should strip of the extra column
2116
        state._discard_merge_parents()
2117
        state._validate()
2118
        expected_dirblocks = [('', [expected_root_entry_direntry]), ('', [])]
2119
        self.assertEqual(expected_dirblocks, state._dirblocks)
2120
2121
    def test_discard_absent(self):
2122
        """If entries are only in a merge, discard should remove the entries"""
2123
        null_stat = dirstate.DirState.NULLSTAT
2124
        present_dir = ('d', '', 0, False, null_stat)
2125
        present_file = ('f', '', 0, False, null_stat)
2126
        absent = dirstate.DirState.NULL_PARENT_DETAILS
2127
        root_key = ('', '', 'a-root-value')
2128
        file_in_root_key = ('', 'file-in-root', 'a-file-id')
2129
        file_in_merged_key = ('', 'file-in-merged', 'b-file-id')
2130
        dirblocks = [('', [(root_key, [present_dir, present_dir, present_dir])]),
2131
                     ('', [(file_in_merged_key,
2132
                            [absent, absent, present_file]),
2133
                           (file_in_root_key,
2134
                            [present_file, present_file, present_file]),
2135
                          ]),
2136
                    ]
2137
2138
        state = self.create_empty_dirstate()
2139
        self.addCleanup(state.unlock)
2140
        state._set_data(['parent-id', 'merged-id'], dirblocks[:])
2141
        state._validate()
2142
2143
        exp_dirblocks = [('', [(root_key, [present_dir, present_dir])]),
2144
                         ('', [(file_in_root_key,
2145
                                [present_file, present_file]),
2146
                              ]),
2147
                        ]
2148
        state._discard_merge_parents()
2149
        state._validate()
2150
        self.assertEqual(exp_dirblocks, state._dirblocks)
2151
2152
    def test_discard_renamed(self):
2153
        null_stat = dirstate.DirState.NULLSTAT
2154
        present_dir = ('d', '', 0, False, null_stat)
2155
        present_file = ('f', '', 0, False, null_stat)
2156
        absent = dirstate.DirState.NULL_PARENT_DETAILS
2157
        root_key = ('', '', 'a-root-value')
2158
        file_in_root_key = ('', 'file-in-root', 'a-file-id')
2159
        # Renamed relative to parent
2160
        file_rename_s_key = ('', 'file-s', 'b-file-id')
2161
        file_rename_t_key = ('', 'file-t', 'b-file-id')
2162
        # And one that is renamed between the parents, but absent in this
2163
        key_in_1 = ('', 'file-in-1', 'c-file-id')
2164
        key_in_2 = ('', 'file-in-2', 'c-file-id')
2165
2166
        dirblocks = [
2167
            ('', [(root_key, [present_dir, present_dir, present_dir])]),
2168
            ('', [(key_in_1,
2169
                   [absent, present_file, ('r', 'file-in-2', 'c-file-id')]),
2170
                  (key_in_2,
2171
                   [absent, ('r', 'file-in-1', 'c-file-id'), present_file]),
2172
                  (file_in_root_key,
2173
                   [present_file, present_file, present_file]),
2174
                  (file_rename_s_key,
2175
                   [('r', 'file-t', 'b-file-id'), absent, present_file]),
2176
                  (file_rename_t_key,
2177
                   [present_file, absent, ('r', 'file-s', 'b-file-id')]),
2178
                 ]),
2179
        ]
2180
        exp_dirblocks = [
2181
            ('', [(root_key, [present_dir, present_dir])]),
2182
            ('', [(key_in_1, [absent, present_file]),
2183
                  (file_in_root_key, [present_file, present_file]),
2184
                  (file_rename_t_key, [present_file, absent]),
2185
                 ]),
2186
        ]
2187
        state = self.create_empty_dirstate()
2188
        self.addCleanup(state.unlock)
2189
        state._set_data(['parent-id', 'merged-id'], dirblocks[:])
2190
        state._validate()
2191
2192
        state._discard_merge_parents()
2193
        state._validate()
2194
        self.assertEqual(exp_dirblocks, state._dirblocks)
2195
2196
    def test_discard_all_subdir(self):
2197
        null_stat = dirstate.DirState.NULLSTAT
2198
        present_dir = ('d', '', 0, False, null_stat)
2199
        present_file = ('f', '', 0, False, null_stat)
2200
        absent = dirstate.DirState.NULL_PARENT_DETAILS
2201
        root_key = ('', '', 'a-root-value')
2202
        subdir_key = ('', 'sub', 'dir-id')
2203
        child1_key = ('sub', 'child1', 'child1-id')
2204
        child2_key = ('sub', 'child2', 'child2-id')
2205
        child3_key = ('sub', 'child3', 'child3-id')
2206
2207
        dirblocks = [
2208
            ('', [(root_key, [present_dir, present_dir, present_dir])]),
2209
            ('', [(subdir_key, [present_dir, present_dir, present_dir])]),
2210
            ('sub', [(child1_key, [absent, absent, present_file]),
2211
                     (child2_key, [absent, absent, present_file]),
2212
                     (child3_key, [absent, absent, present_file]),
2213
                    ]),
2214
        ]
2215
        exp_dirblocks = [
2216
            ('', [(root_key, [present_dir, present_dir])]),
2217
            ('', [(subdir_key, [present_dir, present_dir])]),
2218
            ('sub', []),
2219
        ]
2220
        state = self.create_empty_dirstate()
2221
        self.addCleanup(state.unlock)
2222
        state._set_data(['parent-id', 'merged-id'], dirblocks[:])
2223
        state._validate()
2224
2225
        state._discard_merge_parents()
2226
        state._validate()
2227
        self.assertEqual(exp_dirblocks, state._dirblocks)
3477.2.1 by John Arbash Meinel
Assert that we properly encode inv_entry.symlink_target, (bug #135320)
2228
2229
4241.14.10 by Vincent Ladeuil
Add DirReader parametrized tests.
2230
class Test_InvEntryToDetails(tests.TestCase):
3477.2.1 by John Arbash Meinel
Assert that we properly encode inv_entry.symlink_target, (bug #135320)
2231
2232
    def assertDetails(self, expected, inv_entry):
2233
        details = dirstate.DirState._inv_entry_to_details(inv_entry)
2234
        self.assertEqual(expected, details)
3477.2.2 by John Arbash Meinel
Change how we handle unicode targets, and add a NEWS entry.
2235
        # details should always allow join() and always be a plain str when
2236
        # finished
2237
        (minikind, fingerprint, size, executable, tree_data) = details
2238
        self.assertIsInstance(minikind, str)
2239
        self.assertIsInstance(fingerprint, str)
2240
        self.assertIsInstance(tree_data, str)
3477.2.1 by John Arbash Meinel
Assert that we properly encode inv_entry.symlink_target, (bug #135320)
2241
2242
    def test_unicode_symlink(self):
4241.14.10 by Vincent Ladeuil
Add DirReader parametrized tests.
2243
        inv_entry = inventory.InventoryLink('link-file-id',
2244
                                            u'nam\N{Euro Sign}e',
3477.2.1 by John Arbash Meinel
Assert that we properly encode inv_entry.symlink_target, (bug #135320)
2245
                                            'link-parent-id')
2246
        inv_entry.revision = 'link-revision-id'
4241.14.10 by Vincent Ladeuil
Add DirReader parametrized tests.
2247
        target = u'link-targ\N{Euro Sign}t'
2248
        inv_entry.symlink_target = target
2249
        self.assertDetails(('l', target.encode('UTF-8'), 0, False,
2250
                            'link-revision-id'), inv_entry)
2251
2252
2253
class TestSHA1Provider(tests.TestCaseInTempDir):
4132.2.1 by Ian Clatworthy
make sha1_provider a parameter to DirState()
2254
2255
    def test_sha1provider_is_an_interface(self):
4132.2.5 by Ian Clatworthy
feedback from poolie - use SHA, not Sha, in class names
2256
        p = dirstate.SHA1Provider()
4132.2.1 by Ian Clatworthy
make sha1_provider a parameter to DirState()
2257
        self.assertRaises(NotImplementedError, p.sha1, "foo")
2258
        self.assertRaises(NotImplementedError, p.stat_and_sha1, "foo")
2259
2260
    def test_defaultsha1provider_sha1(self):
2261
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
2262
        self.build_tree_contents([('foo', text)])
2263
        expected_sha = osutils.sha_string(text)
4132.2.5 by Ian Clatworthy
feedback from poolie - use SHA, not Sha, in class names
2264
        p = dirstate.DefaultSHA1Provider()
4132.2.1 by Ian Clatworthy
make sha1_provider a parameter to DirState()
2265
        self.assertEqual(expected_sha, p.sha1('foo'))
2266
2267
    def test_defaultsha1provider_stat_and_sha1(self):
2268
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
2269
        self.build_tree_contents([('foo', text)])
2270
        expected_sha = osutils.sha_string(text)
4132.2.5 by Ian Clatworthy
feedback from poolie - use SHA, not Sha, in class names
2271
        p = dirstate.DefaultSHA1Provider()
4132.2.1 by Ian Clatworthy
make sha1_provider a parameter to DirState()
2272
        statvalue, sha1 = p.stat_and_sha1('foo')
2273
        self.assertTrue(len(statvalue) >= 10)
2274
        self.assertEqual(len(text), statvalue.st_size)
2275
        self.assertEqual(expected_sha, sha1)