/brz/remove-bazaar

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