~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_dirstate.py

  • Committer: Vincent Ladeuil
  • Date: 2016-01-27 13:36:17 UTC
  • mto: This revision was merged to the branch mainline in revision 6614.
  • Revision ID: v.ladeuil+lp@free.fr-20160127133617-gteit32e0nu3938n
Use ssl module for the match_hostname function

Show diffs side-by-side

added added

removed removed

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