~bzr-pqm/bzr/bzr.dev

3640.2.4 by John Arbash Meinel
Copyright updates
1
# Copyright (C) 2007, 2008 Canonical Ltd
2474.1.7 by John Arbash Meinel
Add some tests for a helper function that lets us
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
2474.1.7 by John Arbash Meinel
Add some tests for a helper function that lets us
16
17
"""Tests for the compiled dirstate helpers."""
18
2474.1.57 by John Arbash Meinel
Move code around to refactor according to our pyrex extension design.
19
import bisect
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
20
import os
3696.5.1 by Robert Collins
Integrate in stat-cache-updates-during-commit with faster iter-changes.
21
import time
2474.1.57 by John Arbash Meinel
Move code around to refactor according to our pyrex extension design.
22
2474.1.7 by John Arbash Meinel
Add some tests for a helper function that lets us
23
from bzrlib import (
2474.1.63 by John Arbash Meinel
Found a small bug in the python version of _read_dirblocks.
24
    dirstate,
3640.2.5 by John Arbash Meinel
Change from using AssertionError to using DirstateCorrupt in a few places
25
    errors,
4132.2.4 by Ian Clatworthy
John's enhancements/fixes to the tests
26
    osutils,
2474.1.7 by John Arbash Meinel
Add some tests for a helper function that lets us
27
    tests,
28
    )
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
29
from bzrlib.tests import (
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
30
    test_dirstate,
31
    test_osutils,
32
    )
33
4241.14.19 by Vincent Ladeuil
Modernize dirstate helpers tests parametrization.
34
try:
35
    from bzrlib import _dirstate_helpers_c
36
    has_dirstate_helpers_c = True
37
except ImportError:
38
    has_dirstate_helpers_c = False
39
40
41
class _CompiledDirstateHelpersFeature(tests.Feature):
42
    def _probe(self):
43
        return has_dirstate_helpers_c
44
45
    def feature_name(self):
46
        return 'bzrlib._dirstate_helpers_c'
47
CompiledDirstateHelpersFeature = _CompiledDirstateHelpersFeature()
48
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
49
50
def load_tests(basic_tests, module, loader):
4241.14.23 by Vincent Ladeuil
NEWS entry and final cleanup before submission.
51
    # FIXME: we should also parametrize against SHA1Provider !
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
52
    suite = loader.suiteClass()
4241.14.19 by Vincent Ladeuil
Modernize dirstate helpers tests parametrization.
53
    remaining_tests = basic_tests
54
55
    dir_reader_scenarios = test_osutils.dir_reader_scenarios()
56
57
    ue_scenarios = [('dirstate_Python',
58
                     {'update_entry': dirstate.py_update_entry})]
59
    if has_dirstate_helpers_c:
60
        c_scenario = ('dirstate_C',
61
                     {'update_entry': _dirstate_helpers_c.update_entry})
62
        ue_scenarios.append(c_scenario)
63
    process_entry_tests, remaining_tests = tests.split_suite_by_condition(
64
        remaining_tests, tests.condition_isinstance(TestUpdateEntry))
65
    tests.multiply_tests(process_entry_tests,
66
                         tests.multiply_scenarios(dir_reader_scenarios,
67
                                                  ue_scenarios),
68
                         suite)
69
70
    pe_scenarios = [('dirstate_Python',
71
                     {'_process_entry': dirstate.ProcessEntryPython})]
72
    if has_dirstate_helpers_c:
73
        c_scenario = ('dirstate_C',
74
                     {'_process_entry': _dirstate_helpers_c.ProcessEntryC})
75
        pe_scenarios.append(c_scenario)
76
    process_entry_tests, remaining_tests = tests.split_suite_by_condition(
77
        remaining_tests, tests.condition_isinstance(TestProcessEntry))
78
    tests.multiply_tests(process_entry_tests,
79
                         tests.multiply_scenarios(dir_reader_scenarios,
80
                                                  pe_scenarios),
81
                         suite)
82
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
83
    dir_reader_tests, remaining_tests = tests.split_suite_by_condition(
4241.14.19 by Vincent Ladeuil
Modernize dirstate helpers tests parametrization.
84
        remaining_tests, tests.condition_isinstance(
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
85
            test_dirstate.TestCaseWithDirState))
4241.14.19 by Vincent Ladeuil
Modernize dirstate helpers tests parametrization.
86
    tests.multiply_tests(dir_reader_tests, dir_reader_scenarios, suite)
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
87
    suite.addTest(remaining_tests)
4241.14.19 by Vincent Ladeuil
Modernize dirstate helpers tests parametrization.
88
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
89
    return suite
2474.1.7 by John Arbash Meinel
Add some tests for a helper function that lets us
90
91
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
92
class TestBisectPathMixin(object):
2474.1.66 by John Arbash Meinel
Some restructuring.
93
    """Test that _bisect_path_*() returns the expected values.
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
94
2474.1.66 by John Arbash Meinel
Some restructuring.
95
    _bisect_path_* is intended to work like bisect.bisect_*() except it
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
96
    knows it is working on paths that are sorted by ('path', 'to', 'foo')
97
    chunks rather than by raw 'path/to/foo'.
2474.1.68 by John Arbash Meinel
Review feedback from Martin, mostly documentation updates.
98
99
    Test Cases should inherit from this and override ``get_bisect_path`` return
100
    their implementation, and ``get_bisect`` to return the matching
101
    bisect.bisect_* function.
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
102
    """
103
104
    def get_bisect_path(self):
2474.1.66 by John Arbash Meinel
Some restructuring.
105
        """Return an implementation of _bisect_path_*"""
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
106
        raise NotImplementedError
107
108
    def get_bisect(self):
109
        """Return a version of bisect.bisect_*.
110
111
        Also, for the 'exists' check, return the offset to the real values.
112
        For example bisect_left returns the index of an entry, while
113
        bisect_right returns the index *after* an entry
114
115
        :return: (bisect_func, offset)
116
        """
117
        raise NotImplementedError
118
119
    def assertBisect(self, paths, split_paths, path, exists=True):
120
        """Assert that bisect_split works like bisect_left on the split paths.
121
122
        :param paths: A list of path names
123
        :param split_paths: A list of path names that are already split up by directory
124
            ('path/to/foo' => ('path', 'to', 'foo'))
125
        :param path: The path we are indexing.
126
        :param exists: The path should be present, so make sure the
127
            final location actually points to the right value.
128
129
        All other arguments will be passed along.
130
        """
131
        bisect_path = self.get_bisect_path()
132
        self.assertIsInstance(paths, list)
133
        bisect_path_idx = bisect_path(paths, path)
134
        split_path = self.split_for_dirblocks([path])[0]
135
        bisect_func, offset = self.get_bisect()
136
        bisect_split_idx = bisect_func(split_paths, split_path)
137
        self.assertEqual(bisect_split_idx, bisect_path_idx,
138
                         '%s disagreed. %s != %s'
139
                         ' for key %r'
140
                         % (bisect_path.__name__,
141
                            bisect_split_idx, bisect_path_idx, path)
142
                         )
143
        if exists:
144
            self.assertEqual(path, paths[bisect_path_idx+offset])
145
146
    def split_for_dirblocks(self, paths):
147
        dir_split_paths = []
148
        for path in paths:
149
            dirname, basename = os.path.split(path)
150
            dir_split_paths.append((dirname.split('/'), basename))
151
        dir_split_paths.sort()
152
        return dir_split_paths
153
154
    def test_simple(self):
155
        """In the simple case it works just like bisect_left"""
156
        paths = ['', 'a', 'b', 'c', 'd']
157
        split_paths = self.split_for_dirblocks(paths)
158
        for path in paths:
159
            self.assertBisect(paths, split_paths, path, exists=True)
160
        self.assertBisect(paths, split_paths, '_', exists=False)
161
        self.assertBisect(paths, split_paths, 'aa', exists=False)
162
        self.assertBisect(paths, split_paths, 'bb', exists=False)
163
        self.assertBisect(paths, split_paths, 'cc', exists=False)
164
        self.assertBisect(paths, split_paths, 'dd', exists=False)
165
        self.assertBisect(paths, split_paths, 'a/a', exists=False)
166
        self.assertBisect(paths, split_paths, 'b/b', exists=False)
167
        self.assertBisect(paths, split_paths, 'c/c', exists=False)
168
        self.assertBisect(paths, split_paths, 'd/d', exists=False)
169
170
    def test_involved(self):
171
        """This is where bisect_path_* diverges slightly."""
172
        # This is the list of paths and their contents
173
        # a/
174
        #   a/
175
        #     a
176
        #     z
177
        #   a-a/
178
        #     a
179
        #   a-z/
180
        #     z
181
        #   a=a/
182
        #     a
183
        #   a=z/
184
        #     z
185
        #   z/
186
        #     a
187
        #     z
188
        #   z-a
189
        #   z-z
190
        #   z=a
191
        #   z=z
192
        # a-a/
193
        #   a
194
        # a-z/
195
        #   z
196
        # a=a/
197
        #   a
198
        # a=z/
199
        #   z
200
        # This is the exact order that is stored by dirstate
201
        # All children in a directory are mentioned before an children of
202
        # children are mentioned.
203
        # So all the root-directory paths, then all the
204
        # first sub directory, etc.
205
        paths = [# content of '/'
206
                 '', 'a', 'a-a', 'a-z', 'a=a', 'a=z',
207
                 # content of 'a/'
208
                 'a/a', 'a/a-a', 'a/a-z',
209
                 'a/a=a', 'a/a=z',
210
                 'a/z', 'a/z-a', 'a/z-z',
211
                 'a/z=a', 'a/z=z',
212
                 # content of 'a/a/'
213
                 'a/a/a', 'a/a/z',
214
                 # content of 'a/a-a'
215
                 'a/a-a/a',
216
                 # content of 'a/a-z'
217
                 'a/a-z/z',
218
                 # content of 'a/a=a'
219
                 'a/a=a/a',
220
                 # content of 'a/a=z'
221
                 'a/a=z/z',
222
                 # content of 'a/z/'
223
                 'a/z/a', 'a/z/z',
224
                 # content of 'a-a'
225
                 'a-a/a',
226
                 # content of 'a-z'
227
                 'a-z/z',
228
                 # content of 'a=a'
229
                 'a=a/a',
230
                 # content of 'a=z'
231
                 'a=z/z',
232
                ]
233
        split_paths = self.split_for_dirblocks(paths)
234
        sorted_paths = []
235
        for dir_parts, basename in split_paths:
236
            if dir_parts == ['']:
237
                sorted_paths.append(basename)
238
            else:
239
                sorted_paths.append('/'.join(dir_parts + [basename]))
240
241
        self.assertEqual(sorted_paths, paths)
242
243
        for path in paths:
244
            self.assertBisect(paths, split_paths, path, exists=True)
245
246
247
class TestBisectPathLeft(tests.TestCase, TestBisectPathMixin):
2474.1.68 by John Arbash Meinel
Review feedback from Martin, mostly documentation updates.
248
    """Run all Bisect Path tests against _bisect_path_left_py."""
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
249
250
    def get_bisect_path(self):
2474.1.66 by John Arbash Meinel
Some restructuring.
251
        from bzrlib._dirstate_helpers_py import _bisect_path_left_py
252
        return _bisect_path_left_py
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
253
254
    def get_bisect(self):
255
        return bisect.bisect_left, 0
256
257
258
class TestCompiledBisectPathLeft(TestBisectPathLeft):
2474.1.68 by John Arbash Meinel
Review feedback from Martin, mostly documentation updates.
259
    """Run all Bisect Path tests against _bisect_path_right_c"""
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
260
261
    _test_needs_features = [CompiledDirstateHelpersFeature]
262
263
    def get_bisect_path(self):
2474.1.66 by John Arbash Meinel
Some restructuring.
264
        from bzrlib._dirstate_helpers_c import _bisect_path_left_c
265
        return _bisect_path_left_c
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
266
267
268
class TestBisectPathRight(tests.TestCase, TestBisectPathMixin):
2474.1.68 by John Arbash Meinel
Review feedback from Martin, mostly documentation updates.
269
    """Run all Bisect Path tests against _bisect_path_right_py"""
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
270
271
    def get_bisect_path(self):
2474.1.66 by John Arbash Meinel
Some restructuring.
272
        from bzrlib._dirstate_helpers_py import _bisect_path_right_py
273
        return _bisect_path_right_py
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
274
275
    def get_bisect(self):
276
        return bisect.bisect_right, -1
277
278
279
class TestCompiledBisectPathRight(TestBisectPathRight):
2474.1.68 by John Arbash Meinel
Review feedback from Martin, mostly documentation updates.
280
    """Run all Bisect Path tests against _bisect_path_right_c"""
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
281
282
    _test_needs_features = [CompiledDirstateHelpersFeature]
283
284
    def get_bisect_path(self):
2474.1.66 by John Arbash Meinel
Some restructuring.
285
        from bzrlib._dirstate_helpers_c import _bisect_path_right_c
286
        return _bisect_path_right_c
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
287
288
289
class TestBisectDirblock(tests.TestCase):
290
    """Test that bisect_dirblock() returns the expected values.
291
292
    bisect_dirblock is intended to work like bisect.bisect_left() except it
293
    knows it is working on dirblocks and that dirblocks are sorted by ('path',
294
    'to', 'foo') chunks rather than by raw 'path/to/foo'.
2474.1.68 by John Arbash Meinel
Review feedback from Martin, mostly documentation updates.
295
296
    This test is parameterized by calling get_bisect_dirblock(). Child test
297
    cases can override this function to test against a different
298
    implementation.
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
299
    """
300
301
    def get_bisect_dirblock(self):
302
        """Return an implementation of bisect_dirblock"""
303
        from bzrlib._dirstate_helpers_py import bisect_dirblock_py
304
        return bisect_dirblock_py
305
306
    def assertBisect(self, dirblocks, split_dirblocks, path, *args, **kwargs):
307
        """Assert that bisect_split works like bisect_left on the split paths.
308
309
        :param dirblocks: A list of (path, [info]) pairs.
310
        :param split_dirblocks: A list of ((split, path), [info]) pairs.
311
        :param path: The path we are indexing.
312
313
        All other arguments will be passed along.
314
        """
315
        bisect_dirblock = self.get_bisect_dirblock()
316
        self.assertIsInstance(dirblocks, list)
317
        bisect_split_idx = bisect_dirblock(dirblocks, path, *args, **kwargs)
318
        split_dirblock = (path.split('/'), [])
319
        bisect_left_idx = bisect.bisect_left(split_dirblocks, split_dirblock,
320
                                             *args)
321
        self.assertEqual(bisect_left_idx, bisect_split_idx,
322
                         'bisect_split disagreed. %s != %s'
323
                         ' for key %r'
324
                         % (bisect_left_idx, bisect_split_idx, path)
325
                         )
326
327
    def paths_to_dirblocks(self, paths):
328
        """Convert a list of paths into dirblock form.
329
330
        Also, ensure that the paths are in proper sorted order.
331
        """
332
        dirblocks = [(path, []) for path in paths]
333
        split_dirblocks = [(path.split('/'), []) for path in paths]
334
        self.assertEqual(sorted(split_dirblocks), split_dirblocks)
335
        return dirblocks, split_dirblocks
336
337
    def test_simple(self):
338
        """In the simple case it works just like bisect_left"""
339
        paths = ['', 'a', 'b', 'c', 'd']
340
        dirblocks, split_dirblocks = self.paths_to_dirblocks(paths)
341
        for path in paths:
342
            self.assertBisect(dirblocks, split_dirblocks, path)
343
        self.assertBisect(dirblocks, split_dirblocks, '_')
344
        self.assertBisect(dirblocks, split_dirblocks, 'aa')
345
        self.assertBisect(dirblocks, split_dirblocks, 'bb')
346
        self.assertBisect(dirblocks, split_dirblocks, 'cc')
347
        self.assertBisect(dirblocks, split_dirblocks, 'dd')
348
        self.assertBisect(dirblocks, split_dirblocks, 'a/a')
349
        self.assertBisect(dirblocks, split_dirblocks, 'b/b')
350
        self.assertBisect(dirblocks, split_dirblocks, 'c/c')
351
        self.assertBisect(dirblocks, split_dirblocks, 'd/d')
352
353
    def test_involved(self):
354
        """This is where bisect_left diverges slightly."""
355
        paths = ['', 'a',
356
                 'a/a', 'a/a/a', 'a/a/z', 'a/a-a', 'a/a-z',
357
                 'a/z', 'a/z/a', 'a/z/z', 'a/z-a', 'a/z-z',
358
                 'a-a', 'a-z',
359
                 'z', 'z/a/a', 'z/a/z', 'z/a-a', 'z/a-z',
360
                 'z/z', 'z/z/a', 'z/z/z', 'z/z-a', 'z/z-z',
361
                 'z-a', 'z-z',
362
                ]
363
        dirblocks, split_dirblocks = self.paths_to_dirblocks(paths)
364
        for path in paths:
365
            self.assertBisect(dirblocks, split_dirblocks, path)
366
367
    def test_involved_cached(self):
368
        """This is where bisect_left diverges slightly."""
369
        paths = ['', 'a',
370
                 'a/a', 'a/a/a', 'a/a/z', 'a/a-a', 'a/a-z',
371
                 'a/z', 'a/z/a', 'a/z/z', 'a/z-a', 'a/z-z',
372
                 'a-a', 'a-z',
373
                 'z', 'z/a/a', 'z/a/z', 'z/a-a', 'z/a-z',
374
                 'z/z', 'z/z/a', 'z/z/z', 'z/z-a', 'z/z-z',
375
                 'z-a', 'z-z',
376
                ]
377
        cache = {}
378
        dirblocks, split_dirblocks = self.paths_to_dirblocks(paths)
379
        for path in paths:
380
            self.assertBisect(dirblocks, split_dirblocks, path, cache=cache)
381
382
383
class TestCompiledBisectDirblock(TestBisectDirblock):
384
    """Test that bisect_dirblock() returns the expected values.
385
386
    bisect_dirblock is intended to work like bisect.bisect_left() except it
387
    knows it is working on dirblocks and that dirblocks are sorted by ('path',
388
    'to', 'foo') chunks rather than by raw 'path/to/foo'.
389
390
    This runs all the normal tests that TestBisectDirblock did, but uses the
391
    compiled version.
392
    """
393
394
    _test_needs_features = [CompiledDirstateHelpersFeature]
395
396
    def get_bisect_dirblock(self):
397
        from bzrlib._dirstate_helpers_c import bisect_dirblock_c
398
        return bisect_dirblock_c
399
400
2474.1.57 by John Arbash Meinel
Move code around to refactor according to our pyrex extension design.
401
class TestCmpByDirs(tests.TestCase):
2474.1.68 by John Arbash Meinel
Review feedback from Martin, mostly documentation updates.
402
    """Test an implementation of cmp_by_dirs()
403
404
    cmp_by_dirs() compares 2 paths by their directory sections, rather than as
405
    plain strings.
406
407
    Child test cases can override ``get_cmp_by_dirs`` to test a specific
408
    implementation.
409
    """
2474.1.57 by John Arbash Meinel
Move code around to refactor according to our pyrex extension design.
410
411
    def get_cmp_by_dirs(self):
412
        """Get a specific implementation of cmp_by_dirs."""
413
        from bzrlib._dirstate_helpers_py import cmp_by_dirs_py
414
        return cmp_by_dirs_py
415
416
    def assertCmpByDirs(self, expected, str1, str2):
417
        """Compare the two strings, in both directions.
418
419
        :param expected: The expected comparison value. -1 means str1 comes
420
            first, 0 means they are equal, 1 means str2 comes first
421
        :param str1: string to compare
422
        :param str2: string to compare
423
        """
424
        cmp_by_dirs = self.get_cmp_by_dirs()
425
        if expected == 0:
426
            self.assertEqual(str1, str2)
427
            self.assertEqual(0, cmp_by_dirs(str1, str2))
428
            self.assertEqual(0, cmp_by_dirs(str2, str1))
429
        elif expected > 0:
430
            self.assertPositive(cmp_by_dirs(str1, str2))
431
            self.assertNegative(cmp_by_dirs(str2, str1))
432
        else:
433
            self.assertNegative(cmp_by_dirs(str1, str2))
434
            self.assertPositive(cmp_by_dirs(str2, str1))
435
436
    def test_cmp_empty(self):
437
        """Compare against the empty string."""
438
        self.assertCmpByDirs(0, '', '')
439
        self.assertCmpByDirs(1, 'a', '')
440
        self.assertCmpByDirs(1, 'ab', '')
441
        self.assertCmpByDirs(1, 'abc', '')
442
        self.assertCmpByDirs(1, 'abcd', '')
443
        self.assertCmpByDirs(1, 'abcde', '')
444
        self.assertCmpByDirs(1, 'abcdef', '')
445
        self.assertCmpByDirs(1, 'abcdefg', '')
446
        self.assertCmpByDirs(1, 'abcdefgh', '')
447
        self.assertCmpByDirs(1, 'abcdefghi', '')
448
        self.assertCmpByDirs(1, 'test/ing/a/path/', '')
449
450
    def test_cmp_same_str(self):
451
        """Compare the same string"""
452
        self.assertCmpByDirs(0, 'a', 'a')
453
        self.assertCmpByDirs(0, 'ab', 'ab')
454
        self.assertCmpByDirs(0, 'abc', 'abc')
455
        self.assertCmpByDirs(0, 'abcd', 'abcd')
456
        self.assertCmpByDirs(0, 'abcde', 'abcde')
457
        self.assertCmpByDirs(0, 'abcdef', 'abcdef')
458
        self.assertCmpByDirs(0, 'abcdefg', 'abcdefg')
459
        self.assertCmpByDirs(0, 'abcdefgh', 'abcdefgh')
460
        self.assertCmpByDirs(0, 'abcdefghi', 'abcdefghi')
461
        self.assertCmpByDirs(0, 'testing a long string', 'testing a long string')
462
        self.assertCmpByDirs(0, 'x'*10000, 'x'*10000)
463
        self.assertCmpByDirs(0, 'a/b', 'a/b')
464
        self.assertCmpByDirs(0, 'a/b/c', 'a/b/c')
465
        self.assertCmpByDirs(0, 'a/b/c/d', 'a/b/c/d')
466
        self.assertCmpByDirs(0, 'a/b/c/d/e', 'a/b/c/d/e')
467
468
    def test_simple_paths(self):
469
        """Compare strings that act like normal string comparison"""
470
        self.assertCmpByDirs(-1, 'a', 'b')
471
        self.assertCmpByDirs(-1, 'aa', 'ab')
472
        self.assertCmpByDirs(-1, 'ab', 'bb')
473
        self.assertCmpByDirs(-1, 'aaa', 'aab')
474
        self.assertCmpByDirs(-1, 'aab', 'abb')
475
        self.assertCmpByDirs(-1, 'abb', 'bbb')
476
        self.assertCmpByDirs(-1, 'aaaa', 'aaab')
477
        self.assertCmpByDirs(-1, 'aaab', 'aabb')
478
        self.assertCmpByDirs(-1, 'aabb', 'abbb')
479
        self.assertCmpByDirs(-1, 'abbb', 'bbbb')
480
        self.assertCmpByDirs(-1, 'aaaaa', 'aaaab')
481
        self.assertCmpByDirs(-1, 'a/a', 'a/b')
482
        self.assertCmpByDirs(-1, 'a/b', 'b/b')
483
        self.assertCmpByDirs(-1, 'a/a/a', 'a/a/b')
484
        self.assertCmpByDirs(-1, 'a/a/b', 'a/b/b')
485
        self.assertCmpByDirs(-1, 'a/b/b', 'b/b/b')
486
        self.assertCmpByDirs(-1, 'a/a/a/a', 'a/a/a/b')
487
        self.assertCmpByDirs(-1, 'a/a/a/b', 'a/a/b/b')
488
        self.assertCmpByDirs(-1, 'a/a/b/b', 'a/b/b/b')
489
        self.assertCmpByDirs(-1, 'a/b/b/b', 'b/b/b/b')
490
        self.assertCmpByDirs(-1, 'a/a/a/a/a', 'a/a/a/a/b')
491
492
    def test_tricky_paths(self):
493
        self.assertCmpByDirs(1, 'ab/cd/ef', 'ab/cc/ef')
494
        self.assertCmpByDirs(1, 'ab/cd/ef', 'ab/c/ef')
495
        self.assertCmpByDirs(-1, 'ab/cd/ef', 'ab/cd-ef')
496
        self.assertCmpByDirs(-1, 'ab/cd', 'ab/cd-')
497
        self.assertCmpByDirs(-1, 'ab/cd', 'ab-cd')
498
2474.1.70 by John Arbash Meinel
Lot's of fixes from Martin's comments.
499
    def test_cmp_unicode_not_allowed(self):
500
        cmp_by_dirs = self.get_cmp_by_dirs()
501
        self.assertRaises(TypeError, cmp_by_dirs, u'Unicode', 'str')
502
        self.assertRaises(TypeError, cmp_by_dirs, 'str', u'Unicode')
503
        self.assertRaises(TypeError, cmp_by_dirs, u'Unicode', u'Unicode')
504
505
    def test_cmp_non_ascii(self):
506
        self.assertCmpByDirs(-1, '\xc2\xb5', '\xc3\xa5') # u'\xb5', u'\xe5'
507
        self.assertCmpByDirs(-1, 'a', '\xc3\xa5') # u'a', u'\xe5'
508
        self.assertCmpByDirs(-1, 'b', '\xc2\xb5') # u'b', u'\xb5'
509
        self.assertCmpByDirs(-1, 'a/b', 'a/\xc3\xa5') # u'a/b', u'a/\xe5'
510
        self.assertCmpByDirs(-1, 'b/a', 'b/\xc2\xb5') # u'b/a', u'b/\xb5'
511
2474.1.57 by John Arbash Meinel
Move code around to refactor according to our pyrex extension design.
512
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
513
class TestCompiledCmpByDirs(TestCmpByDirs):
514
    """Test the pyrex implementation of cmp_by_dirs"""
2474.1.7 by John Arbash Meinel
Add some tests for a helper function that lets us
515
516
    _test_needs_features = [CompiledDirstateHelpersFeature]
517
2474.1.41 by John Arbash Meinel
Change the name of cmp_dirblock_strings to cmp_by_dirs
518
    def get_cmp_by_dirs(self):
2474.1.57 by John Arbash Meinel
Move code around to refactor according to our pyrex extension design.
519
        from bzrlib._dirstate_helpers_c import cmp_by_dirs_c
520
        return cmp_by_dirs_c
521
522
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
523
class TestCmpPathByDirblock(tests.TestCase):
2474.1.68 by John Arbash Meinel
Review feedback from Martin, mostly documentation updates.
524
    """Test an implementation of _cmp_path_by_dirblock()
525
526
    _cmp_path_by_dirblock() compares two paths using the sort order used by
527
    DirState. All paths in the same directory are sorted together.
528
529
    Child test cases can override ``get_cmp_path_by_dirblock`` to test a specific
530
    implementation.
531
    """
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
532
533
    def get_cmp_path_by_dirblock(self):
2474.1.66 by John Arbash Meinel
Some restructuring.
534
        """Get a specific implementation of _cmp_path_by_dirblock."""
535
        from bzrlib._dirstate_helpers_py import _cmp_path_by_dirblock_py
536
        return _cmp_path_by_dirblock_py
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
537
538
    def assertCmpPathByDirblock(self, paths):
539
        """Compare all paths and make sure they evaluate to the correct order.
540
541
        This does N^2 comparisons. It is assumed that ``paths`` is properly
542
        sorted list.
543
544
        :param paths: a sorted list of paths to compare
545
        """
546
        # First, make sure the paths being passed in are correct
547
        def _key(p):
548
            dirname, basename = os.path.split(p)
549
            return dirname.split('/'), basename
550
        self.assertEqual(sorted(paths, key=_key), paths)
551
552
        cmp_path_by_dirblock = self.get_cmp_path_by_dirblock()
553
        for idx1, path1 in enumerate(paths):
554
            for idx2, path2 in enumerate(paths):
555
                cmp_val = cmp_path_by_dirblock(path1, path2)
556
                if idx1 < idx2:
557
                    self.assertTrue(cmp_val < 0,
558
                        '%s did not state that %r came before %r, cmp=%s'
559
                        % (cmp_path_by_dirblock.__name__,
560
                           path1, path2, cmp_val))
561
                elif idx1 > idx2:
562
                    self.assertTrue(cmp_val > 0,
563
                        '%s did not state that %r came after %r, cmp=%s'
564
                        % (cmp_path_by_dirblock.__name__,
565
                           path1, path2, cmp_val))
566
                else: # idx1 == idx2
567
                    self.assertTrue(cmp_val == 0,
568
                        '%s did not state that %r == %r, cmp=%s'
569
                        % (cmp_path_by_dirblock.__name__,
570
                           path1, path2, cmp_val))
571
572
    def test_cmp_simple_paths(self):
573
        """Compare against the empty string."""
574
        self.assertCmpPathByDirblock(['', 'a', 'ab', 'abc', 'a/b/c', 'b/d/e'])
575
        self.assertCmpPathByDirblock(['kl', 'ab/cd', 'ab/ef', 'gh/ij'])
576
577
    def test_tricky_paths(self):
578
        self.assertCmpPathByDirblock([
579
            # Contents of ''
580
            '', 'a', 'a-a', 'a=a', 'b',
581
            # Contents of 'a'
582
            'a/a', 'a/a-a', 'a/a=a', 'a/b',
583
            # Contents of 'a/a'
584
            'a/a/a', 'a/a/a-a', 'a/a/a=a',
585
            # Contents of 'a/a/a'
586
            'a/a/a/a', 'a/a/a/b',
587
            # Contents of 'a/a/a-a',
588
            'a/a/a-a/a', 'a/a/a-a/b',
589
            # Contents of 'a/a/a=a',
590
            'a/a/a=a/a', 'a/a/a=a/b',
591
            # Contents of 'a/a-a'
592
            'a/a-a/a',
593
            # Contents of 'a/a-a/a'
594
            'a/a-a/a/a', 'a/a-a/a/b',
595
            # Contents of 'a/a=a'
596
            'a/a=a/a',
597
            # Contents of 'a/b'
598
            'a/b/a', 'a/b/b',
599
            # Contents of 'a-a',
600
            'a-a/a', 'a-a/b',
601
            # Contents of 'a=a',
602
            'a=a/a', 'a=a/b',
603
            # Contents of 'b',
604
            'b/a', 'b/b',
605
            ])
606
        self.assertCmpPathByDirblock([
607
                 # content of '/'
608
                 '', 'a', 'a-a', 'a-z', 'a=a', 'a=z',
609
                 # content of 'a/'
610
                 'a/a', 'a/a-a', 'a/a-z',
611
                 'a/a=a', 'a/a=z',
612
                 'a/z', 'a/z-a', 'a/z-z',
613
                 'a/z=a', 'a/z=z',
614
                 # content of 'a/a/'
615
                 'a/a/a', 'a/a/z',
616
                 # content of 'a/a-a'
617
                 'a/a-a/a',
618
                 # content of 'a/a-z'
619
                 'a/a-z/z',
620
                 # content of 'a/a=a'
621
                 'a/a=a/a',
622
                 # content of 'a/a=z'
623
                 'a/a=z/z',
624
                 # content of 'a/z/'
625
                 'a/z/a', 'a/z/z',
626
                 # content of 'a-a'
627
                 'a-a/a',
628
                 # content of 'a-z'
629
                 'a-z/z',
630
                 # content of 'a=a'
631
                 'a=a/a',
632
                 # content of 'a=z'
633
                 'a=z/z',
634
                ])
635
2474.1.70 by John Arbash Meinel
Lot's of fixes from Martin's comments.
636
    def test_unicode_not_allowed(self):
637
        cmp_path_by_dirblock = self.get_cmp_path_by_dirblock()
638
        self.assertRaises(TypeError, cmp_path_by_dirblock, u'Uni', 'str')
639
        self.assertRaises(TypeError, cmp_path_by_dirblock, 'str', u'Uni')
640
        self.assertRaises(TypeError, cmp_path_by_dirblock, u'Uni', u'Uni')
641
        self.assertRaises(TypeError, cmp_path_by_dirblock, u'x/Uni', 'x/str')
642
        self.assertRaises(TypeError, cmp_path_by_dirblock, 'x/str', u'x/Uni')
643
        self.assertRaises(TypeError, cmp_path_by_dirblock, u'x/Uni', u'x/Uni')
644
645
    def test_nonascii(self):
646
        self.assertCmpPathByDirblock([
647
            # content of '/'
648
            '', 'a', '\xc2\xb5', '\xc3\xa5',
649
            # content of 'a'
650
            'a/a', 'a/\xc2\xb5', 'a/\xc3\xa5',
651
            # content of 'a/a'
652
            'a/a/a', 'a/a/\xc2\xb5', 'a/a/\xc3\xa5',
653
            # content of 'a/\xc2\xb5'
654
            'a/\xc2\xb5/a', 'a/\xc2\xb5/\xc2\xb5', 'a/\xc2\xb5/\xc3\xa5',
655
            # content of 'a/\xc3\xa5'
656
            'a/\xc3\xa5/a', 'a/\xc3\xa5/\xc2\xb5', 'a/\xc3\xa5/\xc3\xa5',
657
            # content of '\xc2\xb5'
658
            '\xc2\xb5/a', '\xc2\xb5/\xc2\xb5', '\xc2\xb5/\xc3\xa5',
659
            # content of '\xc2\xe5'
660
            '\xc3\xa5/a', '\xc3\xa5/\xc2\xb5', '\xc3\xa5/\xc3\xa5',
661
            ])
662
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
663
664
class TestCompiledCmpPathByDirblock(TestCmpPathByDirblock):
2474.1.66 by John Arbash Meinel
Some restructuring.
665
    """Test the pyrex implementation of _cmp_path_by_dirblock"""
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
666
667
    _test_needs_features = [CompiledDirstateHelpersFeature]
668
669
    def get_cmp_by_dirs(self):
2474.1.66 by John Arbash Meinel
Some restructuring.
670
        from bzrlib._dirstate_helpers_c import _cmp_path_by_dirblock_c
671
        return _cmp_path_by_dirblock_c
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
672
673
674
class TestMemRChr(tests.TestCase):
2474.1.66 by John Arbash Meinel
Some restructuring.
675
    """Test memrchr functionality"""
2474.1.58 by John Arbash Meinel
(broken) Try to properly implement DirState._bisect*
676
677
    _test_needs_features = [CompiledDirstateHelpersFeature]
678
679
    def assertMemRChr(self, expected, s, c):
680
        from bzrlib._dirstate_helpers_c import _py_memrchr
681
        self.assertEqual(expected, _py_memrchr(s, c))
682
683
    def test_missing(self):
684
        self.assertMemRChr(None, '', 'a')
685
        self.assertMemRChr(None, '', 'c')
686
        self.assertMemRChr(None, 'abcdefghijklm', 'q')
687
        self.assertMemRChr(None, 'aaaaaaaaaaaaaaaaaaaaaaa', 'b')
688
689
    def test_single_entry(self):
690
        self.assertMemRChr(0, 'abcdefghijklm', 'a')
691
        self.assertMemRChr(1, 'abcdefghijklm', 'b')
692
        self.assertMemRChr(2, 'abcdefghijklm', 'c')
693
        self.assertMemRChr(10, 'abcdefghijklm', 'k')
694
        self.assertMemRChr(11, 'abcdefghijklm', 'l')
695
        self.assertMemRChr(12, 'abcdefghijklm', 'm')
696
697
    def test_multiple(self):
698
        self.assertMemRChr(10, 'abcdefjklmabcdefghijklm', 'a')
699
        self.assertMemRChr(11, 'abcdefjklmabcdefghijklm', 'b')
700
        self.assertMemRChr(12, 'abcdefjklmabcdefghijklm', 'c')
701
        self.assertMemRChr(20, 'abcdefjklmabcdefghijklm', 'k')
702
        self.assertMemRChr(21, 'abcdefjklmabcdefghijklm', 'l')
703
        self.assertMemRChr(22, 'abcdefjklmabcdefghijklm', 'm')
704
        self.assertMemRChr(22, 'aaaaaaaaaaaaaaaaaaaaaaa', 'a')
705
706
    def test_with_nulls(self):
707
        self.assertMemRChr(10, 'abc\0\0\0jklmabc\0\0\0ghijklm', 'a')
708
        self.assertMemRChr(11, 'abc\0\0\0jklmabc\0\0\0ghijklm', 'b')
709
        self.assertMemRChr(12, 'abc\0\0\0jklmabc\0\0\0ghijklm', 'c')
710
        self.assertMemRChr(20, 'abc\0\0\0jklmabc\0\0\0ghijklm', 'k')
711
        self.assertMemRChr(21, 'abc\0\0\0jklmabc\0\0\0ghijklm', 'l')
712
        self.assertMemRChr(22, 'abc\0\0\0jklmabc\0\0\0ghijklm', 'm')
713
        self.assertMemRChr(22, 'aaa\0\0\0aaaaaaa\0\0\0aaaaaaa', 'a')
714
        self.assertMemRChr(9, '\0\0\0\0\0\0\0\0\0\0', '\0')
2474.1.63 by John Arbash Meinel
Found a small bug in the python version of _read_dirblocks.
715
716
717
class TestReadDirblocks(test_dirstate.TestCaseWithDirState):
2474.1.68 by John Arbash Meinel
Review feedback from Martin, mostly documentation updates.
718
    """Test an implementation of _read_dirblocks()
719
720
    _read_dirblocks() reads in all of the dirblock information from the disk
721
    file.
722
723
    Child test cases can override ``get_read_dirblocks`` to test a specific
724
    implementation.
725
    """
2474.1.63 by John Arbash Meinel
Found a small bug in the python version of _read_dirblocks.
726
727
    def get_read_dirblocks(self):
728
        from bzrlib._dirstate_helpers_py import _read_dirblocks_py
729
        return _read_dirblocks_py
730
731
    def test_smoketest(self):
732
        """Make sure that we can create and read back a simple file."""
733
        tree, state, expected = self.create_basic_dirstate()
734
        del tree
735
        state._read_header_if_needed()
736
        self.assertEqual(dirstate.DirState.NOT_IN_MEMORY,
737
                         state._dirblock_state)
738
        read_dirblocks = self.get_read_dirblocks()
739
        read_dirblocks(state)
740
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
741
                         state._dirblock_state)
742
3640.2.1 by John Arbash Meinel
More safety checks around PyString_FromStringAndSize,
743
    def test_trailing_garbage(self):
744
        tree, state, expected = self.create_basic_dirstate()
745
        # We can modify the file as long as it hasn't been read yet.
746
        f = open('dirstate', 'ab')
747
        try:
748
            # Add bogus trailing garbage
749
            f.write('bogus\n')
750
        finally:
751
            f.close()
3640.2.5 by John Arbash Meinel
Change from using AssertionError to using DirstateCorrupt in a few places
752
        e = self.assertRaises(errors.DirstateCorrupt,
753
                              state._read_dirblocks_if_needed)
3640.2.1 by John Arbash Meinel
More safety checks around PyString_FromStringAndSize,
754
        # Make sure we mention the bogus characters in the error
755
        self.assertContainsRe(str(e), 'bogus')
756
2474.1.63 by John Arbash Meinel
Found a small bug in the python version of _read_dirblocks.
757
758
class TestCompiledReadDirblocks(TestReadDirblocks):
2474.1.68 by John Arbash Meinel
Review feedback from Martin, mostly documentation updates.
759
    """Test the pyrex implementation of _read_dirblocks"""
2474.1.63 by John Arbash Meinel
Found a small bug in the python version of _read_dirblocks.
760
761
    _test_needs_features = [CompiledDirstateHelpersFeature]
762
763
    def get_read_dirblocks(self):
764
        from bzrlib._dirstate_helpers_c import _read_dirblocks_c
765
        return _read_dirblocks_c
2474.1.66 by John Arbash Meinel
Some restructuring.
766
767
768
class TestUsingCompiledIfAvailable(tests.TestCase):
769
    """Check that any compiled functions that are available are the default.
770
771
    It is possible to have typos, etc in the import line, such that
772
    _dirstate_helpers_c is actually available, but the compiled functions are
773
    not being used.
774
    """
775
776
    def test_bisect_dirblock(self):
777
        if CompiledDirstateHelpersFeature.available():
778
            from bzrlib._dirstate_helpers_c import bisect_dirblock_c
779
            self.assertIs(bisect_dirblock_c, dirstate.bisect_dirblock)
780
        else:
781
            from bzrlib._dirstate_helpers_py import bisect_dirblock_py
782
            self.assertIs(bisect_dirblock_py, dirstate.bisect_dirblock)
783
784
    def test__bisect_path_left(self):
785
        if CompiledDirstateHelpersFeature.available():
786
            from bzrlib._dirstate_helpers_c import _bisect_path_left_c
787
            self.assertIs(_bisect_path_left_c, dirstate._bisect_path_left)
788
        else:
789
            from bzrlib._dirstate_helpers_py import _bisect_path_left_py
790
            self.assertIs(_bisect_path_left_py, dirstate._bisect_path_left)
791
792
    def test__bisect_path_right(self):
793
        if CompiledDirstateHelpersFeature.available():
794
            from bzrlib._dirstate_helpers_c import _bisect_path_right_c
795
            self.assertIs(_bisect_path_right_c, dirstate._bisect_path_right)
796
        else:
797
            from bzrlib._dirstate_helpers_py import _bisect_path_right_py
798
            self.assertIs(_bisect_path_right_py, dirstate._bisect_path_right)
799
800
    def test_cmp_by_dirs(self):
801
        if CompiledDirstateHelpersFeature.available():
802
            from bzrlib._dirstate_helpers_c import cmp_by_dirs_c
803
            self.assertIs(cmp_by_dirs_c, dirstate.cmp_by_dirs)
804
        else:
805
            from bzrlib._dirstate_helpers_py import cmp_by_dirs_py
806
            self.assertIs(cmp_by_dirs_py, dirstate.cmp_by_dirs)
807
808
    def test__read_dirblocks(self):
809
        if CompiledDirstateHelpersFeature.available():
810
            from bzrlib._dirstate_helpers_c import _read_dirblocks_c
811
            self.assertIs(_read_dirblocks_c, dirstate._read_dirblocks)
812
        else:
813
            from bzrlib._dirstate_helpers_py import _read_dirblocks_py
814
            self.assertIs(_read_dirblocks_py, dirstate._read_dirblocks)
815
3696.4.11 by Robert Collins
Some Cification of iter_changes, and making the python one actually work.
816
    def test_update_entry(self):
817
        if CompiledDirstateHelpersFeature.available():
818
            from bzrlib._dirstate_helpers_c import update_entry
819
            self.assertIs(update_entry, dirstate.update_entry)
820
        else:
821
            from bzrlib.dirstate import py_update_entry
822
            self.assertIs(py_update_entry, dirstate.py_update_entry)
823
3696.4.5 by Robert Collins
Simple 'compiled with pyrex' ProcessEntry class. faster.
824
    def test_process_entry(self):
825
        if CompiledDirstateHelpersFeature.available():
826
            from bzrlib._dirstate_helpers_c import ProcessEntryC
827
            self.assertIs(ProcessEntryC, dirstate._process_entry)
828
        else:
829
            from bzrlib.dirstate import ProcessEntryPython
830
            self.assertIs(ProcessEntryPython, dirstate._process_entry)
831
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
832
833
class TestUpdateEntry(test_dirstate.TestCaseWithDirState):
834
    """Test the DirState.update_entry functions"""
835
4241.14.19 by Vincent Ladeuil
Modernize dirstate helpers tests parametrization.
836
    # Set by load_tests
837
    update_entry = None
838
839
    def setUp(self):
840
        super(TestUpdateEntry, self).setUp()
841
        orig = dirstate.update_entry
842
        def cleanup():
843
            dirstate.update_entry = orig
844
        self.addCleanup(cleanup)
845
        dirstate.update_entry = self.update_entry
846
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
847
    def get_state_with_a(self):
848
        """Create a DirState tracking a single object named 'a'"""
849
        state = test_dirstate.InstrumentedDirState.initialize('dirstate')
850
        self.addCleanup(state.unlock)
851
        state.add('a', 'a-id', 'file', None, '')
852
        entry = state._get_entry(0, path_utf8='a')
853
        return state, entry
854
3696.5.1 by Robert Collins
Integrate in stat-cache-updates-during-commit with faster iter-changes.
855
    def test_observed_sha1_cachable(self):
856
        state, entry = self.get_state_with_a()
857
        atime = time.time() - 10
858
        self.build_tree(['a'])
859
        statvalue = os.lstat('a')
860
        statvalue = test_dirstate._FakeStat(statvalue.st_size, atime, atime,
861
            statvalue.st_dev, statvalue.st_ino, statvalue.st_mode)
862
        state._observed_sha1(entry, "foo", statvalue)
863
        self.assertEqual('foo', entry[1][0][1])
864
        packed_stat = dirstate.pack_stat(statvalue)
865
        self.assertEqual(packed_stat, entry[1][0][4])
866
867
    def test_observed_sha1_not_cachable(self):
868
        state, entry = self.get_state_with_a()
869
        oldval = entry[1][0][1]
870
        oldstat = entry[1][0][4]
871
        self.build_tree(['a'])
872
        statvalue = os.lstat('a')
873
        state._observed_sha1(entry, "foo", statvalue)
874
        self.assertEqual(oldval, entry[1][0][1])
875
        self.assertEqual(oldstat, entry[1][0][4])
876
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
877
    def test_update_entry(self):
3696.5.2 by Robert Collins
Integrate less aggressive sha logic with C iter-changes.
878
        state, _ = self.get_state_with_a()
879
        tree = self.make_branch_and_tree('tree')
880
        tree.lock_write()
881
        empty_revid = tree.commit('empty')
882
        self.build_tree(['tree/a'])
883
        tree.add(['a'], ['a-id'])
884
        with_a_id = tree.commit('with_a')
885
        self.addCleanup(tree.unlock)
886
        state.set_parent_trees(
887
            [(empty_revid, tree.branch.repository.revision_tree(empty_revid))],
888
            [])
889
        entry = state._get_entry(0, path_utf8='a')
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
890
        self.build_tree(['a'])
891
        # Add one where we don't provide the stat or sha already
892
        self.assertEqual(('', 'a', 'a-id'), entry[0])
3696.5.2 by Robert Collins
Integrate less aggressive sha logic with C iter-changes.
893
        self.assertEqual(('f', '', 0, False, dirstate.DirState.NULLSTAT),
894
                         entry[1][0])
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
895
        # Flush the buffers to disk
896
        state.save()
897
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
898
                         state._dirblock_state)
899
900
        stat_value = os.lstat('a')
901
        packed_stat = dirstate.pack_stat(stat_value)
902
        link_or_sha1 = self.update_entry(state, entry, abspath='a',
903
                                          stat_value=stat_value)
3696.5.2 by Robert Collins
Integrate less aggressive sha logic with C iter-changes.
904
        self.assertEqual(None, link_or_sha1)
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
905
3696.5.2 by Robert Collins
Integrate less aggressive sha logic with C iter-changes.
906
        # The dirblock entry should not have cached the file's sha1 (too new)
907
        self.assertEqual(('f', '', 14, False, dirstate.DirState.NULLSTAT),
908
                         entry[1][0])
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
909
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
910
                         state._dirblock_state)
911
        mode = stat_value.st_mode
3696.5.2 by Robert Collins
Integrate less aggressive sha logic with C iter-changes.
912
        self.assertEqual([('is_exec', mode, False)], state._log)
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
913
914
        state.save()
915
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
916
                         state._dirblock_state)
917
918
        # If we do it again right away, we don't know if the file has changed
919
        # so we will re-read the file. Roll the clock back so the file is
920
        # guaranteed to look too new.
921
        state.adjust_time(-10)
3696.5.2 by Robert Collins
Integrate less aggressive sha logic with C iter-changes.
922
        del state._log[:]
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
923
924
        link_or_sha1 = self.update_entry(state, entry, abspath='a',
925
                                          stat_value=stat_value)
3696.5.2 by Robert Collins
Integrate less aggressive sha logic with C iter-changes.
926
        self.assertEqual([('is_exec', mode, False)], state._log)
927
        self.assertEqual(None, link_or_sha1)
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
928
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
929
                         state._dirblock_state)
3696.5.2 by Robert Collins
Integrate less aggressive sha logic with C iter-changes.
930
        self.assertEqual(('f', '', 14, False, dirstate.DirState.NULLSTAT),
931
                         entry[1][0])
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
932
        state.save()
933
3696.5.2 by Robert Collins
Integrate less aggressive sha logic with C iter-changes.
934
        # If it is cachable (the clock has moved forward) but new it still
935
        # won't calculate the sha or cache it.
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
936
        state.adjust_time(+20)
3696.5.2 by Robert Collins
Integrate less aggressive sha logic with C iter-changes.
937
        del state._log[:]
938
        link_or_sha1 = dirstate.update_entry(state, entry, abspath='a',
939
                                          stat_value=stat_value)
940
        self.assertEqual(None, link_or_sha1)
941
        self.assertEqual([('is_exec', mode, False)], state._log)
942
        self.assertEqual(('f', '', 14, False, dirstate.DirState.NULLSTAT),
943
                         entry[1][0])
944
945
        # If the file is no longer new, and the clock has been moved forward
946
        # sufficiently, it will cache the sha.
947
        del state._log[:]
948
        state.set_parent_trees(
949
            [(with_a_id, tree.branch.repository.revision_tree(with_a_id))],
950
            [])
951
        entry = state._get_entry(0, path_utf8='a')
952
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
953
        link_or_sha1 = self.update_entry(state, entry, abspath='a',
954
                                          stat_value=stat_value)
955
        self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
956
                         link_or_sha1)
3696.5.2 by Robert Collins
Integrate less aggressive sha logic with C iter-changes.
957
        self.assertEqual([('is_exec', mode, False), ('sha1', 'a')],
958
                          state._log)
959
        self.assertEqual(('f', link_or_sha1, 14, False, packed_stat),
960
                         entry[1][0])
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
961
962
        # Subsequent calls will just return the cached value
3696.5.2 by Robert Collins
Integrate less aggressive sha logic with C iter-changes.
963
        del state._log[:]
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
964
        link_or_sha1 = self.update_entry(state, entry, abspath='a',
965
                                          stat_value=stat_value)
966
        self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
967
                         link_or_sha1)
3696.5.2 by Robert Collins
Integrate less aggressive sha logic with C iter-changes.
968
        self.assertEqual([], state._log)
969
        self.assertEqual(('f', link_or_sha1, 14, False, packed_stat),
970
                         entry[1][0])
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
971
972
    def test_update_entry_symlink(self):
973
        """Update entry should read symlinks."""
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
974
        self.requireFeature(tests.SymlinkFeature)
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
975
        state, entry = self.get_state_with_a()
976
        state.save()
977
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
978
                         state._dirblock_state)
979
        os.symlink('target', 'a')
980
981
        state.adjust_time(-10) # Make the symlink look new
982
        stat_value = os.lstat('a')
983
        packed_stat = dirstate.pack_stat(stat_value)
984
        link_or_sha1 = self.update_entry(state, entry, abspath='a',
985
                                          stat_value=stat_value)
986
        self.assertEqual('target', link_or_sha1)
987
        self.assertEqual([('read_link', 'a', '')], state._log)
988
        # Dirblock is not updated (the link is too new)
989
        self.assertEqual([('l', '', 6, False, dirstate.DirState.NULLSTAT)],
990
                         entry[1])
991
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
992
                         state._dirblock_state)
993
994
        # Because the stat_value looks new, we should re-read the target
995
        link_or_sha1 = self.update_entry(state, entry, abspath='a',
996
                                          stat_value=stat_value)
997
        self.assertEqual('target', link_or_sha1)
998
        self.assertEqual([('read_link', 'a', ''),
999
                          ('read_link', 'a', ''),
1000
                         ], state._log)
1001
        self.assertEqual([('l', '', 6, False, dirstate.DirState.NULLSTAT)],
1002
                         entry[1])
1003
        state.adjust_time(+20) # Skip into the future, all files look old
1004
        link_or_sha1 = self.update_entry(state, entry, abspath='a',
1005
                                          stat_value=stat_value)
1006
        self.assertEqual('target', link_or_sha1)
1007
        # We need to re-read the link because only now can we cache it
1008
        self.assertEqual([('read_link', 'a', ''),
1009
                          ('read_link', 'a', ''),
1010
                          ('read_link', 'a', ''),
1011
                         ], state._log)
1012
        self.assertEqual([('l', 'target', 6, False, packed_stat)],
1013
                         entry[1])
1014
1015
        # Another call won't re-read the link
1016
        self.assertEqual([('read_link', 'a', ''),
1017
                          ('read_link', 'a', ''),
1018
                          ('read_link', 'a', ''),
1019
                         ], state._log)
1020
        link_or_sha1 = self.update_entry(state, entry, abspath='a',
1021
                                          stat_value=stat_value)
1022
        self.assertEqual('target', link_or_sha1)
1023
        self.assertEqual([('l', 'target', 6, False, packed_stat)],
1024
                         entry[1])
1025
1026
    def do_update_entry(self, state, entry, abspath):
1027
        stat_value = os.lstat(abspath)
1028
        return self.update_entry(state, entry, abspath, stat_value)
1029
1030
    def test_update_entry_dir(self):
1031
        state, entry = self.get_state_with_a()
1032
        self.build_tree(['a/'])
1033
        self.assertIs(None, self.do_update_entry(state, entry, 'a'))
1034
1035
    def test_update_entry_dir_unchanged(self):
1036
        state, entry = self.get_state_with_a()
1037
        self.build_tree(['a/'])
1038
        state.adjust_time(+20)
1039
        self.assertIs(None, self.do_update_entry(state, entry, 'a'))
1040
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
1041
                         state._dirblock_state)
1042
        state.save()
1043
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
1044
                         state._dirblock_state)
1045
        self.assertIs(None, self.do_update_entry(state, entry, 'a'))
1046
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
1047
                         state._dirblock_state)
1048
1049
    def test_update_entry_file_unchanged(self):
3696.5.2 by Robert Collins
Integrate less aggressive sha logic with C iter-changes.
1050
        state, _ = self.get_state_with_a()
1051
        tree = self.make_branch_and_tree('tree')
1052
        tree.lock_write()
1053
        self.build_tree(['tree/a'])
1054
        tree.add(['a'], ['a-id'])
1055
        with_a_id = tree.commit('witha')
1056
        self.addCleanup(tree.unlock)
1057
        state.set_parent_trees(
1058
            [(with_a_id, tree.branch.repository.revision_tree(with_a_id))],
1059
            [])
1060
        entry = state._get_entry(0, path_utf8='a')
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
1061
        self.build_tree(['a'])
1062
        sha1sum = 'b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6'
1063
        state.adjust_time(+20)
1064
        self.assertEqual(sha1sum, self.do_update_entry(state, entry, 'a'))
1065
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
1066
                         state._dirblock_state)
1067
        state.save()
1068
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
1069
                         state._dirblock_state)
1070
        self.assertEqual(sha1sum, self.do_update_entry(state, entry, 'a'))
1071
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
1072
                         state._dirblock_state)
1073
4100.2.6 by Aaron Bentley
Add tests for update_entry with tree references
1074
    def test_update_entry_tree_reference(self):
1075
        state = test_dirstate.InstrumentedDirState.initialize('dirstate')
1076
        self.addCleanup(state.unlock)
1077
        state.add('r', 'r-id', 'tree-reference', None, '')
1078
        self.build_tree(['r/'])
1079
        entry = state._get_entry(0, path_utf8='r')
1080
        self.do_update_entry(state, entry, 'r')
1081
        entry = state._get_entry(0, path_utf8='r')
1082
        self.assertEqual('t', entry[1][0][0])
1083
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
1084
    def create_and_test_file(self, state, entry):
3696.5.2 by Robert Collins
Integrate less aggressive sha logic with C iter-changes.
1085
        """Create a file at 'a' and verify the state finds it during update.
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
1086
1087
        The state should already be versioning *something* at 'a'. This makes
1088
        sure that state.update_entry recognizes it as a file.
1089
        """
1090
        self.build_tree(['a'])
1091
        stat_value = os.lstat('a')
1092
        packed_stat = dirstate.pack_stat(stat_value)
1093
1094
        link_or_sha1 = self.do_update_entry(state, entry, abspath='a')
3696.5.2 by Robert Collins
Integrate less aggressive sha logic with C iter-changes.
1095
        self.assertEqual(None, link_or_sha1)
1096
        self.assertEqual([('f', '', 14, False, dirstate.DirState.NULLSTAT)],
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
1097
                         entry[1])
1098
        return packed_stat
1099
1100
    def create_and_test_dir(self, state, entry):
1101
        """Create a directory at 'a' and verify the state finds it.
1102
1103
        The state should already be versioning *something* at 'a'. This makes
1104
        sure that state.update_entry recognizes it as a directory.
1105
        """
1106
        self.build_tree(['a/'])
1107
        stat_value = os.lstat('a')
1108
        packed_stat = dirstate.pack_stat(stat_value)
1109
1110
        link_or_sha1 = self.do_update_entry(state, entry, abspath='a')
1111
        self.assertIs(None, link_or_sha1)
1112
        self.assertEqual([('d', '', 0, False, packed_stat)], entry[1])
1113
1114
        return packed_stat
1115
4241.14.19 by Vincent Ladeuil
Modernize dirstate helpers tests parametrization.
1116
    # FIXME: Add unicode version
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
1117
    def create_and_test_symlink(self, state, entry):
1118
        """Create a symlink at 'a' and verify the state finds it.
1119
1120
        The state should already be versioning *something* at 'a'. This makes
1121
        sure that state.update_entry recognizes it as a symlink.
1122
1123
        This should not be called if this platform does not have symlink
1124
        support.
1125
        """
1126
        # caller should care about skipping test on platforms without symlinks
1127
        os.symlink('path/to/foo', 'a')
1128
1129
        stat_value = os.lstat('a')
1130
        packed_stat = dirstate.pack_stat(stat_value)
1131
1132
        link_or_sha1 = self.do_update_entry(state, entry, abspath='a')
1133
        self.assertEqual('path/to/foo', link_or_sha1)
1134
        self.assertEqual([('l', 'path/to/foo', 11, False, packed_stat)],
1135
                         entry[1])
1136
        return packed_stat
1137
1138
    def test_update_file_to_dir(self):
1139
        """If a file changes to a directory we return None for the sha.
1140
        We also update the inventory record.
1141
        """
1142
        state, entry = self.get_state_with_a()
1143
        # The file sha1 won't be cached unless the file is old
1144
        state.adjust_time(+10)
1145
        self.create_and_test_file(state, entry)
1146
        os.remove('a')
1147
        self.create_and_test_dir(state, entry)
1148
1149
    def test_update_file_to_symlink(self):
1150
        """File becomes a symlink"""
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
1151
        self.requireFeature(tests.SymlinkFeature)
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
1152
        state, entry = self.get_state_with_a()
1153
        # The file sha1 won't be cached unless the file is old
1154
        state.adjust_time(+10)
1155
        self.create_and_test_file(state, entry)
1156
        os.remove('a')
1157
        self.create_and_test_symlink(state, entry)
1158
1159
    def test_update_dir_to_file(self):
1160
        """Directory becoming a file updates the entry."""
1161
        state, entry = self.get_state_with_a()
1162
        # The file sha1 won't be cached unless the file is old
1163
        state.adjust_time(+10)
1164
        self.create_and_test_dir(state, entry)
1165
        os.rmdir('a')
1166
        self.create_and_test_file(state, entry)
1167
1168
    def test_update_dir_to_symlink(self):
1169
        """Directory becomes a symlink"""
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
1170
        self.requireFeature(tests.SymlinkFeature)
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
1171
        state, entry = self.get_state_with_a()
1172
        # The symlink target won't be cached if it isn't old
1173
        state.adjust_time(+10)
1174
        self.create_and_test_dir(state, entry)
1175
        os.rmdir('a')
1176
        self.create_and_test_symlink(state, entry)
1177
1178
    def test_update_symlink_to_file(self):
1179
        """Symlink becomes a file"""
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
1180
        self.requireFeature(tests.SymlinkFeature)
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
1181
        state, entry = self.get_state_with_a()
1182
        # The symlink and file info won't be cached unless old
1183
        state.adjust_time(+10)
1184
        self.create_and_test_symlink(state, entry)
1185
        os.remove('a')
1186
        self.create_and_test_file(state, entry)
1187
1188
    def test_update_symlink_to_dir(self):
1189
        """Symlink becomes a directory"""
4241.14.12 by Vincent Ladeuil
Far too many modifications for a single commit, need to restart.
1190
        self.requireFeature(tests.SymlinkFeature)
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
1191
        state, entry = self.get_state_with_a()
1192
        # The symlink target won't be cached if it isn't old
1193
        state.adjust_time(+10)
1194
        self.create_and_test_symlink(state, entry)
1195
        os.remove('a')
1196
        self.create_and_test_dir(state, entry)
1197
1198
    def test__is_executable_win32(self):
1199
        state, entry = self.get_state_with_a()
1200
        self.build_tree(['a'])
1201
1202
        # Make sure we are using the win32 implementation of _is_executable
1203
        state._is_executable = state._is_executable_win32
1204
1205
        # The file on disk is not executable, but we are marking it as though
1206
        # it is. With _is_executable_win32 we ignore what is on disk.
1207
        entry[1][0] = ('f', '', 0, True, dirstate.DirState.NULLSTAT)
1208
1209
        stat_value = os.lstat('a')
1210
        packed_stat = dirstate.pack_stat(stat_value)
1211
1212
        state.adjust_time(-10) # Make sure everything is new
1213
        self.update_entry(state, entry, abspath='a', stat_value=stat_value)
1214
1215
        # The row is updated, but the executable bit stays set.
1216
        self.assertEqual([('f', '', 14, True, dirstate.DirState.NULLSTAT)],
1217
                         entry[1])
1218
4241.14.19 by Vincent Ladeuil
Modernize dirstate helpers tests parametrization.
1219
        # Make the disk object look old enough to cache (but it won't cache the
1220
        # sha as it is a new file).
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
1221
        state.adjust_time(+20)
1222
        digest = 'b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6'
1223
        self.update_entry(state, entry, abspath='a', stat_value=stat_value)
3696.5.2 by Robert Collins
Integrate less aggressive sha logic with C iter-changes.
1224
        self.assertEqual([('f', '', 14, True, dirstate.DirState.NULLSTAT)],
1225
            entry[1])
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
1226
4132.2.4 by Ian Clatworthy
John's enhancements/fixes to the tests
1227
    def _prepare_tree(self):
1228
        # Create a tree
1229
        text = 'Hello World\n'
1230
        tree = self.make_branch_and_tree('tree')
1231
        self.build_tree_contents([('tree/a file', text)])
1232
        tree.add('a file', 'a-file-id')
1233
        # Note: dirstate does not sha prior to the first commit
1234
        # so commit now in order for the test to work
1235
        tree.commit('first')
1236
        return tree, text
1237
1238
    def test_sha1provider_sha1_used(self):
1239
        tree, text = self._prepare_tree()
1240
        state = dirstate.DirState.from_tree(tree, 'dirstate',
4132.2.5 by Ian Clatworthy
feedback from poolie - use SHA, not Sha, in class names
1241
            UppercaseSHA1Provider())
4132.2.4 by Ian Clatworthy
John's enhancements/fixes to the tests
1242
        self.addCleanup(state.unlock)
1243
        expected_sha = osutils.sha_string(text.upper() + "foo")
1244
        entry = state._get_entry(0, path_utf8='a file')
1245
        state._sha_cutoff_time()
1246
        state._cutoff_time += 10
4241.14.19 by Vincent Ladeuil
Modernize dirstate helpers tests parametrization.
1247
        sha1 = self.update_entry(state, entry, 'tree/a file',
1248
                                 os.lstat('tree/a file'))
4132.2.4 by Ian Clatworthy
John's enhancements/fixes to the tests
1249
        self.assertEqual(expected_sha, sha1)
1250
1251
    def test_sha1provider_stat_and_sha1_used(self):
1252
        tree, text = self._prepare_tree()
1253
        tree.lock_write()
1254
        self.addCleanup(tree.unlock)
1255
        state = tree._current_dirstate()
4132.2.5 by Ian Clatworthy
feedback from poolie - use SHA, not Sha, in class names
1256
        state._sha1_provider = UppercaseSHA1Provider()
4132.2.4 by Ian Clatworthy
John's enhancements/fixes to the tests
1257
        # If we used the standard provider, it would look like nothing has
1258
        # changed
4241.14.19 by Vincent Ladeuil
Modernize dirstate helpers tests parametrization.
1259
        file_ids_changed = [change[0] for change
1260
                            in tree.iter_changes(tree.basis_tree())]
4132.2.4 by Ian Clatworthy
John's enhancements/fixes to the tests
1261
        self.assertEqual(['a-file-id'], file_ids_changed)
1262
1263
4159.1.2 by Ian Clatworthy
make sha1_provider a parameter to DirState() - tweak tests
1264
class UppercaseSHA1Provider(dirstate.SHA1Provider):
4132.2.5 by Ian Clatworthy
feedback from poolie - use SHA, not Sha, in class names
1265
    """A custom SHA1Provider."""
4132.2.4 by Ian Clatworthy
John's enhancements/fixes to the tests
1266
1267
    def sha1(self, abspath):
1268
        return self.stat_and_sha1(abspath)[1]
1269
1270
    def stat_and_sha1(self, abspath):
1271
        file_obj = file(abspath, 'rb')
1272
        try:
1273
            statvalue = os.fstat(file_obj.fileno())
1274
            text = ''.join(file_obj.readlines())
1275
            sha1 = osutils.sha_string(text.upper() + "foo")
1276
        finally:
1277
            file_obj.close()
1278
        return statvalue, sha1
1279
3696.4.1 by Robert Collins
Refactor to allow a pluggable dirstate update_entry with interface tests.
1280
4241.14.19 by Vincent Ladeuil
Modernize dirstate helpers tests parametrization.
1281
class TestProcessEntry(test_dirstate.TestCaseWithDirState):
1282
1283
    # Set by load_tests
1284
    _process_entry = None
4132.2.4 by Ian Clatworthy
John's enhancements/fixes to the tests
1285
1286
    def setUp(self):
4241.14.19 by Vincent Ladeuil
Modernize dirstate helpers tests parametrization.
1287
        super(TestProcessEntry, self).setUp()
4132.2.4 by Ian Clatworthy
John's enhancements/fixes to the tests
1288
        orig = dirstate._process_entry
1289
        def cleanup():
1290
            dirstate._process_entry = orig
1291
        self.addCleanup(cleanup)
4241.14.19 by Vincent Ladeuil
Modernize dirstate helpers tests parametrization.
1292
        dirstate._process_entry = self._process_entry
4132.2.4 by Ian Clatworthy
John's enhancements/fixes to the tests
1293
1294
    def assertChangedFileIds(self, expected, tree):
1295
        tree.lock_read()
1296
        try:
1297
            file_ids = [info[0] for info
1298
                        in tree.iter_changes(tree.basis_tree())]
1299
        finally:
1300
            tree.unlock()
1301
        self.assertEqual(sorted(expected), sorted(file_ids))
1302
1303
    def test_simple_changes(self):
1304
        tree = self.make_branch_and_tree('tree')
1305
        self.build_tree(['tree/file'])
1306
        tree.add(['file'], ['file-id'])
1307
        self.assertChangedFileIds([tree.get_root_id(), 'file-id'], tree)
1308
        tree.commit('one')
1309
        self.assertChangedFileIds([], tree)
1310
1311
    def test_sha1provider_stat_and_sha1_used(self):
1312
        tree = self.make_branch_and_tree('tree')
1313
        self.build_tree(['tree/file'])
1314
        tree.add(['file'], ['file-id'])
1315
        tree.commit('one')
1316
        tree.lock_write()
1317
        self.addCleanup(tree.unlock)
1318
        state = tree._current_dirstate()
4132.2.5 by Ian Clatworthy
feedback from poolie - use SHA, not Sha, in class names
1319
        state._sha1_provider = UppercaseSHA1Provider()
4132.2.4 by Ian Clatworthy
John's enhancements/fixes to the tests
1320
        self.assertChangedFileIds(['file-id'], tree)
1321