/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_dirstate.py

  • Committer: Jelmer Vernooij
  • Date: 2017-11-11 13:10:32 UTC
  • mto: This revision was merged to the branch mainline in revision 6804.
  • Revision ID: jelmer@jelmer.uk-20171111131032-31lgi8qmvlz8363d
Fix typos.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006-2011 Canonical Ltd
 
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
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Tests of the dirstate functionality being built for WorkingTreeFormat4."""
 
18
 
 
19
import os
 
20
import tempfile
 
21
 
 
22
from .. import (
 
23
    controldir,
 
24
    errors,
 
25
    memorytree,
 
26
    osutils,
 
27
    revision as _mod_revision,
 
28
    revisiontree,
 
29
    tests,
 
30
    )
 
31
from ..bzr import (
 
32
    dirstate,
 
33
    inventory,
 
34
    inventorytree,
 
35
    workingtree_4,
 
36
    )
 
37
from . import (
 
38
    features,
 
39
    test_osutils,
 
40
    )
 
41
from .scenarios import load_tests_apply_scenarios
 
42
 
 
43
 
 
44
# TODO:
 
45
# TESTS to write:
 
46
# general checks for NOT_IN_MEMORY error conditions.
 
47
# set_path_id on a NOT_IN_MEMORY dirstate
 
48
# set_path_id  unicode support
 
49
# set_path_id  setting id of a path not root
 
50
# set_path_id  setting id when there are parents without the id in the parents
 
51
# set_path_id  setting id when there are parents with the id in the parents
 
52
# set_path_id  setting id when state is not in memory
 
53
# set_path_id  setting id when state is in memory unmodified
 
54
# set_path_id  setting id when state is in memory modified
 
55
 
 
56
 
 
57
class TestErrors(tests.TestCase):
 
58
 
 
59
    def test_dirstate_corrupt(self):
 
60
        error = dirstate.DirstateCorrupt('.bzr/checkout/dirstate',
 
61
                                       'trailing garbage: "x"')
 
62
        self.assertEqualDiff("The dirstate file (.bzr/checkout/dirstate)"
 
63
            " appears to be corrupt: trailing garbage: \"x\"",
 
64
            str(error))
 
65
 
 
66
 
 
67
load_tests = load_tests_apply_scenarios
 
68
 
 
69
 
 
70
class TestCaseWithDirState(tests.TestCaseWithTransport):
 
71
    """Helper functions for creating DirState objects with various content."""
 
72
 
 
73
    scenarios = test_osutils.dir_reader_scenarios()
 
74
 
 
75
    # Set by load_tests
 
76
    _dir_reader_class = None
 
77
    _native_to_unicode = None # Not used yet
 
78
 
 
79
    def setUp(self):
 
80
        super(TestCaseWithDirState, self).setUp()
 
81
        self.overrideAttr(osutils,
 
82
                          '_selected_dir_reader', self._dir_reader_class())
 
83
 
 
84
    def create_empty_dirstate(self):
 
85
        """Return a locked but empty dirstate"""
 
86
        state = dirstate.DirState.initialize('dirstate')
 
87
        return state
 
88
 
 
89
    def create_dirstate_with_root(self):
 
90
        """Return a write-locked state with a single root entry."""
 
91
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
 
92
        root_entry_direntry = ('', '', 'a-root-value'), [
 
93
            ('d', '', 0, False, packed_stat),
 
94
            ]
 
95
        dirblocks = []
 
96
        dirblocks.append(('', [root_entry_direntry]))
 
97
        dirblocks.append(('', []))
 
98
        state = self.create_empty_dirstate()
 
99
        try:
 
100
            state._set_data([], dirblocks)
 
101
            state._validate()
 
102
        except:
 
103
            state.unlock()
 
104
            raise
 
105
        return state
 
106
 
 
107
    def create_dirstate_with_root_and_subdir(self):
 
108
        """Return a locked DirState with a root and a subdir"""
 
109
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
 
110
        subdir_entry = ('', 'subdir', 'subdir-id'), [
 
111
            ('d', '', 0, False, packed_stat),
 
112
            ]
 
113
        state = self.create_dirstate_with_root()
 
114
        try:
 
115
            dirblocks = list(state._dirblocks)
 
116
            dirblocks[1][1].append(subdir_entry)
 
117
            state._set_data([], dirblocks)
 
118
        except:
 
119
            state.unlock()
 
120
            raise
 
121
        return state
 
122
 
 
123
    def create_complex_dirstate(self):
 
124
        """This dirstate contains multiple files and directories.
 
125
 
 
126
         /        a-root-value
 
127
         a/       a-dir
 
128
         b/       b-dir
 
129
         c        c-file
 
130
         d        d-file
 
131
         a/e/     e-dir
 
132
         a/f      f-file
 
133
         b/g      g-file
 
134
         b/h\xc3\xa5  h-\xc3\xa5-file  #This is u'\xe5' encoded into utf-8
 
135
 
 
136
        Notice that a/e is an empty directory.
 
137
 
 
138
        :return: The dirstate, still write-locked.
 
139
        """
 
140
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
 
141
        null_sha = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
 
142
        root_entry = ('', '', 'a-root-value'), [
 
143
            ('d', '', 0, False, packed_stat),
 
144
            ]
 
145
        a_entry = ('', 'a', 'a-dir'), [
 
146
            ('d', '', 0, False, packed_stat),
 
147
            ]
 
148
        b_entry = ('', 'b', 'b-dir'), [
 
149
            ('d', '', 0, False, packed_stat),
 
150
            ]
 
151
        c_entry = ('', 'c', 'c-file'), [
 
152
            ('f', null_sha, 10, False, packed_stat),
 
153
            ]
 
154
        d_entry = ('', 'd', 'd-file'), [
 
155
            ('f', null_sha, 20, False, packed_stat),
 
156
            ]
 
157
        e_entry = ('a', 'e', 'e-dir'), [
 
158
            ('d', '', 0, False, packed_stat),
 
159
            ]
 
160
        f_entry = ('a', 'f', 'f-file'), [
 
161
            ('f', null_sha, 30, False, packed_stat),
 
162
            ]
 
163
        g_entry = ('b', 'g', 'g-file'), [
 
164
            ('f', null_sha, 30, False, packed_stat),
 
165
            ]
 
166
        h_entry = ('b', 'h\xc3\xa5', 'h-\xc3\xa5-file'), [
 
167
            ('f', null_sha, 40, False, packed_stat),
 
168
            ]
 
169
        dirblocks = []
 
170
        dirblocks.append(('', [root_entry]))
 
171
        dirblocks.append(('', [a_entry, b_entry, c_entry, d_entry]))
 
172
        dirblocks.append(('a', [e_entry, f_entry]))
 
173
        dirblocks.append(('b', [g_entry, h_entry]))
 
174
        state = dirstate.DirState.initialize('dirstate')
 
175
        state._validate()
 
176
        try:
 
177
            state._set_data([], dirblocks)
 
178
        except:
 
179
            state.unlock()
 
180
            raise
 
181
        return state
 
182
 
 
183
    def check_state_with_reopen(self, expected_result, state):
 
184
        """Check that state has current state expected_result.
 
185
 
 
186
        This will check the current state, open the file anew and check it
 
187
        again.
 
188
        This function expects the current state to be locked for writing, and
 
189
        will unlock it before re-opening.
 
190
        This is required because we can't open a lock_read() while something
 
191
        else has a lock_write().
 
192
            write => mutually exclusive lock
 
193
            read => shared lock
 
194
        """
 
195
        # The state should already be write locked, since we just had to do
 
196
        # some operation to get here.
 
197
        self.assertTrue(state._lock_token is not None)
 
198
        try:
 
199
            self.assertEqual(expected_result[0],  state.get_parent_ids())
 
200
            # there should be no ghosts in this tree.
 
201
            self.assertEqual([], state.get_ghosts())
 
202
            # there should be one fileid in this tree - the root of the tree.
 
203
            self.assertEqual(expected_result[1], list(state._iter_entries()))
 
204
            state.save()
 
205
        finally:
 
206
            state.unlock()
 
207
        del state
 
208
        state = dirstate.DirState.on_file('dirstate')
 
209
        state.lock_read()
 
210
        try:
 
211
            self.assertEqual(expected_result[1], list(state._iter_entries()))
 
212
        finally:
 
213
            state.unlock()
 
214
 
 
215
    def create_basic_dirstate(self):
 
216
        """Create a dirstate with a few files and directories.
 
217
 
 
218
            a
 
219
            b/
 
220
              c
 
221
              d/
 
222
                e
 
223
            b-c
 
224
            f
 
225
        """
 
226
        tree = self.make_branch_and_tree('tree')
 
227
        paths = ['a', 'b/', 'b/c', 'b/d/', 'b/d/e', 'b-c', 'f']
 
228
        file_ids = ['a-id', 'b-id', 'c-id', 'd-id', 'e-id', 'b-c-id', 'f-id']
 
229
        self.build_tree(['tree/' + p for p in paths])
 
230
        tree.set_root_id('TREE_ROOT')
 
231
        tree.add([p.rstrip('/') for p in paths], file_ids)
 
232
        tree.commit('initial', rev_id='rev-1')
 
233
        revision_id = 'rev-1'
 
234
        # a_packed_stat = dirstate.pack_stat(os.stat('tree/a'))
 
235
        t = self.get_transport('tree')
 
236
        a_text = t.get_bytes('a')
 
237
        a_sha = osutils.sha_string(a_text)
 
238
        a_len = len(a_text)
 
239
        # b_packed_stat = dirstate.pack_stat(os.stat('tree/b'))
 
240
        # c_packed_stat = dirstate.pack_stat(os.stat('tree/b/c'))
 
241
        c_text = t.get_bytes('b/c')
 
242
        c_sha = osutils.sha_string(c_text)
 
243
        c_len = len(c_text)
 
244
        # d_packed_stat = dirstate.pack_stat(os.stat('tree/b/d'))
 
245
        # e_packed_stat = dirstate.pack_stat(os.stat('tree/b/d/e'))
 
246
        e_text = t.get_bytes('b/d/e')
 
247
        e_sha = osutils.sha_string(e_text)
 
248
        e_len = len(e_text)
 
249
        b_c_text = t.get_bytes('b-c')
 
250
        b_c_sha = osutils.sha_string(b_c_text)
 
251
        b_c_len = len(b_c_text)
 
252
        # f_packed_stat = dirstate.pack_stat(os.stat('tree/f'))
 
253
        f_text = t.get_bytes('f')
 
254
        f_sha = osutils.sha_string(f_text)
 
255
        f_len = len(f_text)
 
256
        null_stat = dirstate.DirState.NULLSTAT
 
257
        expected = {
 
258
            '':(('', '', 'TREE_ROOT'), [
 
259
                  ('d', '', 0, False, null_stat),
 
260
                  ('d', '', 0, False, revision_id),
 
261
                ]),
 
262
            'a':(('', 'a', 'a-id'), [
 
263
                   ('f', '', 0, False, null_stat),
 
264
                   ('f', a_sha, a_len, False, revision_id),
 
265
                 ]),
 
266
            'b':(('', 'b', 'b-id'), [
 
267
                  ('d', '', 0, False, null_stat),
 
268
                  ('d', '', 0, False, revision_id),
 
269
                 ]),
 
270
            'b/c':(('b', 'c', 'c-id'), [
 
271
                    ('f', '', 0, False, null_stat),
 
272
                    ('f', c_sha, c_len, False, revision_id),
 
273
                   ]),
 
274
            'b/d':(('b', 'd', 'd-id'), [
 
275
                    ('d', '', 0, False, null_stat),
 
276
                    ('d', '', 0, False, revision_id),
 
277
                   ]),
 
278
            'b/d/e':(('b/d', 'e', 'e-id'), [
 
279
                      ('f', '', 0, False, null_stat),
 
280
                      ('f', e_sha, e_len, False, revision_id),
 
281
                     ]),
 
282
            'b-c':(('', 'b-c', 'b-c-id'), [
 
283
                      ('f', '', 0, False, null_stat),
 
284
                      ('f', b_c_sha, b_c_len, False, revision_id),
 
285
                     ]),
 
286
            'f':(('', 'f', 'f-id'), [
 
287
                  ('f', '', 0, False, null_stat),
 
288
                  ('f', f_sha, f_len, False, revision_id),
 
289
                 ]),
 
290
        }
 
291
        state = dirstate.DirState.from_tree(tree, 'dirstate')
 
292
        try:
 
293
            state.save()
 
294
        finally:
 
295
            state.unlock()
 
296
        # Use a different object, to make sure nothing is pre-cached in memory.
 
297
        state = dirstate.DirState.on_file('dirstate')
 
298
        state.lock_read()
 
299
        self.addCleanup(state.unlock)
 
300
        self.assertEqual(dirstate.DirState.NOT_IN_MEMORY,
 
301
                         state._dirblock_state)
 
302
        # This is code is only really tested if we actually have to make more
 
303
        # than one read, so set the page size to something smaller.
 
304
        # We want it to contain about 2.2 records, so that we have a couple
 
305
        # records that we can read per attempt
 
306
        state._bisect_page_size = 200
 
307
        return tree, state, expected
 
308
 
 
309
    def create_duplicated_dirstate(self):
 
310
        """Create a dirstate with a deleted and added entries.
 
311
 
 
312
        This grabs a basic_dirstate, and then removes and re adds every entry
 
313
        with a new file id.
 
314
        """
 
315
        tree, state, expected = self.create_basic_dirstate()
 
316
        # Now we will just remove and add every file so we get an extra entry
 
317
        # per entry. Unversion in reverse order so we handle subdirs
 
318
        tree.unversion(['f-id', 'b-c-id', 'e-id', 'd-id', 'c-id', 'b-id', 'a-id'])
 
319
        tree.add(['a', 'b', 'b/c', 'b/d', 'b/d/e', 'b-c', 'f'],
 
320
                 ['a-id2', 'b-id2', 'c-id2', 'd-id2', 'e-id2', 'b-c-id2', 'f-id2'])
 
321
 
 
322
        # Update the expected dictionary.
 
323
        for path in ['a', 'b', 'b/c', 'b/d', 'b/d/e', 'b-c', 'f']:
 
324
            orig = expected[path]
 
325
            path2 = path + '2'
 
326
            # This record was deleted in the current tree
 
327
            expected[path] = (orig[0], [dirstate.DirState.NULL_PARENT_DETAILS,
 
328
                                        orig[1][1]])
 
329
            new_key = (orig[0][0], orig[0][1], orig[0][2]+'2')
 
330
            # And didn't exist in the basis tree
 
331
            expected[path2] = (new_key, [orig[1][0],
 
332
                                         dirstate.DirState.NULL_PARENT_DETAILS])
 
333
 
 
334
        # We will replace the 'dirstate' file underneath 'state', but that is
 
335
        # okay as lock as we unlock 'state' first.
 
336
        state.unlock()
 
337
        try:
 
338
            new_state = dirstate.DirState.from_tree(tree, 'dirstate')
 
339
            try:
 
340
                new_state.save()
 
341
            finally:
 
342
                new_state.unlock()
 
343
        finally:
 
344
            # But we need to leave state in a read-lock because we already have
 
345
            # a cleanup scheduled
 
346
            state.lock_read()
 
347
        return tree, state, expected
 
348
 
 
349
    def create_renamed_dirstate(self):
 
350
        """Create a dirstate with a few internal renames.
 
351
 
 
352
        This takes the basic dirstate, and moves the paths around.
 
353
        """
 
354
        tree, state, expected = self.create_basic_dirstate()
 
355
        # Rename a file
 
356
        tree.rename_one('a', 'b/g')
 
357
        # And a directory
 
358
        tree.rename_one('b/d', 'h')
 
359
 
 
360
        old_a = expected['a']
 
361
        expected['a'] = (old_a[0], [('r', 'b/g', 0, False, ''), old_a[1][1]])
 
362
        expected['b/g'] = (('b', 'g', 'a-id'), [old_a[1][0],
 
363
                                                ('r', 'a', 0, False, '')])
 
364
        old_d = expected['b/d']
 
365
        expected['b/d'] = (old_d[0], [('r', 'h', 0, False, ''), old_d[1][1]])
 
366
        expected['h'] = (('', 'h', 'd-id'), [old_d[1][0],
 
367
                                             ('r', 'b/d', 0, False, '')])
 
368
 
 
369
        old_e = expected['b/d/e']
 
370
        expected['b/d/e'] = (old_e[0], [('r', 'h/e', 0, False, ''),
 
371
                             old_e[1][1]])
 
372
        expected['h/e'] = (('h', 'e', 'e-id'), [old_e[1][0],
 
373
                                                ('r', 'b/d/e', 0, False, '')])
 
374
 
 
375
        state.unlock()
 
376
        try:
 
377
            new_state = dirstate.DirState.from_tree(tree, 'dirstate')
 
378
            try:
 
379
                new_state.save()
 
380
            finally:
 
381
                new_state.unlock()
 
382
        finally:
 
383
            state.lock_read()
 
384
        return tree, state, expected
 
385
 
 
386
 
 
387
class TestTreeToDirState(TestCaseWithDirState):
 
388
 
 
389
    def test_empty_to_dirstate(self):
 
390
        """We should be able to create a dirstate for an empty tree."""
 
391
        # There are no files on disk and no parents
 
392
        tree = self.make_branch_and_tree('tree')
 
393
        expected_result = ([], [
 
394
            (('', '', tree.get_root_id()), # common details
 
395
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
 
396
             ])])
 
397
        state = dirstate.DirState.from_tree(tree, 'dirstate')
 
398
        state._validate()
 
399
        self.check_state_with_reopen(expected_result, state)
 
400
 
 
401
    def test_1_parents_empty_to_dirstate(self):
 
402
        # create a parent by doing a commit
 
403
        tree = self.make_branch_and_tree('tree')
 
404
        rev_id = tree.commit('first post').encode('utf8')
 
405
        root_stat_pack = dirstate.pack_stat(os.stat(tree.basedir))
 
406
        expected_result = ([rev_id], [
 
407
            (('', '', tree.get_root_id()), # common details
 
408
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
 
409
              ('d', '', 0, False, rev_id), # first parent details
 
410
             ])])
 
411
        state = dirstate.DirState.from_tree(tree, 'dirstate')
 
412
        self.check_state_with_reopen(expected_result, state)
 
413
        state.lock_read()
 
414
        try:
 
415
            state._validate()
 
416
        finally:
 
417
            state.unlock()
 
418
 
 
419
    def test_2_parents_empty_to_dirstate(self):
 
420
        # create a parent by doing a commit
 
421
        tree = self.make_branch_and_tree('tree')
 
422
        rev_id = tree.commit('first post')
 
423
        tree2 = tree.controldir.sprout('tree2').open_workingtree()
 
424
        rev_id2 = tree2.commit('second post', allow_pointless=True)
 
425
        tree.merge_from_branch(tree2.branch)
 
426
        expected_result = ([rev_id, rev_id2], [
 
427
            (('', '', tree.get_root_id()), # common details
 
428
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
 
429
              ('d', '', 0, False, rev_id), # first parent details
 
430
              ('d', '', 0, False, rev_id), # second parent details
 
431
             ])])
 
432
        state = dirstate.DirState.from_tree(tree, 'dirstate')
 
433
        self.check_state_with_reopen(expected_result, state)
 
434
        state.lock_read()
 
435
        try:
 
436
            state._validate()
 
437
        finally:
 
438
            state.unlock()
 
439
 
 
440
    def test_empty_unknowns_are_ignored_to_dirstate(self):
 
441
        """We should be able to create a dirstate for an empty tree."""
 
442
        # There are no files on disk and no parents
 
443
        tree = self.make_branch_and_tree('tree')
 
444
        self.build_tree(['tree/unknown'])
 
445
        expected_result = ([], [
 
446
            (('', '', tree.get_root_id()), # common details
 
447
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
 
448
             ])])
 
449
        state = dirstate.DirState.from_tree(tree, 'dirstate')
 
450
        self.check_state_with_reopen(expected_result, state)
 
451
 
 
452
    def get_tree_with_a_file(self):
 
453
        tree = self.make_branch_and_tree('tree')
 
454
        self.build_tree(['tree/a file'])
 
455
        tree.add('a file', 'a-file-id')
 
456
        return tree
 
457
 
 
458
    def test_non_empty_no_parents_to_dirstate(self):
 
459
        """We should be able to create a dirstate for an empty tree."""
 
460
        # There are files on disk and no parents
 
461
        tree = self.get_tree_with_a_file()
 
462
        expected_result = ([], [
 
463
            (('', '', tree.get_root_id()), # common details
 
464
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
 
465
             ]),
 
466
            (('', 'a file', 'a-file-id'), # common
 
467
             [('f', '', 0, False, dirstate.DirState.NULLSTAT), # current
 
468
             ]),
 
469
            ])
 
470
        state = dirstate.DirState.from_tree(tree, 'dirstate')
 
471
        self.check_state_with_reopen(expected_result, state)
 
472
 
 
473
    def test_1_parents_not_empty_to_dirstate(self):
 
474
        # create a parent by doing a commit
 
475
        tree = self.get_tree_with_a_file()
 
476
        rev_id = tree.commit('first post').encode('utf8')
 
477
        # change the current content to be different this will alter stat, sha
 
478
        # and length:
 
479
        self.build_tree_contents([('tree/a file', 'new content\n')])
 
480
        expected_result = ([rev_id], [
 
481
            (('', '', tree.get_root_id()), # common details
 
482
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
 
483
              ('d', '', 0, False, rev_id), # first parent details
 
484
             ]),
 
485
            (('', 'a file', 'a-file-id'), # common
 
486
             [('f', '', 0, False, dirstate.DirState.NULLSTAT), # current
 
487
              ('f', 'c3ed76e4bfd45ff1763ca206055bca8e9fc28aa8', 24, False,
 
488
               rev_id), # first parent
 
489
             ]),
 
490
            ])
 
491
        state = dirstate.DirState.from_tree(tree, 'dirstate')
 
492
        self.check_state_with_reopen(expected_result, state)
 
493
 
 
494
    def test_2_parents_not_empty_to_dirstate(self):
 
495
        # create a parent by doing a commit
 
496
        tree = self.get_tree_with_a_file()
 
497
        rev_id = tree.commit('first post').encode('utf8')
 
498
        tree2 = tree.controldir.sprout('tree2').open_workingtree()
 
499
        # change the current content to be different this will alter stat, sha
 
500
        # and length:
 
501
        self.build_tree_contents([('tree2/a file', 'merge content\n')])
 
502
        rev_id2 = tree2.commit('second post').encode('utf8')
 
503
        tree.merge_from_branch(tree2.branch)
 
504
        # change the current content to be different this will alter stat, sha
 
505
        # and length again, giving us three distinct values:
 
506
        self.build_tree_contents([('tree/a file', 'new content\n')])
 
507
        expected_result = ([rev_id, rev_id2], [
 
508
            (('', '', tree.get_root_id()), # common details
 
509
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
 
510
              ('d', '', 0, False, rev_id), # first parent details
 
511
              ('d', '', 0, False, rev_id), # second parent details
 
512
             ]),
 
513
            (('', 'a file', 'a-file-id'), # common
 
514
             [('f', '', 0, False, dirstate.DirState.NULLSTAT), # current
 
515
              ('f', 'c3ed76e4bfd45ff1763ca206055bca8e9fc28aa8', 24, False,
 
516
               rev_id), # first parent
 
517
              ('f', '314d796174c9412647c3ce07dfb5d36a94e72958', 14, False,
 
518
               rev_id2), # second parent
 
519
             ]),
 
520
            ])
 
521
        state = dirstate.DirState.from_tree(tree, 'dirstate')
 
522
        self.check_state_with_reopen(expected_result, state)
 
523
 
 
524
    def test_colliding_fileids(self):
 
525
        # test insertion of parents creating several entries at the same path.
 
526
        # we used to have a bug where they could cause the dirstate to break
 
527
        # its ordering invariants.
 
528
        # create some trees to test from
 
529
        parents = []
 
530
        for i in range(7):
 
531
            tree = self.make_branch_and_tree('tree%d' % i)
 
532
            self.build_tree(['tree%d/name' % i,])
 
533
            tree.add(['name'], ['file-id%d' % i])
 
534
            revision_id = 'revid-%d' % i
 
535
            tree.commit('message', rev_id=revision_id)
 
536
            parents.append((revision_id,
 
537
                tree.branch.repository.revision_tree(revision_id)))
 
538
        # now fold these trees into a dirstate
 
539
        state = dirstate.DirState.initialize('dirstate')
 
540
        try:
 
541
            state.set_parent_trees(parents, [])
 
542
            state._validate()
 
543
        finally:
 
544
            state.unlock()
 
545
 
 
546
 
 
547
class TestDirStateOnFile(TestCaseWithDirState):
 
548
 
 
549
    def create_updated_dirstate(self):
 
550
        self.build_tree(['a-file'])
 
551
        tree = self.make_branch_and_tree('.')
 
552
        tree.add(['a-file'], ['a-id'])
 
553
        tree.commit('add a-file')
 
554
        # Save and unlock the state, re-open it in readonly mode
 
555
        state = dirstate.DirState.from_tree(tree, 'dirstate')
 
556
        state.save()
 
557
        state.unlock()
 
558
        state = dirstate.DirState.on_file('dirstate')
 
559
        state.lock_read()
 
560
        return state
 
561
 
 
562
    def test_construct_with_path(self):
 
563
        tree = self.make_branch_and_tree('tree')
 
564
        state = dirstate.DirState.from_tree(tree, 'dirstate.from_tree')
 
565
        # we want to be able to get the lines of the dirstate that we will
 
566
        # write to disk.
 
567
        lines = state.get_lines()
 
568
        state.unlock()
 
569
        self.build_tree_contents([('dirstate', ''.join(lines))])
 
570
        # get a state object
 
571
        # no parents, default tree content
 
572
        expected_result = ([], [
 
573
            (('', '', tree.get_root_id()), # common details
 
574
             # current tree details, but new from_tree skips statting, it
 
575
             # uses set_state_from_inventory, and thus depends on the
 
576
             # inventory state.
 
577
             [('d', '', 0, False, dirstate.DirState.NULLSTAT),
 
578
             ])
 
579
            ])
 
580
        state = dirstate.DirState.on_file('dirstate')
 
581
        state.lock_write() # check_state_with_reopen will save() and unlock it
 
582
        self.check_state_with_reopen(expected_result, state)
 
583
 
 
584
    def test_can_save_clean_on_file(self):
 
585
        tree = self.make_branch_and_tree('tree')
 
586
        state = dirstate.DirState.from_tree(tree, 'dirstate')
 
587
        try:
 
588
            # doing a save should work here as there have been no changes.
 
589
            state.save()
 
590
            # TODO: stat it and check it hasn't changed; may require waiting
 
591
            # for the state accuracy window.
 
592
        finally:
 
593
            state.unlock()
 
594
 
 
595
    def test_can_save_in_read_lock(self):
 
596
        state = self.create_updated_dirstate()
 
597
        try:
 
598
            entry = state._get_entry(0, path_utf8='a-file')
 
599
            # The current size should be 0 (default)
 
600
            self.assertEqual(0, entry[1][0][2])
 
601
            # We should have a real entry.
 
602
            self.assertNotEqual((None, None), entry)
 
603
            # Set the cutoff-time into the future, so things look cacheable
 
604
            state._sha_cutoff_time()
 
605
            state._cutoff_time += 10.0
 
606
            st = os.lstat('a-file')
 
607
            sha1sum = dirstate.update_entry(state, entry, 'a-file', st)
 
608
            # We updated the current sha1sum because the file is cacheable
 
609
            self.assertEqual('ecc5374e9ed82ad3ea3b4d452ea995a5fd3e70e3',
 
610
                             sha1sum)
 
611
 
 
612
            # The dirblock has been updated
 
613
            self.assertEqual(st.st_size, entry[1][0][2])
 
614
            self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
 
615
                             state._dirblock_state)
 
616
 
 
617
            del entry
 
618
            # Now, since we are the only one holding a lock, we should be able
 
619
            # to save and have it written to disk
 
620
            state.save()
 
621
        finally:
 
622
            state.unlock()
 
623
 
 
624
        # Re-open the file, and ensure that the state has been updated.
 
625
        state = dirstate.DirState.on_file('dirstate')
 
626
        state.lock_read()
 
627
        try:
 
628
            entry = state._get_entry(0, path_utf8='a-file')
 
629
            self.assertEqual(st.st_size, entry[1][0][2])
 
630
        finally:
 
631
            state.unlock()
 
632
 
 
633
    def test_save_fails_quietly_if_locked(self):
 
634
        """If dirstate is locked, save will fail without complaining."""
 
635
        state = self.create_updated_dirstate()
 
636
        try:
 
637
            entry = state._get_entry(0, path_utf8='a-file')
 
638
            # No cached sha1 yet.
 
639
            self.assertEqual('', entry[1][0][1])
 
640
            # Set the cutoff-time into the future, so things look cacheable
 
641
            state._sha_cutoff_time()
 
642
            state._cutoff_time += 10.0
 
643
            st = os.lstat('a-file')
 
644
            sha1sum = dirstate.update_entry(state, entry, 'a-file', st)
 
645
            self.assertEqual('ecc5374e9ed82ad3ea3b4d452ea995a5fd3e70e3',
 
646
                             sha1sum)
 
647
            self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
 
648
                             state._dirblock_state)
 
649
 
 
650
            # Now, before we try to save, grab another dirstate, and take out a
 
651
            # read lock.
 
652
            # TODO: jam 20070315 Ideally this would be locked by another
 
653
            #       process. To make sure the file is really OS locked.
 
654
            state2 = dirstate.DirState.on_file('dirstate')
 
655
            state2.lock_read()
 
656
            try:
 
657
                # This won't actually write anything, because it couldn't grab
 
658
                # a write lock. But it shouldn't raise an error, either.
 
659
                # TODO: jam 20070315 We should probably distinguish between
 
660
                #       being dirty because of 'update_entry'. And dirty
 
661
                #       because of real modification. So that save() *does*
 
662
                #       raise a real error if it fails when we have real
 
663
                #       modifications.
 
664
                state.save()
 
665
            finally:
 
666
                state2.unlock()
 
667
        finally:
 
668
            state.unlock()
 
669
 
 
670
        # The file on disk should not be modified.
 
671
        state = dirstate.DirState.on_file('dirstate')
 
672
        state.lock_read()
 
673
        try:
 
674
            entry = state._get_entry(0, path_utf8='a-file')
 
675
            self.assertEqual('', entry[1][0][1])
 
676
        finally:
 
677
            state.unlock()
 
678
 
 
679
    def test_save_refuses_if_changes_aborted(self):
 
680
        self.build_tree(['a-file', 'a-dir/'])
 
681
        state = dirstate.DirState.initialize('dirstate')
 
682
        try:
 
683
            # No stat and no sha1 sum.
 
684
            state.add('a-file', 'a-file-id', 'file', None, '')
 
685
            state.save()
 
686
        finally:
 
687
            state.unlock()
 
688
 
 
689
        # The dirstate should include TREE_ROOT and 'a-file' and nothing else
 
690
        expected_blocks = [
 
691
            ('', [(('', '', 'TREE_ROOT'),
 
692
                   [('d', '', 0, False, dirstate.DirState.NULLSTAT)])]),
 
693
            ('', [(('', 'a-file', 'a-file-id'),
 
694
                   [('f', '', 0, False, dirstate.DirState.NULLSTAT)])]),
 
695
        ]
 
696
 
 
697
        state = dirstate.DirState.on_file('dirstate')
 
698
        state.lock_write()
 
699
        try:
 
700
            state._read_dirblocks_if_needed()
 
701
            self.assertEqual(expected_blocks, state._dirblocks)
 
702
 
 
703
            # Now modify the state, but mark it as inconsistent
 
704
            state.add('a-dir', 'a-dir-id', 'directory', None, '')
 
705
            state._changes_aborted = True
 
706
            state.save()
 
707
        finally:
 
708
            state.unlock()
 
709
 
 
710
        state = dirstate.DirState.on_file('dirstate')
 
711
        state.lock_read()
 
712
        try:
 
713
            state._read_dirblocks_if_needed()
 
714
            self.assertEqual(expected_blocks, state._dirblocks)
 
715
        finally:
 
716
            state.unlock()
 
717
 
 
718
 
 
719
class TestDirStateInitialize(TestCaseWithDirState):
 
720
 
 
721
    def test_initialize(self):
 
722
        expected_result = ([], [
 
723
            (('', '', 'TREE_ROOT'), # common details
 
724
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
 
725
             ])
 
726
            ])
 
727
        state = dirstate.DirState.initialize('dirstate')
 
728
        try:
 
729
            self.assertIsInstance(state, dirstate.DirState)
 
730
            lines = state.get_lines()
 
731
        finally:
 
732
            state.unlock()
 
733
        # On win32 you can't read from a locked file, even within the same
 
734
        # process. So we have to unlock and release before we check the file
 
735
        # contents.
 
736
        self.assertFileEqual(''.join(lines), 'dirstate')
 
737
        state.lock_read() # check_state_with_reopen will unlock
 
738
        self.check_state_with_reopen(expected_result, state)
 
739
 
 
740
 
 
741
class TestDirStateManipulations(TestCaseWithDirState):
 
742
 
 
743
    def make_minimal_tree(self):
 
744
        tree1 = self.make_branch_and_memory_tree('tree1')
 
745
        tree1.lock_write()
 
746
        self.addCleanup(tree1.unlock)
 
747
        tree1.add('')
 
748
        revid1 = tree1.commit('foo')
 
749
        return tree1, revid1
 
750
 
 
751
    def test_update_minimal_updates_id_index(self):
 
752
        state = self.create_dirstate_with_root_and_subdir()
 
753
        self.addCleanup(state.unlock)
 
754
        id_index = state._get_id_index()
 
755
        self.assertEqual(['a-root-value', 'subdir-id'], sorted(id_index))
 
756
        state.add('file-name', 'file-id', 'file', None, '')
 
757
        self.assertEqual(['a-root-value', 'file-id', 'subdir-id'],
 
758
                         sorted(id_index))
 
759
        state.update_minimal(('', 'new-name', 'file-id'), 'f',
 
760
                             path_utf8='new-name')
 
761
        self.assertEqual(['a-root-value', 'file-id', 'subdir-id'],
 
762
                         sorted(id_index))
 
763
        self.assertEqual([('', 'new-name', 'file-id')],
 
764
                         sorted(id_index['file-id']))
 
765
        state._validate()
 
766
 
 
767
    def test_set_state_from_inventory_no_content_no_parents(self):
 
768
        # setting the current inventory is a slow but important api to support.
 
769
        tree1, revid1 = self.make_minimal_tree()
 
770
        inv = tree1.root_inventory
 
771
        root_id = inv.path2id('')
 
772
        expected_result = [], [
 
773
            (('', '', root_id), [
 
774
             ('d', '', 0, False, dirstate.DirState.NULLSTAT)])]
 
775
        state = dirstate.DirState.initialize('dirstate')
 
776
        try:
 
777
            state.set_state_from_inventory(inv)
 
778
            self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
779
                             state._header_state)
 
780
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
781
                             state._dirblock_state)
 
782
        except:
 
783
            state.unlock()
 
784
            raise
 
785
        else:
 
786
            # This will unlock it
 
787
            self.check_state_with_reopen(expected_result, state)
 
788
 
 
789
    def test_set_state_from_scratch_no_parents(self):
 
790
        tree1, revid1 = self.make_minimal_tree()
 
791
        inv = tree1.root_inventory
 
792
        root_id = inv.path2id('')
 
793
        expected_result = [], [
 
794
            (('', '', root_id), [
 
795
             ('d', '', 0, False, dirstate.DirState.NULLSTAT)])]
 
796
        state = dirstate.DirState.initialize('dirstate')
 
797
        try:
 
798
            state.set_state_from_scratch(inv, [], [])
 
799
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
800
                             state._header_state)
 
801
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
802
                             state._dirblock_state)
 
803
        except:
 
804
            state.unlock()
 
805
            raise
 
806
        else:
 
807
            # This will unlock it
 
808
            self.check_state_with_reopen(expected_result, state)
 
809
 
 
810
    def test_set_state_from_scratch_identical_parent(self):
 
811
        tree1, revid1 = self.make_minimal_tree()
 
812
        inv = tree1.root_inventory
 
813
        root_id = inv.path2id('')
 
814
        rev_tree1 = tree1.branch.repository.revision_tree(revid1)
 
815
        d_entry = ('d', '', 0, False, dirstate.DirState.NULLSTAT)
 
816
        parent_entry = ('d', '', 0, False, revid1)
 
817
        expected_result = [revid1], [
 
818
            (('', '', root_id), [d_entry, parent_entry])]
 
819
        state = dirstate.DirState.initialize('dirstate')
 
820
        try:
 
821
            state.set_state_from_scratch(inv, [(revid1, rev_tree1)], [])
 
822
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
823
                             state._header_state)
 
824
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
825
                             state._dirblock_state)
 
826
        except:
 
827
            state.unlock()
 
828
            raise
 
829
        else:
 
830
            # This will unlock it
 
831
            self.check_state_with_reopen(expected_result, state)
 
832
 
 
833
    def test_set_state_from_inventory_preserves_hashcache(self):
 
834
        # https://bugs.launchpad.net/bzr/+bug/146176
 
835
        # set_state_from_inventory should preserve the stat and hash value for
 
836
        # workingtree files that are not changed by the inventory.
 
837
 
 
838
        tree = self.make_branch_and_tree('.')
 
839
        # depends on the default format using dirstate...
 
840
        tree.lock_write()
 
841
        try:
 
842
            # make a dirstate with some valid hashcache data
 
843
            # file on disk, but that's not needed for this test
 
844
            foo_contents = 'contents of foo'
 
845
            self.build_tree_contents([('foo', foo_contents)])
 
846
            tree.add('foo', 'foo-id')
 
847
 
 
848
            foo_stat = os.stat('foo')
 
849
            foo_packed = dirstate.pack_stat(foo_stat)
 
850
            foo_sha = osutils.sha_string(foo_contents)
 
851
            foo_size = len(foo_contents)
 
852
 
 
853
            # should not be cached yet, because the file's too fresh
 
854
            self.assertEqual(
 
855
                (('', 'foo', 'foo-id',),
 
856
                 [('f', '', 0, False, dirstate.DirState.NULLSTAT)]),
 
857
                tree._dirstate._get_entry(0, 'foo-id'))
 
858
            # poke in some hashcache information - it wouldn't normally be
 
859
            # stored because it's too fresh
 
860
            tree._dirstate.update_minimal(
 
861
                ('', 'foo', 'foo-id'),
 
862
                'f', False, foo_sha, foo_packed, foo_size, 'foo')
 
863
            # now should be cached
 
864
            self.assertEqual(
 
865
                (('', 'foo', 'foo-id',),
 
866
                 [('f', foo_sha, foo_size, False, foo_packed)]),
 
867
                tree._dirstate._get_entry(0, 'foo-id'))
 
868
 
 
869
            # extract the inventory, and add something to it
 
870
            inv = tree._get_root_inventory()
 
871
            # should see the file we poked in...
 
872
            self.assertTrue(inv.has_id('foo-id'))
 
873
            self.assertTrue(inv.has_filename('foo'))
 
874
            inv.add_path('bar', 'file', 'bar-id')
 
875
            tree._dirstate._validate()
 
876
            # this used to cause it to lose its hashcache
 
877
            tree._dirstate.set_state_from_inventory(inv)
 
878
            tree._dirstate._validate()
 
879
        finally:
 
880
            tree.unlock()
 
881
 
 
882
        tree.lock_read()
 
883
        try:
 
884
            # now check that the state still has the original hashcache value
 
885
            state = tree._dirstate
 
886
            state._validate()
 
887
            foo_tuple = state._get_entry(0, path_utf8='foo')
 
888
            self.assertEqual(
 
889
                (('', 'foo', 'foo-id',),
 
890
                 [('f', foo_sha, len(foo_contents), False,
 
891
                   dirstate.pack_stat(foo_stat))]),
 
892
                foo_tuple)
 
893
        finally:
 
894
            tree.unlock()
 
895
 
 
896
    def test_set_state_from_inventory_mixed_paths(self):
 
897
        tree1 = self.make_branch_and_tree('tree1')
 
898
        self.build_tree(['tree1/a/', 'tree1/a/b/', 'tree1/a-b/',
 
899
                         'tree1/a/b/foo', 'tree1/a-b/bar'])
 
900
        tree1.lock_write()
 
901
        try:
 
902
            tree1.add(['a', 'a/b', 'a-b', 'a/b/foo', 'a-b/bar'],
 
903
                      ['a-id', 'b-id', 'a-b-id', 'foo-id', 'bar-id'])
 
904
            tree1.commit('rev1', rev_id='rev1')
 
905
            root_id = tree1.get_root_id()
 
906
            inv = tree1.root_inventory
 
907
        finally:
 
908
            tree1.unlock()
 
909
        expected_result1 = [('', '', root_id, 'd'),
 
910
                            ('', 'a', 'a-id', 'd'),
 
911
                            ('', 'a-b', 'a-b-id', 'd'),
 
912
                            ('a', 'b', 'b-id', 'd'),
 
913
                            ('a/b', 'foo', 'foo-id', 'f'),
 
914
                            ('a-b', 'bar', 'bar-id', 'f'),
 
915
                           ]
 
916
        expected_result2 = [('', '', root_id, 'd'),
 
917
                            ('', 'a', 'a-id', 'd'),
 
918
                            ('', 'a-b', 'a-b-id', 'd'),
 
919
                            ('a-b', 'bar', 'bar-id', 'f'),
 
920
                           ]
 
921
        state = dirstate.DirState.initialize('dirstate')
 
922
        try:
 
923
            state.set_state_from_inventory(inv)
 
924
            values = []
 
925
            for entry in state._iter_entries():
 
926
                values.append(entry[0] + entry[1][0][:1])
 
927
            self.assertEqual(expected_result1, values)
 
928
            del inv['b-id']
 
929
            state.set_state_from_inventory(inv)
 
930
            values = []
 
931
            for entry in state._iter_entries():
 
932
                values.append(entry[0] + entry[1][0][:1])
 
933
            self.assertEqual(expected_result2, values)
 
934
        finally:
 
935
            state.unlock()
 
936
 
 
937
    def test_set_path_id_no_parents(self):
 
938
        """The id of a path can be changed trivally with no parents."""
 
939
        state = dirstate.DirState.initialize('dirstate')
 
940
        try:
 
941
            # check precondition to be sure the state does change appropriately.
 
942
            root_entry = (('', '', 'TREE_ROOT'), [('d', '', 0, False, 'x'*32)])
 
943
            self.assertEqual([root_entry], list(state._iter_entries()))
 
944
            self.assertEqual(root_entry, state._get_entry(0, path_utf8=''))
 
945
            self.assertEqual(root_entry,
 
946
                             state._get_entry(0, fileid_utf8='TREE_ROOT'))
 
947
            self.assertEqual((None, None),
 
948
                             state._get_entry(0, fileid_utf8='second-root-id'))
 
949
            state.set_path_id('', 'second-root-id')
 
950
            new_root_entry = (('', '', 'second-root-id'),
 
951
                              [('d', '', 0, False, 'x'*32)])
 
952
            expected_rows = [new_root_entry]
 
953
            self.assertEqual(expected_rows, list(state._iter_entries()))
 
954
            self.assertEqual(new_root_entry, state._get_entry(0, path_utf8=''))
 
955
            self.assertEqual(new_root_entry, 
 
956
                             state._get_entry(0, fileid_utf8='second-root-id'))
 
957
            self.assertEqual((None, None),
 
958
                             state._get_entry(0, fileid_utf8='TREE_ROOT'))
 
959
            # should work across save too
 
960
            state.save()
 
961
        finally:
 
962
            state.unlock()
 
963
        state = dirstate.DirState.on_file('dirstate')
 
964
        state.lock_read()
 
965
        try:
 
966
            state._validate()
 
967
            self.assertEqual(expected_rows, list(state._iter_entries()))
 
968
        finally:
 
969
            state.unlock()
 
970
 
 
971
    def test_set_path_id_with_parents(self):
 
972
        """Set the root file id in a dirstate with parents"""
 
973
        mt = self.make_branch_and_tree('mt')
 
974
        # in case the default tree format uses a different root id
 
975
        mt.set_root_id('TREE_ROOT')
 
976
        mt.commit('foo', rev_id='parent-revid')
 
977
        rt = mt.branch.repository.revision_tree('parent-revid')
 
978
        state = dirstate.DirState.initialize('dirstate')
 
979
        state._validate()
 
980
        try:
 
981
            state.set_parent_trees([('parent-revid', rt)], ghosts=[])
 
982
            root_entry = (('', '', 'TREE_ROOT'),
 
983
                          [('d', '', 0, False, 'x'*32),
 
984
                           ('d', '', 0, False, 'parent-revid')])
 
985
            self.assertEqual(root_entry, state._get_entry(0, path_utf8=''))
 
986
            self.assertEqual(root_entry,
 
987
                             state._get_entry(0, fileid_utf8='TREE_ROOT'))
 
988
            self.assertEqual((None, None),
 
989
                             state._get_entry(0, fileid_utf8='Asecond-root-id'))
 
990
            state.set_path_id('', 'Asecond-root-id')
 
991
            state._validate()
 
992
            # now see that it is what we expected
 
993
            old_root_entry = (('', '', 'TREE_ROOT'),
 
994
                              [('a', '', 0, False, ''),
 
995
                               ('d', '', 0, False, 'parent-revid')])
 
996
            new_root_entry = (('', '', 'Asecond-root-id'),
 
997
                              [('d', '', 0, False, ''),
 
998
                               ('a', '', 0, False, '')])
 
999
            expected_rows = [new_root_entry, old_root_entry]
 
1000
            state._validate()
 
1001
            self.assertEqual(expected_rows, list(state._iter_entries()))
 
1002
            self.assertEqual(new_root_entry, state._get_entry(0, path_utf8=''))
 
1003
            self.assertEqual(old_root_entry, state._get_entry(1, path_utf8=''))
 
1004
            self.assertEqual((None, None),
 
1005
                             state._get_entry(0, fileid_utf8='TREE_ROOT'))
 
1006
            self.assertEqual(old_root_entry,
 
1007
                             state._get_entry(1, fileid_utf8='TREE_ROOT'))
 
1008
            self.assertEqual(new_root_entry,
 
1009
                             state._get_entry(0, fileid_utf8='Asecond-root-id'))
 
1010
            self.assertEqual((None, None),
 
1011
                             state._get_entry(1, fileid_utf8='Asecond-root-id'))
 
1012
            # should work across save too
 
1013
            state.save()
 
1014
        finally:
 
1015
            state.unlock()
 
1016
        # now flush & check we get the same
 
1017
        state = dirstate.DirState.on_file('dirstate')
 
1018
        state.lock_read()
 
1019
        try:
 
1020
            state._validate()
 
1021
            self.assertEqual(expected_rows, list(state._iter_entries()))
 
1022
        finally:
 
1023
            state.unlock()
 
1024
        # now change within an existing file-backed state
 
1025
        state.lock_write()
 
1026
        try:
 
1027
            state._validate()
 
1028
            state.set_path_id('', 'tree-root-2')
 
1029
            state._validate()
 
1030
        finally:
 
1031
            state.unlock()
 
1032
 
 
1033
    def test_set_parent_trees_no_content(self):
 
1034
        # set_parent_trees is a slow but important api to support.
 
1035
        tree1 = self.make_branch_and_memory_tree('tree1')
 
1036
        tree1.lock_write()
 
1037
        try:
 
1038
            tree1.add('')
 
1039
            revid1 = tree1.commit('foo')
 
1040
        finally:
 
1041
            tree1.unlock()
 
1042
        branch2 = tree1.branch.controldir.clone('tree2').open_branch()
 
1043
        tree2 = memorytree.MemoryTree.create_on_branch(branch2)
 
1044
        tree2.lock_write()
 
1045
        try:
 
1046
            revid2 = tree2.commit('foo')
 
1047
            root_id = tree2.get_root_id()
 
1048
        finally:
 
1049
            tree2.unlock()
 
1050
        state = dirstate.DirState.initialize('dirstate')
 
1051
        try:
 
1052
            state.set_path_id('', root_id)
 
1053
            state.set_parent_trees(
 
1054
                ((revid1, tree1.branch.repository.revision_tree(revid1)),
 
1055
                 (revid2, tree2.branch.repository.revision_tree(revid2)),
 
1056
                 ('ghost-rev', None)),
 
1057
                ['ghost-rev'])
 
1058
            # check we can reopen and use the dirstate after setting parent
 
1059
            # trees.
 
1060
            state._validate()
 
1061
            state.save()
 
1062
            state._validate()
 
1063
        finally:
 
1064
            state.unlock()
 
1065
        state = dirstate.DirState.on_file('dirstate')
 
1066
        state.lock_write()
 
1067
        try:
 
1068
            self.assertEqual([revid1, revid2, 'ghost-rev'],
 
1069
                             state.get_parent_ids())
 
1070
            # iterating the entire state ensures that the state is parsable.
 
1071
            list(state._iter_entries())
 
1072
            # be sure that it sets not appends - change it
 
1073
            state.set_parent_trees(
 
1074
                ((revid1, tree1.branch.repository.revision_tree(revid1)),
 
1075
                 ('ghost-rev', None)),
 
1076
                ['ghost-rev'])
 
1077
            # and now put it back.
 
1078
            state.set_parent_trees(
 
1079
                ((revid1, tree1.branch.repository.revision_tree(revid1)),
 
1080
                 (revid2, tree2.branch.repository.revision_tree(revid2)),
 
1081
                 ('ghost-rev', tree2.branch.repository.revision_tree(
 
1082
                                   _mod_revision.NULL_REVISION))),
 
1083
                ['ghost-rev'])
 
1084
            self.assertEqual([revid1, revid2, 'ghost-rev'],
 
1085
                             state.get_parent_ids())
 
1086
            # the ghost should be recorded as such by set_parent_trees.
 
1087
            self.assertEqual(['ghost-rev'], state.get_ghosts())
 
1088
            self.assertEqual(
 
1089
                [(('', '', root_id), [
 
1090
                  ('d', '', 0, False, dirstate.DirState.NULLSTAT),
 
1091
                  ('d', '', 0, False, revid1),
 
1092
                  ('d', '', 0, False, revid1)
 
1093
                  ])],
 
1094
                list(state._iter_entries()))
 
1095
        finally:
 
1096
            state.unlock()
 
1097
 
 
1098
    def test_set_parent_trees_file_missing_from_tree(self):
 
1099
        # Adding a parent tree may reference files not in the current state.
 
1100
        # they should get listed just once by id, even if they are in two
 
1101
        # separate trees.
 
1102
        # set_parent_trees is a slow but important api to support.
 
1103
        tree1 = self.make_branch_and_memory_tree('tree1')
 
1104
        tree1.lock_write()
 
1105
        try:
 
1106
            tree1.add('')
 
1107
            tree1.add(['a file'], ['file-id'], ['file'])
 
1108
            tree1.put_file_bytes_non_atomic('file-id', 'file-content')
 
1109
            revid1 = tree1.commit('foo')
 
1110
        finally:
 
1111
            tree1.unlock()
 
1112
        branch2 = tree1.branch.controldir.clone('tree2').open_branch()
 
1113
        tree2 = memorytree.MemoryTree.create_on_branch(branch2)
 
1114
        tree2.lock_write()
 
1115
        try:
 
1116
            tree2.put_file_bytes_non_atomic('file-id', 'new file-content')
 
1117
            revid2 = tree2.commit('foo')
 
1118
            root_id = tree2.get_root_id()
 
1119
        finally:
 
1120
            tree2.unlock()
 
1121
        # check the layout in memory
 
1122
        expected_result = [revid1.encode('utf8'), revid2.encode('utf8')], [
 
1123
            (('', '', root_id), [
 
1124
             ('d', '', 0, False, dirstate.DirState.NULLSTAT),
 
1125
             ('d', '', 0, False, revid1.encode('utf8')),
 
1126
             ('d', '', 0, False, revid1.encode('utf8'))
 
1127
             ]),
 
1128
            (('', 'a file', 'file-id'), [
 
1129
             ('a', '', 0, False, ''),
 
1130
             ('f', '2439573625385400f2a669657a7db6ae7515d371', 12, False,
 
1131
              revid1.encode('utf8')),
 
1132
             ('f', '542e57dc1cda4af37cb8e55ec07ce60364bb3c7d', 16, False,
 
1133
              revid2.encode('utf8'))
 
1134
             ])
 
1135
            ]
 
1136
        state = dirstate.DirState.initialize('dirstate')
 
1137
        try:
 
1138
            state.set_path_id('', root_id)
 
1139
            state.set_parent_trees(
 
1140
                ((revid1, tree1.branch.repository.revision_tree(revid1)),
 
1141
                 (revid2, tree2.branch.repository.revision_tree(revid2)),
 
1142
                 ), [])
 
1143
        except:
 
1144
            state.unlock()
 
1145
            raise
 
1146
        else:
 
1147
            # check_state_with_reopen will unlock
 
1148
            self.check_state_with_reopen(expected_result, state)
 
1149
 
 
1150
    ### add a path via _set_data - so we dont need delta work, just
 
1151
    # raw data in, and ensure that it comes out via get_lines happily.
 
1152
 
 
1153
    def test_add_path_to_root_no_parents_all_data(self):
 
1154
        # The most trivial addition of a path is when there are no parents and
 
1155
        # its in the root and all data about the file is supplied
 
1156
        self.build_tree(['a file'])
 
1157
        stat = os.lstat('a file')
 
1158
        # the 1*20 is the sha1 pretend value.
 
1159
        state = dirstate.DirState.initialize('dirstate')
 
1160
        expected_entries = [
 
1161
            (('', '', 'TREE_ROOT'), [
 
1162
             ('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
 
1163
             ]),
 
1164
            (('', 'a file', 'a-file-id'), [
 
1165
             ('f', '1'*20, 19, False, dirstate.pack_stat(stat)), # current tree
 
1166
             ]),
 
1167
            ]
 
1168
        try:
 
1169
            state.add('a file', 'a-file-id', 'file', stat, '1'*20)
 
1170
            # having added it, it should be in the output of iter_entries.
 
1171
            self.assertEqual(expected_entries, list(state._iter_entries()))
 
1172
            # saving and reloading should not affect this.
 
1173
            state.save()
 
1174
        finally:
 
1175
            state.unlock()
 
1176
        state = dirstate.DirState.on_file('dirstate')
 
1177
        state.lock_read()
 
1178
        self.addCleanup(state.unlock)
 
1179
        self.assertEqual(expected_entries, list(state._iter_entries()))
 
1180
 
 
1181
    def test_add_path_to_unversioned_directory(self):
 
1182
        """Adding a path to an unversioned directory should error.
 
1183
 
 
1184
        This is a duplicate of TestWorkingTree.test_add_in_unversioned,
 
1185
        once dirstate is stable and if it is merged with WorkingTree3, consider
 
1186
        removing this copy of the test.
 
1187
        """
 
1188
        self.build_tree(['unversioned/', 'unversioned/a file'])
 
1189
        state = dirstate.DirState.initialize('dirstate')
 
1190
        self.addCleanup(state.unlock)
 
1191
        self.assertRaises(errors.NotVersionedError, state.add,
 
1192
                          'unversioned/a file', 'a-file-id', 'file', None, None)
 
1193
 
 
1194
    def test_add_directory_to_root_no_parents_all_data(self):
 
1195
        # The most trivial addition of a dir is when there are no parents and
 
1196
        # its in the root and all data about the file is supplied
 
1197
        self.build_tree(['a dir/'])
 
1198
        stat = os.lstat('a dir')
 
1199
        expected_entries = [
 
1200
            (('', '', 'TREE_ROOT'), [
 
1201
             ('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
 
1202
             ]),
 
1203
            (('', 'a dir', 'a dir id'), [
 
1204
             ('d', '', 0, False, dirstate.pack_stat(stat)), # current tree
 
1205
             ]),
 
1206
            ]
 
1207
        state = dirstate.DirState.initialize('dirstate')
 
1208
        try:
 
1209
            state.add('a dir', 'a dir id', 'directory', stat, None)
 
1210
            # having added it, it should be in the output of iter_entries.
 
1211
            self.assertEqual(expected_entries, list(state._iter_entries()))
 
1212
            # saving and reloading should not affect this.
 
1213
            state.save()
 
1214
        finally:
 
1215
            state.unlock()
 
1216
        state = dirstate.DirState.on_file('dirstate')
 
1217
        state.lock_read()
 
1218
        self.addCleanup(state.unlock)
 
1219
        state._validate()
 
1220
        self.assertEqual(expected_entries, list(state._iter_entries()))
 
1221
 
 
1222
    def _test_add_symlink_to_root_no_parents_all_data(self, link_name, target):
 
1223
        # The most trivial addition of a symlink when there are no parents and
 
1224
        # its in the root and all data about the file is supplied
 
1225
        # bzr doesn't support fake symlinks on windows, yet.
 
1226
        self.requireFeature(features.SymlinkFeature)
 
1227
        os.symlink(target, link_name)
 
1228
        stat = os.lstat(link_name)
 
1229
        expected_entries = [
 
1230
            (('', '', 'TREE_ROOT'), [
 
1231
             ('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
 
1232
             ]),
 
1233
            (('', link_name.encode('UTF-8'), 'a link id'), [
 
1234
             ('l', target.encode('UTF-8'), stat[6],
 
1235
              False, dirstate.pack_stat(stat)), # current tree
 
1236
             ]),
 
1237
            ]
 
1238
        state = dirstate.DirState.initialize('dirstate')
 
1239
        try:
 
1240
            state.add(link_name, 'a link id', 'symlink', stat,
 
1241
                      target.encode('UTF-8'))
 
1242
            # having added it, it should be in the output of iter_entries.
 
1243
            self.assertEqual(expected_entries, list(state._iter_entries()))
 
1244
            # saving and reloading should not affect this.
 
1245
            state.save()
 
1246
        finally:
 
1247
            state.unlock()
 
1248
        state = dirstate.DirState.on_file('dirstate')
 
1249
        state.lock_read()
 
1250
        self.addCleanup(state.unlock)
 
1251
        self.assertEqual(expected_entries, list(state._iter_entries()))
 
1252
 
 
1253
    def test_add_symlink_to_root_no_parents_all_data(self):
 
1254
        self._test_add_symlink_to_root_no_parents_all_data('a link', 'target')
 
1255
 
 
1256
    def test_add_symlink_unicode_to_root_no_parents_all_data(self):
 
1257
        self.requireFeature(features.UnicodeFilenameFeature)
 
1258
        self._test_add_symlink_to_root_no_parents_all_data(
 
1259
            u'\N{Euro Sign}link', u'targ\N{Euro Sign}et')
 
1260
 
 
1261
    def test_add_directory_and_child_no_parents_all_data(self):
 
1262
        # after adding a directory, we should be able to add children to it.
 
1263
        self.build_tree(['a dir/', 'a dir/a file'])
 
1264
        dirstat = os.lstat('a dir')
 
1265
        filestat = os.lstat('a dir/a file')
 
1266
        expected_entries = [
 
1267
            (('', '', 'TREE_ROOT'), [
 
1268
             ('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
 
1269
             ]),
 
1270
            (('', 'a dir', 'a dir id'), [
 
1271
             ('d', '', 0, False, dirstate.pack_stat(dirstat)), # current tree
 
1272
             ]),
 
1273
            (('a dir', 'a file', 'a-file-id'), [
 
1274
             ('f', '1'*20, 25, False,
 
1275
              dirstate.pack_stat(filestat)), # current tree details
 
1276
             ]),
 
1277
            ]
 
1278
        state = dirstate.DirState.initialize('dirstate')
 
1279
        try:
 
1280
            state.add('a dir', 'a dir id', 'directory', dirstat, None)
 
1281
            state.add('a dir/a file', 'a-file-id', 'file', filestat, '1'*20)
 
1282
            # added it, it should be in the output of iter_entries.
 
1283
            self.assertEqual(expected_entries, list(state._iter_entries()))
 
1284
            # saving and reloading should not affect this.
 
1285
            state.save()
 
1286
        finally:
 
1287
            state.unlock()
 
1288
        state = dirstate.DirState.on_file('dirstate')
 
1289
        state.lock_read()
 
1290
        self.addCleanup(state.unlock)
 
1291
        self.assertEqual(expected_entries, list(state._iter_entries()))
 
1292
 
 
1293
    def test_add_tree_reference(self):
 
1294
        # make a dirstate and add a tree reference
 
1295
        state = dirstate.DirState.initialize('dirstate')
 
1296
        expected_entry = (
 
1297
            ('', 'subdir', 'subdir-id'),
 
1298
            [('t', 'subtree-123123', 0, False,
 
1299
              'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')],
 
1300
            )
 
1301
        try:
 
1302
            state.add('subdir', 'subdir-id', 'tree-reference', None, 'subtree-123123')
 
1303
            entry = state._get_entry(0, 'subdir-id', 'subdir')
 
1304
            self.assertEqual(entry, expected_entry)
 
1305
            state._validate()
 
1306
            state.save()
 
1307
        finally:
 
1308
            state.unlock()
 
1309
        # now check we can read it back
 
1310
        state.lock_read()
 
1311
        self.addCleanup(state.unlock)
 
1312
        state._validate()
 
1313
        entry2 = state._get_entry(0, 'subdir-id', 'subdir')
 
1314
        self.assertEqual(entry, entry2)
 
1315
        self.assertEqual(entry, expected_entry)
 
1316
        # and lookup by id should work too
 
1317
        entry2 = state._get_entry(0, fileid_utf8='subdir-id')
 
1318
        self.assertEqual(entry, expected_entry)
 
1319
 
 
1320
    def test_add_forbidden_names(self):
 
1321
        state = dirstate.DirState.initialize('dirstate')
 
1322
        self.addCleanup(state.unlock)
 
1323
        self.assertRaises(errors.BzrError,
 
1324
            state.add, '.', 'ass-id', 'directory', None, None)
 
1325
        self.assertRaises(errors.BzrError,
 
1326
            state.add, '..', 'ass-id', 'directory', None, None)
 
1327
 
 
1328
    def test_set_state_with_rename_b_a_bug_395556(self):
 
1329
        # bug 395556 uncovered a bug where the dirstate ends up with a false
 
1330
        # relocation record - in a tree with no parents there should be no
 
1331
        # absent or relocated records. This then leads to further corruption
 
1332
        # when a commit occurs, as the incorrect relocation gathers an
 
1333
        # incorrect absent in tree 1, and future changes go to pot.
 
1334
        tree1 = self.make_branch_and_tree('tree1')
 
1335
        self.build_tree(['tree1/b'])
 
1336
        tree1.lock_write()
 
1337
        try:
 
1338
            tree1.add(['b'], ['b-id'])
 
1339
            root_id = tree1.get_root_id()
 
1340
            inv = tree1.root_inventory
 
1341
            state = dirstate.DirState.initialize('dirstate')
 
1342
            try:
 
1343
                # Set the initial state with 'b'
 
1344
                state.set_state_from_inventory(inv)
 
1345
                inv.rename('b-id', root_id, 'a')
 
1346
                # Set the new state with 'a', which currently corrupts.
 
1347
                state.set_state_from_inventory(inv)
 
1348
                expected_result1 = [('', '', root_id, 'd'),
 
1349
                                    ('', 'a', 'b-id', 'f'),
 
1350
                                   ]
 
1351
                values = []
 
1352
                for entry in state._iter_entries():
 
1353
                    values.append(entry[0] + entry[1][0][:1])
 
1354
                self.assertEqual(expected_result1, values)
 
1355
            finally:
 
1356
                state.unlock()
 
1357
        finally:
 
1358
            tree1.unlock()
 
1359
 
 
1360
 
 
1361
class TestDirStateHashUpdates(TestCaseWithDirState):
 
1362
 
 
1363
    def do_update_entry(self, state, path):
 
1364
        entry = state._get_entry(0, path_utf8=path)
 
1365
        stat = os.lstat(path)
 
1366
        return dirstate.update_entry(state, entry, os.path.abspath(path), stat)
 
1367
 
 
1368
    def _read_state_content(self, state):
 
1369
        """Read the content of the dirstate file.
 
1370
 
 
1371
        On Windows when one process locks a file, you can't even open() the
 
1372
        file in another process (to read it). So we go directly to
 
1373
        state._state_file. This should always be the exact disk representation,
 
1374
        so it is reasonable to do so.
 
1375
        DirState also always seeks before reading, so it doesn't matter if we
 
1376
        bump the file pointer.
 
1377
        """
 
1378
        state._state_file.seek(0)
 
1379
        return state._state_file.read()
 
1380
 
 
1381
    def test_worth_saving_limit_avoids_writing(self):
 
1382
        tree = self.make_branch_and_tree('.')
 
1383
        self.build_tree(['c', 'd'])
 
1384
        tree.lock_write()
 
1385
        tree.add(['c', 'd'], ['c-id', 'd-id'])
 
1386
        tree.commit('add c and d')
 
1387
        state = InstrumentedDirState.on_file(tree.current_dirstate()._filename,
 
1388
                                             worth_saving_limit=2)
 
1389
        tree.unlock()
 
1390
        state.lock_write()
 
1391
        self.addCleanup(state.unlock)
 
1392
        state._read_dirblocks_if_needed()
 
1393
        state.adjust_time(+20) # Allow things to be cached
 
1394
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
1395
                         state._dirblock_state)
 
1396
        content = self._read_state_content(state)
 
1397
        self.do_update_entry(state, 'c')
 
1398
        self.assertEqual(1, len(state._known_hash_changes))
 
1399
        self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
 
1400
                         state._dirblock_state)
 
1401
        state.save()
 
1402
        # It should not have set the state to IN_MEMORY_UNMODIFIED because the
 
1403
        # hash values haven't been written out.
 
1404
        self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
 
1405
                         state._dirblock_state)
 
1406
        self.assertEqual(content, self._read_state_content(state))
 
1407
        self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
 
1408
                         state._dirblock_state)
 
1409
        self.do_update_entry(state, 'd')
 
1410
        self.assertEqual(2, len(state._known_hash_changes))
 
1411
        state.save()
 
1412
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
1413
                         state._dirblock_state)
 
1414
        self.assertEqual(0, len(state._known_hash_changes))
 
1415
 
 
1416
 
 
1417
class TestGetLines(TestCaseWithDirState):
 
1418
 
 
1419
    def test_get_line_with_2_rows(self):
 
1420
        state = self.create_dirstate_with_root_and_subdir()
 
1421
        try:
 
1422
            self.assertEqual(['#bazaar dirstate flat format 3\n',
 
1423
                'crc32: 41262208\n',
 
1424
                'num_entries: 2\n',
 
1425
                '0\x00\n\x00'
 
1426
                '0\x00\n\x00'
 
1427
                '\x00\x00a-root-value\x00'
 
1428
                'd\x00\x000\x00n\x00AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk\x00\n\x00'
 
1429
                '\x00subdir\x00subdir-id\x00'
 
1430
                'd\x00\x000\x00n\x00AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk\x00\n\x00'
 
1431
                ], state.get_lines())
 
1432
        finally:
 
1433
            state.unlock()
 
1434
 
 
1435
    def test_entry_to_line(self):
 
1436
        state = self.create_dirstate_with_root()
 
1437
        try:
 
1438
            self.assertEqual(
 
1439
                '\x00\x00a-root-value\x00d\x00\x000\x00n'
 
1440
                '\x00AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk',
 
1441
                state._entry_to_line(state._dirblocks[0][1][0]))
 
1442
        finally:
 
1443
            state.unlock()
 
1444
 
 
1445
    def test_entry_to_line_with_parent(self):
 
1446
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
 
1447
        root_entry = ('', '', 'a-root-value'), [
 
1448
            ('d', '', 0, False, packed_stat), # current tree details
 
1449
             # first: a pointer to the current location
 
1450
            ('a', 'dirname/basename', 0, False, ''),
 
1451
            ]
 
1452
        state = dirstate.DirState.initialize('dirstate')
 
1453
        try:
 
1454
            self.assertEqual(
 
1455
                '\x00\x00a-root-value\x00'
 
1456
                'd\x00\x000\x00n\x00AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk\x00'
 
1457
                'a\x00dirname/basename\x000\x00n\x00',
 
1458
                state._entry_to_line(root_entry))
 
1459
        finally:
 
1460
            state.unlock()
 
1461
 
 
1462
    def test_entry_to_line_with_two_parents_at_different_paths(self):
 
1463
        # / in the tree, at / in one parent and /dirname/basename in the other.
 
1464
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
 
1465
        root_entry = ('', '', 'a-root-value'), [
 
1466
            ('d', '', 0, False, packed_stat), # current tree details
 
1467
            ('d', '', 0, False, 'rev_id'), # first parent details
 
1468
             # second: a pointer to the current location
 
1469
            ('a', 'dirname/basename', 0, False, ''),
 
1470
            ]
 
1471
        state = dirstate.DirState.initialize('dirstate')
 
1472
        try:
 
1473
            self.assertEqual(
 
1474
                '\x00\x00a-root-value\x00'
 
1475
                'd\x00\x000\x00n\x00AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk\x00'
 
1476
                'd\x00\x000\x00n\x00rev_id\x00'
 
1477
                'a\x00dirname/basename\x000\x00n\x00',
 
1478
                state._entry_to_line(root_entry))
 
1479
        finally:
 
1480
            state.unlock()
 
1481
 
 
1482
    def test_iter_entries(self):
 
1483
        # we should be able to iterate the dirstate entries from end to end
 
1484
        # this is for get_lines to be easy to read.
 
1485
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
 
1486
        dirblocks = []
 
1487
        root_entries = [(('', '', 'a-root-value'), [
 
1488
            ('d', '', 0, False, packed_stat), # current tree details
 
1489
            ])]
 
1490
        dirblocks.append(('', root_entries))
 
1491
        # add two files in the root
 
1492
        subdir_entry = ('', 'subdir', 'subdir-id'), [
 
1493
            ('d', '', 0, False, packed_stat), # current tree details
 
1494
            ]
 
1495
        afile_entry = ('', 'afile', 'afile-id'), [
 
1496
            ('f', 'sha1value', 34, False, packed_stat), # current tree details
 
1497
            ]
 
1498
        dirblocks.append(('', [subdir_entry, afile_entry]))
 
1499
        # and one in subdir
 
1500
        file_entry2 = ('subdir', '2file', '2file-id'), [
 
1501
            ('f', 'sha1value', 23, False, packed_stat), # current tree details
 
1502
            ]
 
1503
        dirblocks.append(('subdir', [file_entry2]))
 
1504
        state = dirstate.DirState.initialize('dirstate')
 
1505
        try:
 
1506
            state._set_data([], dirblocks)
 
1507
            expected_entries = [root_entries[0], subdir_entry, afile_entry,
 
1508
                                file_entry2]
 
1509
            self.assertEqual(expected_entries, list(state._iter_entries()))
 
1510
        finally:
 
1511
            state.unlock()
 
1512
 
 
1513
 
 
1514
class TestGetBlockRowIndex(TestCaseWithDirState):
 
1515
 
 
1516
    def assertBlockRowIndexEqual(self, block_index, row_index, dir_present,
 
1517
        file_present, state, dirname, basename, tree_index):
 
1518
        self.assertEqual((block_index, row_index, dir_present, file_present),
 
1519
            state._get_block_entry_index(dirname, basename, tree_index))
 
1520
        if dir_present:
 
1521
            block = state._dirblocks[block_index]
 
1522
            self.assertEqual(dirname, block[0])
 
1523
        if dir_present and file_present:
 
1524
            row = state._dirblocks[block_index][1][row_index]
 
1525
            self.assertEqual(dirname, row[0][0])
 
1526
            self.assertEqual(basename, row[0][1])
 
1527
 
 
1528
    def test_simple_structure(self):
 
1529
        state = self.create_dirstate_with_root_and_subdir()
 
1530
        self.addCleanup(state.unlock)
 
1531
        self.assertBlockRowIndexEqual(1, 0, True, True, state, '', 'subdir', 0)
 
1532
        self.assertBlockRowIndexEqual(1, 0, True, False, state, '', 'bdir', 0)
 
1533
        self.assertBlockRowIndexEqual(1, 1, True, False, state, '', 'zdir', 0)
 
1534
        self.assertBlockRowIndexEqual(2, 0, False, False, state, 'a', 'foo', 0)
 
1535
        self.assertBlockRowIndexEqual(2, 0, False, False, state,
 
1536
                                      'subdir', 'foo', 0)
 
1537
 
 
1538
    def test_complex_structure_exists(self):
 
1539
        state = self.create_complex_dirstate()
 
1540
        self.addCleanup(state.unlock)
 
1541
        # Make sure we can find everything that exists
 
1542
        self.assertBlockRowIndexEqual(0, 0, True, True, state, '', '', 0)
 
1543
        self.assertBlockRowIndexEqual(1, 0, True, True, state, '', 'a', 0)
 
1544
        self.assertBlockRowIndexEqual(1, 1, True, True, state, '', 'b', 0)
 
1545
        self.assertBlockRowIndexEqual(1, 2, True, True, state, '', 'c', 0)
 
1546
        self.assertBlockRowIndexEqual(1, 3, True, True, state, '', 'd', 0)
 
1547
        self.assertBlockRowIndexEqual(2, 0, True, True, state, 'a', 'e', 0)
 
1548
        self.assertBlockRowIndexEqual(2, 1, True, True, state, 'a', 'f', 0)
 
1549
        self.assertBlockRowIndexEqual(3, 0, True, True, state, 'b', 'g', 0)
 
1550
        self.assertBlockRowIndexEqual(3, 1, True, True, state,
 
1551
                                      'b', 'h\xc3\xa5', 0)
 
1552
 
 
1553
    def test_complex_structure_missing(self):
 
1554
        state = self.create_complex_dirstate()
 
1555
        self.addCleanup(state.unlock)
 
1556
        # Make sure things would be inserted in the right locations
 
1557
        # '_' comes before 'a'
 
1558
        self.assertBlockRowIndexEqual(0, 0, True, True, state, '', '', 0)
 
1559
        self.assertBlockRowIndexEqual(1, 0, True, False, state, '', '_', 0)
 
1560
        self.assertBlockRowIndexEqual(1, 1, True, False, state, '', 'aa', 0)
 
1561
        self.assertBlockRowIndexEqual(1, 4, True, False, state,
 
1562
                                      '', 'h\xc3\xa5', 0)
 
1563
        self.assertBlockRowIndexEqual(2, 0, False, False, state, '_', 'a', 0)
 
1564
        self.assertBlockRowIndexEqual(3, 0, False, False, state, 'aa', 'a', 0)
 
1565
        self.assertBlockRowIndexEqual(4, 0, False, False, state, 'bb', 'a', 0)
 
1566
        # This would be inserted between a/ and b/
 
1567
        self.assertBlockRowIndexEqual(3, 0, False, False, state, 'a/e', 'a', 0)
 
1568
        # Put at the end
 
1569
        self.assertBlockRowIndexEqual(4, 0, False, False, state, 'e', 'a', 0)
 
1570
 
 
1571
 
 
1572
class TestGetEntry(TestCaseWithDirState):
 
1573
 
 
1574
    def assertEntryEqual(self, dirname, basename, file_id, state, path, index):
 
1575
        """Check that the right entry is returned for a request to getEntry."""
 
1576
        entry = state._get_entry(index, path_utf8=path)
 
1577
        if file_id is None:
 
1578
            self.assertEqual((None, None), entry)
 
1579
        else:
 
1580
            cur = entry[0]
 
1581
            self.assertEqual((dirname, basename, file_id), cur[:3])
 
1582
 
 
1583
    def test_simple_structure(self):
 
1584
        state = self.create_dirstate_with_root_and_subdir()
 
1585
        self.addCleanup(state.unlock)
 
1586
        self.assertEntryEqual('', '', 'a-root-value', state, '', 0)
 
1587
        self.assertEntryEqual('', 'subdir', 'subdir-id', state, 'subdir', 0)
 
1588
        self.assertEntryEqual(None, None, None, state, 'missing', 0)
 
1589
        self.assertEntryEqual(None, None, None, state, 'missing/foo', 0)
 
1590
        self.assertEntryEqual(None, None, None, state, 'subdir/foo', 0)
 
1591
 
 
1592
    def test_complex_structure_exists(self):
 
1593
        state = self.create_complex_dirstate()
 
1594
        self.addCleanup(state.unlock)
 
1595
        self.assertEntryEqual('', '', 'a-root-value', state, '', 0)
 
1596
        self.assertEntryEqual('', 'a', 'a-dir', state, 'a', 0)
 
1597
        self.assertEntryEqual('', 'b', 'b-dir', state, 'b', 0)
 
1598
        self.assertEntryEqual('', 'c', 'c-file', state, 'c', 0)
 
1599
        self.assertEntryEqual('', 'd', 'd-file', state, 'd', 0)
 
1600
        self.assertEntryEqual('a', 'e', 'e-dir', state, 'a/e', 0)
 
1601
        self.assertEntryEqual('a', 'f', 'f-file', state, 'a/f', 0)
 
1602
        self.assertEntryEqual('b', 'g', 'g-file', state, 'b/g', 0)
 
1603
        self.assertEntryEqual('b', 'h\xc3\xa5', 'h-\xc3\xa5-file', state,
 
1604
                              'b/h\xc3\xa5', 0)
 
1605
 
 
1606
    def test_complex_structure_missing(self):
 
1607
        state = self.create_complex_dirstate()
 
1608
        self.addCleanup(state.unlock)
 
1609
        self.assertEntryEqual(None, None, None, state, '_', 0)
 
1610
        self.assertEntryEqual(None, None, None, state, '_\xc3\xa5', 0)
 
1611
        self.assertEntryEqual(None, None, None, state, 'a/b', 0)
 
1612
        self.assertEntryEqual(None, None, None, state, 'c/d', 0)
 
1613
 
 
1614
    def test_get_entry_uninitialized(self):
 
1615
        """Calling get_entry will load data if it needs to"""
 
1616
        state = self.create_dirstate_with_root()
 
1617
        try:
 
1618
            state.save()
 
1619
        finally:
 
1620
            state.unlock()
 
1621
        del state
 
1622
        state = dirstate.DirState.on_file('dirstate')
 
1623
        state.lock_read()
 
1624
        try:
 
1625
            self.assertEqual(dirstate.DirState.NOT_IN_MEMORY,
 
1626
                             state._header_state)
 
1627
            self.assertEqual(dirstate.DirState.NOT_IN_MEMORY,
 
1628
                             state._dirblock_state)
 
1629
            self.assertEntryEqual('', '', 'a-root-value', state, '', 0)
 
1630
        finally:
 
1631
            state.unlock()
 
1632
 
 
1633
 
 
1634
class TestIterChildEntries(TestCaseWithDirState):
 
1635
 
 
1636
    def create_dirstate_with_two_trees(self):
 
1637
        """This dirstate contains multiple files and directories.
 
1638
 
 
1639
         /        a-root-value
 
1640
         a/       a-dir
 
1641
         b/       b-dir
 
1642
         c        c-file
 
1643
         d        d-file
 
1644
         a/e/     e-dir
 
1645
         a/f      f-file
 
1646
         b/g      g-file
 
1647
         b/h\xc3\xa5  h-\xc3\xa5-file  #This is u'\xe5' encoded into utf-8
 
1648
 
 
1649
        Notice that a/e is an empty directory.
 
1650
 
 
1651
        There is one parent tree, which has the same shape with the following variations:
 
1652
        b/g in the parent is gone.
 
1653
        b/h in the parent has a different id
 
1654
        b/i is new in the parent
 
1655
        c is renamed to b/j in the parent
 
1656
 
 
1657
        :return: The dirstate, still write-locked.
 
1658
        """
 
1659
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
 
1660
        null_sha = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
 
1661
        NULL_PARENT_DETAILS = dirstate.DirState.NULL_PARENT_DETAILS
 
1662
        root_entry = ('', '', 'a-root-value'), [
 
1663
            ('d', '', 0, False, packed_stat),
 
1664
            ('d', '', 0, False, 'parent-revid'),
 
1665
            ]
 
1666
        a_entry = ('', 'a', 'a-dir'), [
 
1667
            ('d', '', 0, False, packed_stat),
 
1668
            ('d', '', 0, False, 'parent-revid'),
 
1669
            ]
 
1670
        b_entry = ('', 'b', 'b-dir'), [
 
1671
            ('d', '', 0, False, packed_stat),
 
1672
            ('d', '', 0, False, 'parent-revid'),
 
1673
            ]
 
1674
        c_entry = ('', 'c', 'c-file'), [
 
1675
            ('f', null_sha, 10, False, packed_stat),
 
1676
            ('r', 'b/j', 0, False, ''),
 
1677
            ]
 
1678
        d_entry = ('', 'd', 'd-file'), [
 
1679
            ('f', null_sha, 20, False, packed_stat),
 
1680
            ('f', 'd', 20, False, 'parent-revid'),
 
1681
            ]
 
1682
        e_entry = ('a', 'e', 'e-dir'), [
 
1683
            ('d', '', 0, False, packed_stat),
 
1684
            ('d', '', 0, False, 'parent-revid'),
 
1685
            ]
 
1686
        f_entry = ('a', 'f', 'f-file'), [
 
1687
            ('f', null_sha, 30, False, packed_stat),
 
1688
            ('f', 'f', 20, False, 'parent-revid'),
 
1689
            ]
 
1690
        g_entry = ('b', 'g', 'g-file'), [
 
1691
            ('f', null_sha, 30, False, packed_stat),
 
1692
            NULL_PARENT_DETAILS,
 
1693
            ]
 
1694
        h_entry1 = ('b', 'h\xc3\xa5', 'h-\xc3\xa5-file1'), [
 
1695
            ('f', null_sha, 40, False, packed_stat),
 
1696
            NULL_PARENT_DETAILS,
 
1697
            ]
 
1698
        h_entry2 = ('b', 'h\xc3\xa5', 'h-\xc3\xa5-file2'), [
 
1699
            NULL_PARENT_DETAILS,
 
1700
            ('f', 'h', 20, False, 'parent-revid'),
 
1701
            ]
 
1702
        i_entry = ('b', 'i', 'i-file'), [
 
1703
            NULL_PARENT_DETAILS,
 
1704
            ('f', 'h', 20, False, 'parent-revid'),
 
1705
            ]
 
1706
        j_entry = ('b', 'j', 'c-file'), [
 
1707
            ('r', 'c', 0, False, ''),
 
1708
            ('f', 'j', 20, False, 'parent-revid'),
 
1709
            ]
 
1710
        dirblocks = []
 
1711
        dirblocks.append(('', [root_entry]))
 
1712
        dirblocks.append(('', [a_entry, b_entry, c_entry, d_entry]))
 
1713
        dirblocks.append(('a', [e_entry, f_entry]))
 
1714
        dirblocks.append(('b', [g_entry, h_entry1, h_entry2, i_entry, j_entry]))
 
1715
        state = dirstate.DirState.initialize('dirstate')
 
1716
        state._validate()
 
1717
        try:
 
1718
            state._set_data(['parent'], dirblocks)
 
1719
        except:
 
1720
            state.unlock()
 
1721
            raise
 
1722
        return state, dirblocks
 
1723
 
 
1724
    def test_iter_children_b(self):
 
1725
        state, dirblocks = self.create_dirstate_with_two_trees()
 
1726
        self.addCleanup(state.unlock)
 
1727
        expected_result = []
 
1728
        expected_result.append(dirblocks[3][1][2]) # h2
 
1729
        expected_result.append(dirblocks[3][1][3]) # i
 
1730
        expected_result.append(dirblocks[3][1][4]) # j
 
1731
        self.assertEqual(expected_result,
 
1732
            list(state._iter_child_entries(1, 'b')))
 
1733
 
 
1734
    def test_iter_child_root(self):
 
1735
        state, dirblocks = self.create_dirstate_with_two_trees()
 
1736
        self.addCleanup(state.unlock)
 
1737
        expected_result = []
 
1738
        expected_result.append(dirblocks[1][1][0]) # a
 
1739
        expected_result.append(dirblocks[1][1][1]) # b
 
1740
        expected_result.append(dirblocks[1][1][3]) # d
 
1741
        expected_result.append(dirblocks[2][1][0]) # e
 
1742
        expected_result.append(dirblocks[2][1][1]) # f
 
1743
        expected_result.append(dirblocks[3][1][2]) # h2
 
1744
        expected_result.append(dirblocks[3][1][3]) # i
 
1745
        expected_result.append(dirblocks[3][1][4]) # j
 
1746
        self.assertEqual(expected_result,
 
1747
            list(state._iter_child_entries(1, '')))
 
1748
 
 
1749
 
 
1750
class TestDirstateSortOrder(tests.TestCaseWithTransport):
 
1751
    """Test that DirState adds entries in the right order."""
 
1752
 
 
1753
    def test_add_sorting(self):
 
1754
        """Add entries in lexicographical order, we get path sorted order.
 
1755
 
 
1756
        This tests it to a depth of 4, to make sure we don't just get it right
 
1757
        at a single depth. 'a/a' should come before 'a-a', even though it
 
1758
        doesn't lexicographically.
 
1759
        """
 
1760
        dirs = ['a', 'a/a', 'a/a/a', 'a/a/a/a',
 
1761
                'a-a', 'a/a-a', 'a/a/a-a', 'a/a/a/a-a',
 
1762
               ]
 
1763
        null_sha = ''
 
1764
        state = dirstate.DirState.initialize('dirstate')
 
1765
        self.addCleanup(state.unlock)
 
1766
 
 
1767
        fake_stat = os.stat('dirstate')
 
1768
        for d in dirs:
 
1769
            d_id = d.replace('/', '_')+'-id'
 
1770
            file_path = d + '/f'
 
1771
            file_id = file_path.replace('/', '_')+'-id'
 
1772
            state.add(d, d_id, 'directory', fake_stat, null_sha)
 
1773
            state.add(file_path, file_id, 'file', fake_stat, null_sha)
 
1774
 
 
1775
        expected = ['', '', 'a',
 
1776
                'a/a', 'a/a/a', 'a/a/a/a',
 
1777
                'a/a/a/a-a', 'a/a/a-a', 'a/a-a', 'a-a',
 
1778
               ]
 
1779
        split = lambda p:p.split('/')
 
1780
        self.assertEqual(sorted(expected, key=split), expected)
 
1781
        dirblock_names = [d[0] for d in state._dirblocks]
 
1782
        self.assertEqual(expected, dirblock_names)
 
1783
 
 
1784
    def test_set_parent_trees_correct_order(self):
 
1785
        """After calling set_parent_trees() we should maintain the order."""
 
1786
        dirs = ['a', 'a-a', 'a/a']
 
1787
        null_sha = ''
 
1788
        state = dirstate.DirState.initialize('dirstate')
 
1789
        self.addCleanup(state.unlock)
 
1790
 
 
1791
        fake_stat = os.stat('dirstate')
 
1792
        for d in dirs:
 
1793
            d_id = d.replace('/', '_')+'-id'
 
1794
            file_path = d + '/f'
 
1795
            file_id = file_path.replace('/', '_')+'-id'
 
1796
            state.add(d, d_id, 'directory', fake_stat, null_sha)
 
1797
            state.add(file_path, file_id, 'file', fake_stat, null_sha)
 
1798
 
 
1799
        expected = ['', '', 'a', 'a/a', 'a-a']
 
1800
        dirblock_names = [d[0] for d in state._dirblocks]
 
1801
        self.assertEqual(expected, dirblock_names)
 
1802
 
 
1803
        # *really* cheesy way to just get an empty tree
 
1804
        repo = self.make_repository('repo')
 
1805
        empty_tree = repo.revision_tree(_mod_revision.NULL_REVISION)
 
1806
        state.set_parent_trees([('null:', empty_tree)], [])
 
1807
 
 
1808
        dirblock_names = [d[0] for d in state._dirblocks]
 
1809
        self.assertEqual(expected, dirblock_names)
 
1810
 
 
1811
 
 
1812
class InstrumentedDirState(dirstate.DirState):
 
1813
    """An DirState with instrumented sha1 functionality."""
 
1814
 
 
1815
    def __init__(self, path, sha1_provider, worth_saving_limit=0):
 
1816
        super(InstrumentedDirState, self).__init__(path, sha1_provider,
 
1817
            worth_saving_limit=worth_saving_limit)
 
1818
        self._time_offset = 0
 
1819
        self._log = []
 
1820
        # member is dynamically set in DirState.__init__ to turn on trace
 
1821
        self._sha1_provider = sha1_provider
 
1822
        self._sha1_file = self._sha1_file_and_log
 
1823
 
 
1824
    def _sha_cutoff_time(self):
 
1825
        timestamp = super(InstrumentedDirState, self)._sha_cutoff_time()
 
1826
        self._cutoff_time = timestamp + self._time_offset
 
1827
 
 
1828
    def _sha1_file_and_log(self, abspath):
 
1829
        self._log.append(('sha1', abspath))
 
1830
        return self._sha1_provider.sha1(abspath)
 
1831
 
 
1832
    def _read_link(self, abspath, old_link):
 
1833
        self._log.append(('read_link', abspath, old_link))
 
1834
        return super(InstrumentedDirState, self)._read_link(abspath, old_link)
 
1835
 
 
1836
    def _lstat(self, abspath, entry):
 
1837
        self._log.append(('lstat', abspath))
 
1838
        return super(InstrumentedDirState, self)._lstat(abspath, entry)
 
1839
 
 
1840
    def _is_executable(self, mode, old_executable):
 
1841
        self._log.append(('is_exec', mode, old_executable))
 
1842
        return super(InstrumentedDirState, self)._is_executable(mode,
 
1843
                                                                old_executable)
 
1844
 
 
1845
    def adjust_time(self, secs):
 
1846
        """Move the clock forward or back.
 
1847
 
 
1848
        :param secs: The amount to adjust the clock by. Positive values make it
 
1849
        seem as if we are in the future, negative values make it seem like we
 
1850
        are in the past.
 
1851
        """
 
1852
        self._time_offset += secs
 
1853
        self._cutoff_time = None
 
1854
 
 
1855
 
 
1856
class _FakeStat(object):
 
1857
    """A class with the same attributes as a real stat result."""
 
1858
 
 
1859
    def __init__(self, size, mtime, ctime, dev, ino, mode):
 
1860
        self.st_size = size
 
1861
        self.st_mtime = mtime
 
1862
        self.st_ctime = ctime
 
1863
        self.st_dev = dev
 
1864
        self.st_ino = ino
 
1865
        self.st_mode = mode
 
1866
 
 
1867
    @staticmethod
 
1868
    def from_stat(st):
 
1869
        return _FakeStat(st.st_size, st.st_mtime, st.st_ctime, st.st_dev,
 
1870
            st.st_ino, st.st_mode)
 
1871
 
 
1872
 
 
1873
class TestPackStat(tests.TestCaseWithTransport):
 
1874
 
 
1875
    def assertPackStat(self, expected, stat_value):
 
1876
        """Check the packed and serialized form of a stat value."""
 
1877
        self.assertEqual(expected, dirstate.pack_stat(stat_value))
 
1878
 
 
1879
    def test_pack_stat_int(self):
 
1880
        st = _FakeStat(6859, 1172758614, 1172758617, 777, 6499538, 0o100644)
 
1881
        # Make sure that all parameters have an impact on the packed stat.
 
1882
        self.assertPackStat('AAAay0Xm4FZF5uBZAAADCQBjLNIAAIGk', st)
 
1883
        st.st_size = 7000
 
1884
        #                ay0 => bWE
 
1885
        self.assertPackStat('AAAbWEXm4FZF5uBZAAADCQBjLNIAAIGk', st)
 
1886
        st.st_mtime = 1172758620
 
1887
        #                     4FZ => 4Fx
 
1888
        self.assertPackStat('AAAbWEXm4FxF5uBZAAADCQBjLNIAAIGk', st)
 
1889
        st.st_ctime = 1172758630
 
1890
        #                          uBZ => uBm
 
1891
        self.assertPackStat('AAAbWEXm4FxF5uBmAAADCQBjLNIAAIGk', st)
 
1892
        st.st_dev = 888
 
1893
        #                                DCQ => DeA
 
1894
        self.assertPackStat('AAAbWEXm4FxF5uBmAAADeABjLNIAAIGk', st)
 
1895
        st.st_ino = 6499540
 
1896
        #                                     LNI => LNQ
 
1897
        self.assertPackStat('AAAbWEXm4FxF5uBmAAADeABjLNQAAIGk', st)
 
1898
        st.st_mode = 0o100744
 
1899
        #                                          IGk => IHk
 
1900
        self.assertPackStat('AAAbWEXm4FxF5uBmAAADeABjLNQAAIHk', st)
 
1901
 
 
1902
    def test_pack_stat_float(self):
 
1903
        """On some platforms mtime and ctime are floats.
 
1904
 
 
1905
        Make sure we don't get warnings or errors, and that we ignore changes <
 
1906
        1s
 
1907
        """
 
1908
        st = _FakeStat(7000, 1172758614.0, 1172758617.0,
 
1909
                       777, 6499538, 0o100644)
 
1910
        # These should all be the same as the integer counterparts
 
1911
        self.assertPackStat('AAAbWEXm4FZF5uBZAAADCQBjLNIAAIGk', st)
 
1912
        st.st_mtime = 1172758620.0
 
1913
        #                     FZF5 => FxF5
 
1914
        self.assertPackStat('AAAbWEXm4FxF5uBZAAADCQBjLNIAAIGk', st)
 
1915
        st.st_ctime = 1172758630.0
 
1916
        #                          uBZ => uBm
 
1917
        self.assertPackStat('AAAbWEXm4FxF5uBmAAADCQBjLNIAAIGk', st)
 
1918
        # fractional seconds are discarded, so no change from above
 
1919
        st.st_mtime = 1172758620.453
 
1920
        self.assertPackStat('AAAbWEXm4FxF5uBmAAADCQBjLNIAAIGk', st)
 
1921
        st.st_ctime = 1172758630.228
 
1922
        self.assertPackStat('AAAbWEXm4FxF5uBmAAADCQBjLNIAAIGk', st)
 
1923
 
 
1924
 
 
1925
class TestBisect(TestCaseWithDirState):
 
1926
    """Test the ability to bisect into the disk format."""
 
1927
 
 
1928
    def assertBisect(self, expected_map, map_keys, state, paths):
 
1929
        """Assert that bisecting for paths returns the right result.
 
1930
 
 
1931
        :param expected_map: A map from key => entry value
 
1932
        :param map_keys: The keys to expect for each path
 
1933
        :param state: The DirState object.
 
1934
        :param paths: A list of paths, these will automatically be split into
 
1935
                      (dir, name) tuples, and sorted according to how _bisect
 
1936
                      requires.
 
1937
        """
 
1938
        result = state._bisect(paths)
 
1939
        # For now, results are just returned in whatever order we read them.
 
1940
        # We could sort by (dir, name, file_id) or something like that, but in
 
1941
        # the end it would still be fairly arbitrary, and we don't want the
 
1942
        # extra overhead if we can avoid it. So sort everything to make sure
 
1943
        # equality is true
 
1944
        self.assertEqual(len(map_keys), len(paths))
 
1945
        expected = {}
 
1946
        for path, keys in zip(paths, map_keys):
 
1947
            if keys is None:
 
1948
                # This should not be present in the output
 
1949
                continue
 
1950
            expected[path] = sorted(expected_map[k] for k in keys)
 
1951
 
 
1952
        # The returned values are just arranged randomly based on when they
 
1953
        # were read, for testing, make sure it is properly sorted.
 
1954
        for path in result:
 
1955
            result[path].sort()
 
1956
 
 
1957
        self.assertEqual(expected, result)
 
1958
 
 
1959
    def assertBisectDirBlocks(self, expected_map, map_keys, state, paths):
 
1960
        """Assert that bisecting for dirbblocks returns the right result.
 
1961
 
 
1962
        :param expected_map: A map from key => expected values
 
1963
        :param map_keys: A nested list of paths we expect to be returned.
 
1964
            Something like [['a', 'b', 'f'], ['b/c', 'b/d']]
 
1965
        :param state: The DirState object.
 
1966
        :param paths: A list of directories
 
1967
        """
 
1968
        result = state._bisect_dirblocks(paths)
 
1969
        self.assertEqual(len(map_keys), len(paths))
 
1970
        expected = {}
 
1971
        for path, keys in zip(paths, map_keys):
 
1972
            if keys is None:
 
1973
                # This should not be present in the output
 
1974
                continue
 
1975
            expected[path] = sorted(expected_map[k] for k in keys)
 
1976
        for path in result:
 
1977
            result[path].sort()
 
1978
 
 
1979
        self.assertEqual(expected, result)
 
1980
 
 
1981
    def assertBisectRecursive(self, expected_map, map_keys, state, paths):
 
1982
        """Assert the return value of a recursive bisection.
 
1983
 
 
1984
        :param expected_map: A map from key => entry value
 
1985
        :param map_keys: A list of paths we expect to be returned.
 
1986
            Something like ['a', 'b', 'f', 'b/d', 'b/d2']
 
1987
        :param state: The DirState object.
 
1988
        :param paths: A list of files and directories. It will be broken up
 
1989
            into (dir, name) pairs and sorted before calling _bisect_recursive.
 
1990
        """
 
1991
        expected = {}
 
1992
        for key in map_keys:
 
1993
            entry = expected_map[key]
 
1994
            dir_name_id, trees_info = entry
 
1995
            expected[dir_name_id] = trees_info
 
1996
 
 
1997
        result = state._bisect_recursive(paths)
 
1998
 
 
1999
        self.assertEqual(expected, result)
 
2000
 
 
2001
    def test_bisect_each(self):
 
2002
        """Find a single record using bisect."""
 
2003
        tree, state, expected = self.create_basic_dirstate()
 
2004
 
 
2005
        # Bisect should return the rows for the specified files.
 
2006
        self.assertBisect(expected, [['']], state, [''])
 
2007
        self.assertBisect(expected, [['a']], state, ['a'])
 
2008
        self.assertBisect(expected, [['b']], state, ['b'])
 
2009
        self.assertBisect(expected, [['b/c']], state, ['b/c'])
 
2010
        self.assertBisect(expected, [['b/d']], state, ['b/d'])
 
2011
        self.assertBisect(expected, [['b/d/e']], state, ['b/d/e'])
 
2012
        self.assertBisect(expected, [['b-c']], state, ['b-c'])
 
2013
        self.assertBisect(expected, [['f']], state, ['f'])
 
2014
 
 
2015
    def test_bisect_multi(self):
 
2016
        """Bisect can be used to find multiple records at the same time."""
 
2017
        tree, state, expected = self.create_basic_dirstate()
 
2018
        # Bisect should be capable of finding multiple entries at the same time
 
2019
        self.assertBisect(expected, [['a'], ['b'], ['f']],
 
2020
                          state, ['a', 'b', 'f'])
 
2021
        self.assertBisect(expected, [['f'], ['b/d'], ['b/d/e']],
 
2022
                          state, ['f', 'b/d', 'b/d/e'])
 
2023
        self.assertBisect(expected, [['b'], ['b-c'], ['b/c']],
 
2024
                          state, ['b', 'b-c', 'b/c'])
 
2025
 
 
2026
    def test_bisect_one_page(self):
 
2027
        """Test bisect when there is only 1 page to read"""
 
2028
        tree, state, expected = self.create_basic_dirstate()
 
2029
        state._bisect_page_size = 5000
 
2030
        self.assertBisect(expected,[['']], state, [''])
 
2031
        self.assertBisect(expected,[['a']], state, ['a'])
 
2032
        self.assertBisect(expected,[['b']], state, ['b'])
 
2033
        self.assertBisect(expected,[['b/c']], state, ['b/c'])
 
2034
        self.assertBisect(expected,[['b/d']], state, ['b/d'])
 
2035
        self.assertBisect(expected,[['b/d/e']], state, ['b/d/e'])
 
2036
        self.assertBisect(expected,[['b-c']], state, ['b-c'])
 
2037
        self.assertBisect(expected,[['f']], state, ['f'])
 
2038
        self.assertBisect(expected,[['a'], ['b'], ['f']],
 
2039
                          state, ['a', 'b', 'f'])
 
2040
        self.assertBisect(expected, [['b/d'], ['b/d/e'], ['f']],
 
2041
                          state, ['b/d', 'b/d/e', 'f'])
 
2042
        self.assertBisect(expected, [['b'], ['b/c'], ['b-c']],
 
2043
                          state, ['b', 'b/c', 'b-c'])
 
2044
 
 
2045
    def test_bisect_duplicate_paths(self):
 
2046
        """When bisecting for a path, handle multiple entries."""
 
2047
        tree, state, expected = self.create_duplicated_dirstate()
 
2048
 
 
2049
        # Now make sure that both records are properly returned.
 
2050
        self.assertBisect(expected, [['']], state, [''])
 
2051
        self.assertBisect(expected, [['a', 'a2']], state, ['a'])
 
2052
        self.assertBisect(expected, [['b', 'b2']], state, ['b'])
 
2053
        self.assertBisect(expected, [['b/c', 'b/c2']], state, ['b/c'])
 
2054
        self.assertBisect(expected, [['b/d', 'b/d2']], state, ['b/d'])
 
2055
        self.assertBisect(expected, [['b/d/e', 'b/d/e2']],
 
2056
                          state, ['b/d/e'])
 
2057
        self.assertBisect(expected, [['b-c', 'b-c2']], state, ['b-c'])
 
2058
        self.assertBisect(expected, [['f', 'f2']], state, ['f'])
 
2059
 
 
2060
    def test_bisect_page_size_too_small(self):
 
2061
        """If the page size is too small, we will auto increase it."""
 
2062
        tree, state, expected = self.create_basic_dirstate()
 
2063
        state._bisect_page_size = 50
 
2064
        self.assertBisect(expected, [None], state, ['b/e'])
 
2065
        self.assertBisect(expected, [['a']], state, ['a'])
 
2066
        self.assertBisect(expected, [['b']], state, ['b'])
 
2067
        self.assertBisect(expected, [['b/c']], state, ['b/c'])
 
2068
        self.assertBisect(expected, [['b/d']], state, ['b/d'])
 
2069
        self.assertBisect(expected, [['b/d/e']], state, ['b/d/e'])
 
2070
        self.assertBisect(expected, [['b-c']], state, ['b-c'])
 
2071
        self.assertBisect(expected, [['f']], state, ['f'])
 
2072
 
 
2073
    def test_bisect_missing(self):
 
2074
        """Test that bisect return None if it cannot find a path."""
 
2075
        tree, state, expected = self.create_basic_dirstate()
 
2076
        self.assertBisect(expected, [None], state, ['foo'])
 
2077
        self.assertBisect(expected, [None], state, ['b/foo'])
 
2078
        self.assertBisect(expected, [None], state, ['bar/foo'])
 
2079
        self.assertBisect(expected, [None], state, ['b-c/foo'])
 
2080
 
 
2081
        self.assertBisect(expected, [['a'], None, ['b/d']],
 
2082
                          state, ['a', 'foo', 'b/d'])
 
2083
 
 
2084
    def test_bisect_rename(self):
 
2085
        """Check that we find a renamed row."""
 
2086
        tree, state, expected = self.create_renamed_dirstate()
 
2087
 
 
2088
        # Search for the pre and post renamed entries
 
2089
        self.assertBisect(expected, [['a']], state, ['a'])
 
2090
        self.assertBisect(expected, [['b/g']], state, ['b/g'])
 
2091
        self.assertBisect(expected, [['b/d']], state, ['b/d'])
 
2092
        self.assertBisect(expected, [['h']], state, ['h'])
 
2093
 
 
2094
        # What about b/d/e? shouldn't that also get 2 directory entries?
 
2095
        self.assertBisect(expected, [['b/d/e']], state, ['b/d/e'])
 
2096
        self.assertBisect(expected, [['h/e']], state, ['h/e'])
 
2097
 
 
2098
    def test_bisect_dirblocks(self):
 
2099
        tree, state, expected = self.create_duplicated_dirstate()
 
2100
        self.assertBisectDirBlocks(expected,
 
2101
            [['', 'a', 'a2', 'b', 'b2', 'b-c', 'b-c2', 'f', 'f2']],
 
2102
            state, [''])
 
2103
        self.assertBisectDirBlocks(expected,
 
2104
            [['b/c', 'b/c2', 'b/d', 'b/d2']], state, ['b'])
 
2105
        self.assertBisectDirBlocks(expected,
 
2106
            [['b/d/e', 'b/d/e2']], state, ['b/d'])
 
2107
        self.assertBisectDirBlocks(expected,
 
2108
            [['', 'a', 'a2', 'b', 'b2', 'b-c', 'b-c2', 'f', 'f2'],
 
2109
             ['b/c', 'b/c2', 'b/d', 'b/d2'],
 
2110
             ['b/d/e', 'b/d/e2'],
 
2111
            ], state, ['', 'b', 'b/d'])
 
2112
 
 
2113
    def test_bisect_dirblocks_missing(self):
 
2114
        tree, state, expected = self.create_basic_dirstate()
 
2115
        self.assertBisectDirBlocks(expected, [['b/d/e'], None],
 
2116
            state, ['b/d', 'b/e'])
 
2117
        # Files don't show up in this search
 
2118
        self.assertBisectDirBlocks(expected, [None], state, ['a'])
 
2119
        self.assertBisectDirBlocks(expected, [None], state, ['b/c'])
 
2120
        self.assertBisectDirBlocks(expected, [None], state, ['c'])
 
2121
        self.assertBisectDirBlocks(expected, [None], state, ['b/d/e'])
 
2122
        self.assertBisectDirBlocks(expected, [None], state, ['f'])
 
2123
 
 
2124
    def test_bisect_recursive_each(self):
 
2125
        tree, state, expected = self.create_basic_dirstate()
 
2126
        self.assertBisectRecursive(expected, ['a'], state, ['a'])
 
2127
        self.assertBisectRecursive(expected, ['b/c'], state, ['b/c'])
 
2128
        self.assertBisectRecursive(expected, ['b/d/e'], state, ['b/d/e'])
 
2129
        self.assertBisectRecursive(expected, ['b-c'], state, ['b-c'])
 
2130
        self.assertBisectRecursive(expected, ['b/d', 'b/d/e'],
 
2131
                                   state, ['b/d'])
 
2132
        self.assertBisectRecursive(expected, ['b', 'b/c', 'b/d', 'b/d/e'],
 
2133
                                   state, ['b'])
 
2134
        self.assertBisectRecursive(expected, ['', 'a', 'b', 'b-c', 'f', 'b/c',
 
2135
                                              'b/d', 'b/d/e'],
 
2136
                                   state, [''])
 
2137
 
 
2138
    def test_bisect_recursive_multiple(self):
 
2139
        tree, state, expected = self.create_basic_dirstate()
 
2140
        self.assertBisectRecursive(expected, ['a', 'b/c'], state, ['a', 'b/c'])
 
2141
        self.assertBisectRecursive(expected, ['b/d', 'b/d/e'],
 
2142
                                   state, ['b/d', 'b/d/e'])
 
2143
 
 
2144
    def test_bisect_recursive_missing(self):
 
2145
        tree, state, expected = self.create_basic_dirstate()
 
2146
        self.assertBisectRecursive(expected, [], state, ['d'])
 
2147
        self.assertBisectRecursive(expected, [], state, ['b/e'])
 
2148
        self.assertBisectRecursive(expected, [], state, ['g'])
 
2149
        self.assertBisectRecursive(expected, ['a'], state, ['a', 'g'])
 
2150
 
 
2151
    def test_bisect_recursive_renamed(self):
 
2152
        tree, state, expected = self.create_renamed_dirstate()
 
2153
 
 
2154
        # Looking for either renamed item should find the other
 
2155
        self.assertBisectRecursive(expected, ['a', 'b/g'], state, ['a'])
 
2156
        self.assertBisectRecursive(expected, ['a', 'b/g'], state, ['b/g'])
 
2157
        # Looking in the containing directory should find the rename target,
 
2158
        # and anything in a subdir of the renamed target.
 
2159
        self.assertBisectRecursive(expected, ['a', 'b', 'b/c', 'b/d',
 
2160
                                              'b/d/e', 'b/g', 'h', 'h/e'],
 
2161
                                   state, ['b'])
 
2162
 
 
2163
 
 
2164
class TestDirstateValidation(TestCaseWithDirState):
 
2165
 
 
2166
    def test_validate_correct_dirstate(self):
 
2167
        state = self.create_complex_dirstate()
 
2168
        state._validate()
 
2169
        state.unlock()
 
2170
        # and make sure we can also validate with a read lock
 
2171
        state.lock_read()
 
2172
        try:
 
2173
            state._validate()
 
2174
        finally:
 
2175
            state.unlock()
 
2176
 
 
2177
    def test_dirblock_not_sorted(self):
 
2178
        tree, state, expected = self.create_renamed_dirstate()
 
2179
        state._read_dirblocks_if_needed()
 
2180
        last_dirblock = state._dirblocks[-1]
 
2181
        # we're appending to the dirblock, but this name comes before some of
 
2182
        # the existing names; that's wrong
 
2183
        last_dirblock[1].append(
 
2184
            (('h', 'aaaa', 'a-id'),
 
2185
             [('a', '', 0, False, ''),
 
2186
              ('a', '', 0, False, '')]))
 
2187
        e = self.assertRaises(AssertionError,
 
2188
            state._validate)
 
2189
        self.assertContainsRe(str(e), 'not sorted')
 
2190
 
 
2191
    def test_dirblock_name_mismatch(self):
 
2192
        tree, state, expected = self.create_renamed_dirstate()
 
2193
        state._read_dirblocks_if_needed()
 
2194
        last_dirblock = state._dirblocks[-1]
 
2195
        # add an entry with the wrong directory name
 
2196
        last_dirblock[1].append(
 
2197
            (('', 'z', 'a-id'),
 
2198
             [('a', '', 0, False, ''),
 
2199
              ('a', '', 0, False, '')]))
 
2200
        e = self.assertRaises(AssertionError,
 
2201
            state._validate)
 
2202
        self.assertContainsRe(str(e),
 
2203
            "doesn't match directory name")
 
2204
 
 
2205
    def test_dirblock_missing_rename(self):
 
2206
        tree, state, expected = self.create_renamed_dirstate()
 
2207
        state._read_dirblocks_if_needed()
 
2208
        last_dirblock = state._dirblocks[-1]
 
2209
        # make another entry for a-id, without a correct 'r' pointer to
 
2210
        # the real occurrence in the working tree
 
2211
        last_dirblock[1].append(
 
2212
            (('h', 'z', 'a-id'),
 
2213
             [('a', '', 0, False, ''),
 
2214
              ('a', '', 0, False, '')]))
 
2215
        e = self.assertRaises(AssertionError,
 
2216
            state._validate)
 
2217
        self.assertContainsRe(str(e),
 
2218
            'file a-id is absent in row')
 
2219
 
 
2220
 
 
2221
class TestDirstateTreeReference(TestCaseWithDirState):
 
2222
 
 
2223
    def test_reference_revision_is_none(self):
 
2224
        tree = self.make_branch_and_tree('tree', format='development-subtree')
 
2225
        subtree = self.make_branch_and_tree('tree/subtree',
 
2226
                            format='development-subtree')
 
2227
        subtree.set_root_id('subtree')
 
2228
        tree.add_reference(subtree)
 
2229
        tree.add('subtree')
 
2230
        state = dirstate.DirState.from_tree(tree, 'dirstate')
 
2231
        key = ('', 'subtree', 'subtree')
 
2232
        expected = ('', [(key,
 
2233
            [('t', '', 0, False, 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')])])
 
2234
 
 
2235
        try:
 
2236
            self.assertEqual(expected, state._find_block(key))
 
2237
        finally:
 
2238
            state.unlock()
 
2239
 
 
2240
 
 
2241
class TestDiscardMergeParents(TestCaseWithDirState):
 
2242
 
 
2243
    def test_discard_no_parents(self):
 
2244
        # This should be a no-op
 
2245
        state = self.create_empty_dirstate()
 
2246
        self.addCleanup(state.unlock)
 
2247
        state._discard_merge_parents()
 
2248
        state._validate()
 
2249
 
 
2250
    def test_discard_one_parent(self):
 
2251
        # No-op
 
2252
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
 
2253
        root_entry_direntry = ('', '', 'a-root-value'), [
 
2254
            ('d', '', 0, False, packed_stat),
 
2255
            ('d', '', 0, False, packed_stat),
 
2256
            ]
 
2257
        dirblocks = []
 
2258
        dirblocks.append(('', [root_entry_direntry]))
 
2259
        dirblocks.append(('', []))
 
2260
 
 
2261
        state = self.create_empty_dirstate()
 
2262
        self.addCleanup(state.unlock)
 
2263
        state._set_data(['parent-id'], dirblocks[:])
 
2264
        state._validate()
 
2265
 
 
2266
        state._discard_merge_parents()
 
2267
        state._validate()
 
2268
        self.assertEqual(dirblocks, state._dirblocks)
 
2269
 
 
2270
    def test_discard_simple(self):
 
2271
        # No-op
 
2272
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
 
2273
        root_entry_direntry = ('', '', 'a-root-value'), [
 
2274
            ('d', '', 0, False, packed_stat),
 
2275
            ('d', '', 0, False, packed_stat),
 
2276
            ('d', '', 0, False, packed_stat),
 
2277
            ]
 
2278
        expected_root_entry_direntry = ('', '', 'a-root-value'), [
 
2279
            ('d', '', 0, False, packed_stat),
 
2280
            ('d', '', 0, False, packed_stat),
 
2281
            ]
 
2282
        dirblocks = []
 
2283
        dirblocks.append(('', [root_entry_direntry]))
 
2284
        dirblocks.append(('', []))
 
2285
 
 
2286
        state = self.create_empty_dirstate()
 
2287
        self.addCleanup(state.unlock)
 
2288
        state._set_data(['parent-id', 'merged-id'], dirblocks[:])
 
2289
        state._validate()
 
2290
 
 
2291
        # This should strip of the extra column
 
2292
        state._discard_merge_parents()
 
2293
        state._validate()
 
2294
        expected_dirblocks = [('', [expected_root_entry_direntry]), ('', [])]
 
2295
        self.assertEqual(expected_dirblocks, state._dirblocks)
 
2296
 
 
2297
    def test_discard_absent(self):
 
2298
        """If entries are only in a merge, discard should remove the entries"""
 
2299
        null_stat = dirstate.DirState.NULLSTAT
 
2300
        present_dir = ('d', '', 0, False, null_stat)
 
2301
        present_file = ('f', '', 0, False, null_stat)
 
2302
        absent = dirstate.DirState.NULL_PARENT_DETAILS
 
2303
        root_key = ('', '', 'a-root-value')
 
2304
        file_in_root_key = ('', 'file-in-root', 'a-file-id')
 
2305
        file_in_merged_key = ('', 'file-in-merged', 'b-file-id')
 
2306
        dirblocks = [('', [(root_key, [present_dir, present_dir, present_dir])]),
 
2307
                     ('', [(file_in_merged_key,
 
2308
                            [absent, absent, present_file]),
 
2309
                           (file_in_root_key,
 
2310
                            [present_file, present_file, present_file]),
 
2311
                          ]),
 
2312
                    ]
 
2313
 
 
2314
        state = self.create_empty_dirstate()
 
2315
        self.addCleanup(state.unlock)
 
2316
        state._set_data(['parent-id', 'merged-id'], dirblocks[:])
 
2317
        state._validate()
 
2318
 
 
2319
        exp_dirblocks = [('', [(root_key, [present_dir, present_dir])]),
 
2320
                         ('', [(file_in_root_key,
 
2321
                                [present_file, present_file]),
 
2322
                              ]),
 
2323
                        ]
 
2324
        state._discard_merge_parents()
 
2325
        state._validate()
 
2326
        self.assertEqual(exp_dirblocks, state._dirblocks)
 
2327
 
 
2328
    def test_discard_renamed(self):
 
2329
        null_stat = dirstate.DirState.NULLSTAT
 
2330
        present_dir = ('d', '', 0, False, null_stat)
 
2331
        present_file = ('f', '', 0, False, null_stat)
 
2332
        absent = dirstate.DirState.NULL_PARENT_DETAILS
 
2333
        root_key = ('', '', 'a-root-value')
 
2334
        file_in_root_key = ('', 'file-in-root', 'a-file-id')
 
2335
        # Renamed relative to parent
 
2336
        file_rename_s_key = ('', 'file-s', 'b-file-id')
 
2337
        file_rename_t_key = ('', 'file-t', 'b-file-id')
 
2338
        # And one that is renamed between the parents, but absent in this
 
2339
        key_in_1 = ('', 'file-in-1', 'c-file-id')
 
2340
        key_in_2 = ('', 'file-in-2', 'c-file-id')
 
2341
 
 
2342
        dirblocks = [
 
2343
            ('', [(root_key, [present_dir, present_dir, present_dir])]),
 
2344
            ('', [(key_in_1,
 
2345
                   [absent, present_file, ('r', 'file-in-2', 'c-file-id')]),
 
2346
                  (key_in_2,
 
2347
                   [absent, ('r', 'file-in-1', 'c-file-id'), present_file]),
 
2348
                  (file_in_root_key,
 
2349
                   [present_file, present_file, present_file]),
 
2350
                  (file_rename_s_key,
 
2351
                   [('r', 'file-t', 'b-file-id'), absent, present_file]),
 
2352
                  (file_rename_t_key,
 
2353
                   [present_file, absent, ('r', 'file-s', 'b-file-id')]),
 
2354
                 ]),
 
2355
        ]
 
2356
        exp_dirblocks = [
 
2357
            ('', [(root_key, [present_dir, present_dir])]),
 
2358
            ('', [(key_in_1, [absent, present_file]),
 
2359
                  (file_in_root_key, [present_file, present_file]),
 
2360
                  (file_rename_t_key, [present_file, absent]),
 
2361
                 ]),
 
2362
        ]
 
2363
        state = self.create_empty_dirstate()
 
2364
        self.addCleanup(state.unlock)
 
2365
        state._set_data(['parent-id', 'merged-id'], dirblocks[:])
 
2366
        state._validate()
 
2367
 
 
2368
        state._discard_merge_parents()
 
2369
        state._validate()
 
2370
        self.assertEqual(exp_dirblocks, state._dirblocks)
 
2371
 
 
2372
    def test_discard_all_subdir(self):
 
2373
        null_stat = dirstate.DirState.NULLSTAT
 
2374
        present_dir = ('d', '', 0, False, null_stat)
 
2375
        present_file = ('f', '', 0, False, null_stat)
 
2376
        absent = dirstate.DirState.NULL_PARENT_DETAILS
 
2377
        root_key = ('', '', 'a-root-value')
 
2378
        subdir_key = ('', 'sub', 'dir-id')
 
2379
        child1_key = ('sub', 'child1', 'child1-id')
 
2380
        child2_key = ('sub', 'child2', 'child2-id')
 
2381
        child3_key = ('sub', 'child3', 'child3-id')
 
2382
 
 
2383
        dirblocks = [
 
2384
            ('', [(root_key, [present_dir, present_dir, present_dir])]),
 
2385
            ('', [(subdir_key, [present_dir, present_dir, present_dir])]),
 
2386
            ('sub', [(child1_key, [absent, absent, present_file]),
 
2387
                     (child2_key, [absent, absent, present_file]),
 
2388
                     (child3_key, [absent, absent, present_file]),
 
2389
                    ]),
 
2390
        ]
 
2391
        exp_dirblocks = [
 
2392
            ('', [(root_key, [present_dir, present_dir])]),
 
2393
            ('', [(subdir_key, [present_dir, present_dir])]),
 
2394
            ('sub', []),
 
2395
        ]
 
2396
        state = self.create_empty_dirstate()
 
2397
        self.addCleanup(state.unlock)
 
2398
        state._set_data(['parent-id', 'merged-id'], dirblocks[:])
 
2399
        state._validate()
 
2400
 
 
2401
        state._discard_merge_parents()
 
2402
        state._validate()
 
2403
        self.assertEqual(exp_dirblocks, state._dirblocks)
 
2404
 
 
2405
 
 
2406
class Test_InvEntryToDetails(tests.TestCase):
 
2407
 
 
2408
    def assertDetails(self, expected, inv_entry):
 
2409
        details = dirstate.DirState._inv_entry_to_details(inv_entry)
 
2410
        self.assertEqual(expected, details)
 
2411
        # details should always allow join() and always be a plain str when
 
2412
        # finished
 
2413
        (minikind, fingerprint, size, executable, tree_data) = details
 
2414
        self.assertIsInstance(minikind, str)
 
2415
        self.assertIsInstance(fingerprint, str)
 
2416
        self.assertIsInstance(tree_data, str)
 
2417
 
 
2418
    def test_unicode_symlink(self):
 
2419
        inv_entry = inventory.InventoryLink('link-file-id',
 
2420
                                            u'nam\N{Euro Sign}e',
 
2421
                                            'link-parent-id')
 
2422
        inv_entry.revision = 'link-revision-id'
 
2423
        target = u'link-targ\N{Euro Sign}t'
 
2424
        inv_entry.symlink_target = target
 
2425
        self.assertDetails(('l', target.encode('UTF-8'), 0, False,
 
2426
                            'link-revision-id'), inv_entry)
 
2427
 
 
2428
 
 
2429
class TestSHA1Provider(tests.TestCaseInTempDir):
 
2430
 
 
2431
    def test_sha1provider_is_an_interface(self):
 
2432
        p = dirstate.SHA1Provider()
 
2433
        self.assertRaises(NotImplementedError, p.sha1, "foo")
 
2434
        self.assertRaises(NotImplementedError, p.stat_and_sha1, "foo")
 
2435
 
 
2436
    def test_defaultsha1provider_sha1(self):
 
2437
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
 
2438
        self.build_tree_contents([('foo', text)])
 
2439
        expected_sha = osutils.sha_string(text)
 
2440
        p = dirstate.DefaultSHA1Provider()
 
2441
        self.assertEqual(expected_sha, p.sha1('foo'))
 
2442
 
 
2443
    def test_defaultsha1provider_stat_and_sha1(self):
 
2444
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
 
2445
        self.build_tree_contents([('foo', text)])
 
2446
        expected_sha = osutils.sha_string(text)
 
2447
        p = dirstate.DefaultSHA1Provider()
 
2448
        statvalue, sha1 = p.stat_and_sha1('foo')
 
2449
        self.assertTrue(len(statvalue) >= 10)
 
2450
        self.assertEqual(len(text), statvalue.st_size)
 
2451
        self.assertEqual(expected_sha, sha1)
 
2452
 
 
2453
 
 
2454
class _Repo(object):
 
2455
    """A minimal api to get InventoryRevisionTree to work."""
 
2456
 
 
2457
    def __init__(self):
 
2458
        default_format = controldir.format_registry.make_controldir('default')
 
2459
        self._format = default_format.repository_format
 
2460
 
 
2461
    def lock_read(self):
 
2462
        pass
 
2463
 
 
2464
    def unlock(self):
 
2465
        pass
 
2466
 
 
2467
 
 
2468
class TestUpdateBasisByDelta(tests.TestCase):
 
2469
 
 
2470
    def path_to_ie(self, path, file_id, rev_id, dir_ids):
 
2471
        if path.endswith('/'):
 
2472
            is_dir = True
 
2473
            path = path[:-1]
 
2474
        else:
 
2475
            is_dir = False
 
2476
        dirname, basename = osutils.split(path)
 
2477
        try:
 
2478
            dir_id = dir_ids[dirname]
 
2479
        except KeyError:
 
2480
            dir_id = osutils.basename(dirname) + '-id'
 
2481
        if is_dir:
 
2482
            ie = inventory.InventoryDirectory(file_id, basename, dir_id)
 
2483
            dir_ids[path] = file_id
 
2484
        else:
 
2485
            ie = inventory.InventoryFile(file_id, basename, dir_id)
 
2486
            ie.text_size = 0
 
2487
            ie.text_sha1 = ''
 
2488
        ie.revision = rev_id
 
2489
        return ie
 
2490
 
 
2491
    def create_tree_from_shape(self, rev_id, shape):
 
2492
        dir_ids = {'': 'root-id'}
 
2493
        inv = inventory.Inventory('root-id', rev_id)
 
2494
        for info in shape:
 
2495
            if len(info) == 2:
 
2496
                path, file_id = info
 
2497
                ie_rev_id = rev_id
 
2498
            else:
 
2499
                path, file_id, ie_rev_id = info
 
2500
            if path == '':
 
2501
                # Replace the root entry
 
2502
                del inv._byid[inv.root.file_id]
 
2503
                inv.root.file_id = file_id
 
2504
                inv._byid[file_id] = inv.root
 
2505
                dir_ids[''] = file_id
 
2506
                continue
 
2507
            inv.add(self.path_to_ie(path, file_id, ie_rev_id, dir_ids))
 
2508
        return inventorytree.InventoryRevisionTree(_Repo(), inv, rev_id)
 
2509
 
 
2510
    def create_empty_dirstate(self):
 
2511
        fd, path = tempfile.mkstemp(prefix='bzr-dirstate')
 
2512
        self.addCleanup(os.remove, path)
 
2513
        os.close(fd)
 
2514
        state = dirstate.DirState.initialize(path)
 
2515
        self.addCleanup(state.unlock)
 
2516
        return state
 
2517
 
 
2518
    def create_inv_delta(self, delta, rev_id):
 
2519
        """Translate a 'delta shape' into an actual InventoryDelta"""
 
2520
        dir_ids = {'': 'root-id'}
 
2521
        inv_delta = []
 
2522
        for old_path, new_path, file_id in delta:
 
2523
            if old_path is not None and old_path.endswith('/'):
 
2524
                # Don't have to actually do anything for this, because only
 
2525
                # new_path creates InventoryEntries
 
2526
                old_path = old_path[:-1]
 
2527
            if new_path is None: # Delete
 
2528
                inv_delta.append((old_path, None, file_id, None))
 
2529
                continue
 
2530
            ie = self.path_to_ie(new_path, file_id, rev_id, dir_ids)
 
2531
            inv_delta.append((old_path, new_path, file_id, ie))
 
2532
        return inv_delta
 
2533
 
 
2534
    def assertUpdate(self, active, basis, target):
 
2535
        """Assert that update_basis_by_delta works how we want.
 
2536
 
 
2537
        Set up a DirState object with active_shape for tree 0, basis_shape for
 
2538
        tree 1. Then apply the delta from basis_shape to target_shape,
 
2539
        and assert that the DirState is still valid, and that its stored
 
2540
        content matches the target_shape.
 
2541
        """
 
2542
        active_tree = self.create_tree_from_shape('active', active)
 
2543
        basis_tree = self.create_tree_from_shape('basis', basis)
 
2544
        target_tree = self.create_tree_from_shape('target', target)
 
2545
        state = self.create_empty_dirstate()
 
2546
        state.set_state_from_scratch(active_tree.root_inventory,
 
2547
            [('basis', basis_tree)], [])
 
2548
        delta = target_tree.root_inventory._make_delta(
 
2549
            basis_tree.root_inventory)
 
2550
        state.update_basis_by_delta(delta, 'target')
 
2551
        state._validate()
 
2552
        dirstate_tree = workingtree_4.DirStateRevisionTree(state,
 
2553
            'target', _Repo())
 
2554
        # The target now that delta has been applied should match the
 
2555
        # RevisionTree
 
2556
        self.assertEqual([], list(dirstate_tree.iter_changes(target_tree)))
 
2557
        # And the dirblock state should be identical to the state if we created
 
2558
        # it from scratch.
 
2559
        state2 = self.create_empty_dirstate()
 
2560
        state2.set_state_from_scratch(active_tree.root_inventory,
 
2561
            [('target', target_tree)], [])
 
2562
        self.assertEqual(state2._dirblocks, state._dirblocks)
 
2563
        return state
 
2564
 
 
2565
    def assertBadDelta(self, active, basis, delta):
 
2566
        """Test that we raise InconsistentDelta when appropriate.
 
2567
 
 
2568
        :param active: The active tree shape
 
2569
        :param basis: The basis tree shape
 
2570
        :param delta: A description of the delta to apply. Similar to the form
 
2571
            for regular inventory deltas, but omitting the InventoryEntry.
 
2572
            So adding a file is: (None, 'path', 'file-id')
 
2573
            Adding a directory is: (None, 'path/', 'dir-id')
 
2574
            Renaming a dir is: ('old/', 'new/', 'dir-id')
 
2575
            etc.
 
2576
        """
 
2577
        active_tree = self.create_tree_from_shape('active', active)
 
2578
        basis_tree = self.create_tree_from_shape('basis', basis)
 
2579
        inv_delta = self.create_inv_delta(delta, 'target')
 
2580
        state = self.create_empty_dirstate()
 
2581
        state.set_state_from_scratch(active_tree.root_inventory,
 
2582
            [('basis', basis_tree)], [])
 
2583
        self.assertRaises(errors.InconsistentDelta,
 
2584
            state.update_basis_by_delta, inv_delta, 'target')
 
2585
        ## try:
 
2586
        ##     state.update_basis_by_delta(inv_delta, 'target')
 
2587
        ## except errors.InconsistentDelta, e:
 
2588
        ##     import pdb; pdb.set_trace()
 
2589
        ## else:
 
2590
        ##     import pdb; pdb.set_trace()
 
2591
        self.assertTrue(state._changes_aborted)
 
2592
 
 
2593
    def test_remove_file_matching_active_state(self):
 
2594
        state = self.assertUpdate(
 
2595
            active=[],
 
2596
            basis =[('file', 'file-id')],
 
2597
            target=[],
 
2598
            )
 
2599
 
 
2600
    def test_remove_file_present_in_active_state(self):
 
2601
        state = self.assertUpdate(
 
2602
            active=[('file', 'file-id')],
 
2603
            basis =[('file', 'file-id')],
 
2604
            target=[],
 
2605
            )
 
2606
 
 
2607
    def test_remove_file_present_elsewhere_in_active_state(self):
 
2608
        state = self.assertUpdate(
 
2609
            active=[('other-file', 'file-id')],
 
2610
            basis =[('file', 'file-id')],
 
2611
            target=[],
 
2612
            )
 
2613
 
 
2614
    def test_remove_file_active_state_has_diff_file(self):
 
2615
        state = self.assertUpdate(
 
2616
            active=[('file', 'file-id-2')],
 
2617
            basis =[('file', 'file-id')],
 
2618
            target=[],
 
2619
            )
 
2620
 
 
2621
    def test_remove_file_active_state_has_diff_file_and_file_elsewhere(self):
 
2622
        state = self.assertUpdate(
 
2623
            active=[('file', 'file-id-2'),
 
2624
                    ('other-file', 'file-id')],
 
2625
            basis =[('file', 'file-id')],
 
2626
            target=[],
 
2627
            )
 
2628
 
 
2629
    def test_add_file_matching_active_state(self):
 
2630
        state = self.assertUpdate(
 
2631
            active=[('file', 'file-id')],
 
2632
            basis =[],
 
2633
            target=[('file', 'file-id')],
 
2634
            )
 
2635
 
 
2636
    def test_add_file_in_empty_dir_not_matching_active_state(self):
 
2637
        state = self.assertUpdate(
 
2638
                active=[],
 
2639
                basis=[('dir/', 'dir-id')],
 
2640
                target=[('dir/', 'dir-id', 'basis'), ('dir/file', 'file-id')],
 
2641
                )
 
2642
 
 
2643
    def test_add_file_missing_in_active_state(self):
 
2644
        state = self.assertUpdate(
 
2645
            active=[],
 
2646
            basis =[],
 
2647
            target=[('file', 'file-id')],
 
2648
            )
 
2649
 
 
2650
    def test_add_file_elsewhere_in_active_state(self):
 
2651
        state = self.assertUpdate(
 
2652
            active=[('other-file', 'file-id')],
 
2653
            basis =[],
 
2654
            target=[('file', 'file-id')],
 
2655
            )
 
2656
 
 
2657
    def test_add_file_active_state_has_diff_file_and_file_elsewhere(self):
 
2658
        state = self.assertUpdate(
 
2659
            active=[('other-file', 'file-id'),
 
2660
                    ('file', 'file-id-2')],
 
2661
            basis =[],
 
2662
            target=[('file', 'file-id')],
 
2663
            )
 
2664
 
 
2665
    def test_rename_file_matching_active_state(self):
 
2666
        state = self.assertUpdate(
 
2667
            active=[('other-file', 'file-id')],
 
2668
            basis =[('file', 'file-id')],
 
2669
            target=[('other-file', 'file-id')],
 
2670
            )
 
2671
 
 
2672
    def test_rename_file_missing_in_active_state(self):
 
2673
        state = self.assertUpdate(
 
2674
            active=[],
 
2675
            basis =[('file', 'file-id')],
 
2676
            target=[('other-file', 'file-id')],
 
2677
            )
 
2678
 
 
2679
    def test_rename_file_present_elsewhere_in_active_state(self):
 
2680
        state = self.assertUpdate(
 
2681
            active=[('third', 'file-id')],
 
2682
            basis =[('file', 'file-id')],
 
2683
            target=[('other-file', 'file-id')],
 
2684
            )
 
2685
 
 
2686
    def test_rename_file_active_state_has_diff_source_file(self):
 
2687
        state = self.assertUpdate(
 
2688
            active=[('file', 'file-id-2')],
 
2689
            basis =[('file', 'file-id')],
 
2690
            target=[('other-file', 'file-id')],
 
2691
            )
 
2692
 
 
2693
    def test_rename_file_active_state_has_diff_target_file(self):
 
2694
        state = self.assertUpdate(
 
2695
            active=[('other-file', 'file-id-2')],
 
2696
            basis =[('file', 'file-id')],
 
2697
            target=[('other-file', 'file-id')],
 
2698
            )
 
2699
 
 
2700
    def test_rename_file_active_has_swapped_files(self):
 
2701
        state = self.assertUpdate(
 
2702
            active=[('file', 'file-id'),
 
2703
                    ('other-file', 'file-id-2')],
 
2704
            basis= [('file', 'file-id'),
 
2705
                    ('other-file', 'file-id-2')],
 
2706
            target=[('file', 'file-id-2'),
 
2707
                    ('other-file', 'file-id')])
 
2708
 
 
2709
    def test_rename_file_basis_has_swapped_files(self):
 
2710
        state = self.assertUpdate(
 
2711
            active=[('file', 'file-id'),
 
2712
                    ('other-file', 'file-id-2')],
 
2713
            basis= [('file', 'file-id-2'),
 
2714
                    ('other-file', 'file-id')],
 
2715
            target=[('file', 'file-id'),
 
2716
                    ('other-file', 'file-id-2')])
 
2717
 
 
2718
    def test_rename_directory_with_contents(self):
 
2719
        state = self.assertUpdate( # active matches basis
 
2720
            active=[('dir1/', 'dir-id'),
 
2721
                    ('dir1/file', 'file-id')],
 
2722
            basis= [('dir1/', 'dir-id'),
 
2723
                    ('dir1/file', 'file-id')],
 
2724
            target=[('dir2/', 'dir-id'),
 
2725
                    ('dir2/file', 'file-id')])
 
2726
        state = self.assertUpdate( # active matches target
 
2727
            active=[('dir2/', 'dir-id'),
 
2728
                    ('dir2/file', 'file-id')],
 
2729
            basis= [('dir1/', 'dir-id'),
 
2730
                    ('dir1/file', 'file-id')],
 
2731
            target=[('dir2/', 'dir-id'),
 
2732
                    ('dir2/file', 'file-id')])
 
2733
        state = self.assertUpdate( # active empty
 
2734
            active=[],
 
2735
            basis= [('dir1/', 'dir-id'),
 
2736
                    ('dir1/file', 'file-id')],
 
2737
            target=[('dir2/', 'dir-id'),
 
2738
                    ('dir2/file', 'file-id')])
 
2739
        state = self.assertUpdate( # active present at other location
 
2740
            active=[('dir3/', 'dir-id'),
 
2741
                    ('dir3/file', 'file-id')],
 
2742
            basis= [('dir1/', 'dir-id'),
 
2743
                    ('dir1/file', 'file-id')],
 
2744
            target=[('dir2/', 'dir-id'),
 
2745
                    ('dir2/file', 'file-id')])
 
2746
        state = self.assertUpdate( # active has different ids
 
2747
            active=[('dir1/', 'dir1-id'),
 
2748
                    ('dir1/file', 'file1-id'),
 
2749
                    ('dir2/', 'dir2-id'),
 
2750
                    ('dir2/file', 'file2-id')],
 
2751
            basis= [('dir1/', 'dir-id'),
 
2752
                    ('dir1/file', 'file-id')],
 
2753
            target=[('dir2/', 'dir-id'),
 
2754
                    ('dir2/file', 'file-id')])
 
2755
 
 
2756
    def test_invalid_file_not_present(self):
 
2757
        state = self.assertBadDelta(
 
2758
            active=[('file', 'file-id')],
 
2759
            basis= [('file', 'file-id')],
 
2760
            delta=[('other-file', 'file', 'file-id')])
 
2761
 
 
2762
    def test_invalid_new_id_same_path(self):
 
2763
        # The bad entry comes after
 
2764
        state = self.assertBadDelta(
 
2765
            active=[('file', 'file-id')],
 
2766
            basis= [('file', 'file-id')],
 
2767
            delta=[(None, 'file', 'file-id-2')])
 
2768
        # The bad entry comes first
 
2769
        state = self.assertBadDelta(
 
2770
            active=[('file', 'file-id-2')],
 
2771
            basis=[('file', 'file-id-2')],
 
2772
            delta=[(None, 'file', 'file-id')])
 
2773
 
 
2774
    def test_invalid_existing_id(self):
 
2775
        state = self.assertBadDelta(
 
2776
            active=[('file', 'file-id')],
 
2777
            basis= [('file', 'file-id')],
 
2778
            delta=[(None, 'file', 'file-id')])
 
2779
 
 
2780
    def test_invalid_parent_missing(self):
 
2781
        state = self.assertBadDelta(
 
2782
            active=[],
 
2783
            basis= [],
 
2784
            delta=[(None, 'path/path2', 'file-id')])
 
2785
        # Note: we force the active tree to have the directory, by knowing how
 
2786
        #       path_to_ie handles entries with missing parents
 
2787
        state = self.assertBadDelta(
 
2788
            active=[('path/', 'path-id')],
 
2789
            basis= [],
 
2790
            delta=[(None, 'path/path2', 'file-id')])
 
2791
        state = self.assertBadDelta(
 
2792
            active=[('path/', 'path-id'),
 
2793
                    ('path/path2', 'file-id')],
 
2794
            basis= [],
 
2795
            delta=[(None, 'path/path2', 'file-id')])
 
2796
 
 
2797
    def test_renamed_dir_same_path(self):
 
2798
        # We replace the parent directory, with another parent dir. But the C
 
2799
        # file doesn't look like it has been moved.
 
2800
        state = self.assertUpdate(# Same as basis
 
2801
            active=[('dir/', 'A-id'),
 
2802
                    ('dir/B', 'B-id')],
 
2803
            basis= [('dir/', 'A-id'),
 
2804
                    ('dir/B', 'B-id')],
 
2805
            target=[('dir/', 'C-id'),
 
2806
                    ('dir/B', 'B-id')])
 
2807
        state = self.assertUpdate(# Same as target
 
2808
            active=[('dir/', 'C-id'),
 
2809
                    ('dir/B', 'B-id')],
 
2810
            basis= [('dir/', 'A-id'),
 
2811
                    ('dir/B', 'B-id')],
 
2812
            target=[('dir/', 'C-id'),
 
2813
                    ('dir/B', 'B-id')])
 
2814
        state = self.assertUpdate(# empty active
 
2815
            active=[],
 
2816
            basis= [('dir/', 'A-id'),
 
2817
                    ('dir/B', 'B-id')],
 
2818
            target=[('dir/', 'C-id'),
 
2819
                    ('dir/B', 'B-id')])
 
2820
        state = self.assertUpdate(# different active
 
2821
            active=[('dir/', 'D-id'),
 
2822
                    ('dir/B', 'B-id')],
 
2823
            basis= [('dir/', 'A-id'),
 
2824
                    ('dir/B', 'B-id')],
 
2825
            target=[('dir/', 'C-id'),
 
2826
                    ('dir/B', 'B-id')])
 
2827
 
 
2828
    def test_parent_child_swap(self):
 
2829
        state = self.assertUpdate(# Same as basis
 
2830
            active=[('A/', 'A-id'),
 
2831
                    ('A/B/', 'B-id'),
 
2832
                    ('A/B/C', 'C-id')],
 
2833
            basis= [('A/', 'A-id'),
 
2834
                    ('A/B/', 'B-id'),
 
2835
                    ('A/B/C', 'C-id')],
 
2836
            target=[('A/', 'B-id'),
 
2837
                    ('A/B/', 'A-id'),
 
2838
                    ('A/B/C', 'C-id')])
 
2839
        state = self.assertUpdate(# Same as target
 
2840
            active=[('A/', 'B-id'),
 
2841
                    ('A/B/', 'A-id'),
 
2842
                    ('A/B/C', 'C-id')],
 
2843
            basis= [('A/', 'A-id'),
 
2844
                    ('A/B/', 'B-id'),
 
2845
                    ('A/B/C', 'C-id')],
 
2846
            target=[('A/', 'B-id'),
 
2847
                    ('A/B/', 'A-id'),
 
2848
                    ('A/B/C', 'C-id')])
 
2849
        state = self.assertUpdate(# empty active
 
2850
            active=[],
 
2851
            basis= [('A/', 'A-id'),
 
2852
                    ('A/B/', 'B-id'),
 
2853
                    ('A/B/C', 'C-id')],
 
2854
            target=[('A/', 'B-id'),
 
2855
                    ('A/B/', 'A-id'),
 
2856
                    ('A/B/C', 'C-id')])
 
2857
        state = self.assertUpdate(# different active
 
2858
            active=[('D/', 'A-id'),
 
2859
                    ('D/E/', 'B-id'),
 
2860
                    ('F', 'C-id')],
 
2861
            basis= [('A/', 'A-id'),
 
2862
                    ('A/B/', 'B-id'),
 
2863
                    ('A/B/C', 'C-id')],
 
2864
            target=[('A/', 'B-id'),
 
2865
                    ('A/B/', 'A-id'),
 
2866
                    ('A/B/C', 'C-id')])
 
2867
 
 
2868
    def test_change_root_id(self):
 
2869
        state = self.assertUpdate( # same as basis
 
2870
            active=[('', 'root-id'),
 
2871
                    ('file', 'file-id')],
 
2872
            basis= [('', 'root-id'),
 
2873
                    ('file', 'file-id')],
 
2874
            target=[('', 'target-root-id'),
 
2875
                    ('file', 'file-id')])
 
2876
        state = self.assertUpdate( # same as target
 
2877
            active=[('', 'target-root-id'),
 
2878
                    ('file', 'file-id')],
 
2879
            basis= [('', 'root-id'),
 
2880
                    ('file', 'file-id')],
 
2881
            target=[('', 'target-root-id'),
 
2882
                    ('file', 'root-id')])
 
2883
        state = self.assertUpdate( # all different
 
2884
            active=[('', 'active-root-id'),
 
2885
                    ('file', 'file-id')],
 
2886
            basis= [('', 'root-id'),
 
2887
                    ('file', 'file-id')],
 
2888
            target=[('', 'target-root-id'),
 
2889
                    ('file', 'root-id')])
 
2890
 
 
2891
    def test_change_file_absent_in_active(self):
 
2892
        state = self.assertUpdate(
 
2893
            active=[],
 
2894
            basis= [('file', 'file-id')],
 
2895
            target=[('file', 'file-id')])
 
2896
 
 
2897
    def test_invalid_changed_file(self):
 
2898
        state = self.assertBadDelta( # Not present in basis
 
2899
            active=[('file', 'file-id')],
 
2900
            basis= [],
 
2901
            delta=[('file', 'file', 'file-id')])
 
2902
        state = self.assertBadDelta( # present at another location in basis
 
2903
            active=[('file', 'file-id')],
 
2904
            basis= [('other-file', 'file-id')],
 
2905
            delta=[('file', 'file', 'file-id')])