/brz/remove-bazaar

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